123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- // 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"
- "gorm.io/gorm"
- "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 checkVehicleChargeConfUpdateParam(req *pb_v1.ChargeConfUpdateRequest) error {
- switch {
- case req.Id < 1:
- return status.Error(10003, "id不能为空")
- 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 checkChargeConfUpdateParam(req *pb_v1.ChargeConfUpdateRequest) error {
- if charge_utils.ChargeTypeM[req.ChargeType] == "" {
- return status.Error(10003, "费用类型不能为空")
- }
- req.BillPeriodType = 1
- if !req.LateFeeEnable {
- req.LateFeePercent = 0
- req.LateFeeMaxDays = 0
- req.LateFeeDay = 0
- }
- 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 checkVehicleChargeConfUpdateParam(req)
- }
- switch {
- case req.Id < 1:
- return status.Error(10003, "id不能为空")
- 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 ChargeConfUpdate(ctx context.Context, req *pb_v1.ChargeConfUpdateRequest) (reply *pb_v1.ChargeConfUpdateReply, err error) {
- reply = &pb_v1.ChargeConfUpdateReply{}
- // 捕获各个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 = checkChargeConfUpdateParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- // 获取旧数据
- old := dbmodel.NewChargeConf(dbname)
- where := map[string]interface{}{
- "id": req.Id,
- //"garden_id":req.GardenId,
- }
- err = old.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if old.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- if old.ChargeTimeType != req.ChargeTimeType ||
- old.ChargeType != req.ChargeType ||
- old.ChargeBasis != req.ChargeBasis {
- isBind, err := charge_utils.IsChargeBindObj(req.Id, dbname)
- if err != nil {
- return nil, err
- }
- if isBind {
- return nil, status.Error(10003, "已绑定对象,无法修改")
- }
- }
- reply.Origin = &pb_v1.ChargeConfUpdateRequest{
- GardenId: req.GardenId,
- ChargeType: old.ChargeType,
- ChargeName: old.ChargeName,
- ChargeTimeType: old.ChargeTimeType,
- ChargeBasis: old.ChargeBasis,
- FixAmount: old.FixAmount,
- FixAmountName: old.FixAmountName,
- UnitPrice: old.UnitPrice,
- ChargeEffectiveTime: old.ChargeEffectiveTime,
- BillPeriod: old.BillPeriod,
- BillPeriodType: old.BillPeriodType,
- LateFeeEnable: false,
- LateFeeMaxDays: old.LateFeeMaxDays,
- LateFeeDay: old.LateFeeDay,
- LateFeePercent: old.LateFeePercent,
- Id: req.Id,
- }
- if old.LateFeeEnable == 1 {
- reply.Origin.LateFeeEnable = true
- }
- // 更新新数据
- now := time.Now()
- chargeConf := dbmodel.NewChargeConf(dbname)
- lateFeeEnable := int32(1)
- if !req.LateFeeEnable {
- lateFeeEnable = 2
- }
- if req.ChargeBasis == charge_utils.ChargeBasisSelf {
- req.FixAmount = 0
- req.UnitPrice = 0
- }
- values := map[string]interface{}{
- "charge_name": req.ChargeName,
- "charge_basis": req.ChargeBasis,
- "fix_amount": req.FixAmount,
- "fix_amount_name": req.FixAmountName,
- "unit_price": req.UnitPrice,
- "updated_at": now,
- "charge_type": req.ChargeType,
- "charge_time_type": req.ChargeTimeType,
- "bill_period": req.BillPeriod,
- "charge_effective_time": req.ChargeEffectiveTime,
- "late_fee_enable": lateFeeEnable,
- "late_fee_day": req.LateFeeDay,
- "late_fee_max_days": req.LateFeeMaxDays,
- "late_fee_percent": req.LateFeePercent,
- }
- where = map[string]interface{}{
- "id": req.Id,
- }
- db := database.DB().Begin()
- err = chargeConf.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- // 同步更新账单中的滞纳金比例
- if lateFeeEnable != old.LateFeeEnable && !req.LateFeeEnable {
- bill := dbmodel.NewChargeBill(dbname)
- where := map[string]interface{}{
- "charge_id": req.Id,
- }
- values := map[string]interface{}{
- "late_fee": 0,
- }
- err = bill.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- }
- db.Commit()
- return reply, nil
- }
|