123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // 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 checkChargeOrderCancelParam(req *pb_v1.ChargeOrderCancelRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.OrderId == "":
- return status.Error(10003, "订单号不能为空")
- }
- return nil
- }
- func ChargeOrderCancel(ctx context.Context, req *pb_v1.ChargeOrderCancelRequest) (reply *pb_v1.ChargeOrderCancelReply, err error) {
- reply = &pb_v1.ChargeOrderCancelReply{}
- // 捕获各个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 = checkChargeOrderCancelParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- // 查看订单
- order := dbmodel.NewChargeOrder(dbname)
- where := map[string]interface{}{
- "order_id":req.OrderId,
- }
- err = order.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound{
- return nil, errors.DataBaseError
- }
- if order.OrderId == "" {
- return nil, errors.ErrRecordNotFound
- }
- if order.Status != charge_utils.PayStatusUnPay {
- return nil, status.Error(10003, "当前状态不能取消")
- }
- if time.Now().Unix() - order.CreatedAt.Unix() < 10 *60 {
- //return nil, status.Error(10003, "订单10钟内不能取消")
- }
- if req.ByUser {
- values := map[string]interface{}{
- "deleted":1,
- }
- err = order.Update(database.DB(), where, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- return reply, nil
- }
- // 删除订单
- db := database.DB().Begin()
- defer func () {
- if err != nil {
- db.Rollback()
- }
- }()
- err = order.Delete(db, where)
- if err != nil {
- return nil, errors.DataBaseError
- }
- // 修改账单状态为待缴
- bill := dbmodel.NewChargeBill(dbname)
- where = map[string]interface{}{
- "order_id":req.OrderId,
- }
- values := map[string]interface{}{
- "status":charge_utils.PayStatusUnPay,
- "order_id":"",
- }
- err = bill.Update(db, where, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- db.Commit()
- return reply, nil
- }
|