// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package charge_conf import ( "context" "encoding/json" "fmt" "property-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkVehicleChargeConfAddParam(req *pb_v1.ChargeConfAddRequest) error { switch { case req.GardenId < 1: return status.Error(10003, "小区不能为空") case charge_utils.ChargeTypeM[req.ChargeType] == "": return status.Error(10003, "费用类型不能为空") case req.ChargeBasis == charge_utils.ChargeBasisFix && req.FixAmount < 1: return status.Error(10003, "按固定费用计价,固定费用不能为空") case req.ChargeName == "": return status.Error(10003, "费用名称不能为空") case req.ChargeBasis != charge_utils.ChargeBasisFix && req.ChargeBasis != charge_utils.ChargeBasisSelf: return status.Error(10003, "月租车费用只能为固定/自定义") } return nil } func checkChargeConfAddParam(req *pb_v1.ChargeConfAddRequest) error { if charge_utils.ChargeTypeM[req.ChargeType] == "" { return status.Error(10003, "费用类型不能为空") } if !req.LateFeeEnable { req.LateFeePercent = 0 req.LateFeeMaxDays = 0 req.LateFeeDay = 0 } req.BillPeriodType = 1 switch { case req.ChargeType == charge_utils.ChargeTypeProperty: req.ChargeTimeType = charge_utils.ChargeTimeTypePeriod if req.ChargeBasis != charge_utils.ChargeBasisUsedArea && req.ChargeBasis != charge_utils.ChargeBasisArea && req.ChargeBasis != charge_utils.ChargeBasisFix { return status.Error(10003, "物业费只能按房屋面积/使用面积/固定费用") } case charge_utils.IsPowerChargeType[req.ChargeType]: req.ChargeTimeType = 0 if req.ChargeBasis != charge_utils.ChargeBasisUsed { return status.Error(10003, "水电气费只能按使用量") } case req.ChargeType == charge_utils.ChargeTypeSpace: req.ChargeTimeType = charge_utils.ChargeTimeTypePeriod if req.ChargeBasis != charge_utils.ChargeBasisSpaceArea && req.ChargeBasis != charge_utils.ChargeBasisFix { return status.Error(10003, "车位管理费只能按房屋面积/使用面积/固定费用") } case req.ChargeType == charge_utils.ChargeTypeVehicle: req.ChargeTimeType = 0 return checkVehicleChargeConfAddParam(req) } switch { case req.GardenId < 1: return status.Error(10003, "小区不能为空") case charge_utils.ChargeBasisM[req.ChargeBasis] == "" && !charge_utils.IsPowerChargeType[req.ChargeType]: return status.Error(10003, "计费规则不能为空") case charge_utils.ChargeTypeM[req.ChargeType] == "": return status.Error(10003, "费用类型不能为空") case charge_utils.ChargeTimeTypeM[req.ChargeTimeType] == "" && !charge_utils.IsPowerChargeType[req.ChargeType]: return status.Error(10003, "缴费时间类型不能为空") case req.ChargeBasis == charge_utils.ChargeBasisFix && req.FixAmount < 1: return status.Error(10003, "按固定费用计价,固定费用不能为空") case req.ChargeName == "": return status.Error(10003, "费用名称不能为空") case req.ChargeBasis != charge_utils.ChargeBasisFix && req.UnitPrice < 1: return status.Error(10003, "单价不能为空") case req.ChargeTimeType == charge_utils.ChargeTimeTypePeriod && charge_utils.BillPeriodM[req.BillPeriod] == "": return status.Error(10003, "账单生成周期错误") case req.ChargeEffectiveTime < 1: return status.Error(10003, "费用生效时间错误") case req.LateFeeEnable: if req.LateFeeDay == 0 || req.LateFeePercent <= 0 { return status.Error(10003, "滞纳金参数不能为空") } } return nil } func ChargeConfAdd(ctx context.Context, req *pb_v1.ChargeConfAddRequest) (reply *pb_v1.ChargeConfAddReply, err error) { reply = &pb_v1.ChargeConfAddReply{} // 捕获各个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 = checkChargeConfAddParam(req) if err != nil { return nil, err } now := time.Now() dbname := utils.GetGardenDbName(req.GardenId) lateFeeEnable := int32(1) if !req.LateFeeEnable { lateFeeEnable = 2 } if req.ChargeBasis == charge_utils.ChargeBasisSelf { req.FixAmount = 0 req.UnitPrice = 0 } chargeConf := &dbmodel.TChargeConf{ ChargeEffectiveTime: req.ChargeEffectiveTime, BillPeriodType: req.BillPeriodType, BillPeriod: req.BillPeriod, LateFeePercent: req.LateFeePercent, LateFeeDay: req.LateFeeDay, LateFeeEnable: lateFeeEnable, LateFeeMaxDays: req.LateFeeMaxDays, ChargeType: req.ChargeType, ChargeName: req.ChargeName, ChargeTimeType: req.ChargeTimeType, ChargeBasis: req.ChargeBasis, FixAmount: req.FixAmount, FixAmountName: req.FixAmountName, UnitPrice: req.UnitPrice, UpdatedAt: now, CreatedAt: now, } chargeConf.SetTable(dbname) err = chargeConf.Insert(database.DB()) if err != nil { return nil, errors.DataBaseError } reply.Id = chargeConf.ID return reply, nil }