package charge import ( "encoding/json" "fmt" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" "time" ) type BillAddParam struct { HouseInfo *dbmodel.THouseUnitBuilding PowerInfo *dbmodel.TChargePower VehicleInfo *dbmodel.TVehicle SpaceInfo *dbmodel.TSpacePark ChargeInfo *dbmodel.TChargeConf BindInfo *dbmodel.TChargeBind ObjType int32 Months int64 } func getTimeMonthTimestamp(t time.Time) int64 { return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()).Unix() } func getTimeDayTimestamp(t time.Time) int64 { return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix() } func HouseOnceShouldPay(chargeInfo *dbmodel.TChargeConf, houseInfo *dbmodel.THouseUnitBuilding, bindInfo *dbmodel.TChargeBind) (int64, string) { // 固定费用 chargeDesc := dbmodel.ChargeDesc{ ChargeBasis: chargeInfo.ChargeBasis, UnitPrice: chargeInfo.UnitPrice, FixAmount: chargeInfo.FixAmount, FixAmountName: chargeInfo.FixAmountName, } if chargeInfo.ChargeBasis == charge_utils.ChargeBasisFix { amount := chargeInfo.FixAmount bytes, _ := json.Marshal(chargeDesc) return amount, string(bytes) } // 单价计费 amount := int64(0) if chargeInfo.ChargeBasis == charge_utils.ChargeBasisArea { chargeDesc.ObjArea = houseInfo.HouseArea amount = int64(float64(chargeInfo.UnitPrice) * houseInfo.HouseArea) } if chargeInfo.ChargeBasis == charge_utils.ChargeBasisUsedArea { chargeDesc.ObjArea = houseInfo.HouseUsedArea amount = int64(float64(chargeInfo.UnitPrice) * houseInfo.HouseUsedArea) } if chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf { chargeDesc.CustomFee = bindInfo.CustomFee amount = bindInfo.CustomFee } if chargeInfo.FixAmount > 0 { amount += chargeInfo.FixAmount } bytes, _ := json.Marshal(chargeDesc) return amount, string(bytes) } func HousePowerShouldPay(chargeInfo *dbmodel.TChargeConf, powerInfo *dbmodel.TChargePower) (int64, string) { chargeDesc := dbmodel.ChargeDesc{ ChargeBasis: chargeInfo.ChargeBasis, UnitPrice: chargeInfo.UnitPrice, FixAmount: chargeInfo.FixAmount, FixAmountName: chargeInfo.FixAmountName, } amount := int64((powerInfo.CurrentAmount - powerInfo.LastAmount) * float64(chargeInfo.UnitPrice)) chargeDesc.PowerLastUsed = powerInfo.LastAmount chargeDesc.PowerCurrentUsed = powerInfo.CurrentAmount if chargeInfo.FixAmount > 0 { months := charge_utils.GetMonths(powerInfo.Start, powerInfo.End) amount += amount * months } bytes, _ := json.Marshal(chargeDesc) return amount, string(bytes) } func BillAddHouseOnce(param *BillAddParam, db *gorm.DB, dbname string) error { amount, desc := HouseOnceShouldPay(param.ChargeInfo, param.HouseInfo, param.BindInfo) bill := dbmodel.TChargeBill{ ObjType: charge_utils.ObjTypeHouse, ObjId: param.HouseInfo.ID, ChargeId: param.ChargeInfo.ID, ChargeBindId: param.BindInfo.ID, ChargeTimeType: param.ChargeInfo.ChargeTimeType, ChargeDesc: desc, Comment: "", Amount: amount, ChargePowerId: 0, ChargeType: param.ChargeInfo.ChargeType, ChargeName: param.ChargeInfo.ChargeName, Uid: 0, PayMode: 0, PackageId: 0, ChargeEnd: param.BindInfo.Start, ChargeStart: param.BindInfo.Start, OrderId: "", PayTime: 0, HouseId: param.HouseInfo.ID, ObjName: fmt.Sprintf("%v-%v-%v", param.HouseInfo.BuildingNumber, param.HouseInfo.UnitNumber, param.HouseInfo.HouseNumber), LateFee: 0, Status: charge_utils.PayStatusUnPay, } bill.SetTable(dbname) err := bill.Insert(db) if err != nil { return errors.DataBaseError } bind := dbmodel.NewChargeBind(dbname) where := map[string]interface{}{ "id": param.BindInfo.ID, } values := map[string]interface{}{ "bill_last_time": param.BindInfo.Start, } err = bind.Update(db, where, values) if err != nil { return errors.DataBaseError } return nil } func BillAddHousePower(param *BillAddParam, db *gorm.DB, dbname string) error { //now := time.Now() amount, desc := HousePowerShouldPay(param.ChargeInfo, param.PowerInfo) if amount <= 0 { return nil } bill := dbmodel.TChargeBill{ ObjType: charge_utils.ObjTypeHouse, ObjId: param.HouseInfo.ID, ChargeId: param.ChargeInfo.ID, ChargeBindId: param.BindInfo.ID, ChargeTimeType: param.ChargeInfo.ChargeTimeType, ChargeDesc: desc, Comment: "", Amount: amount, ChargePowerId: param.PowerInfo.ID, ChargeType: param.ChargeInfo.ChargeType, ChargeName: param.ChargeInfo.ChargeName, Uid: 0, PayMode: 0, PackageId: 0, ChargeEnd: param.PowerInfo.End, ChargeStart: param.PowerInfo.Start, OrderId: "", PayTime: 0, HouseId: param.HouseInfo.ID, ObjName: fmt.Sprintf("%v-%v-%v", param.HouseInfo.BuildingNumber, param.HouseInfo.UnitNumber, param.HouseInfo.HouseNumber), LateFee: 0, Status: charge_utils.PayStatusUnPay, } bill.SetTable(dbname) err := bill.Insert(db) if err != nil { return errors.DataBaseError } bind := dbmodel.NewChargeBind(dbname) where := map[string]interface{}{ "id": param.BindInfo.ID, } values := map[string]interface{}{ "bill_last_time": param.PowerInfo.End, } err = bind.Update(db, where, values) if err != nil { return errors.DataBaseError } power := dbmodel.NewChargePower(dbname) where = map[string]interface{}{ "id": param.PowerInfo.ID, } values = map[string]interface{}{ "bill_id": bill.ID, } err = power.Update(db, where, values) if err != nil { return errors.DataBaseError } return nil }