pic_sync.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 checkGateUserPicSyncParam(req *pb_v1.GateUserPicSyncRequest) 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 GateUserPicSync(ctx context.Context, req *pb_v1.GateUserPicSyncRequest) (reply *pb_v1.GateUserPicSyncReply, err error) {
  28. reply = &pb_v1.GateUserPicSyncReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. err = checkGateUserPicSyncParam(req)
  42. if err != nil {
  43. return nil, err
  44. }
  45. up := dbmodel.TUserPic{}
  46. where := [][2]interface{}{}
  47. where = dbmodel.WhereAdd(where, "id", req.Id)
  48. err = up.Find(database.DB(), where)
  49. if err != nil && err != gorm.ErrRecordNotFound {
  50. return nil, errors.DataBaseError
  51. }
  52. if up.ID == 0 {
  53. return nil, errors.ErrRecordNotFound
  54. }
  55. if up.ApproveStatus != 2 {
  56. return nil, status.Error(10003, "当前状态不能同步")
  57. }
  58. if time.Now().Unix()-up.UpdatedAt.Unix() < 60 {
  59. return nil, status.Error(10003, "下发操作间隔时间不能小于1分钟")
  60. }
  61. uid, _ := strconv.ParseInt(up.Uid, 10, 64)
  62. unitInfo, _, _, err := gate_utils.GetUserUnitIds(req.GardenId, []int64{uid})
  63. if err != nil {
  64. return nil, err
  65. }
  66. if len(unitInfo[uid]) == 0 {
  67. return reply, nil
  68. }
  69. devices, err := getAllPicDevices(unitInfo[uid], up.GardenId)
  70. if err != nil {
  71. return nil, errors.DataBaseError
  72. }
  73. //if !checkOnlineStatus(devices) {
  74. // return nil, status.Error(10003, "当前有设备未在线,无法完成下发,请检查设备后审批")
  75. //}
  76. gps, err := GetGatePicsByRecordId(up.ID, false)
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. needAdds, needUpdates := whichDeviceNeedAdd(devices, gps)
  81. db := database.DB().Begin()
  82. if err := WhitePicAddToDb(up, needAdds, db); err != nil {
  83. db.Rollback()
  84. return nil, err
  85. }
  86. db.Commit()
  87. WhitePicAddToDevice(up, needAdds)
  88. WhitePicUpdate(up, needUpdates, gps)
  89. values := map[string]interface{}{
  90. "updated_at": time.Now(),
  91. }
  92. _ = up.Update(database.DB(), where, values)
  93. return reply, nil
  94. }