household_sync_check.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. dbmodel "property-device/model"
  13. pb_v1 "property-device/pb/v1"
  14. "property-device/utils/gate_utils"
  15. )
  16. func checkHouseholdGateSyncCheckParam(req *pb_v1.HouseholdGateSyncCheckRequest) error {
  17. switch {
  18. case req.GardenId == 0:
  19. return status.Error(10003, "小区不能为空")
  20. case req.UnitId == 0:
  21. return status.Error(10003, "单元id不能为空")
  22. case req.Uid == 0:
  23. return status.Error(10003, "uid不能为空")
  24. }
  25. return nil
  26. }
  27. // 住户申请房屋后,检查是否有设备需要同步人脸
  28. func HouseholdGateSyncCheck(ctx context.Context, req *pb_v1.HouseholdGateSyncCheckRequest) (reply *pb_v1.HouseholdGateSyncCheckReply, err error) {
  29. reply = &pb_v1.HouseholdGateSyncCheckReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. err = checkHouseholdGateSyncCheckParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. // 查找审核通过的录入记录
  47. up := dbmodel.TUserPic{}
  48. where := [][2]interface{}{}
  49. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  50. where = dbmodel.WhereAdd(where, "uid", req.Uid)
  51. err = up.Find(database.DB(), where)
  52. if err != nil && err != gorm.ErrRecordNotFound {
  53. return nil, errors.DataBaseError
  54. }
  55. if up.ID == 0 {
  56. return reply, nil
  57. }
  58. if up.ApproveStatus != 2 {
  59. return reply, nil
  60. }
  61. // 查找绑定了该单元的设备
  62. gu := dbmodel.TGateUnit{}
  63. where = [][2]interface{}{}
  64. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  65. where = dbmodel.WhereAdd(where, "unit_id", req.UnitId)
  66. protocols := []int32{}
  67. for protocol, array := range gate_utils.GateProtocolFuntionMap {
  68. if array[1] == 1 {
  69. protocols = append(protocols, protocol)
  70. }
  71. }
  72. if len(protocols) == 0 {
  73. return reply, nil
  74. }
  75. where = dbmodel.WhereAdd(where, "protocol in", protocols)
  76. guList, err := gu.List(database.DB(), where, nil, -1, -1)
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. if len(guList) == 0 {
  81. return reply, nil
  82. }
  83. deviceIds := make([]int64, len(guList))
  84. for i, v := range guList {
  85. deviceIds[i] = v.DeviceId
  86. }
  87. // 查找记录已下发的设备
  88. gp := dbmodel.TGatePic{}
  89. where = [][2]interface{}{}
  90. where = dbmodel.WhereAdd(where, "record_id", up.ID)
  91. gplist, err := gp.List(database.DB(), where, nil, -1, -1)
  92. if err != nil {
  93. return nil, errors.DataBaseError
  94. }
  95. exist := map[int64]bool{}
  96. for _, v := range gplist {
  97. exist[v.DeviceId] = true
  98. }
  99. // 得到需要下发的设备
  100. needAddIds := []int64{}
  101. for _, v := range deviceIds {
  102. if !exist[v] {
  103. needAddIds = append(needAddIds, v)
  104. }
  105. }
  106. if len(needAddIds) == 0 {
  107. return reply, nil
  108. }
  109. // 获取设备信息
  110. devices, err := GetDevicesByIds(needAddIds)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if len(devices) == 0 {
  115. return reply, nil
  116. }
  117. // 进行下发
  118. db := database.DB().Begin()
  119. if err := WhitePicAddToDb(up, devices, db); err != nil {
  120. db.Rollback()
  121. return nil, err
  122. }
  123. db.Commit()
  124. WhitePicAddToDevice(up, devices)
  125. return reply, nil
  126. }