349 lines
7.5 KiB
Go
349 lines
7.5 KiB
Go
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"
|
||
"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)
|
||
|
||
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:会员)
|
||
if req.ApplicationScope == 1 {
|
||
// 全场通用
|
||
if req.QuestionId == nil && req.SystemMemberId == nil {
|
||
responses.FailWithMessage("请填入关联选项", c)
|
||
return
|
||
}
|
||
}
|
||
|
||
if req.ApplicationScope == 2 {
|
||
// 单项
|
||
if req.QuestionId == nil {
|
||
responses.FailWithMessage("请填入关联算一算", c)
|
||
return
|
||
}
|
||
}
|
||
|
||
// 适用范围(1:全场通用 2:单项 3:会员)
|
||
if req.ApplicationScope == 3 {
|
||
// 会员
|
||
if req.SystemMemberId == nil {
|
||
responses.FailWithMessage("请填入关联会员", c)
|
||
return
|
||
}
|
||
}
|
||
|
||
// 有效类型-绝对时效
|
||
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,
|
||
IsMutex: req.IsMutex,
|
||
CouponCount: req.CouponCount,
|
||
CouponTakeCount: 0,
|
||
CouponUsedCount: 0,
|
||
CouponPrice: req.CouponPrice,
|
||
ValidType: req.ValidType,
|
||
ValidStartTime: nil,
|
||
ValidEndTime: nil,
|
||
CouponDesc: req.CouponDesc,
|
||
QuestionId: nil,
|
||
SystemMemberId: nil,
|
||
}
|
||
|
||
// 符合满减标准金额
|
||
if req.WithAmount != nil {
|
||
coupon.WithAmount = req.WithAmount
|
||
}
|
||
|
||
// 有效天数
|
||
if req.ValidDays != nil {
|
||
coupon.ValidDays = req.ValidDays
|
||
}
|
||
|
||
// 时间区间
|
||
if req.ValidStartTime != nil {
|
||
// 获取本地时区
|
||
location, err := time.LoadLocation("Local")
|
||
if err != nil {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("新增失败", c)
|
||
return
|
||
}
|
||
|
||
t, err := time.ParseInLocation("2006-01-02 15:04:05", *req.ValidStartTime, location)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("新增失败", c)
|
||
return
|
||
}
|
||
|
||
validStartTime := model.LocalTime(t)
|
||
|
||
coupon.ValidStartTime = &validStartTime
|
||
}
|
||
|
||
if req.ValidEndTime != nil {
|
||
// 获取本地时区
|
||
location, err := time.LoadLocation("Local")
|
||
if err != nil {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("新增失败", c)
|
||
return
|
||
}
|
||
|
||
t, err := time.ParseInLocation("2006-01-02 15:04:05", *req.ValidEndTime, location)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("新增失败", c)
|
||
return
|
||
}
|
||
|
||
validEndTime := model.LocalTime(t)
|
||
coupon.ValidEndTime = &validEndTime
|
||
|
||
// 检测结束时间-不允许当天结束
|
||
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
|
||
}
|
||
}
|
||
|
||
// 关联单项id
|
||
if req.QuestionId != nil {
|
||
questionId, err := strconv.ParseInt(*req.QuestionId, 10, 64)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("新增失败", c)
|
||
return
|
||
}
|
||
|
||
coupon.QuestionId = &questionId
|
||
}
|
||
|
||
// 关联会员id
|
||
if req.SystemMemberId != nil {
|
||
systemMemberId, err := strconv.ParseInt(*req.SystemMemberId, 10, 64)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
responses.FailWithMessage("新增失败", c)
|
||
return
|
||
}
|
||
|
||
coupon.QuestionId = &systemMemberId
|
||
}
|
||
|
||
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)
|
||
}
|