team.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/errors"
  6. "access-control-monitor/model/v1"
  7. param_v1 "access-control-monitor/param/v1"
  8. "access-control-monitor/pb"
  9. pb_v1 "access-control-monitor/pb/v1"
  10. "context"
  11. "github.com/jaryhe/gopkgs/database"
  12. "github.com/jaryhe/gopkgs/logger"
  13. "github.com/jinzhu/gorm"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. "access-control-monitor/parser"
  17. "time"
  18. )
  19. // 班组注册
  20. func TeamIn(db *gorm.DB, body param_v1.TeamInfoBody, projectId int64, code string, now time.Time) error {
  21. p := &model.TeamInoutInfo{
  22. ProjectId: projectId,
  23. Code: code,
  24. TeamSysNo: body.TeamSysNo,
  25. OccurTime: now,
  26. InOut: 1,
  27. }
  28. if err := p.Insert(db); err != nil {
  29. return errors.DataBaseError
  30. }
  31. return nil
  32. }
  33. func TeamOut(db *gorm.DB, teamSysNo int, now time.Time, projectId int64, code string) error {
  34. p := &model.TeamInoutInfo{
  35. ProjectId: projectId,
  36. Code: code,
  37. TeamSysNo: teamSysNo,
  38. OccurTime: now,
  39. InOut: 2,
  40. }
  41. if err := p.Insert(db); err != nil {
  42. return errors.DataBaseError
  43. }
  44. return nil
  45. }
  46. func PostTeamToProvincial(body param_v1.TeamInfoBody, code string) (string, error) {
  47. mreq := pb_v1.ProvincialPostTeamRequest{
  48. Code:code,
  49. TeamLeaderPhone:body.TeamLeaderPhone,
  50. TeamLeaderName:body.TeamLeaderName,
  51. TeamSysNo:int64(body.TeamSysNo),
  52. TeamName:body.TeamName,
  53. CorpName:body.CorpName,
  54. CorpCode:body.CorpCode,
  55. }
  56. mreply, err := pb.Provincial.ProvincialPostTeam(context.Background(), &mreq)
  57. if err != nil {
  58. logger.Error("ProvincialPostTeam",
  59. zap.String("err", err.Error()))
  60. return "", err
  61. }
  62. return mreply.Id, nil
  63. }
  64. func TeamInfoRegister(body param_v1.TeamInfoBody, projectId int64, code string) error {
  65. timeNow := time.Now()
  66. p := &model.TeamMasterInfo{
  67. Code: code,
  68. ProjectId: projectId,
  69. CorpName: body.CorpName,
  70. CorpCode: body.CorpCode,
  71. TeamLeaderName: body.TeamLeaderName,
  72. TeamLeaderPhone: body.TeamLeaderPhone,
  73. TeamName: body.TeamName,
  74. TeamSysNo: body.TeamSysNo,
  75. Remark: body.Remark,
  76. CreatedAt: timeNow,
  77. UpdatedAt: timeNow,
  78. Sn: body.Sn,
  79. }
  80. old := model.TeamMasterInfo{}
  81. where := map[string]interface{}{
  82. "ProjectId": projectId,
  83. }
  84. or := map[string]interface{}{
  85. "TeamSysNo": body.TeamSysNo,
  86. "TeamName":body.TeamName,
  87. }
  88. err := old.FindOr(database.DB(), where, or)
  89. if err != nil && err != gorm.ErrRecordNotFound {
  90. return errors.DataBaseError
  91. }
  92. if old.ID > 0 {
  93. return status.Error(10002, "项目中班组(班组号或班组名)已存在")
  94. }
  95. // TODO
  96. if parser.Conf.UseProvincial {
  97. provincialId, err := PostTeamToProvincial(body, code)
  98. if err != nil {
  99. return err
  100. }
  101. p.ProvincialVerifyID = provincialId
  102. }
  103. db := database.DB().Begin()
  104. err = p.Insert(db)
  105. if err != nil {
  106. db.Rollback()
  107. return errors.DataBaseError
  108. }
  109. err = TeamIn(db, body, projectId, code, timeNow)
  110. if err != nil {
  111. db.Rollback()
  112. return errors.DataBaseError
  113. }
  114. db.Commit()
  115. return nil
  116. return nil
  117. }
  118. func TeamDelete(teamSysNo []int, projectId int64, code string) error {
  119. old := model.TeamMasterInfo{}
  120. where := map[string]interface{}{
  121. "TeamSysNo in": teamSysNo,
  122. "ProjectId": projectId,
  123. }
  124. db := database.DB().Begin()
  125. infos, err := model.GetTeamSysNo(db, projectId, teamSysNo)
  126. if err != nil {
  127. db.Rollback()
  128. return errors.DataBaseError
  129. }
  130. tmp := model.ProjectWorkInfo{}
  131. err = tmp.Find(db, where)
  132. if err != nil && err != gorm.ErrRecordNotFound {
  133. db.Rollback()
  134. return errors.DataBaseError
  135. }
  136. if tmp.Id > 0 {
  137. db.Rollback()
  138. return status.Error(10002, "请先删除班组下的人员")
  139. }
  140. err = old.Delete(db, where)
  141. if err != nil {
  142. db.Rollback()
  143. return errors.DataBaseError
  144. }
  145. for _, v := range infos {
  146. err = TeamOut(database.DB(), v.TeamSysNo, time.Now(), projectId, code)
  147. if err != nil {
  148. db.Rollback()
  149. return errors.DataBaseError
  150. }
  151. }
  152. db.Commit()
  153. return nil
  154. }
  155. func TeamUpdate(body param_v1.TeamInfoBody, projectId int64, code string) error {
  156. p := &model.TeamMasterInfo{}
  157. old := model.TeamMasterInfo{}
  158. where := map[string]interface{}{
  159. "TeamSysNo": body.TeamSysNo,
  160. "ProjectId": projectId,
  161. }
  162. err := old.Find(database.DB(), where)
  163. if err != nil && err != gorm.ErrRecordNotFound {
  164. return errors.DataBaseError
  165. }
  166. // 不存在
  167. if old.ID == 0 {
  168. return status.Error(10002, "没有找到该班组")
  169. }
  170. if old.TeamName != body.TeamName {
  171. tmp := model.TeamMasterInfo{}
  172. where := map[string]interface{}{
  173. "TeamName": body.TeamName,
  174. "ProjectId": projectId,
  175. "TeamSysNo !=": body.TeamSysNo,
  176. }
  177. err := tmp.Find(database.DB(), where)
  178. if err != nil && err != gorm.ErrRecordNotFound {
  179. return errors.DataBaseError
  180. }
  181. //
  182. if tmp.ID > 0 {
  183. return status.Error(10002, "班组名称重复")
  184. }
  185. }
  186. // 已存在则更新
  187. where["ID"] = old.ID
  188. values := map[string]interface{}{
  189. "CorpName": body.CorpName,
  190. "CorpCode": body.CorpCode,
  191. "TeamLeaderName": body.TeamLeaderName,
  192. "TeamLeaderPhone": body.TeamLeaderPhone,
  193. "TeamName": body.TeamName,
  194. "Remark": body.Remark,
  195. }
  196. err = p.Update(database.DB(), where, values)
  197. if err != nil {
  198. return errors.DataBaseError
  199. }
  200. return nil
  201. }