staff_attendance.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "context"
  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. "access-control-monitor/pb"
  17. pb_v1 "access-control-monitor/pb/v1"
  18. "access-control-monitor/parser"
  19. )
  20. // 添加考勤数据至月表
  21. func addAttendanceToMonth(db *gorm.DB, content param_v1.AttendanceBody, projectId int64, code string, provincialId string) error {
  22. attendance := &model.WorkerAttendanceInfo{}
  23. dayTime := strings.Split(content.Datetime, " ")[0]
  24. month := utils.GetMonthFromDate(dayTime)
  25. if month < 1 {
  26. logger.Error("addAttendanceToMonth",
  27. zap.String("param", dayTime),
  28. zap.String("error", "get month failed"))
  29. return errors.ServiceError
  30. }
  31. attendance.TabType = month
  32. return addAttendance(db, content, projectId, code, attendance, provincialId)
  33. }
  34. // 添加考勤数据至当天表
  35. func addAttendanceToDay(db *gorm.DB, content param_v1.AttendanceBody, projectId int64, code string, provincialId string) error {
  36. attendance := &model.WorkerAttendanceInfo{
  37. TabType: model.AttendanceTabDay,
  38. }
  39. return addAttendance(db, content, projectId, code, attendance, provincialId)
  40. }
  41. func addAttendance(db *gorm.DB, content param_v1.AttendanceBody, projectId int64, code string, attendance *model.WorkerAttendanceInfo, provincialId string) error {
  42. attendance.Date = utils.ConvertDateTime(content.Datetime)
  43. attendance.TeamSysNo = content.TeamSysNo
  44. attendance.TeamName = content.TeamName
  45. attendance.WorkNo = content.WorkNo
  46. attendance.ProjectId = projectId
  47. attendance.Code = code
  48. attendance.WorkName = content.WorkName
  49. attendance.IDCardNumber = content.IDCardNumber
  50. attendance.IDCardType = content.IDCardType
  51. attendance.Direction = content.Direction
  52. attendance.Sn = content.Sn
  53. attendance.ProvincialVerifyID = provincialId
  54. err := attendance.Insert(database.DB())
  55. if err != nil {
  56. return errors.DataBaseError
  57. }
  58. return nil
  59. }
  60. func convertDate(date string) string {
  61. return utils.ConvertDateTime(date).Format("20060102150405")
  62. }
  63. func PostStaffAttendanceToProvincial(body param_v1.AttendanceBody, code string, oriCert string) (string, error) {
  64. mreq := pb_v1.ProvincialPostStaffAttendanceRequest{
  65. Code: code,
  66. TeamSysNo: int64(body.TeamSysNo),
  67. TeamName: body.TeamName,
  68. WorkerName:body.WorkName,
  69. IdCardType:int32(body.IDCardType),
  70. Direction:int32(body.Direction),
  71. Date:convertDate(body.Datetime),
  72. }
  73. //todo
  74. var err error
  75. mreq.IdCardNumber, err = utils.ProvincialEncrypt([]byte(body.IDCardNumber), []byte(parser.Conf.ThirdParty.Provincial.AppSecret))
  76. if err != nil {
  77. logger.Error("ProvincialEncrypt",
  78. zap.String("err", err.Error()))
  79. }
  80. mreply, err := pb.Provincial.ProvincialPostStaffAttendance(context.Background(), &mreq)
  81. if err != nil {
  82. logger.Error("ProvincialPostStaffAttendance",
  83. zap.String("err", err.Error()))
  84. return "", err
  85. }
  86. return mreply.Id, nil
  87. }
  88. // 添加考勤数据
  89. func AddAttendance(content param_v1.AttendanceBody, projectId int64, code string) error {
  90. provincialId := ""
  91. if parser.Conf.UseProvincial {
  92. provincialId, _ = PostStaffAttendanceToProvincial(content, code, content.IDCardNumber)
  93. }
  94. content.IDCardNumber = certEncrypt(content.IDCardNumber)
  95. dayTask := func(db *gorm.DB) error {
  96. return addAttendanceToDay(db, content, projectId, code, provincialId)
  97. }
  98. monthTask := func(db *gorm.DB) error {
  99. return addAttendanceToMonth(db, content, projectId, code, provincialId)
  100. }
  101. err := transtasker.Exec(database.DB(),
  102. transtasker.NewTask(dayTask),
  103. transtasker.NewTask(monthTask))
  104. return err
  105. }