233 lines
7.0 KiB
Go
233 lines
7.0 KiB
Go
package market
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"github.com/gin-gonic/gin"
|
||
model "go-admin/app/admin/models"
|
||
orm "go-admin/common/global"
|
||
"go-admin/logger"
|
||
"go-admin/tools"
|
||
"go-admin/tools/app"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
// ErpMarketingCouponList 优惠券列表
|
||
// @Summary 优惠券列表
|
||
// @Tags 营销管理,V1.4.4
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpMarketingCouponListReq true "优惠券列表模型"
|
||
// @Success 200 {object} models.ErpMarketingCouponListResp
|
||
// @Router /api/v1/marketing/coupon/list [post]
|
||
func ErpMarketingCouponList(c *gin.Context) {
|
||
req := &model.ErpMarketingCouponListReq{}
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
||
return
|
||
}
|
||
|
||
resp, err := req.List()
|
||
if err != nil {
|
||
logger.Error("ErpMarketingCouponList err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "获取失败"+err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, resp, "OK")
|
||
return
|
||
}
|
||
|
||
// ErpMarketingCouponCreate 新增优惠券
|
||
// @Summary 新增优惠券
|
||
// @Tags 营销管理,V1.4.4
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpMarketingCouponCreateReq true "新增优惠券模型"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/marketing/coupon/create [post]
|
||
func ErpMarketingCouponCreate(c *gin.Context) {
|
||
var req = new(model.ErpMarketingCouponCreateReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
|
||
return
|
||
}
|
||
|
||
err := tools.Validate(req) //必填参数校验
|
||
if err != nil {
|
||
app.Error(c, http.StatusBadRequest, err, err.Error())
|
||
return
|
||
}
|
||
|
||
if !model.IsExistingCategoryByNumber(req.CategoryNumber) {
|
||
app.Error(c, http.StatusBadRequest, errors.New("未查询到该分类编号"), "未查询到该分类编号")
|
||
return
|
||
}
|
||
|
||
err = model.CreateErpMarketingCoupon(req)
|
||
if err != nil {
|
||
logger.Error("CreateErpMarketingCoupon err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, nil, "新增成功")
|
||
return
|
||
}
|
||
|
||
// ErpMarketingCouponEdit 编辑优惠券
|
||
// @Summary 编辑优惠券
|
||
// @Tags 营销管理,V1.4.4
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpMarketingCouponEditReq true "编辑优惠券模型"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/marketing/coupon/edit [post]
|
||
func ErpMarketingCouponEdit(c *gin.Context) {
|
||
var req = new(model.ErpMarketingCouponEditReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
|
||
return
|
||
}
|
||
|
||
err := tools.Validate(req) //必填参数校验
|
||
if err != nil {
|
||
app.Error(c, http.StatusBadRequest, err, err.Error())
|
||
return
|
||
}
|
||
|
||
err = model.EditErpMarketingCoupon(req)
|
||
if err != nil {
|
||
logger.Error("EditErpMarketingCoupon err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, nil, "编辑成功")
|
||
return
|
||
}
|
||
|
||
// ErpMarketingCouponDelete 删除优惠券
|
||
// @Summary 删除优惠券
|
||
// @Tags 营销管理,V1.4.4
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpMarketingCouponDeleteReq true "删除优惠券模型"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/marketing/coupon/delete [post]
|
||
func ErpMarketingCouponDelete(c *gin.Context) {
|
||
var req = new(model.ErpMarketingCouponDeleteReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
|
||
return
|
||
}
|
||
|
||
err := tools.Validate(req) //必填参数校验
|
||
if err != nil {
|
||
app.Error(c, http.StatusBadRequest, err, err.Error())
|
||
return
|
||
}
|
||
|
||
err = model.DeleteErpMarketingCoupon(req)
|
||
if err != nil {
|
||
logger.Error("DeleteErpMarketingCoupon err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, nil, "删除成功")
|
||
return
|
||
}
|
||
|
||
// ErpMarketingCouponStart 启动优惠券发放
|
||
// @Summary 启动优惠券发放
|
||
// @Tags 营销管理,V1.4.4
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpMarketingCouponStartReq true "启动优惠券发放模型"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/marketing/coupon/start [post]
|
||
func ErpMarketingCouponStart(c *gin.Context) {
|
||
var req = new(model.ErpMarketingCouponStartReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
|
||
return
|
||
}
|
||
|
||
err := tools.Validate(req) //必填参数校验
|
||
if err != nil {
|
||
app.Error(c, http.StatusBadRequest, err, err.Error())
|
||
return
|
||
}
|
||
|
||
// 检查是否有正在进行的任务
|
||
taskList, err := model.GetTaskProgress(req.ErpCouponId)
|
||
if err != nil {
|
||
app.Error(c, http.StatusInternalServerError, err, err.Error())
|
||
return
|
||
}
|
||
|
||
var waitSendTaskList []model.CouponIssuanceTask
|
||
|
||
if len(taskList) != 0 {
|
||
for _, task := range taskList {
|
||
switch task.Status {
|
||
case model.TaskInProgress: // 执行中
|
||
app.Error(c, http.StatusInternalServerError, errors.New(fmt.Sprintf("[%s]已在执行中,请勿重复启动", task.ErpCouponName)),
|
||
fmt.Sprintf("[%s]已在执行中,请勿重复启动", task.ErpCouponName))
|
||
return
|
||
case model.TaskCompleted: // 已完成
|
||
app.Error(c, http.StatusInternalServerError, errors.New(fmt.Sprintf("[%s]已执行完成", task.ErpCouponName)),
|
||
fmt.Sprintf("[%s]已执行完成", task.ErpCouponName))
|
||
return
|
||
default:
|
||
waitSendTaskList = append(waitSendTaskList, task)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 校验批量选择的优惠券使用人群是否相同
|
||
if !model.CheckUserType(req.ErpCouponId) {
|
||
app.Error(c, http.StatusInternalServerError, errors.New("优惠券适用人群不同,不能同时启动"), "优惠券适用人群不同,不能同时启动")
|
||
}
|
||
|
||
// 更新短信和备注
|
||
err = orm.Eloquent.Table("erp_coupon").Where("id in ?", req.ErpCouponId).
|
||
Updates(map[string]interface{}{
|
||
"remark": req.Remark,
|
||
"state": model.ErpCouponSending,
|
||
"start_time": time.Now(),
|
||
"sms_content": req.SmsContent,
|
||
"updated_at": time.Now(),
|
||
}).Error
|
||
if err != nil {
|
||
logger.Errorf("更新任务完成状态失败: %v", err)
|
||
app.Error(c, http.StatusInternalServerError, err, err.Error())
|
||
}
|
||
|
||
go model.StartCouponIssuanceTask(req, waitSendTaskList)
|
||
|
||
app.OK(c, nil, "启动成功")
|
||
return
|
||
}
|
||
|
||
// ErpMarketingCouponData 优惠券数据
|
||
// @Summary 优惠券数据
|
||
// @Tags 营销管理,V1.4.4
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpMarketingCouponDataReq true "优惠券数据模型"
|
||
// @Success 200 {object} models.ErpMarketingCouponDataResp
|
||
// @Router /api/v1/marketing/coupon/data [post]
|
||
func ErpMarketingCouponData(c *gin.Context) {
|
||
|
||
app.OK(c, nil, "OK")
|
||
return
|
||
}
|