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 }