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" "strconv" "time" ) func checkGateUserPicApproveParam(req *pb_v1.GateUserPicApproveRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.Id == 0: return status.Error(10003, "记录id不能为空") } return nil } func WhitePicAddToDb(up dbmodel.TUserPic, devices []dbmodel.TGate, db *gorm.DB) error { now := time.Now() gps := []dbmodel.TGatePic{} for _, v := range devices { // 不支持人脸的跳过 if gate_utils.GateProtocolFuntionMap[v.Protocol][1] != 1 { continue } item := dbmodel.TGatePic{} item.RecordId = up.ID item.Status = 1 item.DeviceId = v.ID item.CreatedAt = now item.UpdatedAt = now item.Sn = v.Sn item.Protocol = v.Protocol gps = append(gps, item) } if len(gps) == 0 { return nil } gp := dbmodel.TGatePic{} err := gp.InsertMulti(db, &gps) if err != nil { return errors.DataBaseError } return nil } func WhitePicAddToDevice(up dbmodel.TUserPic, devices []dbmodel.TGate) { // 白名单添加到设备 for _, v := range devices { device := v whiter := NewPicWhiter(&up, &device, nil) if whiter == nil { continue } whiter.PersonAdd() whiter.PicAdd() if whiter.Command() { gate_utils.CommandCacheIncrease(device.Sn, device.Protocol) } } } func WhitePicUpdate(up dbmodel.TUserPic, devices map[int64]dbmodel.TGate, gps []dbmodel.TGatePic) { // 白名单更新到设备 for _, v := range gps { if _, ok := devices[v.DeviceId]; !ok { continue } gp := v device := devices[v.DeviceId] whiter := NewPicWhiter(&up, &device, &gp) whiter.PicUpdate() if whiter.Command() { gate_utils.CommandCacheIncrease(device.Sn, device.Protocol) } } } func GetGatePicsByRecordId(recordId int64, excluedSuc bool) ([]dbmodel.TGatePic, error) { g := dbmodel.TGatePic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", recordId) if excluedSuc { where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc) } list, err := g.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } return list, nil } func GetDevicesByIds(ids []int64) ([]dbmodel.TGate, error) { g := dbmodel.TGate{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id in", ids) list, err := g.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } return list, nil } func getAllPicDevices(unitIds []int64, gardenId int64) ([]dbmodel.TGate, error) { 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 nil, nil } devices := make([]int64, len(ulist)) for i, v := range ulist { devices[i] = v.DeviceId } return GetDevicesByIds(devices) } func checkOnlineStatus(devices []dbmodel.TGate) bool { for _, v := range devices { if v.Status == 2 { return false } } return true } func whichDeviceNeedAdd(all []dbmodel.TGate, gps []dbmodel.TGatePic) (needAdds []dbmodel.TGate, needUpdates map[int64]dbmodel.TGate) { existM := map[int64]bool{} notSucM := map[int64]bool{} for _, v := range gps { existM[v.DeviceId] = true if v.Status != gate_utils.WhiteAddStatusAllSuc { notSucM[v.DeviceId] = true } } needUpdates = map[int64]dbmodel.TGate{} for _, v := range all { if notSucM[v.ID] { needUpdates[v.ID] = v } if !existM[v.ID] { needAdds = append(needAdds, v) } } return needAdds, needUpdates } func GateUserPicApprove(ctx context.Context, req *pb_v1.GateUserPicApproveRequest) (reply *pb_v1.GateUserPicApproveReply, err error) { reply = &pb_v1.GateUserPicApproveReply{} // 捕获各个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 = checkGateUserPicApproveParam(req) if err != nil { return nil, err } up := dbmodel.TUserPic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", req.Id) where = dbmodel.WhereAdd(where, "garden_id", req.GardenId) err = up.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if up.ApproveStatus != 1 { return nil, status.Error(10003, "当前状态不能审核") } // 审核不通过 if !req.Status { values := map[string]interface{}{ "feedback": req.Feedback, "approve_status": 3, "approved_at": time.Now().Unix(), } err = up.Update(database.DB(), where, values) if err != nil { return nil, errors.DataBaseError } return reply, nil } uid, _ := strconv.ParseInt(up.Uid, 10, 64) unitInfo, _, _, err := gate_utils.GetUserUnitIds(req.GardenId, []int64{uid}) if err != nil { return nil, err } if len(unitInfo[uid]) == 0 { return reply, nil } // 审核通过 devices, err := getAllPicDevices(unitInfo[uid], up.GardenId) if err != nil { return nil, errors.DataBaseError } //if !checkOnlineStatus(devices) { // return nil, status.Error(10003, "当前有设备未在线,无法完成下发,请检查设备后审批") //} gps, err := GetGatePicsByRecordId(up.ID, false) if err != nil { return nil, errors.DataBaseError } needAdds, needUpdates := whichDeviceNeedAdd(devices, gps) db := database.DB().Begin() values := map[string]interface{}{ "feedback": req.Feedback, "approve_status": 2, "approved_at": time.Now().Unix(), } err = up.Update(db, where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } if err := WhitePicAddToDb(up, needAdds, db); err != nil { db.Rollback() return nil, err } db.Commit() WhitePicAddToDevice(up, needAdds) WhitePicUpdate(up, needUpdates, gps) return reply, nil }