staff.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package v1
  4. import (
  5. "access-control-monitor/consts"
  6. "access-control-monitor/errors"
  7. "access-control-monitor/model/v1"
  8. param_v1 "access-control-monitor/param/v1"
  9. "access-control-monitor/utils"
  10. "google.golang.org/grpc/status"
  11. "github.com/jinzhu/gorm"
  12. "github.com/jaryhe/gopkgs/database"
  13. )
  14. // 注册员工
  15. func Register(body param_v1.RegisterBody, projectId int64) error {
  16. staff := &model.Staff{}
  17. utils.StructCopy(staff, &body, "json")
  18. staff.Status = consts.StaffNew
  19. staff.ProjectId = projectId
  20. switch {
  21. case staff.Name == "":
  22. return status.Error(10003, "参数错误,姓名为空")
  23. case staff.WorkNo == "":
  24. return status.Error(10003, "参数错误,工号为空")
  25. case staff.IdCert == "":
  26. return status.Error(10003, "参数错误,身份证号为空")
  27. case staff.StaffType == 0:
  28. return status.Error(10003, "参数错误,工种为空")
  29. case staff.WorkType == 0:
  30. return status.Error(10003, "参数错误,员工类型为空")
  31. }
  32. err := staff.Insert(database.DB())
  33. if err != nil {
  34. return errors.DataBaseError
  35. }
  36. return nil
  37. }
  38. // 设备主动推送删除员工
  39. func StaffDelete(sn string, work_nos []string) error {
  40. staff := &model.Staff{}
  41. filter := map[string]interface{}{"sn": sn}
  42. db := database.DB().Begin()
  43. for _, work_no := range work_nos {
  44. filter["work_no"] = work_no
  45. err := staff.Delete(db, filter)
  46. if err != nil {
  47. db.Rollback()
  48. return errors.DataBaseError
  49. }
  50. }
  51. db.Commit()
  52. return nil
  53. }
  54. // 设备被动删除接口----------------------------------------------------------------------------------------
  55. // 获取删除员工列表
  56. func GetDelete(sn string) ([]string, error) {
  57. staff := &model.Staff{}
  58. staffs := []model.Staff{}
  59. out := []string{}
  60. db := database.DB().Begin()
  61. filter := map[string]interface{}{"sn": sn, "status": consts.StaffStayDelete}
  62. err := staff.QueryAll(db, filter, &staffs)
  63. if err != nil {
  64. return nil, err
  65. }
  66. for _, v := range staffs {
  67. out = append(out, v.WorkNo)
  68. filed := map[string]interface{}{"status": consts.StaffStayDelete}
  69. err := v.UpdateSome(db, filed)
  70. if err != nil {
  71. db.Rollback()
  72. return nil, errors.DataBaseError
  73. }
  74. }
  75. // 无待删除人员
  76. if len(out) == 0 {
  77. return nil, errors.NoDeleteError
  78. }
  79. db.Commit()
  80. return out, nil
  81. }
  82. // 获取批量下载
  83. func GetBatchDownload(sn string) ([]param_v1.BatchDownload, error) {
  84. batchDownloads := []param_v1.BatchDownload{}
  85. staff := &model.Staff{}
  86. staffs := []model.Staff{}
  87. db := database.DB().Begin()
  88. filter := map[string]interface{}{"sn": sn, "status": consts.StaffNew}
  89. err := staff.QueryAll(db, filter, &staffs)
  90. if err != nil {
  91. if err != gorm.ErrRecordNotFound {
  92. return nil, errors.DataBaseError
  93. } else {
  94. return nil, errors.NoDownloadError
  95. }
  96. }
  97. for _, v := range staffs {
  98. batchDownload := param_v1.BatchDownload{Name: v.Name,
  99. WorkNo: v.WorkNo,
  100. IdCert: v.IdCert,
  101. Photo: v.Photo}
  102. batchDownloads = append(batchDownloads, batchDownload)
  103. filed := map[string]interface{}{"status": consts.StaffStayDownload}
  104. err := v.UpdateSome(db, filed)
  105. if err != nil {
  106. db.Rollback()
  107. return nil, errors.DataBaseError
  108. }
  109. }
  110. // 无待下发人员
  111. if len(batchDownloads) == 0 {
  112. return nil, errors.NoDownloadError
  113. }
  114. db.Commit()
  115. return batchDownloads, nil
  116. }
  117. // 删除员工
  118. func Delete(sn string) error {
  119. staff := &model.Staff{}
  120. filter := map[string]interface{}{"sn": sn, "status": consts.StaffStayDelete}
  121. err := staff.Delete(database.DB(), filter)
  122. if err != nil {
  123. return errors.DataBaseError
  124. }
  125. return nil
  126. }
  127. // 设置下发状态
  128. func SetDownloadStatus(sn string, status int) error {
  129. staff := &model.Staff{}
  130. fileds := map[string]interface{}{"status": status}
  131. filter := map[string]interface{}{"status": consts.StaffStayDownload, "sn": sn}
  132. err := staff.UpdateColumn(database.DB(), filter, fileds)
  133. if err != nil {
  134. return errors.DataBaseError
  135. }
  136. return nil
  137. }