pic_list.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. "property-device/errors"
  11. dbmodel "property-device/model"
  12. pb_v1 "property-device/pb/v1"
  13. "property-device/utils/gate_utils"
  14. "strconv"
  15. )
  16. func checkGateUserPicListParam(req *pb_v1.GateUserPicListRequest) error {
  17. switch {
  18. case req.GardenId == 0:
  19. return status.Error(10003, "小区不能为空")
  20. }
  21. if req.PageSize == 0 {
  22. req.PageSize = 10
  23. }
  24. if req.Page == 0 {
  25. req.Page = 1
  26. }
  27. return nil
  28. }
  29. func GateUserPicStatusAllCheck(gardeId int64) error {
  30. up := dbmodel.TUserPic{}
  31. where := [][2]interface{}{}
  32. where = dbmodel.WhereAdd(where, "garden_id", gardeId)
  33. where = dbmodel.WhereAdd(where, "down_status !=", 2)
  34. where = dbmodel.WhereAdd(where, "approve_status", 2)
  35. list, err := up.List(database.DB(), where, nil, -1, -1)
  36. if err != nil {
  37. return errors.DataBaseError
  38. }
  39. for _, v := range list {
  40. _, err = GateUserPicCheck(v.DownStatus, v.ID)
  41. if err != nil {
  42. return err
  43. }
  44. }
  45. return nil
  46. }
  47. func getGateUserDevices(recordIds []int64) (map[int64][]*pb_v1.GateUserPicDevice, error) {
  48. gp := dbmodel.TGatePic{}
  49. where := [][2]interface{}{}
  50. where = dbmodel.WhereAdd(where, "record_id in", recordIds)
  51. list, err := gp.List(database.DB(), where, nil, -1, -1)
  52. if err != nil {
  53. return nil, errors.DataBaseError
  54. }
  55. m := map[int64]bool{}
  56. deviceIds := []int64{}
  57. for _, v := range list {
  58. if m[v.DeviceId] {
  59. continue
  60. }
  61. m[v.DeviceId] = true
  62. deviceIds = append(deviceIds, v.DeviceId)
  63. }
  64. ret := map[int64][]*pb_v1.GateUserPicDevice{}
  65. if len(deviceIds) == 0 {
  66. return ret, nil
  67. }
  68. gate := dbmodel.TGate{}
  69. where = [][2]interface{}{}
  70. where = dbmodel.WhereAdd(where, "id in", deviceIds)
  71. glist, err := gate.List(database.DB(), where, nil, -1, -1)
  72. if err != nil {
  73. return nil, errors.DataBaseError
  74. }
  75. gm := map[int64]dbmodel.TGate{}
  76. for _, v := range glist {
  77. gm[v.ID] = v
  78. }
  79. for _, v := range list {
  80. item := pb_v1.GateUserPicDevice{
  81. DeviceId: v.DeviceId,
  82. DeviceName: gm[v.DeviceId].DeviceName,
  83. Location: gm[v.DeviceId].Location,
  84. Direction: gm[v.DeviceId].Direction,
  85. }
  86. if v.Status == gate_utils.WhiteAddStatusWait || v.Status == gate_utils.WhiteAddStatusPersonSuc {
  87. item.DownStatus = 1
  88. } else if v.Status == gate_utils.WhiteAddStatusAllSuc {
  89. item.DownStatus = 2
  90. } else {
  91. item.DownStatus = 3
  92. }
  93. ret[v.RecordId] = append(ret[v.RecordId], &item)
  94. }
  95. return ret, nil
  96. }
  97. // 获取未审核或已拒绝的人脸关联的设备
  98. func getNotApprovedUerPicDevice(gardenId int64, uids []int64) (map[int64][]*pb_v1.GateUserPicDevice, error) {
  99. ret := map[int64][]*pb_v1.GateUserPicDevice{}
  100. if len(uids) == 0 {
  101. return ret, nil
  102. }
  103. // 获取单元id
  104. unitInfo, _, _, err := gate_utils.GetUserUnitIds(gardenId, uids)
  105. if err != nil {
  106. return nil, err
  107. }
  108. m := map[int64]bool{}
  109. unitIds := []int64{}
  110. for _, array := range unitInfo {
  111. for _, v := range array {
  112. if m[v] {
  113. continue
  114. }
  115. m[v] = true
  116. unitIds = append(unitIds, v)
  117. }
  118. }
  119. if len(unitIds) == 0 {
  120. return ret, nil
  121. }
  122. // 获取单元关联的设备id
  123. up := dbmodel.TGateUnit{}
  124. where := [][2]interface{}{}
  125. where = dbmodel.WhereAdd(where, "garden_id", gardenId)
  126. where = dbmodel.WhereAdd(where, "unit_id in", unitIds)
  127. protocols := []int32{}
  128. for protocol, array := range gate_utils.GateProtocolFuntionMap {
  129. if array[1] == 1 {
  130. protocols = append(protocols, protocol)
  131. }
  132. }
  133. where = dbmodel.WhereAdd(where, "protocol in", protocols)
  134. ulist, err := up.List(database.DB(), where, nil, -1, -1)
  135. if err != nil {
  136. return nil, errors.DataBaseError
  137. }
  138. if len(ulist) == 0 {
  139. return ret, nil
  140. }
  141. // 获取设备信息
  142. unitDeviceM := map[int64][]int64{}
  143. deviceIds := make([]int64, len(ulist))
  144. for i, v := range ulist {
  145. deviceIds[i] = v.DeviceId
  146. unitDeviceM[v.UnitId] = append(unitDeviceM[v.UnitId], v.DeviceId)
  147. }
  148. devices, err := GetDevicesByIds(deviceIds)
  149. if err != nil {
  150. return nil, err
  151. }
  152. deviceM := map[int64]dbmodel.TGate{}
  153. for _, v := range devices {
  154. deviceM[v.ID] = v
  155. }
  156. // 组装人脸对应的设备信息
  157. udm := map[string]bool{}
  158. for uid, units := range unitInfo {
  159. for _, unit := range units {
  160. dids := unitDeviceM[unit]
  161. for _, did := range dids {
  162. if udm[fmt.Sprintf("%d-%d", uid, did)] {
  163. continue
  164. }
  165. if dv, ok := deviceM[did]; ok {
  166. udm[fmt.Sprintf("%d-%d", uid, did)] = true
  167. ret[uid] = append(ret[uid], &pb_v1.GateUserPicDevice{DeviceId: did, DeviceName: dv.DeviceName, Location: dv.Location, Direction: dv.Direction, DownStatus: 1})
  168. }
  169. }
  170. }
  171. }
  172. return ret, nil
  173. }
  174. func GateUserPicList(ctx context.Context, req *pb_v1.GateUserPicListRequest) (reply *pb_v1.GateUserPicListReply, err error) {
  175. reply = &pb_v1.GateUserPicListReply{}
  176. // 捕获各个task中的异常并返回给调用者
  177. defer func() {
  178. if r := recover(); r != nil {
  179. err = fmt.Errorf("%+v", r)
  180. e := &status.Status{}
  181. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  182. logger.Error("err",
  183. zap.String("system_err", err.Error()),
  184. zap.Stack("stacktrace"))
  185. }
  186. }
  187. }()
  188. err = checkGateUserPicListParam(req)
  189. if err != nil {
  190. return nil, err
  191. }
  192. if req.Page == 1 {
  193. if err = GateUserPicStatusAllCheck(req.GardenId); err != nil {
  194. return nil, err
  195. }
  196. }
  197. up := dbmodel.TUserPic{}
  198. where := [][2]interface{}{}
  199. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  200. if req.DownStatus > 0 {
  201. where = dbmodel.WhereAdd(where, "down_status", req.DownStatus)
  202. }
  203. if req.Name != "" {
  204. where = dbmodel.WhereAdd(where, "name", req.Name)
  205. }
  206. reply.Page = req.Page
  207. reply.Total, err = up.Count(database.DB(), where, nil)
  208. if err != nil {
  209. return nil, errors.DataBaseError
  210. }
  211. if reply.Total == 0 {
  212. return reply, nil
  213. }
  214. list, err := up.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  215. if err != nil {
  216. return nil, errors.DataBaseError
  217. }
  218. recordIds := make([]int64, len(list))
  219. uids := []int64{}
  220. for i, v := range list {
  221. recordIds[i] = v.ID
  222. if v.ApproveStatus != 2 {
  223. uidInt, _ := strconv.ParseInt(v.Uid, 10, 64)
  224. uids = append(uids, uidInt)
  225. }
  226. }
  227. // 获取设备信息
  228. deviceM, err := getGateUserDevices(recordIds)
  229. if err != nil {
  230. return nil, err
  231. }
  232. uDeviceM, err := getNotApprovedUerPicDevice(req.GardenId, uids)
  233. if err != nil {
  234. return nil, err
  235. }
  236. reply.List = make([]*pb_v1.GateUserPicItem, len(list))
  237. for i, v := range list {
  238. item := &pb_v1.GateUserPicItem{
  239. Id: v.ID,
  240. IdNumber: v.IdNumber,
  241. Name: v.Name,
  242. HouseName: v.HouseName,
  243. DownStatus: v.DownStatus,
  244. //GatePermissions: deviceM[v.ID],
  245. ApproveStatus: v.ApproveStatus,
  246. CreatedAt: v.CreatedAt.Unix(),
  247. PicUrl: v.PicUrl,
  248. UserType: v.UserType,
  249. }
  250. if v.ApproveStatus == 2 {
  251. item.GatePermissions = deviceM[v.ID]
  252. } else {
  253. uidInt, _ := strconv.ParseInt(v.Uid, 10, 64)
  254. item.GatePermissions = uDeviceM[uidInt]
  255. }
  256. item.Uid, _ = strconv.ParseInt(v.Uid, 10, 64)
  257. if v.ApproveStatus > 1 {
  258. item.ApprovedAt = v.ApprovedAt
  259. }
  260. reply.List[i] = item
  261. }
  262. return reply, nil
  263. }