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" "gorm.io/gorm" "property-device/errors" dbmodel "property-device/model" pb_v1 "property-device/pb/v1" "property-device/utils/gate_utils" ) func checkHouseholdGateSyncCheckParam(req *pb_v1.HouseholdGateSyncCheckRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.UnitId == 0: return status.Error(10003, "单元id不能为空") case req.Uid == 0: return status.Error(10003, "uid不能为空") } return nil } // 住户申请房屋后,检查是否有设备需要同步人脸 func HouseholdGateSyncCheck(ctx context.Context, req *pb_v1.HouseholdGateSyncCheckRequest) (reply *pb_v1.HouseholdGateSyncCheckReply, err error) { reply = &pb_v1.HouseholdGateSyncCheckReply{} // 捕获各个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 = checkHouseholdGateSyncCheckParam(req) if err != nil { return nil, err } // 查找审核通过的录入记录 up := dbmodel.TUserPic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "garden_id", req.GardenId) where = dbmodel.WhereAdd(where, "uid", req.Uid) err = up.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if up.ID == 0 { return reply, nil } if up.ApproveStatus != 2 { return reply, nil } // 查找绑定了该单元的设备 gu := dbmodel.TGateUnit{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "garden_id", req.GardenId) where = dbmodel.WhereAdd(where, "unit_id", req.UnitId) protocols := []int32{} for protocol, array := range gate_utils.GateProtocolFuntionMap { if array[1] == 1 { protocols = append(protocols, protocol) } } if len(protocols) == 0 { return reply, nil } where = dbmodel.WhereAdd(where, "protocol in", protocols) guList, err := gu.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } if len(guList) == 0 { return reply, nil } deviceIds := make([]int64, len(guList)) for i, v := range guList { deviceIds[i] = v.DeviceId } // 查找记录已下发的设备 gp := dbmodel.TGatePic{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", up.ID) gplist, err := gp.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } exist := map[int64]bool{} for _, v := range gplist { exist[v.DeviceId] = true } // 得到需要下发的设备 needAddIds := []int64{} for _, v := range deviceIds { if !exist[v] { needAddIds = append(needAddIds, v) } } if len(needAddIds) == 0 { return reply, nil } // 获取设备信息 devices, err := GetDevicesByIds(needAddIds) if err != nil { return nil, err } if len(devices) == 0 { return reply, nil } // 进行下发 db := database.DB().Begin() if err := WhitePicAddToDb(up, devices, db); err != nil { db.Rollback() return nil, err } db.Commit() WhitePicAddToDevice(up, devices) return reply, nil }