123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- // 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 checkChargePrePayInfoParam(req *pb_v1.ChargePrePayInfoRequest) 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, "月数不能为空")
- }
- return nil
- }
- func ChargePrePayInfo(ctx context.Context, req *pb_v1.ChargePrePayInfoRequest) (reply *pb_v1.ChargePrePayInfoReply, err error) {
- reply = &pb_v1.ChargePrePayInfoReply{}
- // 捕获各个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 = checkChargePrePayInfoParam(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
- }
- start := int64(0)
- end := int64(0)
- now := time.Now()
- if bind.ChargeType == charge_utils.ChargeTypeVehicle {
- if bind.Start == 0 {
- start = getTimeDayTimestamp(now)
- end = time.Unix(start, 0).AddDate(0, int(req.Months), 0).Unix()
- } else {
- if bind.End < getTimeDayTimestamp(now) {
- return nil, status.Error(10003, "当前费用已断缴,请到物业缴费")
- }
- start = bind.End
- end = time.Unix(start, 0).AddDate(0, int(req.Months), 0).Unix()
- }
- } else {
- 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()
- }
- }
- reply.ShouldPayAmount, reply.ChargeDesc, _ = charge_utils.PrePayInfo(chargeInfo, bind, req.Months)
- reply.ChargeType = bind.ChargeType
- reply.ChargeName = chargeInfo.ChargeName
- reply.Start = start
- reply.End = end
- return reply, nil
- }
|