pic_add.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package gate_pic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "gorm.io/gorm"
  11. "property-device/errors"
  12. "property-device/impl/v1/oss_utils"
  13. dbmodel "property-device/model"
  14. "property-device/pb"
  15. pb_v1 "property-device/pb/v1"
  16. "property-device/utils/gate_utils"
  17. "time"
  18. )
  19. func checkGateUserPicAddParam(req *pb_v1.GateUserPicAddRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.Uid == 0:
  24. return status.Error(10003, "人员不能为空")
  25. case req.Name == "":
  26. return status.Error(10003, "姓名不能为空")
  27. case req.PicUrl == "":
  28. return status.Error(10003, "照片不能为空")
  29. }
  30. return nil
  31. }
  32. const SystemMsgCodeFaceAdd = "8"
  33. func picMsgAdd(gardenId int64) {
  34. mreq := pb_v1.SystemMsgAddRequest{
  35. GardenId: gardenId,
  36. Content: "有新的人脸申请待审批",
  37. Code: SystemMsgCodeFaceAdd,
  38. Uid: 0,
  39. }
  40. pb.Garden.SystemMsgAdd(context.Background(), &mreq)
  41. }
  42. func GateUserPicAdd(ctx context.Context, req *pb_v1.GateUserPicAddRequest) (reply *pb_v1.GateUserPicAddReply, err error) {
  43. reply = &pb_v1.GateUserPicAddReply{}
  44. // 捕获各个task中的异常并返回给调用者
  45. defer func() {
  46. if r := recover(); r != nil {
  47. err = fmt.Errorf("%+v", r)
  48. e := &status.Status{}
  49. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  50. logger.Error("err",
  51. zap.String("system_err", err.Error()),
  52. zap.Stack("stacktrace"))
  53. }
  54. }
  55. }()
  56. err = checkGateUserPicAddParam(req)
  57. if err != nil {
  58. return nil, err
  59. }
  60. now := time.Now()
  61. unitInfo, houseNames, userType, err := gate_utils.GetUserUnitIds(req.GardenId, []int64{req.Uid})
  62. if err != nil {
  63. return nil, err
  64. }
  65. if len(unitInfo[req.Uid]) == 0 {
  66. return nil, status.Error(10003, "请先绑定房屋")
  67. }
  68. gunit := dbmodel.TGateUnit{}
  69. where := [][2]interface{}{}
  70. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  71. where = dbmodel.WhereAdd(where, "unit_id in", unitInfo[req.Uid])
  72. // 支持人脸的设备
  73. protocols := []int32{}
  74. for k, v := range gate_utils.GateProtocolFuntionMap {
  75. if v[1] == 1 {
  76. protocols = append(protocols, k)
  77. }
  78. }
  79. where = dbmodel.WhereAdd(where, "protocol in", protocols)
  80. count, err := gunit.Count(database.DB(), where, nil)
  81. if err != nil {
  82. return nil, errors.DataBaseError
  83. }
  84. if count == 0 {
  85. return nil, status.Error(10003, "当前无可用设备")
  86. }
  87. p := &dbmodel.TUserPic{}
  88. where = [][2]interface{}{}
  89. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  90. where = dbmodel.WhereAdd(where, "uid", req.Uid)
  91. err = p.Find(database.DB(), where)
  92. if err != nil && err != gorm.ErrRecordNotFound {
  93. return nil, errors.DataBaseError
  94. }
  95. if p.ID > 0 && p.ApproveStatus == 2 {
  96. return nil, status.Error(10003, "已存在审核通过的记录,不能重复申请,请联系管理人员")
  97. }
  98. if p.ID > 0 && p.ApproveStatus == 1 {
  99. return nil, status.Error(10003, "已存在待审核的记录,不能重复申请,请联系管理人员")
  100. }
  101. if p.ID > 0 {
  102. values := map[string]interface{}{
  103. "name": req.Name,
  104. "id_number": req.IdNumber,
  105. "pic_url": req.PicUrl,
  106. "approve_status": 1,
  107. "updated_at": now,
  108. "down_status": 1,
  109. "user_type": userType,
  110. }
  111. db := database.DB().Begin()
  112. err = p.Update(db, where, values)
  113. if err != nil {
  114. db.Rollback()
  115. return nil, errors.DataBaseError
  116. }
  117. if err = oss_utils.OssObjAdd([]string{req.PicUrl}, nil); err != nil {
  118. db.Rollback()
  119. return nil, err
  120. }
  121. db.Commit()
  122. picMsgAdd(req.GardenId)
  123. return reply, nil
  124. }
  125. p.PicUrl = req.PicUrl
  126. p.GardenId = req.GardenId
  127. p.Name = req.Name
  128. p.Uid = fmt.Sprintf("%d", req.Uid)
  129. p.IdNumber = req.IdNumber
  130. p.ApproveStatus = 1
  131. p.CreatedAt = now
  132. p.UpdatedAt = now
  133. p.DownStatus = 1
  134. p.Phone = req.Phone
  135. p.HouseName = houseNames[req.Uid]
  136. p.UserType = userType
  137. db := database.DB().Begin()
  138. err = p.Insert(db)
  139. if err != nil {
  140. db.Rollback()
  141. return nil, err
  142. }
  143. if err = oss_utils.OssObjAdd([]string{req.PicUrl}, nil); err != nil {
  144. db.Rollback()
  145. return nil, err
  146. }
  147. db.Commit()
  148. picMsgAdd(req.GardenId)
  149. return reply, nil
  150. }