You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.2 KiB
188 lines
5.2 KiB
package couponusers
|
|
|
|
import (
|
|
"CouponBackendGo/api/v1/couponusers"
|
|
"CouponBackendGo/internal/model/dto"
|
|
"CouponBackendGo/internal/service"
|
|
"CouponBackendGo/utility"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"mime/multipart"
|
|
)
|
|
|
|
type CouponUsers struct{}
|
|
|
|
func NewCouponUsers() *CouponUsers {
|
|
return &CouponUsers{}
|
|
}
|
|
|
|
// 获取拥有卡券的用户列表
|
|
func (c *CouponUsers) GetCouponUsers(r *ghttp.Request) {
|
|
//解析请求
|
|
var req *couponusers.GetCouponUsersReq
|
|
if err := r.Parse(&req); err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
result, err := service.CouponUsers().GetCouponUsersByCondition(r.Context(), req.CouponId, req.Jwcode, req.Name)
|
|
//错误处理
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
//没有错误,但获取到了0条
|
|
if result == nil {
|
|
r.Response.WriteJsonExit(dto.Error("没有符合条件的用户"))
|
|
}
|
|
//成功处理
|
|
r.Response.WriteJsonExit(dto.SuccessWithData(result))
|
|
|
|
}
|
|
|
|
// 删除用户卡券
|
|
func (c *CouponUsers) DeleteCouponUserByJwcode(r *ghttp.Request) {
|
|
// 解析请求
|
|
var req *couponusers.DelCouponUserByJwcodeReq
|
|
if err := r.Parse(&req); err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
result, err := service.CouponUsers().DeleteCouponUserByJwcode(r.Context(), req.CouponId, req.Jwcode)
|
|
//错误处理
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
//获取影响到的数据条数
|
|
affected, err := result.RowsAffected()
|
|
//没有错误,但删除0条
|
|
if affected == 0 {
|
|
r.Response.WriteJsonExit(dto.Error("删除失败,删除了0条"))
|
|
}
|
|
//成功处理
|
|
r.Response.WriteJsonExit(dto.SuccessWithMsg("删除成功"))
|
|
}
|
|
|
|
// 接受前端传来的excel表格,并解析,返回jwcode切片,可以有重复的,在插入时进行验证,如果有重复的,只插入一次
|
|
func (c *CouponUsers) InsertJwcodeByExcel(r *ghttp.Request) {
|
|
// 从请求中获取文件
|
|
file, _, err := r.FormFile("excelFile")
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
// 延时关闭文件
|
|
defer func(file multipart.File) {
|
|
err := file.Close()
|
|
if err != nil {
|
|
// 处理关闭文件时的错误
|
|
r.Response.WriteJsonExit(dto.Error("获取的文件关闭失败"))
|
|
}
|
|
}(file)
|
|
|
|
//参数校验,检查文件是否为空
|
|
if file == nil {
|
|
r.Response.WriteJsonExit(dto.Error("获取的文件为空"))
|
|
}
|
|
|
|
jwcodes, err := service.CouponUsers().InsertJwcodeByExcel(file)
|
|
//错误处理
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
//成功处理
|
|
r.Response.WriteJsonExit(dto.SuccessWithData(jwcodes))
|
|
}
|
|
|
|
// 给用户发放卡券
|
|
func (c *CouponUsers) InsertCouponUser(r *ghttp.Request) {
|
|
// 解析请求
|
|
var req *couponusers.InsertCouponUserReq
|
|
if err := r.Parse(&req); err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
|
|
result, err := service.CouponUsers().InsertCouponUsersByJwcodes(r.Context(), req.Jwcodes, req.CouponId)
|
|
//错误处理
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
//没报错,但插入数据0条
|
|
if result == 0 {
|
|
r.Response.WriteJsonExit(dto.Error("发放失败,发放了0人"))
|
|
}
|
|
//成功处理
|
|
r.Response.WriteJsonExit(dto.SuccessWithMsg(fmt.Sprintf("发放成功,发放了%d人", result)))
|
|
}
|
|
|
|
/*近期使用*/
|
|
// 导入满足条件的用户jwcode到redis
|
|
func (c *CouponUsers) InsertJwcodesToRedisByExcel(r *ghttp.Request) {
|
|
// 从请求中获取文件
|
|
file, _, err := r.FormFile("EligibleJwcodesExcel")
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
// 延时关闭文件
|
|
defer func(file multipart.File) {
|
|
err := file.Close()
|
|
if err != nil {
|
|
// 处理关闭文件时的错误
|
|
r.Response.WriteJsonExit(dto.Error("获取的文件关闭失败"))
|
|
}
|
|
}(file)
|
|
|
|
//参数校验,检查文件是否为空
|
|
if file == nil {
|
|
r.Response.WriteJsonExit(dto.Error("获取的文件为空"))
|
|
}
|
|
|
|
err = service.CouponUsers().InsertJwcodesToRedisByExcel(file)
|
|
//错误处理
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
//成功处理
|
|
r.Response.WriteJsonExit(dto.SuccessWithMsg("导入redis成功"))
|
|
|
|
}
|
|
|
|
// 给单个用户发放卡券
|
|
func (c *CouponUsers) IssueCouponUser(r *ghttp.Request) {
|
|
// 解析请求参数
|
|
//没有token,用于测试
|
|
//var req *couponusers.InsertCouponUserReq
|
|
//if err := r.Parse(&req); err != nil {
|
|
// r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
//}
|
|
|
|
/*正式*/
|
|
var req *couponusers.IssueCouponUserReq
|
|
if err := r.Parse(&req); err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
//解析token,获取jwcode
|
|
token := r.Header.Get("token")
|
|
if token == "" {
|
|
r.Response.WriteJsonExit(dto.Error("token为空"))
|
|
}
|
|
|
|
jwcode, err := utility.GetJwcodeJSON(token)
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
/*正式*/
|
|
|
|
//没有token,用于测试
|
|
//err := service.CouponUsers().IssueCouponToUser(r.Context(), req.Jwcodes[0], req.CouponId)
|
|
/*正式*/
|
|
err = service.CouponUsers().IssueCouponToUser(r.Context(), jwcode, req.CouponId)
|
|
/*正式*/
|
|
//错误处理
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(dto.Error(err.Error()))
|
|
}
|
|
//成功处理
|
|
r.Response.WriteJsonExit(dto.SuccessWithMsg("发放成功"))
|
|
|
|
}
|
|
|
|
/*近期使用*/
|
|
|
|
//未编写
|
|
//导出拥有卡券的用户
|