gate_unit_add.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package gate_unit
  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/gate_pic"
  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 checkGateUnitAddParam(req *pb_v1.GateUnitAddRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.DeviceId == 0:
  24. return status.Error(10003, "设备不能为空")
  25. }
  26. return nil
  27. }
  28. func getGateInfo(deviceId int64, sn string, protocol int32) (*dbmodel.TGate, error) {
  29. p := &dbmodel.TGate{}
  30. where := [][2]interface{}{}
  31. if deviceId > 0 {
  32. where = dbmodel.WhereAdd(where, "id", deviceId)
  33. } else {
  34. where = dbmodel.WhereAdd(where, "sn", sn)
  35. where = dbmodel.WhereAdd(where, "protocol", protocol)
  36. }
  37. err := p.Find(database.DB(), where)
  38. if err != nil && err != gorm.ErrRecordNotFound {
  39. return nil, errors.DataBaseError
  40. }
  41. if p.ID == 0 {
  42. return nil, errors.ErrRecordNotFound
  43. }
  44. return p, nil
  45. }
  46. func getUidsByUnit(gardenId int64, unitIds []int64) ([]int64, error) {
  47. mreq := pb_v1.GardenHouseholdUidsFromUnitIdRequest{GardenId: gardenId, UnitIds: unitIds}
  48. mreply, err := pb.Garden.GardenHouseholdUidsFromUnitId(context.Background(), &mreq)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return mreply.Uids, nil
  53. }
  54. func checkNeedAddWhite(gateInfo *dbmodel.TGate, gardenId int64, unitIds []int64, db *gorm.DB) error {
  55. if gate_utils.GateProtocolFuntionMap[gateInfo.Protocol][1] != 1 {
  56. return nil
  57. }
  58. if len(unitIds) == 0 {
  59. return nil
  60. }
  61. uids, err := getUidsByUnit(gardenId, unitIds)
  62. if err != nil {
  63. return err
  64. }
  65. // 查找所有与单元相关用户的照片申请记录
  66. up := dbmodel.TUserPic{}
  67. where := [][2]interface{}{}
  68. where = dbmodel.WhereAdd(where, "garden_id", gardenId)
  69. where = dbmodel.WhereAdd(where, "uid in", uids)
  70. where = dbmodel.WhereAdd(where, "approve_status", 2)
  71. ulist, err := up.List(db, where, nil, -1, -1)
  72. if err != nil {
  73. return errors.DataBaseError
  74. }
  75. if len(ulist) == 0 {
  76. return nil
  77. }
  78. rids := make([]int64, len(ulist))
  79. m := map[int64]dbmodel.TUserPic{}
  80. for i, v := range ulist {
  81. rids[i] = v.ID
  82. m[v.ID] = v
  83. }
  84. // 查找照片记录已添加的设备
  85. gp := dbmodel.TGatePic{}
  86. where = [][2]interface{}{}
  87. where = dbmodel.WhereAdd(where, "record_id in", rids)
  88. glist, err := gp.List(db, where, nil, -1, -1)
  89. if err != nil {
  90. return errors.DataBaseError
  91. }
  92. added := map[int64]bool{}
  93. for _, v := range glist {
  94. if v.DeviceId == gateInfo.ID {
  95. added[v.RecordId] = true
  96. }
  97. }
  98. // 得到还需添向当前设备加百名单的照片记录
  99. needAdds := []dbmodel.TUserPic{}
  100. for rid, v := range m {
  101. if added[rid] {
  102. continue
  103. }
  104. needAdds = append(needAdds, v)
  105. }
  106. if len(needAdds) == 0 {
  107. return nil
  108. }
  109. // 添加设备和照片的关联记录
  110. for _, v := range needAdds {
  111. err = gate_pic.WhitePicAddToDb(v, []dbmodel.TGate{*gateInfo}, db)
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. // 下发数据到设备
  117. for _, v := range needAdds {
  118. gate_pic.WhitePicAddToDevice(v, []dbmodel.TGate{*gateInfo})
  119. }
  120. return nil
  121. }
  122. func GateUnitAdd(ctx context.Context, req *pb_v1.GateUnitAddRequest) (reply *pb_v1.GateUnitAddReply, err error) {
  123. reply = &pb_v1.GateUnitAddReply{}
  124. // 捕获各个task中的异常并返回给调用者
  125. defer func() {
  126. if r := recover(); r != nil {
  127. err = fmt.Errorf("%+v", r)
  128. e := &status.Status{}
  129. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  130. logger.Error("err",
  131. zap.String("system_err", err.Error()),
  132. zap.Stack("stacktrace"))
  133. }
  134. }
  135. }()
  136. err = checkGateUnitAddParam(req)
  137. if err != nil {
  138. return nil, err
  139. }
  140. gateInfo, err := getGateInfo(req.DeviceId, "", 0)
  141. if err != nil {
  142. return nil, err
  143. }
  144. now := time.Now()
  145. p := &dbmodel.TGateUnit{}
  146. where := [][2]interface{}{}
  147. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  148. where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
  149. list, err := p.List(database.DB(), where, nil, -1, -1)
  150. if err != nil {
  151. return nil, errors.DataBaseError
  152. }
  153. // 确定要删除的和要增加的
  154. addArray := []dbmodel.TGateUnit{}
  155. addUnitIds := []int64{}
  156. delArray := []int64{}
  157. m := map[int64]bool{}
  158. for _, v := range list {
  159. m[v.UnitId] = true
  160. }
  161. for _, v := range req.UnitId {
  162. if m[v] {
  163. delete(m, v)
  164. continue
  165. }
  166. item := dbmodel.TGateUnit{
  167. UnitId: v,
  168. GardenId: req.GardenId,
  169. DeviceId: req.DeviceId,
  170. CreatedAt: now,
  171. UpdatedAt: now,
  172. Protocol: gateInfo.Protocol,
  173. Sn: gateInfo.Sn,
  174. }
  175. addArray = append(addArray, item)
  176. addUnitIds = append(addUnitIds, v)
  177. }
  178. for k, _ := range m {
  179. delArray = append(delArray, k)
  180. }
  181. if len(delArray) > 0 {
  182. return nil, status.Error(10003, "范围不能缩减")
  183. }
  184. db := database.DB().Begin()
  185. err = p.InsertMulti(db, &addArray)
  186. if err != nil {
  187. db.Rollback()
  188. return nil, errors.DataBaseError
  189. }
  190. err = checkNeedAddWhite(gateInfo, req.GardenId, addUnitIds, db)
  191. if err != nil {
  192. db.Rollback()
  193. return nil, err
  194. }
  195. db.Commit()
  196. return reply, nil
  197. }