You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
3.5 KiB
141 lines
3.5 KiB
package order
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"recook/internal/api/mobile/pay/public"
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/goods"
|
|
"recook/internal/model/order"
|
|
"recook/internal/model/user"
|
|
"recook/internal/v2/lib/jcook"
|
|
"recook/internal/v2/lib/shama"
|
|
"recook/tools"
|
|
)
|
|
|
|
type cancelOrderParam struct {
|
|
UserID uint `json:"userId"`
|
|
OrderID uint `json:"orderId"`
|
|
}
|
|
|
|
// CancelOrder 取消订单 标记订单取消
|
|
func CancelOrder(c *gin.Context) {
|
|
var p cancelOrderParam
|
|
err := tools.ParseParams(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
var orderInfo []order.Information
|
|
|
|
if !public.Judge(p.OrderID) {
|
|
if err = dbc.DB.Find(&orderInfo, "virtual_id = ?", p.OrderID).Error; err != nil {
|
|
back.Fail(c, "订单错误")
|
|
return
|
|
}
|
|
} else {
|
|
if err = dbc.DB.Find(&orderInfo, p.OrderID).Error; err != nil {
|
|
back.Fail(c, "订单错误")
|
|
return
|
|
}
|
|
}
|
|
|
|
for _, v := range orderInfo {
|
|
if v.Status != 0 {
|
|
back.Fail(c, "该订单已付款,不可取消")
|
|
return
|
|
}
|
|
}
|
|
|
|
for _, v := range orderInfo {
|
|
if err := CancelOrExpireOrder(&v, true); err != nil {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
back.Suc(c, "", nil)
|
|
}
|
|
|
|
// CancelOrExpireOrder 0未付款 1支付成功 2订单取消 3订单过期 4交易成功 5订单关闭
|
|
func CancelOrExpireOrder(orderInfo *order.Information, cancel bool) error {
|
|
tx := dbc.DB.Begin()
|
|
{
|
|
if (orderInfo.Kind == 1 || orderInfo.Kind == 2) && orderInfo.ApplyStatus != 11 && orderInfo.ApplyStatus != 12 {
|
|
client := jcook.GetClient()
|
|
req := jcook.OrderCancelReq{
|
|
OrderID: orderInfo.JCookOrderID,
|
|
CancelReasonCode: 100,
|
|
}
|
|
var resp jcook.OrderCancelResp
|
|
if err := client.Exec(req, &resp); err != nil {
|
|
return err
|
|
}
|
|
if resp.CancelStatus == 1 {
|
|
tx.Model(orderInfo).Update("apply_status", 12) // 取消申请失败
|
|
} else {
|
|
tx.Model(orderInfo).Update("apply_status", 11) // 取消申请成功
|
|
}
|
|
}
|
|
|
|
if orderInfo.Kind == 3 {
|
|
client := shama.GetClient()
|
|
req := shama.OrderCancelReq{
|
|
OrderID: orderInfo.ShaMaOrderID,
|
|
CancelReasonCode: 100,
|
|
}
|
|
var resp shama.OrderCancelResp
|
|
if err := client.Exec(req, &resp); err != nil {
|
|
return err
|
|
}
|
|
if resp.CancelStatus == 1 {
|
|
tx.Model(orderInfo).Update("apply_status", 12) // 取消申请失败
|
|
} else {
|
|
tx.Model(orderInfo).Update("apply_status", 11) // 取消申请成功
|
|
}
|
|
}
|
|
var wallet user.Wallet
|
|
tx.First(&wallet, "user_id = ?", orderInfo.UserID)
|
|
|
|
var goodsList []order.GoodsDetail
|
|
tx.Find(&goodsList, "order_id = ?", orderInfo.ID)
|
|
|
|
// 标记订单取消
|
|
if cancel {
|
|
if err := tx.Model(&orderInfo).Updates(order.Information{Status: 2}).Error; err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
} else {
|
|
if err := tx.Model(&orderInfo).Updates(order.Information{Status: 3}).Error; err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
}
|
|
|
|
// 存库撤回 瑞币明细
|
|
for _, v := range goodsList {
|
|
// 排除订单取消时活动商品库存-1导致数据溢出的情况
|
|
var sku goods.Sku
|
|
tx.First(&sku, v.SkuID)
|
|
salesVolume := sku.SalesVolume - v.Quantity
|
|
if orderInfo.OrderType == 2 {
|
|
salesVolume = sku.SaleVolume2 - v.Quantity
|
|
}
|
|
if sku.SalesVolume < v.Quantity {
|
|
salesVolume = 0
|
|
}
|
|
if err := tx.Model(&sku).Updates(map[string]interface{}{
|
|
"sales_volume": salesVolume,
|
|
"inventory": sku.Inventory + v.Quantity,
|
|
}).Error; err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
tx.Commit()
|
|
return nil
|
|
}
|