2024-06-13 14:30:06 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"qteam/app/constants/common"
|
|
|
|
"qteam/app/constants/errorcode"
|
|
|
|
"qteam/app/http/entities/front"
|
|
|
|
"qteam/app/models/ordermodel"
|
|
|
|
"qteam/app/models/productmodel"
|
|
|
|
"qteam/app/models/usersmodel"
|
|
|
|
"qteam/app/utils"
|
|
|
|
redis_util "qteam/app/utils/redis"
|
|
|
|
"qteam/config"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2024-06-13 14:50:26 +08:00
|
|
|
"xorm.io/builder"
|
2024-06-13 14:30:06 +08:00
|
|
|
)
|
|
|
|
|
2024-06-13 18:45:49 +08:00
|
|
|
func CreateOrderService(userId int, productId int) (code int, data front.XinYeOrderResponse) {
|
2024-06-13 14:30:06 +08:00
|
|
|
var err error
|
|
|
|
{ // redis保证用户当前抽奖结束才能开始下次抽奖
|
|
|
|
key := utils.GetRealKey("lottery_code:" + strconv.Itoa(userId) + strconv.Itoa(productId))
|
|
|
|
ok, err := redis_util.AcquireLock(key, time.Second*10)
|
|
|
|
if ok {
|
|
|
|
defer redis_util.Del(key)
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
utils.Log(nil, "CreateOrderService", err.Error())
|
|
|
|
}
|
|
|
|
code = errorcode.OrderLottery
|
2024-06-13 18:45:49 +08:00
|
|
|
return code, data
|
2024-06-13 14:30:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
session := ordermodel.GetInstance().GetDb().NewSession()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
_ = session.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = session.Close()
|
|
|
|
}()
|
|
|
|
var product productmodel.Product
|
|
|
|
has, err := productmodel.GetInstance().GetDb().Where("id =?", productId).Get(&product)
|
|
|
|
if err != nil {
|
2024-06-13 18:45:49 +08:00
|
|
|
return errorcode.SystemError, data
|
2024-06-13 14:30:06 +08:00
|
|
|
}
|
|
|
|
if !has {
|
2024-06-13 18:45:49 +08:00
|
|
|
return errorcode.OrderProductNotExist, data
|
2024-06-13 14:30:06 +08:00
|
|
|
}
|
|
|
|
var user usersmodel.Users
|
|
|
|
_, err = usersmodel.GetInstance().GetDb().Where("id =?", userId).Get(&user)
|
|
|
|
if err != nil {
|
2024-06-13 18:45:49 +08:00
|
|
|
return errorcode.SystemError, data
|
2024-06-13 14:30:06 +08:00
|
|
|
}
|
|
|
|
order := ordermodel.Order{
|
|
|
|
OrderNo: utils.GenerateOrderNumber(),
|
|
|
|
UserId: userId,
|
|
|
|
ProductId: productId,
|
|
|
|
ProductName: product.Name,
|
|
|
|
UserName: user.Name,
|
2024-06-13 18:45:49 +08:00
|
|
|
Price: product.Price,
|
2024-06-13 14:30:06 +08:00
|
|
|
Mobile: user.Phone,
|
|
|
|
ActivityId: config.GetConf().ActivityId,
|
|
|
|
VoucherId: product.ThirdProductId,
|
|
|
|
State: common.STATUSABLED,
|
|
|
|
CreateTime: time.Now(),
|
|
|
|
}
|
|
|
|
if _, err := session.Insert(order); err != nil {
|
|
|
|
utils.Log(nil, "CreateOrderService", err.Error())
|
2024-06-13 18:45:49 +08:00
|
|
|
return errorcode.SystemError, data
|
2024-06-13 14:30:06 +08:00
|
|
|
}
|
|
|
|
if err = session.Begin(); err != nil {
|
2024-06-13 18:45:49 +08:00
|
|
|
return errorcode.SystemError, data
|
2024-06-13 14:30:06 +08:00
|
|
|
}
|
2024-06-13 18:45:49 +08:00
|
|
|
code, data = CreateXyOrder(order)
|
|
|
|
if code != errorcode.Success {
|
|
|
|
return errorcode.SystemError, data
|
|
|
|
}
|
|
|
|
return errorcode.Success, data
|
2024-06-13 14:30:06 +08:00
|
|
|
}
|
|
|
|
|
2024-06-13 18:45:49 +08:00
|
|
|
func OrderQueryService(userId int, OrderRequest *front.OrderListRequest) (code int, data []ordermodel.Order, count int64) {
|
2024-06-13 14:30:06 +08:00
|
|
|
activityId := config.GetConf().ActivityId
|
|
|
|
repo := ordermodel.GetInstance().GetDb()
|
|
|
|
count, err := repo.Where("user_id = ? and activity_id = ?", userId, activityId).
|
|
|
|
Desc("id").Limit(OrderRequest.PageSize, (OrderRequest.Page-1)*OrderRequest.PageSize).FindAndCount(&data)
|
|
|
|
code = handErr(err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-13 14:50:26 +08:00
|
|
|
|
|
|
|
func OrderDetailService(order *ordermodel.Order) (has bool, err error) {
|
|
|
|
repo := ordermodel.GetInstance().GetDb()
|
|
|
|
conn := builder.NewCond()
|
|
|
|
if order.Id != 0 {
|
2024-06-14 10:06:05 +08:00
|
|
|
conn = conn.And(builder.Eq{"id": order.Id})
|
2024-06-13 14:50:26 +08:00
|
|
|
}
|
|
|
|
if order.OrderNo != "" {
|
2024-06-14 10:06:05 +08:00
|
|
|
conn = conn.And(builder.Eq{"order_no": order.OrderNo})
|
2024-06-13 14:50:26 +08:00
|
|
|
}
|
|
|
|
return repo.Where(conn).Get(order)
|
|
|
|
}
|
|
|
|
|
|
|
|
func OrdersUpdateService(req front.OrdersUpdateRequest) (err error) {
|
|
|
|
repo := ordermodel.GetInstance().GetDb()
|
2024-06-14 10:06:05 +08:00
|
|
|
var order ordermodel.Order
|
|
|
|
if req.Id != 0 {
|
|
|
|
order.Id = req.Id
|
|
|
|
}
|
|
|
|
if req.Status != 0 {
|
|
|
|
order.State = req.Status
|
|
|
|
}
|
|
|
|
if req.VoucherLink != "" {
|
|
|
|
order.VoucherLink = req.VoucherLink
|
|
|
|
}
|
|
|
|
_, err = repo.Where("Id = ?", req.Id).Update(&order)
|
2024-06-13 14:50:26 +08:00
|
|
|
return
|
|
|
|
}
|