123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- // 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"
- dbmodel "property-garden/model"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- )
- func checkChargeRecoverBillParam(req *pb_v1.ChargeRecoverBillRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.BillId == 0:
- return status.Error(10003, "账单id不能为空")
- }
- return nil
- }
- func ChargeRecoverBill(ctx context.Context, req *pb_v1.ChargeRecoverBillRequest) (reply *pb_v1.ChargeRecoverBillReply, err error) {
- reply = &pb_v1.ChargeRecoverBillReply{}
- // 捕获各个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 = checkChargeRecoverBillParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- bill := dbmodel.NewChargeBillDel(dbname)
- where := map[string]interface{}{
- "id": req.BillId,
- }
- err = bill.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if bill.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- recover := dbmodel.TChargeBill{
- ID: 0,
- ObjType: bill.ObjType,
- ObjId: bill.ObjId,
- ChargeId: bill.ChargeId,
- ChargeBindId: bill.ChargeBindId,
- ChargeTimeType: bill.ChargeTimeType,
- ChargeDesc: bill.ChargeDesc,
- Comment: bill.Comment,
- Amount: bill.Amount,
- ChargePowerId: bill.ChargePowerId,
- ChargeType: bill.ChargeType,
- ChargeName: bill.ChargeName,
- Uid: bill.Uid,
- PayMode: bill.PayMode,
- PackageId: bill.PackageId,
- ChargeEnd: bill.ChargeEnd,
- ChargeStart: bill.ChargeStart,
- OrderId: bill.OrderId,
- PayTime: bill.PayTime,
- HouseId: bill.HouseId,
- ObjName: bill.ObjName,
- LateFee: bill.LateFee,
- CreatedAt: bill.CreatedAt,
- UpdatedAt: bill.UpdatedAt,
- PayType: bill.PayType,
- Status: bill.Status,
- }
- db := database.DB().Begin()
- defer func() {
- if err != nil {
- db.Rollback()
- }
- }()
- recover.SetTable(dbname)
- oldBill := dbmodel.NewChargeBill(dbname)
- filter := map[string]interface{}{
- "charge_bind_id": bill.ChargeBindId,
- "charge_id": bill.ChargeId,
- }
- or := map[string]interface{}{
- fmt.Sprintf("charge_end <= %d and charge_end >= %d", bill.ChargeEnd, bill.ChargeStart): "",
- fmt.Sprintf("charge_start <= %d and charge_start >= %d", bill.ChargeEnd, bill.ChargeStart): "",
- }
- count := int64(0)
- count, err = oldBill.Count(db, filter, or)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if count > 0 {
- return nil, status.Error(10003, "无法恢复,当前账单和已有账单有冲突")
- }
- err = recover.Insert(db)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if recover.ChargePowerId > 0 {
- power := dbmodel.NewChargePower(dbname)
- filter := map[string]interface{}{
- "bill_id": 0,
- "id": recover.ChargePowerId,
- }
- values := map[string]interface{}{
- "bill_id": recover.ID,
- }
- err = power.Update(db, filter, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- }
- err = bill.Delete(db, where)
- if err != nil {
- return nil, errors.DataBaseError
- }
- db.Commit()
- return reply, nil
- }
|