package gate_unit 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" "property-device/impl/v1/gate_pic" dbmodel "property-device/model" "property-device/pb" pb_v1 "property-device/pb/v1" "property-device/utils/gate_utils" "time" ) func checkGateUnitAddParam(req *pb_v1.GateUnitAddRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.DeviceId == 0: return status.Error(10003, "设备不能为空") } return nil } func getGateInfo(deviceId int64, sn string, protocol int32) (*dbmodel.TGate, error) { p := &dbmodel.TGate{} where := [][2]interface{}{} if deviceId > 0 { where = dbmodel.WhereAdd(where, "id", deviceId) } else { where = dbmodel.WhereAdd(where, "sn", sn) where = dbmodel.WhereAdd(where, "protocol", protocol) } err := p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if p.ID == 0 { return nil, errors.ErrRecordNotFound } return p, nil } func getUidsByUnit(gardenId int64, unitIds []int64) ([]int64, error) { mreq := pb_v1.GardenHouseholdUidsFromUnitIdRequest{GardenId: gardenId, UnitIds: unitIds} mreply, err := pb.Garden.GardenHouseholdUidsFromUnitId(context.Background(), &mreq) if err != nil { return nil, err } return mreply.Uids, nil } func checkNeedAddWhite(gateInfo *dbmodel.TGate, gardenId int64, unitIds []int64, db *gorm.DB) error { if gate_utils.GateProtocolFuntionMap[gateInfo.Protocol][1] != 1 { return nil } if len(unitIds) == 0 { return nil } uids, err := getUidsByUnit(gardenId, unitIds) if err != nil { return err } // 查找所有与单元相关用户的照片申请记录 up := dbmodel.TUserPic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "garden_id", gardenId) where = dbmodel.WhereAdd(where, "uid in", uids) where = dbmodel.WhereAdd(where, "approve_status", 2) ulist, err := up.List(db, where, nil, -1, -1) if err != nil { return errors.DataBaseError } if len(ulist) == 0 { return nil } rids := make([]int64, len(ulist)) m := map[int64]dbmodel.TUserPic{} for i, v := range ulist { rids[i] = v.ID m[v.ID] = v } // 查找照片记录已添加的设备 gp := dbmodel.TGatePic{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id in", rids) glist, err := gp.List(db, where, nil, -1, -1) if err != nil { return errors.DataBaseError } added := map[int64]bool{} for _, v := range glist { if v.DeviceId == gateInfo.ID { added[v.RecordId] = true } } // 得到还需添向当前设备加百名单的照片记录 needAdds := []dbmodel.TUserPic{} for rid, v := range m { if added[rid] { continue } needAdds = append(needAdds, v) } if len(needAdds) == 0 { return nil } // 添加设备和照片的关联记录 for _, v := range needAdds { err = gate_pic.WhitePicAddToDb(v, []dbmodel.TGate{*gateInfo}, db) if err != nil { return err } } // 下发数据到设备 for _, v := range needAdds { gate_pic.WhitePicAddToDevice(v, []dbmodel.TGate{*gateInfo}) } return nil } func GateUnitAdd(ctx context.Context, req *pb_v1.GateUnitAddRequest) (reply *pb_v1.GateUnitAddReply, err error) { reply = &pb_v1.GateUnitAddReply{} // 捕获各个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 = checkGateUnitAddParam(req) if err != nil { return nil, err } gateInfo, err := getGateInfo(req.DeviceId, "", 0) if err != nil { return nil, err } now := time.Now() p := &dbmodel.TGateUnit{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "garden_id", req.GardenId) where = dbmodel.WhereAdd(where, "device_id", req.DeviceId) list, err := p.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } // 确定要删除的和要增加的 addArray := []dbmodel.TGateUnit{} addUnitIds := []int64{} delArray := []int64{} m := map[int64]bool{} for _, v := range list { m[v.UnitId] = true } for _, v := range req.UnitId { if m[v] { delete(m, v) continue } item := dbmodel.TGateUnit{ UnitId: v, GardenId: req.GardenId, DeviceId: req.DeviceId, CreatedAt: now, UpdatedAt: now, Protocol: gateInfo.Protocol, Sn: gateInfo.Sn, } addArray = append(addArray, item) addUnitIds = append(addUnitIds, v) } for k, _ := range m { delArray = append(delArray, k) } if len(delArray) > 0 { return nil, status.Error(10003, "范围不能缩减") } db := database.DB().Begin() err = p.InsertMulti(db, &addArray) if err != nil { db.Rollback() return nil, errors.DataBaseError } err = checkNeedAddWhite(gateInfo, req.GardenId, addUnitIds, db) if err != nil { db.Rollback() return nil, err } db.Commit() return reply, nil }