pic_approve.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. "strconv"
  16. "time"
  17. )
  18. func checkGateUserPicApproveParam(req *pb_v1.GateUserPicApproveRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. case req.Id == 0:
  23. return status.Error(10003, "记录id不能为空")
  24. }
  25. return nil
  26. }
  27. func WhitePicAddToDb(up dbmodel.TUserPic, devices []dbmodel.TGate, db *gorm.DB) error {
  28. now := time.Now()
  29. gps := []dbmodel.TGatePic{}
  30. for _, v := range devices {
  31. // 不支持人脸的跳过
  32. if gate_utils.GateProtocolFuntionMap[v.Protocol][1] != 1 {
  33. continue
  34. }
  35. item := dbmodel.TGatePic{}
  36. item.RecordId = up.ID
  37. item.Status = 1
  38. item.DeviceId = v.ID
  39. item.CreatedAt = now
  40. item.UpdatedAt = now
  41. item.Sn = v.Sn
  42. item.Protocol = v.Protocol
  43. gps = append(gps, item)
  44. }
  45. if len(gps) == 0 {
  46. return nil
  47. }
  48. gp := dbmodel.TGatePic{}
  49. err := gp.InsertMulti(db, &gps)
  50. if err != nil {
  51. return errors.DataBaseError
  52. }
  53. return nil
  54. }
  55. func WhitePicAddToDevice(up dbmodel.TUserPic, devices []dbmodel.TGate) {
  56. // 白名单添加到设备
  57. for _, v := range devices {
  58. device := v
  59. whiter := NewPicWhiter(&up, &device, nil)
  60. if whiter == nil {
  61. continue
  62. }
  63. whiter.PersonAdd()
  64. whiter.PicAdd()
  65. if whiter.Command() {
  66. gate_utils.CommandCacheIncrease(device.Sn, device.Protocol)
  67. }
  68. }
  69. }
  70. func WhitePicUpdate(up dbmodel.TUserPic, devices map[int64]dbmodel.TGate, gps []dbmodel.TGatePic) {
  71. // 白名单更新到设备
  72. for _, v := range gps {
  73. if _, ok := devices[v.DeviceId]; !ok {
  74. continue
  75. }
  76. gp := v
  77. device := devices[v.DeviceId]
  78. whiter := NewPicWhiter(&up, &device, &gp)
  79. whiter.PicUpdate()
  80. if whiter.Command() {
  81. gate_utils.CommandCacheIncrease(device.Sn, device.Protocol)
  82. }
  83. }
  84. }
  85. func GetGatePicsByRecordId(recordId int64, excluedSuc bool) ([]dbmodel.TGatePic, error) {
  86. g := dbmodel.TGatePic{}
  87. where := [][2]interface{}{}
  88. where = dbmodel.WhereAdd(where, "record_id", recordId)
  89. if excluedSuc {
  90. where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc)
  91. }
  92. list, err := g.List(database.DB(), where, nil, -1, -1)
  93. if err != nil {
  94. return nil, errors.DataBaseError
  95. }
  96. return list, nil
  97. }
  98. func GetDevicesByIds(ids []int64) ([]dbmodel.TGate, error) {
  99. g := dbmodel.TGate{}
  100. where := [][2]interface{}{}
  101. where = dbmodel.WhereAdd(where, "id in", ids)
  102. list, err := g.List(database.DB(), where, nil, -1, -1)
  103. if err != nil {
  104. return nil, errors.DataBaseError
  105. }
  106. return list, nil
  107. }
  108. func getAllPicDevices(unitIds []int64, gardenId int64) ([]dbmodel.TGate, error) {
  109. up := dbmodel.TGateUnit{}
  110. where := [][2]interface{}{}
  111. where = dbmodel.WhereAdd(where, "garden_id", gardenId)
  112. where = dbmodel.WhereAdd(where, "unit_id in", unitIds)
  113. protocols := []int32{}
  114. for protocol, array := range gate_utils.GateProtocolFuntionMap {
  115. if array[1] == 1 {
  116. protocols = append(protocols, protocol)
  117. }
  118. }
  119. where = dbmodel.WhereAdd(where, "protocol in", protocols)
  120. ulist, err := up.List(database.DB(), where, nil, -1, -1)
  121. if err != nil {
  122. return nil, errors.DataBaseError
  123. }
  124. if len(ulist) == 0 {
  125. return nil, nil
  126. }
  127. devices := make([]int64, len(ulist))
  128. for i, v := range ulist {
  129. devices[i] = v.DeviceId
  130. }
  131. return GetDevicesByIds(devices)
  132. }
  133. func checkOnlineStatus(devices []dbmodel.TGate) bool {
  134. for _, v := range devices {
  135. if v.Status == 2 {
  136. return false
  137. }
  138. }
  139. return true
  140. }
  141. func whichDeviceNeedAdd(all []dbmodel.TGate, gps []dbmodel.TGatePic) (needAdds []dbmodel.TGate, needUpdates map[int64]dbmodel.TGate) {
  142. existM := map[int64]bool{}
  143. notSucM := map[int64]bool{}
  144. for _, v := range gps {
  145. existM[v.DeviceId] = true
  146. if v.Status != gate_utils.WhiteAddStatusAllSuc {
  147. notSucM[v.DeviceId] = true
  148. }
  149. }
  150. needUpdates = map[int64]dbmodel.TGate{}
  151. for _, v := range all {
  152. if notSucM[v.ID] {
  153. needUpdates[v.ID] = v
  154. }
  155. if !existM[v.ID] {
  156. needAdds = append(needAdds, v)
  157. }
  158. }
  159. return needAdds, needUpdates
  160. }
  161. func GateUserPicApprove(ctx context.Context, req *pb_v1.GateUserPicApproveRequest) (reply *pb_v1.GateUserPicApproveReply, err error) {
  162. reply = &pb_v1.GateUserPicApproveReply{}
  163. // 捕获各个task中的异常并返回给调用者
  164. defer func() {
  165. if r := recover(); r != nil {
  166. err = fmt.Errorf("%+v", r)
  167. e := &status.Status{}
  168. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  169. logger.Error("err",
  170. zap.String("system_err", err.Error()),
  171. zap.Stack("stacktrace"))
  172. }
  173. }
  174. }()
  175. err = checkGateUserPicApproveParam(req)
  176. if err != nil {
  177. return nil, err
  178. }
  179. up := dbmodel.TUserPic{}
  180. where := [][2]interface{}{}
  181. where = dbmodel.WhereAdd(where, "id", req.Id)
  182. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  183. err = up.Find(database.DB(), where)
  184. if err != nil && err != gorm.ErrRecordNotFound {
  185. return nil, errors.DataBaseError
  186. }
  187. if up.ApproveStatus != 1 {
  188. return nil, status.Error(10003, "当前状态不能审核")
  189. }
  190. // 审核不通过
  191. if !req.Status {
  192. values := map[string]interface{}{
  193. "feedback": req.Feedback,
  194. "approve_status": 3,
  195. "approved_at": time.Now().Unix(),
  196. }
  197. err = up.Update(database.DB(), where, values)
  198. if err != nil {
  199. return nil, errors.DataBaseError
  200. }
  201. return reply, nil
  202. }
  203. uid, _ := strconv.ParseInt(up.Uid, 10, 64)
  204. unitInfo, _, _, err := gate_utils.GetUserUnitIds(req.GardenId, []int64{uid})
  205. if err != nil {
  206. return nil, err
  207. }
  208. if len(unitInfo[uid]) == 0 {
  209. return reply, nil
  210. }
  211. // 审核通过
  212. devices, err := getAllPicDevices(unitInfo[uid], up.GardenId)
  213. if err != nil {
  214. return nil, errors.DataBaseError
  215. }
  216. //if !checkOnlineStatus(devices) {
  217. // return nil, status.Error(10003, "当前有设备未在线,无法完成下发,请检查设备后审批")
  218. //}
  219. gps, err := GetGatePicsByRecordId(up.ID, false)
  220. if err != nil {
  221. return nil, errors.DataBaseError
  222. }
  223. needAdds, needUpdates := whichDeviceNeedAdd(devices, gps)
  224. db := database.DB().Begin()
  225. values := map[string]interface{}{
  226. "feedback": req.Feedback,
  227. "approve_status": 2,
  228. "approved_at": time.Now().Unix(),
  229. }
  230. err = up.Update(db, where, values)
  231. if err != nil {
  232. db.Rollback()
  233. return nil, errors.DataBaseError
  234. }
  235. if err := WhitePicAddToDb(up, needAdds, db); err != nil {
  236. db.Rollback()
  237. return nil, err
  238. }
  239. db.Commit()
  240. WhitePicAddToDevice(up, needAdds)
  241. WhitePicUpdate(up, needUpdates, gps)
  242. return reply, nil
  243. }