staff_attendance.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "access-control-monitor/errors"
  6. "access-control-monitor/model/v1"
  7. param_v1 "access-control-monitor/param/v1"
  8. "access-control-monitor/utils"
  9. "fmt"
  10. "go.uber.org/zap"
  11. "strings"
  12. "github.com/jinzhu/gorm"
  13. "github.com/jaryhe/gopkgs/database"
  14. "github.com/jaryhe/gopkgs/logger"
  15. "github.com/jaryhe/gopkgs/tasker/transtasker"
  16. )
  17. // 添加考勤数据至月表
  18. func addAttendanceToMonth(db *gorm.DB, content []param_v1.AttendanceContent, projectId int64) error {
  19. for _, v := range content {
  20. attendance := &model.StaffAttendance{}
  21. dayTime := strings.Split(v.RecogTime, " ")[0]
  22. month := utils.GetMonthFromDate(dayTime)
  23. if month < 1 {
  24. logger.Error("addAttendanceToMonth",
  25. zap.String("param", dayTime),
  26. zap.String("error", "get month failed"))
  27. return errors.ServiceError
  28. }
  29. attendance.TabType = month
  30. filter := map[string]interface{}{"project_id": projectId, "work_no": v.WorkNo, "day_time": dayTime}
  31. err := attendance.Query(db, filter)
  32. // 当天有考勤数据
  33. if err == nil {
  34. attendanceStr := fmt.Sprintf("%s/%s-%d", attendance.Attendance, v.RecogTime, v.IsIn)
  35. fileds := map[string]interface{}{"attendance": attendanceStr, "in_out": v.IsIn, "updated_at":v.RecogTime}
  36. err := attendance.UpdateSome(db, fileds)
  37. if err != nil {
  38. return errors.DataBaseError
  39. }
  40. } else {
  41. // 无考勤数据
  42. if err == gorm.ErrRecordNotFound {
  43. attendance.WorkNo = v.WorkNo
  44. attendance.ProjectId = projectId
  45. attendance.DayTime = dayTime
  46. attendance.InOut = v.IsIn
  47. attendance.UpdatedAt = v.RecogTime
  48. attendance.Attendance = fmt.Sprintf("%s-%d", v.RecogTime, v.IsIn)
  49. err := attendance.Insert(db)
  50. if err != nil {
  51. return errors.DataBaseError
  52. }
  53. } else {
  54. return errors.DataBaseError
  55. }
  56. }
  57. }
  58. return nil
  59. }
  60. // 添加考勤数据至当天表
  61. func addAttendanceToDay(db *gorm.DB, content []param_v1.AttendanceContent, projectId int64) error {
  62. attendance := &model.StaffAttendance{
  63. TabType:model.AttendanceTabDay,
  64. }
  65. return addAttendance(db, content, projectId, attendance)
  66. }
  67. // 添加考勤数据至总表
  68. func addAttendanceToTotal(db *gorm.DB, content []param_v1.AttendanceContent, projectId int64) error {
  69. attendance := &model.StaffAttendance{
  70. TabType:model.AttendanceTab,
  71. }
  72. return addAttendance(db, content, projectId, attendance)
  73. }
  74. func addAttendance(db *gorm.DB, content []param_v1.AttendanceContent, projectId int64, attendance *model.StaffAttendance) error {
  75. for _, v := range content {
  76. dayTime := strings.Split(v.RecogTime, " ")[0]
  77. filter := map[string]interface{}{"project_id": projectId, "work_no": v.WorkNo, "day_time": dayTime}
  78. err := attendance.Query(database.DB(), filter)
  79. // 当天有考勤数据
  80. if err == nil {
  81. attendanceStr := fmt.Sprintf("%s/%s-%d", attendance.Attendance, v.RecogTime, v.IsIn)
  82. fileds := map[string]interface{}{"attendance": attendanceStr, "in_out": v.IsIn, "updated_at":v.RecogTime}
  83. err := attendance.UpdateSome(database.DB(), fileds)
  84. if err != nil {
  85. return errors.DataBaseError
  86. }
  87. } else {
  88. // 无考勤数据
  89. if err == gorm.ErrRecordNotFound {
  90. attendance.WorkNo = v.WorkNo
  91. attendance.ProjectId = projectId
  92. attendance.DayTime = dayTime
  93. attendance.InOut = v.IsIn
  94. attendance.UpdatedAt = v.RecogTime
  95. attendance.Attendance = fmt.Sprintf("%s-%d", v.RecogTime, v.IsIn)
  96. err := attendance.Insert(database.DB())
  97. if err != nil {
  98. return errors.DataBaseError
  99. }
  100. } else {
  101. return errors.DataBaseError
  102. }
  103. }
  104. }
  105. return nil
  106. }
  107. // 添加考勤数据
  108. func AddAttendance(content []param_v1.AttendanceContent, projectId int64) error {
  109. dayTask := func (db *gorm.DB) error {
  110. return addAttendanceToDay(db, content, projectId)
  111. }
  112. monthTask := func(db *gorm.DB) error {
  113. return addAttendanceToMonth(db, content, projectId)
  114. }
  115. totalTask := func(db *gorm.DB) error {
  116. return nil
  117. //return addAttendanceToTotal(db, content, projectId)
  118. }
  119. err := transtasker.Exec(database.DB(),
  120. transtasker.NewTask(dayTask),
  121. transtasker.NewTask(monthTask), transtasker.NewTask(totalTask))
  122. return err
  123. }