package charge import ( "fmt" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "time" "git.getensh.com/common/gopkgs/database" "google.golang.org/grpc/status" ) type houseBindParam struct { chargeInfo *dbmodel.TChargeConf houseIds []int64 houseInfoM map[int64]dbmodel.THouseUnitBuilding } func bindChargeHouse(req *pb_v1.ChargeBindRequest, param *houseBindParam, db *gorm.DB, dbname string) (error) { now := time.Now() arrayChargeHouse := []dbmodel.TChargeBind{} // 去重 olds := map[int64]bool{} binded := dbmodel.NewChargeBind(dbname) where := map[string]interface{}{ "obj_id in": param.houseIds, "obj_type": charge_utils.ObjTypeHouse, } if charge_utils.IsOtherChargeType[param.chargeInfo.ChargeType] { // 不能重复绑定同一个项目 where["charge_id"] = param.chargeInfo.ID } else { // 不能重复绑定同一种类型 where["charge_type"] = param.chargeInfo.ChargeType } bindedList, err := binded.List(database.DB(), where, nil, -1, -1) if err != nil { return errors.DataBaseError } for _, v := range bindedList { olds[v.ObjId] = true } for _, v := range param.houseIds { // 已存在不重复添加 if olds[v] { continue } tmp := dbmodel.TChargeBind{ ObjId: v, ChargeId: req.ChargeId, Start: req.Start, CreatedAt: now, UpdatedAt: now, ChargeType: param.chargeInfo.ChargeType, ObjType: charge_utils.ObjTypeHouse, //ChargeBasis:param.chargeInfo.ChargeBasis, HouseId:v, ObjName: fmt.Sprintf("%v-%v-%v", param.houseInfoM[v].BuildingNumber,param.houseInfoM[v].UnitNumber,param.houseInfoM[v].HouseNumber), } if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf { tmp.CustomFee = req.VehicleFee } if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisArea { tmp.ObjArea = param.houseInfoM[v].HouseArea } if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisUsedArea { tmp.ObjArea = param.houseInfoM[v].HouseUsedArea } if !charge_utils.IsOtherChargeType[param.chargeInfo.ChargeType] { tmp.UniqFlag.Int32 = 1 tmp.UniqFlag.Valid = true } if charge_utils.IsPowerChargeType[param.chargeInfo.ChargeType] { tmp.Start = 0 } arrayChargeHouse = append(arrayChargeHouse, tmp) } if len(arrayChargeHouse) == 0 { return nil } p := dbmodel.NewChargeBind(dbname) err = p.InsertMulti(db, &arrayChargeHouse) if err != nil { return errors.DataBaseError } // 一次性费用直接创建账单 if param.chargeInfo.ChargeTimeType == charge_utils.ChargeTimeTypeOne { for _, v := range arrayChargeHouse { houseInfo := param.houseInfoM[v.ObjId] bindInfo := v bparam := &BillAddParam{ HouseInfo:&houseInfo, PowerInfo:nil, VehicleInfo:nil, SpaceInfo:nil, ChargeInfo:param.chargeInfo, BindInfo: &bindInfo, ObjType: charge_utils.ObjTypeHouse, } err = BillAddHouseOnce(bparam, db, dbname) if err != nil { return err } } } return err } func addByHouseIds(req *pb_v1.ChargeBindRequest, chargeInfo *dbmodel.TChargeConf, db *gorm.DB, dbname string) (reply *pb_v1.ChargeBindReply, err error) { if !charge_utils.IsPowerChargeType[chargeInfo.ChargeType] && req.Start < 1 { return nil, status.Error(10003, "开始时间不能为空") } // 获取房屋信息 houseInfoM, err := charge_utils.GetHouseInfos(req.ObjIds, dbname) if err != nil { return nil, err } if len(houseInfoM) == 0 { return nil, status.Error(10003, "房屋未录入") } houseIds := []int64{} for houseId, _ := range houseInfoM { houseIds = append(houseIds, houseId) } length := len(houseIds) param := houseBindParam{ chargeInfo:chargeInfo, houseInfoM:houseInfoM, } // 分批 for i := 0; i < length; i += 100 { if i+100 >= length { param.houseIds = houseIds[i:] err = bindChargeHouse(req, ¶m, db, dbname) } else { param.houseIds = houseIds[i:i+100] err = bindChargeHouse(req, ¶m, db, dbname) } if err != nil { return nil, err } } return &pb_v1.ChargeBindReply{}, nil }