Compare commits

...

2 Commits

Author SHA1 Message Date
qiyunfanbo126.com 8ef1e5ba09 修改:未支付订单15分钟自动取消,增加库存,购买就减库存 2024-07-16 15:52:49 +08:00
qiyunfanbo126.com b24a0b5525 bug:产品下架仍可以购买 2024-07-16 14:13:30 +08:00
5 changed files with 48 additions and 9 deletions

View File

@ -2,8 +2,40 @@ package console
import (
"github.com/qit-team/snow-core/command"
"qteam/app/models/ordersmodel"
"strconv"
"strings"
"time"
)
func RegisterCommand(c *command.Command) {
c.AddFunc("test", test)
}
func ClearUnpayOrder() {
var expire = time.Now().Add(-1 * time.Minute).Format(time.DateTime)
var orders []ordersmodel.Orders
ordersmodel.GetInstance().GetDb().Where("state = 1 and create_time <= ?", expire).Select("id,product_id").Find(&orders)
var ids = make([]int, 0)
var prodIds = make([]string, 0)
for _, v := range orders {
ids = append(ids, v.Id)
prodIds = append(prodIds, strconv.Itoa(v.ProductId))
}
if len(prodIds) > 0 {
var session = ordersmodel.GetInstance().GetDb().NewSession()
session.Begin()
_, err := session.In("id", ids).Update(&ordersmodel.Orders{State: 9})
if err == nil {
_, err = session.Exec("update Products set stock=stock+1 where id in (" + strings.Join(prodIds, ",") + ")")
if err == nil {
session.Commit()
} else {
session.Rollback()
}
} else {
session.Rollback()
}
}
}

View File

@ -9,7 +9,5 @@ import (
* @wiki https://godoc.org/github.com/robfig/cron
*/
func RegisterSchedule(c *cron.Cron) {
//c.AddFunc("0 30 * * * *", test)
//c.AddFunc("@hourly", test)
c.AddFunc("@every 10s", test)
c.AddFunc("@every 1m", ClearUnpayOrder)
}

View File

@ -77,7 +77,7 @@ var MsgZH = map[int]string{
YouChuOrderRefundFail: "邮储服务异常",
ProductStockFAIL: "库存不足",
InsertUserFail: "用户新增失败",
UserNotExist: "用户不存在",
UserNotExist: "产品不存在",
Fail: "请求失败",
YouChuCodeFail: "Code解析失败",
YouChuCustNoEmpty: "客户编号为空",

View File

@ -47,6 +47,7 @@ func OrderQuery(c *gin.Context) {
order := ordersmodel.Orders{}
order.Id = orderId
order.OrderNo = request.OrderNo
order.UserId = controllers.GetUserId(c)
var OrderQueryResponse front.OrderQueryResponse
has, err := services.OrderDetailService(&order)
if err != nil {

View File

@ -42,7 +42,7 @@ func CreateOrderService(userId int, productId int) (code int, data front.InsertO
_ = session.Close()
}()
var product productsmodel.Products
has, err := productsmodel.GetInstance().GetDb().Where("id = ?", productId).Get(&product)
has, err := productsmodel.GetInstance().GetDb().Where("id = ? and status = 1", productId).Get(&product)
if err != nil {
return errorcode.SystemError, data
}
@ -76,7 +76,15 @@ func CreateOrderService(userId int, productId int) (code int, data front.InsertO
utils.Log(nil, "CreateOrderService", err.Error())
return errorcode.SystemError, data
} else {
_ = session.Commit()
rs, err := session.Exec("update Products set stock = stock-1 where stock >= 1 and id = ?", product.Id)
var affect, _ = rs.RowsAffected()
if err != nil || affect < 1 {
return errorcode.ProductStockFAIL, data
session.Rollback()
} else {
_ = session.Commit()
}
}
data.OrderNo = order.OrderNo
data.NotifyUrl = config.GetConf().YouChu.NotifyUrl
@ -228,8 +236,8 @@ func OrderNotify(request front.YouChuRequest) (NotifyResponse front.YouChuOrderN
} else {
order.State = common.ORDER_STATUS_FINISH
order.VoucherLink = send.Data.ShortUrl
Stock, _ := strconv.Atoi(productDetail.Stock)
productDetail.Stock = strconv.Itoa(Stock - 1)
//Stock, _ := strconv.Atoi(productDetail.Stock)
//productDetail.Stock = strconv.Itoa(Stock - 1)
}
} else {
@ -237,7 +245,7 @@ func OrderNotify(request front.YouChuRequest) (NotifyResponse front.YouChuOrderN
//order.RefundOrderSta = YouChuOrderNotifyRequest.OrderSta
return front.YouChuOrderNotifyResponse{RespCode: "000005", RespMsg: "订单标识错误"}
}
_, err = session.Where("order_no = ?", YouChuOrderNotifyRequest.ReqTraceId).Update(&order)
_, err = session.Cols("stock,state").Where("order_no = ?", YouChuOrderNotifyRequest.ReqTraceId).Update(&order)
if err != nil {
utils.Log(nil, "OrderNotify-MarketSend", err.Error())
_ = session.Rollback()