card_list.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "property-device/errors"
  11. dbmodel "property-device/model"
  12. pb_v1 "property-device/pb/v1"
  13. "property-device/utils/gate_utils"
  14. )
  15. func checkGateCardListParam(req *pb_v1.GateCardListRequest) error {
  16. switch {
  17. case req.GardenId == 0:
  18. return status.Error(10003, "小区不能为空")
  19. }
  20. if req.PageSize == 0 {
  21. req.PageSize = 10
  22. }
  23. if req.Page == 0 {
  24. req.Page = 1
  25. }
  26. return nil
  27. }
  28. func GateUserCardCheck(originStatus int32, id int64) (int32, error) {
  29. up := dbmodel.TUserCard{}
  30. gp := dbmodel.TGateCard{}
  31. where := [][2]interface{}{}
  32. where = dbmodel.WhereAdd(where, "record_id", id)
  33. where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc)
  34. count, err := gp.Count(database.DB(), where, nil)
  35. if err != nil {
  36. return 0, errors.DataBaseError
  37. }
  38. // 全部成功
  39. if count == 0 {
  40. where := [][2]interface{}{}
  41. where = dbmodel.WhereAdd(where, "id", id)
  42. values := map[string]interface{}{"down_status": 2}
  43. err = up.Update(database.DB(), where, values)
  44. if err != nil {
  45. return 0, errors.DataBaseError
  46. }
  47. return 2, nil
  48. }
  49. if originStatus == 1 {
  50. // 查看是否有下发失败的
  51. where = [][2]interface{}{}
  52. where = dbmodel.WhereAdd(where, "record_id", id)
  53. where = dbmodel.WhereAdd(where, "status >", gate_utils.WhiteAddStatusAllSuc)
  54. count, err = gp.Count(database.DB(), where, nil)
  55. if err != nil {
  56. return 0, errors.DataBaseError
  57. }
  58. if count > 0 {
  59. where := [][2]interface{}{}
  60. where = dbmodel.WhereAdd(where, "id", id)
  61. values := map[string]interface{}{"down_status": 3}
  62. err = up.Update(database.DB(), where, values)
  63. if err != nil {
  64. return 3, errors.DataBaseError
  65. }
  66. return 3, nil
  67. }
  68. }
  69. return 1, nil
  70. }
  71. func GateUserCardStatusAllCheck(gardeId int64) error {
  72. up := dbmodel.TUserCard{}
  73. where := [][2]interface{}{}
  74. where = dbmodel.WhereAdd(where, "garden_id", gardeId)
  75. where = dbmodel.WhereAdd(where, "down_status !=", 2)
  76. list, err := up.List(database.DB(), where, nil, -1, -1)
  77. if err != nil {
  78. return errors.DataBaseError
  79. }
  80. for _, v := range list {
  81. _, err = GateUserCardCheck(v.DownStatus, v.ID)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }
  88. func getGateCardDevices(recordIds []int64) (map[int64][]*pb_v1.GateCardDevice, error) {
  89. gp := dbmodel.TGateCard{}
  90. where := [][2]interface{}{}
  91. where = dbmodel.WhereAdd(where, "record_id in", recordIds)
  92. list, err := gp.List(database.DB(), where, nil, -1, -1)
  93. if err != nil {
  94. return nil, errors.DataBaseError
  95. }
  96. m := map[int64]bool{}
  97. deviceIds := []int64{}
  98. for _, v := range list {
  99. if m[v.DeviceId] {
  100. continue
  101. }
  102. m[v.DeviceId] = true
  103. deviceIds = append(deviceIds, v.DeviceId)
  104. }
  105. ret := map[int64][]*pb_v1.GateCardDevice{}
  106. if len(deviceIds) == 0 {
  107. return ret, nil
  108. }
  109. gate := dbmodel.TGate{}
  110. where = [][2]interface{}{}
  111. where = dbmodel.WhereAdd(where, "id in", deviceIds)
  112. glist, err := gate.List(database.DB(), where, nil, -1, -1)
  113. if err != nil {
  114. return nil, errors.DataBaseError
  115. }
  116. gm := map[int64]dbmodel.TGate{}
  117. for _, v := range glist {
  118. gm[v.ID] = v
  119. }
  120. for _, v := range list {
  121. item := pb_v1.GateCardDevice{
  122. DeviceId: v.DeviceId,
  123. DeviceName: gm[v.DeviceId].DeviceName,
  124. Location: gm[v.DeviceId].Location,
  125. Direction: gm[v.DeviceId].Direction,
  126. }
  127. if v.Status == gate_utils.WhiteAddStatusWait || v.Status == gate_utils.WhiteAddStatusPersonSuc {
  128. item.DownStatus = 1
  129. } else if v.Status == gate_utils.WhiteAddStatusAllSuc {
  130. item.DownStatus = 2
  131. } else {
  132. item.DownStatus = 3
  133. }
  134. ret[v.RecordId] = append(ret[v.RecordId], &item)
  135. }
  136. return ret, nil
  137. }
  138. func GateCardList(ctx context.Context, req *pb_v1.GateCardListRequest) (reply *pb_v1.GateCardListReply, err error) {
  139. reply = &pb_v1.GateCardListReply{}
  140. // 捕获各个task中的异常并返回给调用者
  141. defer func() {
  142. if r := recover(); r != nil {
  143. err = fmt.Errorf("%+v", r)
  144. e := &status.Status{}
  145. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  146. logger.Error("err",
  147. zap.String("system_err", err.Error()),
  148. zap.Stack("stacktrace"))
  149. }
  150. }
  151. }()
  152. err = checkGateCardListParam(req)
  153. if err != nil {
  154. return nil, err
  155. }
  156. if req.Page == 1 {
  157. if err = GateUserCardStatusAllCheck(req.GardenId); err != nil {
  158. return nil, err
  159. }
  160. }
  161. up := dbmodel.TUserCard{}
  162. where := [][2]interface{}{}
  163. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  164. if req.DownStatus > 0 {
  165. where = dbmodel.WhereAdd(where, "down_status", req.DownStatus)
  166. }
  167. if req.Name != "" {
  168. where = dbmodel.WhereAdd(where, "name", req.Name)
  169. }
  170. reply.Page = req.Page
  171. reply.Total, err = up.Count(database.DB(), where, nil)
  172. if err != nil {
  173. return nil, errors.DataBaseError
  174. }
  175. if reply.Total == 0 {
  176. return reply, nil
  177. }
  178. list, err := up.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  179. if err != nil {
  180. return nil, errors.DataBaseError
  181. }
  182. recordIds := make([]int64, len(list))
  183. for i, v := range list {
  184. recordIds[i] = v.ID
  185. }
  186. // 获取设备信息
  187. deviceM, err := getGateCardDevices(recordIds)
  188. if err != nil {
  189. return nil, err
  190. }
  191. reply.List = make([]*pb_v1.GateCardItem, len(list))
  192. for i, v := range list {
  193. item := &pb_v1.GateCardItem{
  194. Id: v.ID,
  195. CardNumber: v.CardNumber,
  196. Name: v.Name,
  197. DownStatus: v.DownStatus,
  198. GatePermissions: deviceM[v.ID],
  199. CreatedAt: v.CreatedAt.Unix(),
  200. }
  201. reply.List[i] = item
  202. }
  203. return reply, nil
  204. }