pic_del.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "time"
  16. )
  17. func checkGateUserPicDelParam(req *pb_v1.GateUserPicDelRequest) error {
  18. switch {
  19. case req.GardenId == 0:
  20. return status.Error(10003, "小区不能为空")
  21. case req.Id == 0:
  22. return status.Error(10003, "记录id不能为空")
  23. }
  24. return nil
  25. }
  26. func CheckUserPicDel(recordId int64, db *gorm.DB) error {
  27. gp := dbmodel.TGatePic{}
  28. up := dbmodel.TUserPic{}
  29. where := [][2]interface{}{}
  30. where = dbmodel.WhereAdd(where, "record_id", recordId)
  31. count, err := gp.Count(database.DB(), where, nil)
  32. if err != nil {
  33. return errors.DataBaseError
  34. }
  35. if count > 0 {
  36. return nil
  37. }
  38. where = [][2]interface{}{}
  39. where = dbmodel.WhereAdd(where, "id", recordId)
  40. err = up.Delete(database.DB(), where)
  41. if err != nil {
  42. return errors.DataBaseError
  43. }
  44. return nil
  45. }
  46. func WhitePicDelFromDb(up dbmodel.TUserPic, db *gorm.DB) ([]dbmodel.TGatePic, error) {
  47. gp := dbmodel.TGatePic{}
  48. where := [][2]interface{}{}
  49. where = dbmodel.WhereAdd(where, "record_id", up.ID)
  50. where = dbmodel.WhereAdd(where, "status >", gate_utils.WhiteAddStatusAllSuc)
  51. err := gp.Delete(db, where)
  52. if err != nil {
  53. return nil, errors.DataBaseError
  54. }
  55. t := time.Now().Add(-60 * time.Second)
  56. where = [][2]interface{}{}
  57. where = dbmodel.WhereAdd(where, "record_id", up.ID)
  58. where = dbmodel.WhereAdd(where, "status", gate_utils.WhiteAddStatusWait)
  59. where = dbmodel.WhereAdd(where, "created_at <", t.Format("2006-01-02 15:04:05"))
  60. err = gp.Delete(db, where)
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. where = [][2]interface{}{}
  65. where = dbmodel.WhereAdd(where, "record_id", up.ID)
  66. glist, err := gp.List(db, where, nil, -1, -1)
  67. if err != nil {
  68. return nil, errors.DataBaseError
  69. }
  70. if len(glist) > 0 {
  71. return glist, nil
  72. }
  73. where = [][2]interface{}{}
  74. where = dbmodel.WhereAdd(where, "id", up.ID)
  75. err = up.Delete(db, where)
  76. if err != nil {
  77. return nil, errors.DataBaseError
  78. }
  79. return nil, nil
  80. }
  81. func WhitePicDelFromDevice(up dbmodel.TUserPic, devices []dbmodel.TGate) {
  82. // 白名单添加到设备
  83. for _, v := range devices {
  84. device := v
  85. whiter := NewPicWhiter(&up, &device, nil)
  86. if whiter == nil {
  87. continue
  88. }
  89. whiter.PersonDel()
  90. if whiter.Command() {
  91. gate_utils.CommandCacheIncrease(device.Sn, device.Protocol)
  92. }
  93. }
  94. }
  95. func WhitePicDel(up dbmodel.TUserPic, db *gorm.DB) error {
  96. gplist, err := WhitePicDelFromDb(up, db)
  97. if err != nil {
  98. return err
  99. }
  100. if len(gplist) == 0 {
  101. return nil
  102. }
  103. deviceIds := make([]int64, len(gplist))
  104. for i, v := range gplist {
  105. deviceIds[i] = v.DeviceId
  106. }
  107. devices, err := GetDevicesByIds(deviceIds)
  108. if err != nil {
  109. return err
  110. }
  111. WhitePicDelFromDevice(up, devices)
  112. return nil
  113. }
  114. func GateUserPicDel(ctx context.Context, req *pb_v1.GateUserPicDelRequest) (reply *pb_v1.GateUserPicDelReply, err error) {
  115. reply = &pb_v1.GateUserPicDelReply{}
  116. // 捕获各个task中的异常并返回给调用者
  117. defer func() {
  118. if r := recover(); r != nil {
  119. err = fmt.Errorf("%+v", r)
  120. e := &status.Status{}
  121. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  122. logger.Error("err",
  123. zap.String("system_err", err.Error()),
  124. zap.Stack("stacktrace"))
  125. }
  126. }
  127. }()
  128. err = checkGateUserPicDelParam(req)
  129. if err != nil {
  130. return nil, err
  131. }
  132. up := dbmodel.TUserPic{}
  133. where := [][2]interface{}{}
  134. where = dbmodel.WhereAdd(where, "id", req.Id)
  135. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  136. err = up.Find(database.DB(), where)
  137. if err != nil && err != gorm.ErrRecordNotFound {
  138. return nil, errors.DataBaseError
  139. }
  140. if up.ID == 0 {
  141. return nil, errors.ErrRecordNotFound
  142. }
  143. db := database.DB().Begin()
  144. err = WhitePicDel(up, db)
  145. if err != nil {
  146. db.Rollback()
  147. return nil, err
  148. }
  149. db.Commit()
  150. return reply, nil
  151. }