145 lines
4.8 KiB
Go
145 lines
4.8 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 userCoupons []*models.UserCoupons
|
|
database.Instance().Model(&models.UserCoupons{}).Where("uid = ? and coupons_status = 1", headerInfo.Uid).Find(&userCoupons)
|
|
var userCouponsMap = make(map[int]*models.UserCoupons)
|
|
for _, value := range userCoupons {
|
|
userCouponsMap[value.Cid] = value
|
|
}
|
|
|
|
var coupons []models.Coupons
|
|
database.Instance().Model(&models.Coupons{}).Where("status = 1 and send_status = 1 and expire_time > ?", time.Now().UnixNano()/int64(time.Millisecond)).Find(&coupons)
|
|
for index, coupon := range coupons {
|
|
m := userCouponsMap[coupon.Id]
|
|
if m != nil && m.Id > 0 {
|
|
coupons[index].ReceiverStatus = 1
|
|
}
|
|
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 userCoupons []models.UserCoupons
|
|
var coupons []models.Coupons
|
|
database.Instance().Model(&models.UserCoupons{}).Where("uid = ? and coupons_status = ?", headerInfo.Uid, getUserCouponsListRequest.Status).Find(&userCoupons)
|
|
for _, userCoupon := range userCoupons {
|
|
uc := CouponsMap[userCoupon.Cid]
|
|
if uc.Id == 0 {
|
|
var couponsData *models.Coupons
|
|
database.Instance().Model(&models.Coupons{}).Where("id = ?", userCoupon.Cid).Find(&couponsData)
|
|
uc = models.Coupons{Id: couponsData.Id, Name: couponsData.Name, Source: couponsData.Source, Discount: couponsData.Discount, GoodsSubType: couponsData.GoodsSubType}
|
|
}
|
|
uc.LastExpireTime = userCoupon.LastExpireTime.Format("2006-01-02")
|
|
uc.PeriodInfo = strings.Split(uc.Period, ",")
|
|
uc.Id = userCoupon.Id
|
|
coupons = append(coupons, uc)
|
|
}
|
|
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 = ? and status = 1", 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 = ? and coupons_status = 1", 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,
|
|
//领取后最后过期时间
|
|
LastExpireTime: time.Now().AddDate(0, 0, coupons.DrawDay),
|
|
}
|
|
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)
|
|
})
|
|
}}
|
|
}
|