card_del.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package gate_card
  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 checkGateCardDelParam(req *pb_v1.GateCardDelRequest) 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 CheckUserCardDel(recordId int64, db *gorm.DB) error {
  27. gp := dbmodel.TGateCard{}
  28. up := dbmodel.TUserCard{}
  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 WhiteCardDelFromDb(up dbmodel.TUserCard, db *gorm.DB) ([]dbmodel.TGateCard, error) {
  47. gp := dbmodel.TGateCard{}
  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. where = dbmodel.WhereAdd(where, "protocol", gate_utils.GateProtocolSaiboMqttV1)
  67. err = gp.Delete(db, where)
  68. if err != nil {
  69. return nil, errors.DataBaseError
  70. }
  71. where = [][2]interface{}{}
  72. where = dbmodel.WhereAdd(where, "record_id", up.ID)
  73. glist, err := gp.List(db, where, nil, -1, -1)
  74. if err != nil {
  75. return nil, errors.DataBaseError
  76. }
  77. if len(glist) > 0 {
  78. return glist, nil
  79. }
  80. where = [][2]interface{}{}
  81. where = dbmodel.WhereAdd(where, "id", up.ID)
  82. err = up.Delete(db, where)
  83. if err != nil {
  84. return nil, errors.DataBaseError
  85. }
  86. return nil, nil
  87. }
  88. func WhiteCardDelFromDevice(up dbmodel.TUserCard, devices []dbmodel.TGate, gm map[int64]dbmodel.TGateCard) {
  89. for _, v := range devices {
  90. device := v
  91. gcard := gm[v.ID]
  92. whiter := NewCardWhiter(&up, &device, &gcard)
  93. if whiter == nil {
  94. continue
  95. }
  96. whiter.PersonDel()
  97. if whiter.Command() {
  98. gate_utils.CommandCacheIncrease(device.Sn, device.Protocol)
  99. }
  100. }
  101. }
  102. func WhiteCardDel(up dbmodel.TUserCard, db *gorm.DB) error {
  103. gplist, err := WhiteCardDelFromDb(up, db)
  104. if err != nil {
  105. return err
  106. }
  107. if len(gplist) == 0 {
  108. return nil
  109. }
  110. deviceIds := make([]int64, len(gplist))
  111. gm := map[int64]dbmodel.TGateCard{}
  112. for i, v := range gplist {
  113. deviceIds[i] = v.DeviceId
  114. gm[v.DeviceId] = v
  115. }
  116. devices, err := GetDevicesByIds(deviceIds)
  117. if err != nil {
  118. return err
  119. }
  120. WhiteCardDelFromDevice(up, devices, gm)
  121. return nil
  122. }
  123. func GateCardDel(ctx context.Context, req *pb_v1.GateCardDelRequest) (reply *pb_v1.GateCardDelReply, err error) {
  124. reply = &pb_v1.GateCardDelReply{}
  125. // 捕获各个task中的异常并返回给调用者
  126. defer func() {
  127. if r := recover(); r != nil {
  128. err = fmt.Errorf("%+v", r)
  129. e := &status.Status{}
  130. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  131. logger.Error("err",
  132. zap.String("system_err", err.Error()),
  133. zap.Stack("stacktrace"))
  134. }
  135. }
  136. }()
  137. err = checkGateCardDelParam(req)
  138. if err != nil {
  139. return nil, err
  140. }
  141. up := dbmodel.TUserCard{}
  142. where := [][2]interface{}{}
  143. where = dbmodel.WhereAdd(where, "id", req.Id)
  144. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  145. err = up.Find(database.DB(), where)
  146. if err != nil && err != gorm.ErrRecordNotFound {
  147. return nil, errors.DataBaseError
  148. }
  149. if up.ID == 0 {
  150. return nil, errors.ErrRecordNotFound
  151. }
  152. db := database.DB().Begin()
  153. err = WhiteCardDel(up, db)
  154. if err != nil {
  155. db.Rollback()
  156. return nil, errors.DataBaseError
  157. }
  158. db.Commit()
  159. return reply, nil
  160. }