// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package charge 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-garden/errors" "property-garden/impl/v1/charge_utils" "property-garden/impl/v1/fee" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" ) func checkChargeGenerateBillParam(req *pb_v1.ChargeGenerateBillRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.BindId == 0: return status.Error(10003, "关系id不能为空") } return nil } func generatePowerBill(bind *dbmodel.TChargeBind, chargeInfo *dbmodel.TChargeConf, dbname string) error { p := dbmodel.NewChargePower(dbname) where := map[string]interface{}{ "charge_type":bind.ChargeType, "house_id":bind.ObjId, "bill_id":0, } list, err := p.List(database.DB(), where, nil, -1, -1) if err != nil { return errors.DataBaseError } if len(list) == 0 { return status.Error(10003, "无账单可生成") } house := dbmodel.NewHouseUnitBuilding(dbname) where = map[string]interface{}{ "id":bind.ObjId, } err = house.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound{ return errors.DataBaseError } if house.ID == 0 { return status.Error(10003, "房屋已被删除") } db := database.DB().Begin() for i, _ := range list { param := &BillAddParam{ HouseInfo:house, PowerInfo:&list[i], VehicleInfo: nil, SpaceInfo: nil, ChargeInfo: chargeInfo, BindInfo: bind, ObjType: charge_utils.ObjTypeHouse, } err = BillAddHousePower(param, db, dbname) if err != nil { db.Rollback() return err } } db.Commit() return nil } func ChargeGenerateBill(ctx context.Context, req *pb_v1.ChargeGenerateBillRequest) (reply *pb_v1.ChargeGenerateBillReply, err error) { reply = &pb_v1.ChargeGenerateBillReply{} // 捕获各个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 = checkChargeGenerateBillParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) bind := dbmodel.NewChargeBind(dbname) where := map[string]interface{}{ "id":req.BindId, } err = bind.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound{ return nil, errors.DataBaseError } if bind.ID == 0 { return nil, errors.ErrRecordNotFound } chargeInfo := dbmodel.NewChargeConf(dbname) where = map[string]interface{}{ "id":bind.ChargeId, } err = chargeInfo.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound{ return nil, errors.DataBaseError } if chargeInfo.ID == 0 { return nil, errors.ErrRecordNotFound } if bind.ChargeType == charge_utils.ChargeTypeVehicle { return nil, status.Error(10003, "车辆费用不能手动生成账单") } if chargeInfo.ChargeTimeType == charge_utils.ChargeTimeTypeOne { return nil, status.Error(10003, "一次性费用不能手动生成账单") } if charge_utils.IsPowerChargeType[bind.ChargeType] { return reply, generatePowerBill(bind, chargeInfo, dbname) } //周期性费用 mreq := pb_v1.GenerateBillRequest{ GardenId:req.GardenId, ChargeBindId:req.BindId, EndTime:req.End, } _, err = fee.GenerateBill(ctx, &mreq) if err != nil { return nil, err } return reply, nil }