|
|
@ -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 |
|
|
|
}) |
|
|
|
} |