services.go 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/xuri/excelize/v2"
  12. )
  13. var dingTalk = new(DingTalk)
  14. type DingTalkService struct {
  15. isSyncing bool
  16. syncLocker *sync.Mutex
  17. }
  18. func (service *DingTalkService) initialize() {
  19. service.isSyncing = false
  20. service.syncLocker = new(sync.Mutex)
  21. dingTalk.refreshToken()
  22. go func() {
  23. for {
  24. time.Sleep(time.Minute * 5)
  25. dingTalk.refreshToken()
  26. }
  27. }()
  28. }
  29. func getSubDepartments(dingTalk *DingTalk, parentId int, departments []Department) []Department {
  30. subDepartments := dingTalk.getDepartments(parentId)
  31. for i := 0; i < len(subDepartments); i++ {
  32. departments = append(departments, subDepartments[i])
  33. departments = getSubDepartments(dingTalk, subDepartments[i].Id, departments)
  34. }
  35. return departments
  36. }
  37. func getDepartmentEmployees(dingTalk *DingTalk, departmentId int, employees []Employee) []Employee {
  38. departmentEmployees := dingTalk.getEmployees(departmentId)
  39. for i := 0; i < len(departmentEmployees); i++ {
  40. employees = append(employees, departmentEmployees[i])
  41. }
  42. subDepartments := dingTalk.getDepartments(departmentId)
  43. for i := 0; i < len(subDepartments); i++ {
  44. subDepartment := subDepartments[i]
  45. employees = getDepartmentEmployees(dingTalk, subDepartment.Id, employees)
  46. }
  47. return employees
  48. }
  49. func (service *DingTalkService) GeteEmployeeIsLeader(authCode string) bool {
  50. var employeeId = dingTalk.getEmployeeIdByAuthCode(authCode)
  51. return dingTalk.getIsLeaderById(employeeId)
  52. }
  53. func (service *DingTalkService) GeteEmployeeNameByAuthCode(authCode string) string {
  54. return dingTalk.getEmployeeNameByAuthCode(authCode)
  55. }
  56. func (service *DingTalkService) GetDepartmentEmployees(departmentId int) []Employee {
  57. employees := make([]Employee, 0)
  58. return getDepartmentEmployees(dingTalk, departmentId, employees)
  59. }
  60. func (service *DingTalkService) GetDepartments(departmentId int) []Department {
  61. departments := make([]Department, 0)
  62. return getSubDepartments(dingTalk, departmentId, departments)
  63. }
  64. func (service *DingTalkService) SyncDepartmentEmployees(parentId int, syncedCount *int, employeeCount int, progressId string) {
  65. defer func() {
  66. var p = any(recover())
  67. if p != nil {
  68. errStr := fmt.Sprintln("error_") + fmt.Sprintf("%v", p)
  69. progressService.AddOrUpdateProgress(progressId, errStr)
  70. }
  71. }()
  72. employees := dingTalk.getEmployees(parentId)
  73. for i := 0; i < len(employees); i++ {
  74. employee := employees[i]
  75. exist := databaseManager.GetEmployeeById(employee.Id)
  76. if exist == nil {
  77. databaseManager.AddEmployee(&employee)
  78. } else {
  79. databaseManager.UpdateEmployee(&employee)
  80. }
  81. *syncedCount++
  82. progressValue := *syncedCount * 100 / employeeCount
  83. progressService.AddOrUpdateProgress(progressId, strconv.Itoa(progressValue))
  84. }
  85. departments := dingTalk.getDepartments(parentId)
  86. for i := 0; i < len(departments); i++ {
  87. department := departments[i]
  88. exist := databaseManager.GetDepartmentById(department.Id)
  89. if exist == nil {
  90. databaseManager.AddDepartment(&department)
  91. } else {
  92. databaseManager.UpdateDepartment(&department)
  93. }
  94. service.SyncDepartmentEmployees(department.Id, syncedCount, employeeCount, progressId)
  95. }
  96. }
  97. func (service *DingTalkService) SyncEmployeess2(progressId string) {
  98. defer func() {
  99. var p = any(recover())
  100. if p != nil {
  101. errStr := fmt.Sprintln("error_") + fmt.Sprintf("%v", p)
  102. progressService.AddOrUpdateProgress(progressId, errStr)
  103. if service.isSyncing {
  104. service.isSyncing = false
  105. service.syncLocker.Unlock()
  106. }
  107. }
  108. }()
  109. if service.isSyncing {
  110. progressService.AddOrUpdateProgress(progressId, "error_一个同步操作已经在进行中。")
  111. return
  112. }
  113. service.syncLocker.Lock()
  114. service.isSyncing = true
  115. dingTalk.refreshToken()
  116. //Create or update default department
  117. rootDepartment := Department{1, "VINNO", 0}
  118. exist := databaseManager.GetDepartmentById(rootDepartment.Id)
  119. if exist == nil {
  120. databaseManager.AddDepartment(&rootDepartment)
  121. } else {
  122. databaseManager.UpdateDepartment(&rootDepartment)
  123. }
  124. employeeCount := dingTalk.getEmployeeCount()
  125. syncCount := 0
  126. dingTalkService.SyncDepartmentEmployees(rootDepartment.Id, &syncCount, employeeCount, progressId)
  127. service.isSyncing = false
  128. service.syncLocker.Unlock()
  129. }
  130. func (service *DingTalkService) SyncEmployees(departmentId int) {
  131. service.syncLocker.Lock()
  132. if service.isSyncing {
  133. service.syncLocker.Unlock()
  134. return
  135. } else {
  136. service.isSyncing = true
  137. service.syncLocker.Unlock()
  138. }
  139. dingTalk.refreshToken()
  140. employees := service.GetDepartmentEmployees(departmentId)
  141. for i := 0; i < len(employees); i++ {
  142. employee := employees[i]
  143. exist := databaseManager.GetEmployeeById(employee.Id)
  144. if exist == nil {
  145. databaseManager.AddEmployee(&employee)
  146. } else {
  147. databaseManager.UpdateEmployee(&employee)
  148. }
  149. }
  150. service.syncLocker.Lock()
  151. service.isSyncing = false
  152. service.syncLocker.Unlock()
  153. }
  154. func (service *DingTalkService) SendTextMessage(userId string, text string) {
  155. dingTalk.sendTextMessage([]string{userId}, text)
  156. }
  157. func (service *DingTalkService) SendUrlMessage(userId string, title string, text string, imageUrl string, url string) {
  158. dingTalk.sendUrlMessage([]string{userId}, title, text, imageUrl, url)
  159. }
  160. func (service *DingTalkService) SendActionCard(userId string, title string, markdown string, singleTitle string, singleUrl string) {
  161. dingTalk.sendActionCard([]string{userId}, title, markdown, singleTitle, singleUrl)
  162. }
  163. type LoginService struct {
  164. }
  165. func createSession(accountId string) *Session {
  166. session := new(Session)
  167. session.sessionId = NewUUId()
  168. session.accountId = accountId
  169. session.updateTime = time.Now()
  170. sessionManager.RemoveSessionByAccountId(session.accountId)
  171. sessionManager.AddSession(session)
  172. return session
  173. }
  174. func (service *LoginService) Login(name string, password string) string {
  175. result := databaseManager.GetAdminByName(name)
  176. if result == nil {
  177. var err any = errors.New("用户名不存在。")
  178. panic(err)
  179. } else {
  180. if result.Password != password {
  181. var err any = errors.New("密码错误。")
  182. panic(err)
  183. } else {
  184. //强制替换session
  185. session := createSession(result.Id)
  186. return session.sessionId
  187. }
  188. }
  189. }
  190. func (service *LoginService) Logoff(sessionId string) {
  191. sessionManager.RemoveSessionBySessionId(sessionId)
  192. }
  193. func CheckSession(sessionId string) {
  194. if strings.Index(sessionId, "ViewCode") != 0 {
  195. session := sessionManager.GetSessionBySessionId(sessionId)
  196. if session == nil {
  197. var err any = errors.New("无效的登录信息。")
  198. panic(err)
  199. }
  200. }
  201. }
  202. type AdminService struct {
  203. }
  204. func (service *AdminService) Add(sessionId string, admin Admin) {
  205. CheckSession(sessionId)
  206. exist := databaseManager.GetAdminByName(admin.Name)
  207. if exist != nil {
  208. var err any = "管理员已经存在."
  209. panic(err)
  210. }
  211. databaseManager.AddAdmin(&admin)
  212. }
  213. func (service *AdminService) GetAdminIds(sessionId string) []string {
  214. CheckSession(sessionId)
  215. return []string{
  216. "14033025461181119", //邓
  217. "0117116430501258458", //高
  218. "1403411622464238794", //邢
  219. }
  220. }
  221. func (service *AdminService) Update(sessionId string, adminName string, adminPassword string) {
  222. CheckSession(sessionId)
  223. exist := databaseManager.GetAdminByName(adminName)
  224. if exist == nil {
  225. var err any = "管理员不存在."
  226. panic(err)
  227. }
  228. exist.Password = adminPassword
  229. databaseManager.UpdateAdminPassword(exist)
  230. }
  231. func (service *AdminService) Get(sessionId string, id string) Admin {
  232. CheckSession(sessionId)
  233. return *databaseManager.GetAdminById(id)
  234. }
  235. type DepartmentService struct {
  236. }
  237. func (service *DepartmentService) GetDepartment(sessionId string, departmentId int) Department {
  238. CheckSession(sessionId)
  239. result := databaseManager.GetDepartmentById(departmentId)
  240. return *result
  241. }
  242. func (service *DepartmentService) GetChildDepartments(sessionId string, departmentId int) []Department {
  243. CheckSession(sessionId)
  244. departments := make([]Department, 0)
  245. result := databaseManager.GetDepartmentsByParentId(departmentId)
  246. for i := 0; i < len(result); i++ {
  247. departments = append(departments, *result[i])
  248. }
  249. return departments
  250. }
  251. type EmployeeService struct {
  252. }
  253. func getEmployeesByDepartmentId(departmentId int, employees []Employee) []Employee {
  254. result := databaseManager.GetEmployeeManyByDepartmentId(departmentId)
  255. for i := 0; i < len(result); i++ {
  256. employees = append(employees, *result[i])
  257. }
  258. departments := databaseManager.GetDepartmentsByParentId(departmentId)
  259. for i := 0; i < len(departments); i++ {
  260. employees = getEmployeesByDepartmentId(departments[i].Id, employees)
  261. }
  262. return employees
  263. }
  264. func (service *EmployeeService) GetReviewers(sessionId string) []Employee {
  265. CheckSession(sessionId)
  266. employees := make([]Employee, 0)
  267. result := databaseManager.GetReviewers()
  268. for i := 0; i < len(result); i++ {
  269. employees = append(employees, *result[i])
  270. }
  271. return employees
  272. }
  273. func (service *EmployeeService) GetManyByDepartmentId(sessionId string, departmentId int) []Employee {
  274. CheckSession(sessionId)
  275. employees := make([]Employee, 0)
  276. employees = getEmployeesByDepartmentId(departmentId, employees)
  277. return employees
  278. }
  279. func (service *EmployeeService) GetLeaveQuotas(email string, leaveType int) []LeaveQuota {
  280. employee := databaseManager.GetEmployeeByEmail(email)
  281. if employee != nil {
  282. return dingTalk.getLeaveQuotas(employee.Id, leaveType)
  283. } else {
  284. var err any = "账号不存在。"
  285. panic(err)
  286. }
  287. }
  288. func (service *EmployeeService) UpdateEmployee(sessionId string, employee Employee) {
  289. CheckSession(sessionId)
  290. databaseManager.UpdateEmployee(&employee)
  291. }
  292. func (service *EmployeeService) GetEmployeeCount(sessionId string) int {
  293. CheckSession(sessionId)
  294. return int(databaseManager.GetEmployeeCount())
  295. }
  296. func (service *EmployeeService) GetStruggleEmployeeCount(sessionId string) int {
  297. CheckSession(sessionId)
  298. return int(databaseManager.GetStruggleEmployeeCount())
  299. }
  300. func (service *EmployeeService) UpdateEmployeeDepartment(sessionId string, employee Employee) {
  301. CheckSession(sessionId)
  302. databaseManager.UpdateEmployeeDepartment(&employee)
  303. }
  304. func (service *EmployeeService) UpdateEmployeeIsStruggle(sessionId string, employee Employee) {
  305. CheckSession(sessionId)
  306. databaseManager.UpdateEmployeeIsStruggle(&employee)
  307. }
  308. func (service *EmployeeService) UpdateEmployeeIsReviewer(sessionId string, employee Employee) {
  309. CheckSession(sessionId)
  310. databaseManager.UpdateEmployeeIsReviewer(&employee)
  311. }
  312. func (service *EmployeeService) GetByName(sessionId string, employeeName string) Employee {
  313. CheckSession(sessionId)
  314. result := databaseManager.GetEmployeeByName(employeeName)
  315. if result == nil {
  316. var err any = "账号不存在。"
  317. panic(err)
  318. }
  319. return *result
  320. }
  321. func (service *EmployeeService) Get(sessionId string, employeeId string) Employee {
  322. CheckSession(sessionId)
  323. result := databaseManager.GetEmployeeById(employeeId)
  324. if result == nil {
  325. var err any = "账号不存在。"
  326. panic(err)
  327. }
  328. return *result
  329. }
  330. func (service *EmployeeService) Delete(sessionId string, employeeId string) {
  331. CheckSession(sessionId)
  332. result := databaseManager.GetEmployeeById(employeeId)
  333. if result == nil {
  334. var err any = "账号不存在。"
  335. panic(err)
  336. }
  337. databaseManager.DeleteEmployee(employeeId)
  338. }
  339. type WorkingDayService struct {
  340. }
  341. func (service *WorkingDayService) Update(sessionId string, workingDayInfo WorkingDayInfo) {
  342. CheckSession(sessionId)
  343. exist := databaseManager.GetWorkingDayInfoById(workingDayInfo.Id)
  344. if exist == nil {
  345. var err any = "工作日不存在。"
  346. panic(err)
  347. }
  348. databaseManager.UpdateWorkingDayInfo(&workingDayInfo)
  349. }
  350. func (service *WorkingDayService) GetMany(sessionId string, employeeId string, statisticsId string) []WorkingDayInfo {
  351. CheckSession(sessionId)
  352. workingDayInfos := make([]WorkingDayInfo, 0)
  353. result := databaseManager.GetWorkingDayInfoMany(employeeId, statisticsId)
  354. for i := 0; i < len(result); i++ {
  355. workingDayInfos = append(workingDayInfos, *result[i])
  356. }
  357. return workingDayInfos
  358. }
  359. type WorkingDayStatisticsService struct {
  360. checkHistory map[string]*CheckHistory
  361. }
  362. func (service *WorkingDayStatisticsService) CheckEmployeeStatistics(email string, startStr string, endStr string) int {
  363. if service.checkHistory == nil {
  364. service.checkHistory = make(map[string]*CheckHistory)
  365. }
  366. if service.checkHistory[strings.ToLower(email)] != nil {
  367. now := time.Now()
  368. history := service.checkHistory[strings.ToLower(email)]
  369. if history.CheckDay.Year() == now.Year() && history.CheckDay.Month() == now.Month() && history.CheckDay.Day() == now.Day() {
  370. return history.TotalUnits
  371. }
  372. }
  373. totalUnits := 0
  374. employee := databaseManager.GetEmployeeByEmail(email)
  375. if employee == nil {
  376. var err any = "查询的员工不存在。"
  377. panic(err)
  378. }
  379. start := strToDateTime(startStr)
  380. end := strToDateTime(endStr)
  381. holidayInfos := databaseManager.GetHolidayDayInfos(time.Now().AddDate(-1, 0, 0), time.Now().AddDate(1, 0, 0))
  382. holidays := make(map[int64]*HolidayInfo)
  383. adjustDays := make(map[int64]*HolidayInfo)
  384. for i := 0; i < len(holidayInfos); i++ {
  385. holidays[holidayInfos[i].Holiday.Unix()] = holidayInfos[i]
  386. if holidayInfos[i].IsAdjusted {
  387. adjustDays[holidayInfos[i].AdjustedDay.Unix()] = holidayInfos[i]
  388. }
  389. }
  390. attendanceList := dingTalk.getEmployeeAttendanceDataList(employee.Id, start, end)
  391. statisticsId := NewUUId()
  392. for j := 0; j < len(attendanceList); j++ {
  393. attendanceData := attendanceList[j]
  394. day := attendanceData.Day
  395. workingDayInfo := new(WorkingDayInfo)
  396. workingDayInfo.Id = NewUUId()
  397. workingDayInfo.StatisticsId = statisticsId
  398. workingDayInfo.EmployeeId = employee.Id
  399. workingDayInfo.Day = day
  400. workingDayInfo.Start = attendanceData.StartTime
  401. if attendanceData.StartResult != "正常" {
  402. workingDayInfo.StartResult = attendanceData.StartResult
  403. }
  404. workingDayInfo.End = attendanceData.EndTime
  405. if attendanceData.EndResult != "正常" {
  406. workingDayInfo.EndResult = attendanceData.EndResult
  407. }
  408. workingDayInfo.State = attendanceData.State
  409. isHoliday := holidays[attendanceData.Day.Unix()] != nil
  410. workingDayInfo.IsHoliday = isHoliday
  411. if isHoliday {
  412. holidayInfo := holidays[attendanceData.Day.Unix()]
  413. if holidayInfo.IsAdjusted {
  414. workingDayInfo.HolidayAdjusted = true
  415. workingDayInfo.AdjustedDay = holidayInfo.AdjustedDay
  416. }
  417. }
  418. isAdjustedDay := adjustDays[attendanceData.Day.Unix()] != nil
  419. if isAdjustedDay {
  420. holidayInfo := adjustDays[attendanceData.Day.Unix()]
  421. day = holidayInfo.Holiday
  422. workingDayInfo.HolidayAdjusted = true
  423. workingDayInfo.AdjustedDay = holidayInfo.Holiday
  424. if workingDayInfo.State == 休息 {
  425. workingDayInfo.State = 调班
  426. }
  427. //此天是调班日,按休假的那天算
  428. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(day, employee.AttendanceRules, attendanceData, false)
  429. workingDayInfo.WorkingHours = calcWorkingHours(day, employee.AttendanceRules, attendanceData, false, true, false)
  430. } else {
  431. if workingDayInfo.HolidayAdjusted {
  432. //此天是休息日,但是如果来按休假的那天来计算
  433. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(workingDayInfo.AdjustedDay, employee.AttendanceRules, attendanceData, true)
  434. workingDayInfo.WorkingHours = calcWorkingHours(workingDayInfo.AdjustedDay, employee.AttendanceRules, attendanceData, true, true, false)
  435. } else {
  436. //此天可能休息也可能不休息,但是没有调班
  437. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(day, employee.AttendanceRules, attendanceData, isHoliday)
  438. if isHoliday {
  439. holiday := holidays[day.Unix()]
  440. isNationalLegal := false
  441. if holiday != nil {
  442. isNationalLegal = holiday.IsNationalLegal
  443. }
  444. workingDayInfo.WorkingHours = calcWorkingHours(day, employee.AttendanceRules, attendanceData, true, false, isNationalLegal)
  445. } else {
  446. workingDayInfo.WorkingHours = calcWorkingHours(day, employee.AttendanceRules, attendanceData, false, false, false)
  447. }
  448. }
  449. }
  450. workingDayInfo.IsWeekend = day.Weekday() == time.Saturday || day.Weekday() == time.Sunday
  451. overHours := workingDayInfo.WorkingHours - workingDayInfo.RequiredWorkingHours
  452. if overHours < 0 {
  453. overHours = 0
  454. }
  455. units := int(overHours / 2.5)
  456. if units > 1 {
  457. //工作日最多1次
  458. if !workingDayInfo.IsWeekend && !workingDayInfo.IsHoliday {
  459. units = 1
  460. } else {
  461. //周末必须达标3次
  462. if units >= 3 {
  463. units = 3
  464. } else {
  465. units = 0
  466. }
  467. }
  468. }
  469. workingDayInfo.ObtainedUnits = units
  470. workingDayInfo.AdjustedObtainedUnits = workingDayInfo.ObtainedUnits
  471. databaseManager.AddWorkingDayInfo(workingDayInfo)
  472. totalUnits += workingDayInfo.ObtainedUnits
  473. }
  474. history := new(CheckHistory)
  475. history.CheckDay = time.Now()
  476. history.TotalUnits = totalUnits
  477. service.checkHistory[strings.ToLower(email)] = history
  478. return totalUnits
  479. }
  480. func (service *WorkingDayStatisticsService) CheckEmployeePerformanceHours(email string, startStr string, endStr string) int {
  481. totalHours := 0.0
  482. employee := databaseManager.GetEmployeeByEmail(email)
  483. if employee == nil {
  484. var err any = "查询的员工不存在。"
  485. panic(err)
  486. }
  487. start := strToDateTime(startStr)
  488. end := strToDateTime(endStr)
  489. attendanceList := dingTalk.getEmployeeAttendanceDataList(employee.Id, start, end)
  490. for j := 0; j < len(attendanceList); j++ {
  491. attendanceData := attendanceList[j]
  492. hours := calcPerformanceHours(employee.AttendanceRules, attendanceData)
  493. totalHours += hours
  494. }
  495. return int(totalHours / 8)
  496. }
  497. func (service *WorkingDayStatisticsService) Update(sessionId string, workingDayStatistics WorkingDayStatistics) {
  498. CheckSession(sessionId)
  499. exist := databaseManager.GetWorkingDayStatisticsById(workingDayStatistics.Id)
  500. if exist == nil {
  501. var err any = "工作统计不存在。"
  502. panic(err)
  503. }
  504. databaseManager.UpdateWorkingDayStatistics(&workingDayStatistics)
  505. }
  506. func (service *WorkingDayStatisticsService) UpdateComment(sessionId string, workingDayStatisticsId string, comment string) {
  507. CheckSession(sessionId)
  508. exist := databaseManager.GetWorkingDayStatisticsById(workingDayStatisticsId)
  509. if exist == nil {
  510. var err any = "工作统计不存在。"
  511. panic(err)
  512. }
  513. databaseManager.UpdateWorkingDayStatisticsComment(workingDayStatisticsId, comment)
  514. }
  515. func (service *WorkingDayStatisticsService) Get(sessionId string, workingDayStatisticsId string) WorkingDayStatistics {
  516. CheckSession(sessionId)
  517. result := databaseManager.GetWorkingDayStatisticsById(workingDayStatisticsId)
  518. return *result
  519. }
  520. func (service *WorkingDayStatisticsService) GetMany(sessionId string, workingDayStatisticsIds []string) []WorkingDayStatistics {
  521. CheckSession(sessionId)
  522. workingDayStatisticsList := make([]WorkingDayStatistics, 0)
  523. result := databaseManager.GetWorkingDayStatisticsByIds(workingDayStatisticsIds)
  524. for i := 0; i < len(result); i++ {
  525. workingDayStatisticsList = append(workingDayStatisticsList, *result[i])
  526. }
  527. return workingDayStatisticsList
  528. }
  529. type StatisticsTableService struct {
  530. }
  531. func createDepartmentName(departmentId int) string {
  532. if departmentId != 1 {
  533. department := databaseManager.GetDepartmentById(departmentId)
  534. if department != nil {
  535. departmentName := department.Name
  536. if department.ParentId != 1 {
  537. return createDepartmentName(department.ParentId) + "/" + departmentName
  538. } else {
  539. return departmentName
  540. }
  541. }
  542. }
  543. return ""
  544. }
  545. func (service *StatisticsTableService) Add(sessionId string, title string, startStr string, endStr string, requiredReachedCount int) {
  546. CheckSession(sessionId)
  547. exist := databaseManager.GetStatisticsTableByTitle(title)
  548. if exist != nil {
  549. var err any = "同名工作统计集合已经存在。"
  550. panic(err)
  551. }
  552. start := strToDateTime(startStr)
  553. end := strToDateTime(endStr)
  554. holidayInfos := databaseManager.GetHolidayDayInfos(time.Now().AddDate(-1, 0, 0), time.Now().AddDate(1, 0, 0))
  555. holidays := make(map[int64]*HolidayInfo)
  556. adjustDays := make(map[int64]*HolidayInfo)
  557. for i := 0; i < len(holidayInfos); i++ {
  558. holidays[holidayInfos[i].Holiday.Unix()] = holidayInfos[i]
  559. if holidayInfos[i].IsAdjusted {
  560. adjustDays[holidayInfos[i].AdjustedDay.Unix()] = holidayInfos[i]
  561. }
  562. }
  563. workingDayStatisticsIds := make([]string, 0)
  564. employees := databaseManager.GetStruggleEmployeeMany()
  565. for i := 0; i < len(employees); i++ {
  566. totalUnits := 0
  567. attendanceList := dingTalk.getEmployeeAttendanceDataList(employees[i].Id, start, end)
  568. statisticsId := NewUUId()
  569. for j := 0; j < len(attendanceList); j++ {
  570. attendanceData := attendanceList[j]
  571. day := attendanceData.Day
  572. workingDayInfo := new(WorkingDayInfo)
  573. workingDayInfo.Id = NewUUId()
  574. workingDayInfo.StatisticsId = statisticsId
  575. workingDayInfo.EmployeeId = employees[i].Id
  576. workingDayInfo.Day = day
  577. workingDayInfo.Start = attendanceData.StartTime
  578. if attendanceData.StartResult != "正常" {
  579. workingDayInfo.StartResult = attendanceData.StartResult
  580. }
  581. workingDayInfo.End = attendanceData.EndTime
  582. if attendanceData.EndResult != "正常" {
  583. workingDayInfo.EndResult = attendanceData.EndResult
  584. }
  585. workingDayInfo.State = attendanceData.State
  586. isHoliday := holidays[attendanceData.Day.Unix()] != nil
  587. workingDayInfo.IsHoliday = isHoliday
  588. if isHoliday {
  589. holidayInfo := holidays[attendanceData.Day.Unix()]
  590. if holidayInfo.IsAdjusted {
  591. workingDayInfo.HolidayAdjusted = true
  592. workingDayInfo.AdjustedDay = holidayInfo.AdjustedDay
  593. }
  594. }
  595. isAdjustedDay := adjustDays[attendanceData.Day.Unix()] != nil
  596. if isAdjustedDay {
  597. holidayInfo := adjustDays[attendanceData.Day.Unix()]
  598. day = holidayInfo.Holiday
  599. workingDayInfo.HolidayAdjusted = true
  600. workingDayInfo.AdjustedDay = holidayInfo.Holiday
  601. if workingDayInfo.State == 休息 {
  602. workingDayInfo.State = 调班
  603. }
  604. //此天是调班日,按休假的那天算
  605. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(day, employees[i].AttendanceRules, attendanceData, false)
  606. workingDayInfo.WorkingHours = calcWorkingHours(day, employees[i].AttendanceRules, attendanceData, false, true, false)
  607. } else {
  608. if workingDayInfo.HolidayAdjusted {
  609. //此天是休息日,但是如果来按休假的那天来计算
  610. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(workingDayInfo.AdjustedDay, employees[i].AttendanceRules, attendanceData, true)
  611. workingDayInfo.WorkingHours = calcWorkingHours(workingDayInfo.AdjustedDay, employees[i].AttendanceRules, attendanceData, true, true, false)
  612. } else {
  613. //此天可能休息也可能不休息,但是没有调班
  614. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(day, employees[i].AttendanceRules, attendanceData, isHoliday)
  615. if isHoliday {
  616. holiday := holidays[day.Unix()]
  617. isNationalLegal := false
  618. if holiday != nil {
  619. isNationalLegal = holiday.IsNationalLegal
  620. }
  621. workingDayInfo.WorkingHours = calcWorkingHours(day, employees[i].AttendanceRules, attendanceData, true, false, isNationalLegal)
  622. } else {
  623. workingDayInfo.WorkingHours = calcWorkingHours(day, employees[i].AttendanceRules, attendanceData, false, false, false)
  624. }
  625. }
  626. }
  627. workingDayInfo.IsWeekend = day.Weekday() == time.Saturday || day.Weekday() == time.Sunday
  628. overHours := workingDayInfo.WorkingHours - workingDayInfo.RequiredWorkingHours
  629. if overHours < 0 {
  630. overHours = 0
  631. }
  632. units := int(overHours / 2.5)
  633. if units > 1 {
  634. //工作日最多1次
  635. if !workingDayInfo.IsWeekend && !workingDayInfo.IsHoliday {
  636. units = 1
  637. } else {
  638. //周末必须达标3次
  639. if units >= 3 {
  640. units = 3
  641. } else {
  642. units = 0
  643. }
  644. }
  645. }
  646. workingDayInfo.ObtainedUnits = units
  647. workingDayInfo.AdjustedObtainedUnits = workingDayInfo.ObtainedUnits
  648. databaseManager.AddWorkingDayInfo(workingDayInfo)
  649. totalUnits += workingDayInfo.ObtainedUnits
  650. }
  651. workingDayStatistics := new(WorkingDayStatistics)
  652. workingDayStatistics.Id = statisticsId
  653. workingDayStatistics.Employee = *employees[i]
  654. workingDayStatistics.DepartmentId = employees[i].DepartmentId
  655. workingDayStatistics.DepartmentName = createDepartmentName(employees[i].DepartmentId)
  656. workingDayStatistics.Start = start
  657. workingDayStatistics.End = end
  658. workingDayStatistics.RequiredReachedCount = requiredReachedCount
  659. workingDayStatistics.ReachedCount = totalUnits
  660. workingDayStatistics.AdjustedReachedCount = totalUnits
  661. workingDayStatistics.Adjusted = false
  662. workingDayStatistics.AdjustDescription = ""
  663. workingDayStatistics.AdjustedBy = ""
  664. workingDayStatistics.Comment = ""
  665. workingDayStatistics.Reviewer = ""
  666. databaseManager.AddWorkingDayStatistics(workingDayStatistics)
  667. workingDayStatisticsIds = append(workingDayStatisticsIds, workingDayStatistics.Id)
  668. }
  669. table := new(StatisticsTable)
  670. table.Id = NewUUId()
  671. table.Title = title
  672. table.RequiredReachedCount = requiredReachedCount
  673. table.WorkingDayStatisticsIds = workingDayStatisticsIds
  674. table.CreateTime = time.Now()
  675. databaseManager.AddStatisticsTable(table)
  676. }
  677. func (service *StatisticsTableService) Add2(sessionId string, title string, startStr string, endStr string, requiredReachedCount int, progressId string) {
  678. defer func() {
  679. var p = any(recover())
  680. if p != nil {
  681. errStr := fmt.Sprintln("error_") + fmt.Sprintf("%v", p)
  682. progressService.AddOrUpdateProgress(progressId, errStr)
  683. }
  684. }()
  685. CheckSession(sessionId)
  686. exist := databaseManager.GetStatisticsTableByTitle(title)
  687. if exist != nil {
  688. var err any = "同名工作统计集合已经存在。"
  689. panic(err)
  690. }
  691. start := strToDateTime(startStr)
  692. end := strToDateTime(endStr)
  693. holidayInfos := databaseManager.GetHolidayDayInfos(time.Now().AddDate(-1, 0, 0), time.Now().AddDate(1, 0, 0))
  694. holidays := make(map[int64]*HolidayInfo)
  695. adjustDays := make(map[int64]*HolidayInfo)
  696. for i := 0; i < len(holidayInfos); i++ {
  697. holidays[holidayInfos[i].Holiday.Unix()] = holidayInfos[i]
  698. if holidayInfos[i].IsAdjusted {
  699. adjustDays[holidayInfos[i].AdjustedDay.Unix()] = holidayInfos[i]
  700. }
  701. }
  702. workingDayStatisticsIds := make([]string, 0)
  703. employees := databaseManager.GetStruggleEmployeeMany()
  704. for i := 0; i < len(employees); i++ {
  705. totalUnits := 0
  706. attendanceList := dingTalk.getEmployeeAttendanceDataList(employees[i].Id, start, end)
  707. statisticsId := NewUUId()
  708. for j := 0; j < len(attendanceList); j++ {
  709. attendanceData := attendanceList[j]
  710. day := attendanceData.Day
  711. workingDayInfo := new(WorkingDayInfo)
  712. workingDayInfo.Id = NewUUId()
  713. workingDayInfo.StatisticsId = statisticsId
  714. workingDayInfo.EmployeeId = employees[i].Id
  715. workingDayInfo.Day = day
  716. workingDayInfo.Start = attendanceData.StartTime
  717. if attendanceData.StartResult != "正常" {
  718. workingDayInfo.StartResult = attendanceData.StartResult
  719. }
  720. workingDayInfo.End = attendanceData.EndTime
  721. if attendanceData.EndResult != "正常" {
  722. workingDayInfo.EndResult = attendanceData.EndResult
  723. }
  724. workingDayInfo.State = attendanceData.State
  725. isHoliday := holidays[attendanceData.Day.Unix()] != nil
  726. workingDayInfo.IsHoliday = isHoliday
  727. if isHoliday {
  728. holidayInfo := holidays[attendanceData.Day.Unix()]
  729. if holidayInfo.IsAdjusted {
  730. workingDayInfo.HolidayAdjusted = true
  731. workingDayInfo.AdjustedDay = holidayInfo.AdjustedDay
  732. }
  733. }
  734. isAdjustedDay := adjustDays[attendanceData.Day.Unix()] != nil
  735. if isAdjustedDay {
  736. holidayInfo := adjustDays[attendanceData.Day.Unix()]
  737. day = holidayInfo.Holiday
  738. workingDayInfo.HolidayAdjusted = true
  739. workingDayInfo.AdjustedDay = holidayInfo.Holiday
  740. if workingDayInfo.State == 休息 {
  741. workingDayInfo.State = 调班
  742. }
  743. //此天是调班日,按休假的那天算
  744. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(day, employees[i].AttendanceRules, attendanceData, false)
  745. workingDayInfo.WorkingHours = calcWorkingHours(day, employees[i].AttendanceRules, attendanceData, false, true, false)
  746. } else {
  747. if workingDayInfo.HolidayAdjusted {
  748. //此天是休息日,但是如果来按休假的那天来计算
  749. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(workingDayInfo.AdjustedDay, employees[i].AttendanceRules, attendanceData, true)
  750. workingDayInfo.WorkingHours = calcWorkingHours(workingDayInfo.AdjustedDay, employees[i].AttendanceRules, attendanceData, true, true, false)
  751. } else {
  752. //此天可能休息也可能不休息,但是没有调班
  753. workingDayInfo.RequiredWorkingHours = calcRequiredWorkingHours(day, employees[i].AttendanceRules, attendanceData, isHoliday)
  754. if isHoliday {
  755. holiday := holidays[day.Unix()]
  756. isNationalLegal := false
  757. if holiday != nil {
  758. isNationalLegal = holiday.IsNationalLegal
  759. }
  760. workingDayInfo.WorkingHours = calcWorkingHours(day, employees[i].AttendanceRules, attendanceData, true, false, isNationalLegal)
  761. } else {
  762. workingDayInfo.WorkingHours = calcWorkingHours(day, employees[i].AttendanceRules, attendanceData, false, false, false)
  763. }
  764. }
  765. }
  766. workingDayInfo.IsWeekend = day.Weekday() == time.Saturday || day.Weekday() == time.Sunday
  767. overHours := workingDayInfo.WorkingHours - workingDayInfo.RequiredWorkingHours
  768. if overHours < 0 {
  769. overHours = 0
  770. }
  771. units := int(overHours / 2.5)
  772. if units > 1 {
  773. //工作日最多1次
  774. if !workingDayInfo.IsWeekend && !workingDayInfo.IsHoliday {
  775. units = 1
  776. } else {
  777. //周末必须达标3次
  778. if units >= 3 {
  779. units = 3
  780. } else {
  781. units = 0
  782. }
  783. }
  784. }
  785. workingDayInfo.ObtainedUnits = units
  786. workingDayInfo.AdjustedObtainedUnits = workingDayInfo.ObtainedUnits
  787. databaseManager.AddWorkingDayInfo(workingDayInfo)
  788. totalUnits += workingDayInfo.ObtainedUnits
  789. }
  790. workingDayStatistics := new(WorkingDayStatistics)
  791. workingDayStatistics.Id = statisticsId
  792. workingDayStatistics.Employee = *employees[i]
  793. workingDayStatistics.DepartmentId = employees[i].DepartmentId
  794. workingDayStatistics.DepartmentName = createDepartmentName(employees[i].DepartmentId)
  795. workingDayStatistics.Start = start
  796. workingDayStatistics.End = end
  797. workingDayStatistics.RequiredReachedCount = requiredReachedCount
  798. workingDayStatistics.ReachedCount = totalUnits
  799. workingDayStatistics.AdjustedReachedCount = totalUnits
  800. workingDayStatistics.Adjusted = false
  801. workingDayStatistics.AdjustDescription = ""
  802. workingDayStatistics.AdjustedBy = ""
  803. workingDayStatistics.Comment = ""
  804. workingDayStatistics.Reviewer = ""
  805. databaseManager.AddWorkingDayStatistics(workingDayStatistics)
  806. workingDayStatisticsIds = append(workingDayStatisticsIds, workingDayStatistics.Id)
  807. employeeCount := len(employees)
  808. progressValue := (i + 1) * 100 / employeeCount
  809. progressService.AddOrUpdateProgress(progressId, strconv.Itoa(progressValue))
  810. }
  811. table := new(StatisticsTable)
  812. table.Id = NewUUId()
  813. table.Title = title
  814. table.RequiredReachedCount = requiredReachedCount
  815. table.WorkingDayStatisticsIds = workingDayStatisticsIds
  816. table.CreateTime = time.Now()
  817. databaseManager.AddStatisticsTable(table)
  818. }
  819. func (service *StatisticsTableService) Delete(sessionId string, statisticsTableId string) {
  820. CheckSession(sessionId)
  821. databaseManager.DeleteStatisticsTable(statisticsTableId)
  822. }
  823. func (service *StatisticsTableService) CreateAndDownloadReport(sessionId string, reportTitle string, workingDayStatisticsList []string) string {
  824. CheckSession(sessionId)
  825. f := excelize.NewFile()
  826. oldName := f.GetSheetName(0)
  827. f.SetSheetName(oldName, reportTitle)
  828. //写表头
  829. header := map[string]string{"A": "员工姓名", "B": "部门", "C": "达标次数", "D": "已调整", "E": "调整说明", "F": "备注", "G": "审核人"}
  830. row := 1
  831. style, err := f.NewStyle(`{"font":{"bold":true,"color":"#000000"}}`)
  832. if err == nil {
  833. f.SetCellStyle(reportTitle, "A1", "G1", style)
  834. }
  835. f.SetColWidth(reportTitle, "A", "A", 25)
  836. f.SetColWidth(reportTitle, "B", "B", 35)
  837. f.SetColWidth(reportTitle, "C", "D", 10)
  838. f.SetColWidth(reportTitle, "E", "F", 35)
  839. f.SetColWidth(reportTitle, "G", "G", 25)
  840. for k, v := range header {
  841. f.SetCellValue(reportTitle, k+strconv.Itoa(row), v)
  842. }
  843. row++
  844. for i := 0; i < len(workingDayStatisticsList); i++ {
  845. statistics := databaseManager.GetWorkingDayStatisticsById(workingDayStatisticsList[i])
  846. if statistics != nil {
  847. adjustedStr := "否"
  848. if statistics.Adjusted {
  849. adjustedStr = "是"
  850. }
  851. f.SetCellValue(reportTitle, "A"+strconv.Itoa(row), statistics.Employee.Name)
  852. f.SetCellValue(reportTitle, "B"+strconv.Itoa(row), statistics.DepartmentName)
  853. f.SetCellValue(reportTitle, "C"+strconv.Itoa(row), statistics.AdjustedReachedCount)
  854. f.SetCellValue(reportTitle, "D"+strconv.Itoa(row), adjustedStr)
  855. f.SetCellValue(reportTitle, "E"+strconv.Itoa(row), statistics.AdjustDescription)
  856. f.SetCellValue(reportTitle, "F"+strconv.Itoa(row), statistics.Comment)
  857. f.SetCellValue(reportTitle, "G"+strconv.Itoa(row), statistics.Reviewer)
  858. row++
  859. }
  860. }
  861. buffer := new(bytes.Buffer)
  862. f.Write(buffer)
  863. f.Close()
  864. encodeString := base64.StdEncoding.EncodeToString(buffer.Bytes())
  865. return encodeString
  866. }
  867. func (service *StatisticsTableService) GetMany(sessionId string) []StatisticsTable {
  868. CheckSession(sessionId)
  869. statisticsTables := make([]StatisticsTable, 0)
  870. result := databaseManager.GetStatisticsTableMany()
  871. for i := 0; i < len(result); i++ {
  872. statisticsTables = append(statisticsTables, *result[i])
  873. }
  874. return statisticsTables
  875. }
  876. type ViewCodeService struct {
  877. }
  878. func (service *ViewCodeService) SendViewCode(sessionId string, title string, receiver string, cc string, contentTemplate string, viewCode ViewCode) {
  879. CheckSession(sessionId)
  880. emailSetting := databaseManager.GetEmailSetting()
  881. if emailSetting == nil {
  882. var err any = "服务器邮件参数未配置。"
  883. panic(err)
  884. }
  885. //Create url
  886. url := "http://localhost:8088/viewstatistcs?viewCode=" + viewCode.Code
  887. domainSetting := databaseManager.GetDomainSetting()
  888. if domainSetting != nil {
  889. url = domainSetting.Domain + "/viewstatistcs?viewCode=" + viewCode.Code
  890. }
  891. content := contentTemplate
  892. content = strings.Replace(content, "{URL}", url, -1)
  893. content = strings.Replace(content, "{ExpiredTime}", viewCode.ExpiredTime.Format("2006-01-02"), -1)
  894. sendEmail(emailSetting.Host, emailSetting.Port, emailSetting.Sender, emailSetting.SenderPassword, receiver, cc, title, content)
  895. }
  896. func (service *ViewCodeService) Get(code string) ViewCode {
  897. result := databaseManager.GetViewCode(code)
  898. if result == nil {
  899. var err any = "统计码不存在。"
  900. panic(err)
  901. }
  902. return *result
  903. }
  904. func (service *ViewCodeService) Add(sessionId string, viewCode ViewCode) {
  905. CheckSession(sessionId)
  906. exist := databaseManager.GetViewCode(viewCode.Code)
  907. if exist != nil {
  908. var err any = "统计码已存在。"
  909. panic(err)
  910. }
  911. databaseManager.AddViewCode(&viewCode)
  912. }
  913. type EmailService struct {
  914. }
  915. func (service *EmailService) GetEmailSetting(sessionId string) EmailSetting {
  916. CheckSession(sessionId)
  917. emailSetting := databaseManager.GetEmailSetting()
  918. if emailSetting == nil {
  919. return EmailSetting{"", "", "", "", ""}
  920. }
  921. return *emailSetting
  922. }
  923. func (service *EmailService) AddOrUpdateEmailSetting(sessionId string, setting EmailSetting) {
  924. CheckSession(sessionId)
  925. emailSetting := databaseManager.GetEmailSetting()
  926. if emailSetting == nil {
  927. databaseManager.AddEmailSetting(&setting)
  928. } else {
  929. emailSetting.Host = setting.Host
  930. emailSetting.Port = setting.Port
  931. emailSetting.Sender = setting.Sender
  932. emailSetting.SenderPassword = setting.SenderPassword
  933. databaseManager.UpdateEmailSetting(emailSetting)
  934. }
  935. }
  936. type DomainService struct {
  937. }
  938. func (service *DomainService) GetDomainSetting(sessionId string) DomainSetting {
  939. CheckSession(sessionId)
  940. domainSetting := databaseManager.GetDomainSetting()
  941. if domainSetting == nil {
  942. return DomainSetting{"", ""}
  943. }
  944. return *domainSetting
  945. }
  946. func (service *DomainService) AddOrUpdateDomainSetting(sessionId string, setting DomainSetting) {
  947. CheckSession(sessionId)
  948. domainSetting := databaseManager.GetDomainSetting()
  949. if domainSetting == nil {
  950. databaseManager.AddDomainSetting(&setting)
  951. } else {
  952. domainSetting.Domain = setting.Domain
  953. databaseManager.UpdateDomainSetting(domainSetting)
  954. }
  955. }
  956. type MessageTemplateService struct {
  957. }
  958. func (service *MessageTemplateService) GetMessageTemplate(sessionId string, name string) MessageTemplate {
  959. CheckSession(sessionId)
  960. template := databaseManager.GetMessageTemplate(name)
  961. if template == nil {
  962. return MessageTemplate{"", "", ""}
  963. }
  964. return *template
  965. }
  966. func (service *MessageTemplateService) DeleteMessageTemplate(sessionId string, name string) {
  967. CheckSession(sessionId)
  968. template := databaseManager.GetMessageTemplate(name)
  969. if template == nil {
  970. var err any = "模板不存在。"
  971. panic(err)
  972. }
  973. databaseManager.DeleteMessageTemplate(template)
  974. }
  975. func (service *MessageTemplateService) AddOrUpdateGetMessageTemplate(sessionId string, template MessageTemplate) {
  976. CheckSession(sessionId)
  977. exist := databaseManager.GetMessageTemplate(template.Name)
  978. if exist == nil {
  979. databaseManager.AddMessageTemplate(&template)
  980. } else {
  981. exist.Content = template.Content
  982. databaseManager.UpdateMessageTemplate(exist)
  983. }
  984. }
  985. type HolidayService struct {
  986. }
  987. func (service *HolidayService) GetMany(sessionId string) []HolidayInfo {
  988. CheckSession(sessionId)
  989. holidays := make([]HolidayInfo, 0)
  990. result := databaseManager.GetHolidayDayInfos(time.Now().AddDate(-1, 0, 0), time.Now().AddDate(1, 0, 0))
  991. for i := 0; i < len(result); i++ {
  992. holidays = append(holidays, *result[i])
  993. }
  994. return holidays
  995. }
  996. func (service *HolidayService) Add(sessionId string, holiday HolidayInfo) {
  997. CheckSession(sessionId)
  998. exist := databaseManager.GetHolidayInfo(holiday.Holiday)
  999. if exist != nil {
  1000. var err any = "节假日已存在。"
  1001. panic(err)
  1002. }
  1003. databaseManager.AddHolidayInfo(&holiday)
  1004. }
  1005. func (service *HolidayService) Delete(sessionId string, holiday HolidayInfo) {
  1006. CheckSession(sessionId)
  1007. exist := databaseManager.GetHolidayInfo(holiday.Holiday)
  1008. if exist == nil {
  1009. var err any = "节假日不存在。"
  1010. panic(err)
  1011. }
  1012. databaseManager.DeleteHolidayInfo(holiday.Id)
  1013. }
  1014. type ProgressService struct {
  1015. progressMap map[string]string
  1016. syncLocker *sync.Mutex
  1017. }
  1018. func (service *ProgressService) initialize() {
  1019. service.syncLocker = new(sync.Mutex)
  1020. service.progressMap = make(map[string]string)
  1021. }
  1022. func (service *ProgressService) AddOrUpdateProgress(progressId string, progressValue string) {
  1023. service.syncLocker.Lock()
  1024. service.progressMap[progressId] = progressValue
  1025. service.syncLocker.Unlock()
  1026. }
  1027. func (service *ProgressService) GetProgress(progressId string) string {
  1028. service.syncLocker.Lock()
  1029. if value, ok := service.progressMap[progressId]; ok {
  1030. service.syncLocker.Unlock()
  1031. return value
  1032. } else {
  1033. service.syncLocker.Unlock()
  1034. return "0"
  1035. }
  1036. }
  1037. func (service *ProgressService) DeleteProgress(progressId string) {
  1038. service.syncLocker.Lock()
  1039. delete(service.progressMap, progressId)
  1040. service.syncLocker.Unlock()
  1041. }