Browse Source

order

master
黄梓健 5 years ago
parent
commit
ff8eff6b5a
  1. 37
      controllers/client/good.go
  2. 2
      models/customer_order.go
  3. 1
      services/live/live.go
  4. 69
      services/pay/handle.go

37
controllers/client/good.go

@ -288,42 +288,7 @@ func (t *GoodCtl) RefundOrder() {
// 取消订单
func (t *GoodCtl) CancelOrder() {
outTradeNo := t.MustGet("out_trade_no")
order := new(models.CustomerOrder)
exist, err := order.GetByOutTradeNO(outTradeNo)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_ORDER_NOT_EXIST, "用户订单不存在")
if order.Status != 0 {
t.ERROR("非待支付状态不能取消", code.MSG_CUSTOMER_ORDER_ERROR)
}
session := core.GetXormAuto().NewSession()
defer session.Close()
err = session.Begin()
t.CheckErr(err)
order.Status = 9
order.CancelTime = time.Now()
_, err = session.Where("is_delete=0 and status=? and out_trade_no=?",
0, outTradeNo).Cols("status, cancel_time").Update(order)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
subs := make([]*models.CustomerOrderSub, 0)
err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
for _, sub := range subs {
_, err = session.Where("id=?", sub.GoodsId).
Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
if err != nil {
session.Rollback()
t.CheckErr(err)
}
}
err = session.Commit()
t.CheckErr(err)
pay_service.HandleCancelOrder(outTradeNo)
pay_service.Close(outTradeNo)
t.SUCCESS("成功取消订单")
}

2
models/customer_order.go

@ -29,7 +29,7 @@ type CustomerOrder struct {
TotalAmount float64 `json:"total_amount" xorm:"not null default 0.00 comment('订单总额') DECIMAL(18)"`
PayAmount float64 `json:"pay_amount" xorm:"not null default 0.00 comment('支付金额') DECIMAL(18)"`
Postage float64 `json:"postage" xorm:"not null default 0.00 comment('邮费[0免邮]') DECIMAL(18)"`
Status int `json:"status" xorm:"not null default 0 comment('订单状态[0未支付1已支付2待发货3已发货4确认收货5申请退款6已退款]')"`
Status int `json:"status" xorm:"not null default 0 comment('订单状态[0未支付1已支付待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]')"`
// 快递信息
Receiver string `json:"receiver" xorm:"not null default '' comment('收件人') VARCHAR(128)"`

1
services/live/live.go

@ -0,0 +1 @@
package live

69
services/pay/handle.go

@ -3,6 +3,7 @@ package pay_service
import (
"errors"
"fmt"
"github.com/xormplus/xorm"
"hudongzhuanjia/logger"
"hudongzhuanjia/models"
im_service "hudongzhuanjia/services/im"
@ -49,12 +50,7 @@ func loopOrder() {
}
if order.ExpireAt <= time.Now().Unix() { // 订单超时
if order.Status == 0 {
//err = Close(order.OutTradeNo) // 超时关闭订单
//if err != nil {
// logger.Error("订单关闭出现错误: 错误原因->", err)
//}
//order.Status = 5
go HandleFailed(order)
go HandleTimeout(order)
}
continue
}
@ -246,32 +242,12 @@ func HandleSuccess(order *models.UserOrder) error {
return nil
}
func HandleFailed(order *models.UserOrder) error {
func HandleTimeout(order *models.UserOrder) error {
// 退还库存
if order.GoodType == 4 {
err := Close(order.OutTradeNo)
err := HandleCancelOrder(order.OutTradeNo)
if err != nil {
err = fmt.Errorf("直播订单关闭错误: err-> %v, out_trade_no-> %+v", err, order.OutTradeNo)
return err
}
customerOrder := new(models.CustomerOrder)
exist, err := customerOrder.GetByOutTradeNO(order.OutTradeNo)
if err != nil || !exist {
err = fmt.Errorf("不存在客户订单: err-> %v, out_trade_no->%v", err, order.OutTradeNo)
logger.Error(err.Error())
return err
}
subs, err := models.GetCustomerOrderSubsByOrderNos(customerOrder.OrderNo)
if err != nil {
err = fmt.Errorf("不存在客户子订单: err-> %v, out_trade_no->%v", err, order.OutTradeNo)
return err
}
for _, sub := range subs {
_, err = new(models.CustomerGoods).IncrStockById(sub["goods_id"], sub["goods_num"])
if err != nil {
err = fmt.Errorf("库存返回失败: err->%v, out_trade_no->%v", err, order.OutTradeNo)
return err
}
logger.Error("下订单", err)
}
}
return nil
@ -301,3 +277,38 @@ func HandleReward() error {
}
return nil
}
func HandleCancelOrder(outTradeNo string) error {
return models.Commit(func(session *xorm.Session) error {
order := new(models.CustomerOrder)
exist, err := session.Where("is_delete=0 and out_trade_no=?",
outTradeNo).Get(order)
if err != nil {
return err
}
if !exist || order.Status != 0 {
return errors.New("客户订单异常")
}
order.Status = 9
order.CancelTime = time.Now()
_, err = session.Where("is_delete=0 and out_trade_no=?",
outTradeNo).Cols("status, cancel_time").Update(order)
if err != nil {
return err
}
subs := make([]*models.CustomerOrderSub, 0)
err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
if err != nil {
return err
}
for _, sub := range subs {
_, err = session.Where("id=?", sub.GoodsId).
Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
if err != nil {
return err
}
}
return nil
})
}
Loading…
Cancel
Save