// 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" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" ) func checkChargePrePayParam(req *pb_v1.ChargePrePayRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.BindId == 0: return status.Error(10003, "房屋/车位/车辆没有绑定费用") case req.Months == 0: return status.Error(10003, "月数不能为空") case req.ShouldPayAmount == 0: return status.Error(10003, "应收金额不能为空") case req.PayAmount == 0: return status.Error(10003, "实收金额不能为空") } return nil } type PrePayBillParam struct { ChargeInfo *dbmodel.TChargeConf BindInfo *dbmodel.TChargeBind Start int64 End int64 Amount int64 Desc string Comment string PayTime int64 Order string PayType int32 } func PrePayBillAdd(param *PrePayBillParam, db *gorm.DB, dbname string) (int64, error) { bill := dbmodel.TChargeBill{ ObjType: param.BindInfo.ObjType, ObjId: param.BindInfo.ObjId, ChargeId: param.BindInfo.ChargeId, ChargeBindId: param.BindInfo.ID, ChargeTimeType: param.ChargeInfo.ChargeTimeType, ChargeDesc: param.Desc, Comment: param.Comment, Amount: param.Amount, ChargePowerId: 0, ChargeType: param.ChargeInfo.ChargeType, ChargeName: param.ChargeInfo.ChargeName, Uid: 0, PayMode: 2, PackageId: 0, ChargeEnd: param.Start, ChargeStart: param.End, OrderId: param.Order, PayTime: param.PayTime, HouseId: param.BindInfo.HouseId, ObjName: param.BindInfo.ObjName, LateFee: 0, PayType: param.PayType, Status: charge_utils.PayStatusPayed, } bill.SetTablePayed(dbname) err := bill.Insert(db) if err != nil { return 0, errors.DataBaseError } return bill.ID, nil } func PrePayOrderAdd(req *pb_v1.ChargePrePayRequest, now time.Time, billId int64, orderId string, objName string, db *gorm.DB, dbname string, desc string) (dbmodel.TChargeOrder, error) { order := dbmodel.TChargeOrder{ OrderId: orderId, Comment: req.Comment, Amount: req.ShouldPayAmount, PayAmount: req.PayAmount, PayType: req.PayType, CreatedAt: now, Uid: 0, PackageId: 0, PrepayId: "", TransactionId: "", Status: charge_utils.PayStatusPayed, PayingDesc: desc, PayTime: now.Unix(), BillIds: fmt.Sprintf("%d", billId), ObjName: objName, } order.SetTable(dbname) err := order.Insert(db) if err != nil { return order, errors.DataBaseError } return order, nil } func ChargePrePay(ctx context.Context, req *pb_v1.ChargePrePayRequest) (reply *pb_v1.ChargePrePayReply, err error) { reply = &pb_v1.ChargePrePayReply{} // 捕获各个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 = checkChargePrePayParam(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 } // 是否有未缴账单 bill := dbmodel.NewChargeBill(dbname) where = map[string]interface{}{ "charge_bind_id": req.BindId, } count, err := bill.Count(database.DB(), where, nil) if err != nil { return nil, errors.DataBaseError } if count > 0 { return nil, status.Error(10003, "当前费用有未缴账单,请先缴清欠费") } // 费用信息 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 } // 获取月数对应的应缴费用 shouldPayAmount, _, chargeDesc := charge_utils.PrePayInfo(chargeInfo, bind, req.Months) if req.ShouldPayAmount != shouldPayAmount { return nil, status.Error(10003, "应缴费用不一致") } // 账单开始和截止月份 start := int64(0) end := int64(0) if bind.BillLastTime > 0 { start = bind.BillLastTime end = time.Unix(bind.BillLastTime, 0).AddDate(0, int(req.Months), 0).Unix() } else { if bind.Start == 0 { return nil, status.Error(10003, "当前费用未生效,请先变更开始时间") } start = bind.Start end = time.Unix(start, 0).AddDate(0, int(req.Months), 0).Unix() } // 新增已缴账单 now := time.Now() oid, err := GenerateOrderId(req.GardenId, OrderTypeLife) if err != nil { return nil, err } param := PrePayBillParam{ ChargeInfo: chargeInfo, BindInfo: bind, Start: start, End: end, Amount: req.ShouldPayAmount, Desc: chargeDesc, Comment: req.Comment, PayTime: now.Unix(), Order: oid, PayType: req.PayType, } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() billId, err := PrePayBillAdd(¶m, db, dbname) if err != nil { return nil, err } desc := "" if bind.ChargeType == charge_utils.ChargeTypeProperty { desc = "预存物业费" } else if bind.ChargeType == charge_utils.ChargeTypeSpace { desc = "预存车位管理费" } // 新增对应订单 order, err := PrePayOrderAdd(req, now, billId, param.Order, bind.ObjName, db, dbname, desc) if err != nil { return nil, err } // 修改绑定关系里的账单截止时间戳 where = map[string]interface{}{ "id": req.BindId, } values := map[string]interface{}{ "bill_last_time": end, } err = bind.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } if err = charge_utils.CompanyDealIncrease(req.GardenId, order.PayAmount, order.PayTime); err != nil { return nil, err } db.Commit() reply.OrderId = param.Order return reply, nil }