card_add.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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"
  15. "property-device/utils/gate_utils"
  16. "time"
  17. )
  18. func checkGateCardAddParam(req *pb_v1.GateCardAddRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. case len(req.DeviceIds) == 0:
  23. return status.Error(10003, "设备不能为空")
  24. case req.CardNumber == "":
  25. return status.Error(10003, "卡号不能为空")
  26. }
  27. if req.Name == "" {
  28. req.Name = "匿名用户"
  29. }
  30. return nil
  31. }
  32. func GetDevicesByIds(ids []int64) ([]dbmodel.TGate, error) {
  33. g := dbmodel.TGate{}
  34. if len(ids) == 0 {
  35. return []dbmodel.TGate{}, nil
  36. }
  37. where := [][2]interface{}{}
  38. where = dbmodel.WhereAdd(where, "id in", ids)
  39. list, err := g.List(database.DB(), where, nil, -1, -1)
  40. if err != nil {
  41. return nil, errors.DataBaseError
  42. }
  43. return list, nil
  44. }
  45. func getNeedAddCardDevices(req *pb_v1.GateCardAddRequest, recordId int64) ([]dbmodel.TGate, error) {
  46. gcard := dbmodel.TGateCard{}
  47. where := [][2]interface{}{}
  48. where = dbmodel.WhereAdd(where, "record_id", recordId)
  49. gclist, err := gcard.List(database.DB(), where, nil, -1, -1)
  50. if err != nil {
  51. return nil, errors.DataBaseError
  52. }
  53. added := map[int64]bool{}
  54. for _, v := range gclist {
  55. added[v.DeviceId] = true
  56. }
  57. needAddDeviceIds := []int64{}
  58. for _, v := range req.DeviceIds {
  59. if added[v] {
  60. continue
  61. }
  62. needAddDeviceIds = append(needAddDeviceIds, v)
  63. }
  64. if len(needAddDeviceIds) == 0 {
  65. return nil, nil
  66. }
  67. needAddDevices, err := GetDevicesByIds(needAddDeviceIds)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return needAddDevices, nil
  72. }
  73. func gernerateCardUid() string {
  74. return fmt.Sprintf("auto-%d-%s", time.Now().Unix(), utils.GenerateRandomStr(6, "mix"))
  75. }
  76. func checkCardExist(req *pb_v1.GateCardAddRequest) (*dbmodel.TUserCard, error) {
  77. ucard := dbmodel.TUserCard{}
  78. where := [][2]interface{}{}
  79. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  80. where = dbmodel.WhereAdd(where, "card_number", req.CardNumber)
  81. err := ucard.Find(database.DB(), where)
  82. if err != nil && err != gorm.ErrRecordNotFound {
  83. return nil, errors.DataBaseError
  84. }
  85. now := time.Now()
  86. if ucard.ID == 0 {
  87. ucard.Uid = gernerateCardUid()
  88. ucard.Name = req.Name
  89. ucard.CardNumber = req.CardNumber
  90. ucard.DownStatus = 1
  91. ucard.HouseId = 0
  92. ucard.HouseName = ""
  93. ucard.CreatedAt = now
  94. ucard.UpdatedAt = now
  95. ucard.GardenId = req.GardenId
  96. err = ucard.Insert(database.DB())
  97. if err != nil {
  98. return nil, errors.DataBaseError
  99. }
  100. }
  101. return &ucard, nil
  102. }
  103. func GateCardAdd(ctx context.Context, req *pb_v1.GateCardAddRequest) (reply *pb_v1.GateCardAddReply, err error) {
  104. reply = &pb_v1.GateCardAddReply{}
  105. // 捕获各个task中的异常并返回给调用者
  106. defer func() {
  107. if r := recover(); r != nil {
  108. err = fmt.Errorf("%+v", r)
  109. e := &status.Status{}
  110. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  111. logger.Error("err",
  112. zap.String("system_err", err.Error()),
  113. zap.Stack("stacktrace"))
  114. }
  115. }
  116. }()
  117. err = checkGateCardAddParam(req)
  118. if err != nil {
  119. return nil, err
  120. }
  121. now := time.Now()
  122. ucard, err := checkCardExist(req)
  123. if err != nil {
  124. return nil, err
  125. }
  126. // 获取设备
  127. devices, err := getNeedAddCardDevices(req, ucard.ID)
  128. if err != nil {
  129. return nil, err
  130. }
  131. if len(devices) == 0 {
  132. return reply, nil
  133. }
  134. // 添加关联记录
  135. db := database.DB().Begin()
  136. array := make([]dbmodel.TGateCard, len(devices))
  137. for i, v := range devices {
  138. array[i].DeviceId = v.ID
  139. array[i].RecordId = ucard.ID
  140. array[i].Status = 1
  141. array[i].Sn = v.Sn
  142. array[i].Protocol = v.Protocol
  143. array[i].CreatedAt = now
  144. array[i].UpdatedAt = now
  145. if v.Protocol == gate_utils.GateProtocolSaiboMqttV1 {
  146. array[i].Status = gate_utils.WhiteAddStatusAllSuc
  147. }
  148. }
  149. gcard := dbmodel.TGateCard{}
  150. err = gcard.InsertMulti(db, &array)
  151. if err != nil {
  152. db.Rollback()
  153. return nil, err
  154. }
  155. db.Commit()
  156. for i, v := range array {
  157. gcard := v
  158. device := devices[i]
  159. whiter := NewCardWhiter(ucard, &device, &gcard)
  160. if whiter == nil {
  161. continue
  162. }
  163. whiter.PersonAdd()
  164. if whiter.Command() {
  165. gate_utils.CommandCacheIncrease(v.Sn, v.Protocol)
  166. }
  167. }
  168. return reply, nil
  169. }