123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- package gate_pic
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "property-device/errors"
- dbmodel "property-device/model"
- pb_v1 "property-device/pb/v1"
- "property-device/utils/gate_utils"
- "strconv"
- )
- func checkGateUserPicListParam(req *pb_v1.GateUserPicListRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- if req.Page == 0 {
- req.Page = 1
- }
- return nil
- }
- func GateUserPicStatusAllCheck(gardeId int64) error {
- up := dbmodel.TUserPic{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", gardeId)
- where = dbmodel.WhereAdd(where, "down_status !=", 2)
- where = dbmodel.WhereAdd(where, "approve_status", 2)
- list, err := up.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return errors.DataBaseError
- }
- for _, v := range list {
- _, err = GateUserPicCheck(v.DownStatus, v.ID)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func getGateUserDevices(recordIds []int64) (map[int64][]*pb_v1.GateUserPicDevice, error) {
- gp := dbmodel.TGatePic{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "record_id in", recordIds)
- list, err := gp.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- m := map[int64]bool{}
- deviceIds := []int64{}
- for _, v := range list {
- if m[v.DeviceId] {
- continue
- }
- m[v.DeviceId] = true
- deviceIds = append(deviceIds, v.DeviceId)
- }
- ret := map[int64][]*pb_v1.GateUserPicDevice{}
- if len(deviceIds) == 0 {
- return ret, nil
- }
- gate := dbmodel.TGate{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id in", deviceIds)
- glist, err := gate.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- gm := map[int64]dbmodel.TGate{}
- for _, v := range glist {
- gm[v.ID] = v
- }
- for _, v := range list {
- item := pb_v1.GateUserPicDevice{
- DeviceId: v.DeviceId,
- DeviceName: gm[v.DeviceId].DeviceName,
- Location: gm[v.DeviceId].Location,
- Direction: gm[v.DeviceId].Direction,
- }
- if v.Status == gate_utils.WhiteAddStatusWait || v.Status == gate_utils.WhiteAddStatusPersonSuc {
- item.DownStatus = 1
- } else if v.Status == gate_utils.WhiteAddStatusAllSuc {
- item.DownStatus = 2
- } else {
- item.DownStatus = 3
- }
- ret[v.RecordId] = append(ret[v.RecordId], &item)
- }
- return ret, nil
- }
- // 获取未审核或已拒绝的人脸关联的设备
- func getNotApprovedUerPicDevice(gardenId int64, uids []int64) (map[int64][]*pb_v1.GateUserPicDevice, error) {
- ret := map[int64][]*pb_v1.GateUserPicDevice{}
- if len(uids) == 0 {
- return ret, nil
- }
- // 获取单元id
- unitInfo, _, _, err := gate_utils.GetUserUnitIds(gardenId, uids)
- if err != nil {
- return nil, err
- }
- m := map[int64]bool{}
- unitIds := []int64{}
- for _, array := range unitInfo {
- for _, v := range array {
- if m[v] {
- continue
- }
- m[v] = true
- unitIds = append(unitIds, v)
- }
- }
- if len(unitIds) == 0 {
- return ret, nil
- }
- // 获取单元关联的设备id
- up := dbmodel.TGateUnit{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", gardenId)
- where = dbmodel.WhereAdd(where, "unit_id in", unitIds)
- protocols := []int32{}
- for protocol, array := range gate_utils.GateProtocolFuntionMap {
- if array[1] == 1 {
- protocols = append(protocols, protocol)
- }
- }
- where = dbmodel.WhereAdd(where, "protocol in", protocols)
- ulist, err := up.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if len(ulist) == 0 {
- return ret, nil
- }
- // 获取设备信息
- unitDeviceM := map[int64][]int64{}
- deviceIds := make([]int64, len(ulist))
- for i, v := range ulist {
- deviceIds[i] = v.DeviceId
- unitDeviceM[v.UnitId] = append(unitDeviceM[v.UnitId], v.DeviceId)
- }
- devices, err := GetDevicesByIds(deviceIds)
- if err != nil {
- return nil, err
- }
- deviceM := map[int64]dbmodel.TGate{}
- for _, v := range devices {
- deviceM[v.ID] = v
- }
- // 组装人脸对应的设备信息
- udm := map[string]bool{}
- for uid, units := range unitInfo {
- for _, unit := range units {
- dids := unitDeviceM[unit]
- for _, did := range dids {
- if udm[fmt.Sprintf("%d-%d", uid, did)] {
- continue
- }
- if dv, ok := deviceM[did]; ok {
- udm[fmt.Sprintf("%d-%d", uid, did)] = true
- ret[uid] = append(ret[uid], &pb_v1.GateUserPicDevice{DeviceId: did, DeviceName: dv.DeviceName, Location: dv.Location, Direction: dv.Direction, DownStatus: 1})
- }
- }
- }
- }
- return ret, nil
- }
- func GateUserPicList(ctx context.Context, req *pb_v1.GateUserPicListRequest) (reply *pb_v1.GateUserPicListReply, err error) {
- reply = &pb_v1.GateUserPicListReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- err = checkGateUserPicListParam(req)
- if err != nil {
- return nil, err
- }
- if req.Page == 1 {
- if err = GateUserPicStatusAllCheck(req.GardenId); err != nil {
- return nil, err
- }
- }
- up := dbmodel.TUserPic{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
- if req.DownStatus > 0 {
- where = dbmodel.WhereAdd(where, "down_status", req.DownStatus)
- }
- if req.Name != "" {
- where = dbmodel.WhereAdd(where, "name", req.Name)
- }
- reply.Page = req.Page
- reply.Total, err = up.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := up.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- recordIds := make([]int64, len(list))
- uids := []int64{}
- for i, v := range list {
- recordIds[i] = v.ID
- if v.ApproveStatus != 2 {
- uidInt, _ := strconv.ParseInt(v.Uid, 10, 64)
- uids = append(uids, uidInt)
- }
- }
- // 获取设备信息
- deviceM, err := getGateUserDevices(recordIds)
- if err != nil {
- return nil, err
- }
- uDeviceM, err := getNotApprovedUerPicDevice(req.GardenId, uids)
- if err != nil {
- return nil, err
- }
- reply.List = make([]*pb_v1.GateUserPicItem, len(list))
- for i, v := range list {
- item := &pb_v1.GateUserPicItem{
- Id: v.ID,
- IdNumber: v.IdNumber,
- Name: v.Name,
- HouseName: v.HouseName,
- DownStatus: v.DownStatus,
- //GatePermissions: deviceM[v.ID],
- ApproveStatus: v.ApproveStatus,
- CreatedAt: v.CreatedAt.Unix(),
- PicUrl: v.PicUrl,
- UserType: v.UserType,
- }
- if v.ApproveStatus == 2 {
- item.GatePermissions = deviceM[v.ID]
- } else {
- uidInt, _ := strconv.ParseInt(v.Uid, 10, 64)
- item.GatePermissions = uDeviceM[uidInt]
- }
- item.Uid, _ = strconv.ParseInt(v.Uid, 10, 64)
- if v.ApproveStatus > 1 {
- item.ApprovedAt = v.ApprovedAt
- }
- reply.List[i] = item
- }
- return reply, nil
- }
|