2024-10-21 17:01:16 +08:00

323 lines
7.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"hepa-calc-admin-api/api/dao"
"hepa-calc-admin-api/api/dto"
"hepa-calc-admin-api/api/model"
"hepa-calc-admin-api/api/requests"
"hepa-calc-admin-api/api/responses"
"hepa-calc-admin-api/global"
"hepa-calc-admin-api/utils"
"strconv"
"strings"
"time"
)
type Coupon struct{}
// GetCouponPage 获取系统优惠卷列表-分页
func (b *Coupon) GetCouponPage(c *gin.Context) {
couponRequest := requests.CouponRequest{}
req := couponRequest.GetCouponPage
if err := c.ShouldBind(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
if req.Page == 0 {
req.Page = 1
}
if req.PageSize == 0 {
req.PageSize = 20
}
// 获取数据
couponDao := dao.CouponDao{}
coupon, total, err := couponDao.GetCouponPageSearch(req, req.Page, req.PageSize)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 处理返回值
g := dto.GetCouponListDto(coupon)
result := make(map[string]interface{})
result["page"] = req.Page
result["page_size"] = req.PageSize
result["total"] = total
result["data"] = g
responses.OkWithData(result, c)
}
// GetCoupon 获取系统优惠卷详情
func (b *Coupon) GetCoupon(c *gin.Context) {
id := c.Param("coupon_id")
if id == "" {
responses.FailWithMessage("缺少参数", c)
return
}
// 将 id 转换为 int64 类型
couponId, err := strconv.ParseInt(id, 10, 64)
if err != nil {
responses.Fail(c)
return
}
// 获取订单数据
couponDao := dao.CouponDao{}
coupon, err := couponDao.GetCouponById(couponId)
if err != nil {
responses.FailWithMessage("优惠卷异常", c)
return
}
// 处理返回值
g := dto.GetCouponDto(coupon)
// 加载会员id
g.LoadSystemMemberIds(coupon.SystemMemberIds)
responses.OkWithData(g, c)
}
// PutCouponStatus 操作系统优惠卷状态
func (b *Coupon) PutCouponStatus(c *gin.Context) {
couponRequest := requests.CouponRequest{}
req := couponRequest.PutCouponStatus
if err := c.ShouldBind(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
id := c.Param("coupon_id")
if id == "" {
responses.FailWithMessage("缺少参数", c)
return
}
// 将 id 转换为 int64 类型
couponId, err := strconv.ParseInt(id, 10, 64)
if err != nil {
responses.Fail(c)
return
}
// 获取优惠卷数据
couponDao := dao.CouponDao{}
coupon, err := couponDao.GetCouponById(couponId)
if err != nil {
responses.FailWithMessage("优惠卷异常", c)
return
}
// 检测优惠卷删除状态
if coupon.CouponStatus == req.CouponStatus {
responses.Ok(c)
return
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
couponData := make(map[string]interface{})
couponData["coupon_status"] = req.CouponStatus
err = couponDao.EditCouponById(tx, couponId, couponData)
if err != nil {
tx.Rollback()
responses.FailWithMessage("操作失败", c)
return
}
tx.Commit()
responses.Ok(c)
}
// AddSystemCoupon 新增系统优惠卷
func (r *Coupon) AddSystemCoupon(c *gin.Context) {
couponRequest := requests.CouponRequest{}
req := couponRequest.AddSystemCoupon
if err := c.ShouldBindJSON(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
// 满减
if req.CouponType == 2 {
if *req.WithAmount == 0 {
responses.FailWithMessage("请填入符合满减标准金额", c)
return
}
}
// 适用范围1:全场通用 2:单项 3:会员)
systemMemberIds := make([]string, len(req.SystemMemberIds))
if req.ApplicationScope == 3 {
// 会员
if req.SystemMemberIds == nil {
responses.FailWithMessage("请填入关联会员", c)
return
}
// 关联会员id
systemMemberDao := dao.SystemMemberDao{}
for i, id := range req.SystemMemberIds {
systemMemberId, err := strconv.ParseInt(*id, 10, 64)
if err != nil {
responses.FailWithMessage("新增失败", c)
return
}
systemMember, err := systemMemberDao.GetSystemMemberById(systemMemberId)
if err != nil || systemMember == nil {
responses.FailWithMessage("新增失败", c)
return
}
systemMemberIds[i] = *id
}
}
// 有效类型-绝对时效
if req.ValidType == 1 {
if req.ValidStartTime == nil {
responses.FailWithMessage("请填入开始使用时间", c)
return
}
if req.ValidEndTime == nil {
responses.FailWithMessage("请填入结束使用时间", c)
return
}
}
// 有效类型-相对时效
if req.ValidType == 2 {
if req.ValidDays == nil {
responses.FailWithMessage("请填入有效天数", c)
return
}
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
fmt.Println(r)
}
}()
// 新增优惠卷表
coupon := &model.Coupon{
CouponName: req.CouponName,
CouponType: req.CouponType,
CouponStatus: 1,
ApplicationScope: req.ApplicationScope,
DistributionObject: req.DistributionObject,
IsMutex: req.IsMutex,
CouponCount: req.CouponCount,
CouponTakeCount: 0,
CouponUsedCount: 0,
CouponPrice: req.CouponPrice,
ValidType: req.ValidType,
ValidStartTime: nil,
ValidEndTime: nil,
CouponDesc: req.CouponDesc,
SystemMemberIds: strings.Join(systemMemberIds, ","),
}
// 符合满减标准金额
if req.WithAmount != nil {
coupon.WithAmount = req.WithAmount
}
// 有效天数
if req.ValidDays != nil {
coupon.ValidDays = req.ValidDays
}
// 时间区间
if req.ValidStartTime != nil {
// 字符串时间转localtime
validStartTime, err := utils.StrToLocalTime(*req.ValidStartTime)
if err != nil {
tx.Rollback()
responses.FailWithMessage("新增失败", c)
return
}
coupon.ValidStartTime = validStartTime
}
if req.ValidEndTime != nil {
// 字符串时间转localtime
validEndTime, err := utils.StrToLocalTime(*req.ValidEndTime)
if err != nil {
tx.Rollback()
responses.FailWithMessage("新增失败", c)
return
}
coupon.ValidEndTime = validEndTime
// 获取本地时区
location, err := time.LoadLocation("Local")
if err != nil {
tx.Rollback()
responses.FailWithMessage("新增失败", c)
return
}
// 检测结束时间-不允许当天结束
now := time.Now()
year, month, day := now.Date()
endOfDay := time.Date(year, month, day, 23, 59, 59, 0, location)
if time.Time(*validEndTime).Before(endOfDay) {
tx.Rollback()
responses.FailWithMessage("优惠卷过期时间最低设置为明天", c)
return
}
}
couponDao := dao.CouponDao{}
coupon, err := couponDao.AddCoupon(tx, coupon)
if err != nil || coupon == nil {
tx.Rollback()
responses.FailWithMessage(err.Error(), c)
return
}
tx.Commit()
responses.Ok(c)
}