121 lines
3.9 KiB
Go
121 lines
3.9 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/context"
|
|
"io"
|
|
"pet-house.com/business/models"
|
|
"pet-house.com/business/utils"
|
|
"pet-house.com/core/server/database"
|
|
"pet-house.com/core/server/web/web_iris"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func (p DefParty) getCouponsList() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(CouponsBase+"/getCouponsList", func(ctx *context.Context) {
|
|
headerInfo := GetHeaderBaseInfo(ctx)
|
|
var userInfo *models.User
|
|
database.Instance().Model(&models.User{}).Where("id = ?", headerInfo.Uid).Find(&userInfo)
|
|
if userInfo == nil || userInfo.Id == 0 {
|
|
UserNotExistError.Fail(ctx, nil)
|
|
return
|
|
}
|
|
var coupons []models.Coupons
|
|
database.Instance().Model(&models.Coupons{}).Where("status = 1 and expire_time > ?", time.Now().UnixNano()/int64(time.Millisecond)).Find(&coupons)
|
|
for index, coupon := range coupons {
|
|
coupons[index].PeriodInfo = strings.Split(coupon.Period, ",")
|
|
}
|
|
Success(ctx, headerInfo, coupons)
|
|
})
|
|
}}
|
|
}
|
|
|
|
type GetUserCouponsListRequest struct {
|
|
Status int
|
|
}
|
|
|
|
func (p DefParty) getUserCouponsList() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(CouponsBase+"/getUserCouponsList", func(ctx *context.Context) {
|
|
headerInfo := GetHeaderBaseInfo(ctx)
|
|
var getUserCouponsListRequest GetUserCouponsListRequest
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
json.Unmarshal(body, &getUserCouponsListRequest)
|
|
if getUserCouponsListRequest.Status == 0 {
|
|
ParamError.Fail(ctx, nil)
|
|
return
|
|
}
|
|
var userInfo *models.User
|
|
database.Instance().Model(&models.User{}).Where("id = ?", headerInfo.Uid).Find(&userInfo)
|
|
if userInfo == nil || userInfo.Id == 0 {
|
|
UserNotExistError.Fail(ctx, nil)
|
|
return
|
|
}
|
|
|
|
var coupons []models.Coupons
|
|
database.Instance().Model(&models.Coupons{}).Joins("JOIN user_coupons on coupons.id = user_coupons.cid and coupons.expire_time > ?", time.Now().UnixNano()/int64(time.Millisecond)).Where(" user_coupons.coupons_status = ? and user_coupons.uid = ?", getUserCouponsListRequest.Status, headerInfo.Uid).Find(&coupons)
|
|
for index, coupon := range coupons {
|
|
coupons[index].PeriodInfo = strings.Split(coupon.Period, ",")
|
|
}
|
|
Success(ctx, headerInfo, coupons)
|
|
})
|
|
}}
|
|
}
|
|
|
|
type DrawCouponsRequest struct {
|
|
Cid int
|
|
}
|
|
|
|
func (p DefParty) drawCoupons() web_iris.Party {
|
|
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
|
index.Post(CouponsBase+"/drawCoupons", func(ctx *context.Context) {
|
|
headerInfo := GetHeaderBaseInfo(ctx)
|
|
var drawCouponsRequest DrawCouponsRequest
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
json.Unmarshal(body, &drawCouponsRequest)
|
|
if drawCouponsRequest.Cid == 0 {
|
|
ParamError.Fail(ctx, nil)
|
|
return
|
|
}
|
|
var userInfo *models.User
|
|
database.Instance().Model(&models.User{}).Where("id = ?", headerInfo.Uid).Find(&userInfo)
|
|
if userInfo == nil || userInfo.Id == 0 {
|
|
UserNotExistError.Fail(ctx, nil)
|
|
return
|
|
}
|
|
|
|
var coupons *models.Coupons
|
|
database.Instance().Model(&models.Coupons{}).Where("id = ?", drawCouponsRequest.Cid).Find(&coupons)
|
|
if coupons.Id == 0 {
|
|
CouponsNotExistError.Fail(ctx, nil)
|
|
return
|
|
}
|
|
lockMap := &utils.KeyedMutex{}
|
|
lockMap.Lock(headerInfo.Token)
|
|
var userCoupons *models.UserCoupons
|
|
database.Instance().Model(&models.UserCoupons{}).Where("uid = ? and cid = ?", headerInfo.Uid, drawCouponsRequest.Cid).Find(&userCoupons)
|
|
if userCoupons.Id > 0 {
|
|
UserCouponsNExistError.Fail(ctx, nil)
|
|
lockMap.Unlock(headerInfo.Token)
|
|
return
|
|
}
|
|
newUserCoupons := models.UserCoupons{
|
|
Uid: headerInfo.Uid,
|
|
Cid: drawCouponsRequest.Cid,
|
|
}
|
|
err := database.Instance().Create(&newUserCoupons).Error
|
|
if err != nil {
|
|
ServerError.Fail(ctx, nil)
|
|
lockMap.Unlock(headerInfo.Token)
|
|
return
|
|
}
|
|
|
|
lockMap.Unlock(headerInfo.Token)
|
|
Success(ctx, headerInfo, nil)
|
|
})
|
|
}}
|
|
}
|