Browse Source

12.31日,将导入用户jwcode并回显改为直接导入并上传,以及传到后端一个数组并上传两个接口,导出未写

master
lijikun 6 months ago
parent
commit
d2b9b3619b
  1. 16
      api/v1/couponusers/couponUsers.go
  2. 5
      internal/cmd/cmd.go
  3. 31
      internal/controller/couponusers/couponUsers.go
  4. 48
      internal/logic/couponusers/couponUsers.go
  5. 6
      internal/service/couponusers.go

16
api/v1/couponusers/couponUsers.go

@ -1,5 +1,6 @@
package couponusers
// GetCouponUsersReq 查询卡券用户
type GetCouponUsersReq struct {
CouponId int `json:"couponId" v:"required#卡券id不能为空" dc:"卡券id"`
Jwcode int `json:"jwcode" dc:"查询条件中的精网号"`
@ -8,6 +9,7 @@ type GetCouponUsersReq struct {
PageSize int `json:"pageSize" v:"required#分页信息不能为空" dc:"每页数量"`
}
// GetCouponUsersRes 查询卡券用户
type GetCouponUsersRes struct {
Name string `json:"name" dc:"姓名"`
Jwcode int `json:"jwcode" dc:"精网号"`
@ -15,29 +17,41 @@ type GetCouponUsersRes struct {
ShopName string `json:"shopName" dc:"门店"`
}
// DelCouponUserByJwcodeReq 删除卡券用户
type DelCouponUserByJwcodeReq struct {
CouponId int `json:"couponId" v:"required#卡券id不能为空" dc:"卡券id"`
Jwcode int `json:"jwcode" v:"required#精网号不能为空" dc:"精网号"`
}
// InsertCouponUserReq 给多个用户发放卡券 导入excel
type InsertCouponUserReq struct {
CouponId int `json:"couponId" v:"required#卡券id不能为空" dc:"卡券id"`
Jwcodes []int `json:"jwcodes" v:"required#精网号不能为空" dc:"精网号"`
//Jwcodes []int `json:"jwcodes" v:"required#精网号不能为空" dc:"精网号"`
}
// InsertCouponUserByJwcodeStrReq 给多个用户发放卡券 传jwcodes字符串
type InsertCouponUserByJwcodeStrReq struct {
CouponId int `json:"couponId" v:"required#卡券id不能为空" dc:"卡券id"`
Jwcodes string `json:"jwcodes" v:"required#精网号不能为空" dc:"精网号"`
}
// IssueCouponUserReq 给单个用户发放卡券
type IssueCouponUserReq struct {
CouponId int `json:"couponId" v:"required#卡券id不能为空" dc:"卡券id"`
}
// IsEligibleUserReq 检测用户是否有抽取资格,是否已抽取
type IsEligibleUserReq struct {
CouponIds []int `json:"couponIds" v:"required#用于检测是否已抽取,不能为空" dc:"能抽出的卡券id"`
}
// AddRecordReq 添加武器
type AddRecordReq struct {
CouponId int `json:"couponId" dc:"卡券id"`
Name string `json:"name" dc:"选择武器名称"`
}
// InsertCouponUserRes 判断能否抽取卡券,给单个用户发放卡券 用于存放满足条件的精网号
type InsertCouponUserRes struct {
Jwcode int `json:"jwcode" dc:"精网号"`
}

5
internal/cmd/cmd.go

@ -22,8 +22,9 @@ var (
group.Middleware(middleware.MiddlewareCORS)
group.POST("/get-coupon-users", couponusers.NewCouponUsers().GetCouponUsers) // 获取拥有卡券的用户列表
group.POST("/delete-coupon-user", couponusers.NewCouponUsers().DeleteCouponUserByJwcode) //删除用户卡券
group.POST("/import-excel", couponusers.NewCouponUsers().InsertJwcodeByExcel) // 通过excel导入jwcode
group.POST("/insert-coupon-user", couponusers.NewCouponUsers().InsertCouponUser) //给用户发放卡券
group.POST("/insert-coupon-users-by-excel", couponusers.NewCouponUsers().InsertCouponUserByExcel) // 通过excel导入jwcode并发放卡券
//group.POST("/insert-coupon-user", couponusers.NewCouponUsers().InsertCouponUser) //给用户发放卡券
group.POST("/insert-coupon-users-by-str", couponusers.NewCouponUsers().InsertCouponUserByJwcodeStr) //通过jwcodes字符串导入jwcode并发放卡券
//近期使用
group.POST("/insert-users-to-redis", couponusers.NewCouponUsers().InsertJwcodesToRedisByExcel) //导入满足条件的用户jwcode到redis
group.POST("/is-eligible-user", couponusers.NewCouponUsers().IsEligibleUser) //判断用户是否满足领取条件

31
internal/controller/couponusers/couponUsers.go

@ -60,7 +60,14 @@ func (c *CouponUsers) DeleteCouponUserByJwcode(r *ghttp.Request) {
}
// 接受前端传来的excel表格,并解析,返回jwcode切片,可以有重复的,在插入时进行验证,如果有重复的,只插入一次
func (c *CouponUsers) InsertJwcodeByExcel(r *ghttp.Request) {
func (c *CouponUsers) InsertCouponUserByExcel(r *ghttp.Request) {
/*解析请求*/
var req *couponusers.InsertCouponUserReq
if err := r.Parse(&req); err != nil {
r.Response.WriteJsonExit(dto.Error(err.Error()))
}
/*解析请求*/
// 从请求中获取文件
file, _, err := r.FormFile("excelFile")
if err != nil {
@ -80,24 +87,32 @@ func (c *CouponUsers) InsertJwcodeByExcel(r *ghttp.Request) {
r.Response.WriteJsonExit(dto.Error("获取的文件为空"))
}
jwcodes, err := service.CouponUsers().InsertJwcodeByExcel(file)
/*调用方法解析excel文件,并将解析出结果传入另一个函数去发放卡券*/
result, err := service.CouponUsers().InsertJwcodeByExcel(r.Context(), file, 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.SuccessWithData(jwcodes))
r.Response.WriteJsonExit(dto.SuccessWithMsg(fmt.Sprintf("发放成功,发放了%d人", result)))
/*调用方法解析excel文件,并将解析出结果传入另一个函数去发放卡券*/
}
// 给用户发放卡券
func (c *CouponUsers) InsertCouponUser(r *ghttp.Request) {
// 传入jwcodes字符串,解析并发放卡券
func (c *CouponUsers) InsertCouponUserByJwcodeStr(r *ghttp.Request) {
// 解析请求
var req *couponusers.InsertCouponUserReq
var req *couponusers.InsertCouponUserByJwcodeStrReq
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)
result, err := service.CouponUsers().InsertCouponUsersByJwcodeStr(r.Context(), req.Jwcodes, req.CouponId)
//错误处理
if err != nil {
r.Response.WriteJsonExit(dto.Error(err.Error()))
@ -111,7 +126,7 @@ func (c *CouponUsers) InsertCouponUser(r *ghttp.Request) {
}
/*近期使用*/
// 导入满足条件的用户jwcode到redis
// 导入满足条件的用户jwcode到redis //不导入到redis了,导入到数据库表中
func (c *CouponUsers) InsertJwcodesToRedisByExcel(r *ghttp.Request) {
// 从请求中获取文件
file, _, err := r.FormFile("EligibleJwcodesExcel")

48
internal/logic/couponusers/couponUsers.go

@ -109,11 +109,13 @@ func (s *sCouponUsers) DeleteCouponUserByJwcode(ctx context.Context, couponId, j
}
// 通过excel导入精网号
func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) {
func (s *sCouponUsers) InsertJwcodeByExcel(ctx context.Context, file multipart.File, couponId int) (num int, err error) {
// func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) {
//打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容
f, err := excelize.OpenReader(file)
if err != nil {
return nil, errors.New("打开文件失败")
return 0, errors.New("打开文件失败")
}
// 延时关闭文件
defer func(f *excelize.File) {
@ -126,15 +128,16 @@ func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int,
//读取所有工作表名称
GetSheetMap := f.GetSheetMap()
if len(GetSheetMap) == 0 {
return nil, errors.New("没有工作表")
return 0, errors.New("没有工作表")
}
rows, err := f.GetRows("Sheet1")
if err != nil {
return nil, err
return 0, err
}
// 将每行的第一列转换为int,并添加到切片中
var jwcodes []int
for i, row := range rows {
//跳过第一行
if i == 0 {
@ -154,15 +157,46 @@ func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int,
//将字符串转换为整数
jwcode, err := strconv.Atoi(jwcodeStr)
if err != nil {
return nil, errors.New("参数转换失败")
return 0, errors.New("参数转换失败")
}
jwcodes = append(jwcodes, jwcode)
/*参数校验*/
}
return //返回jwcodes切片
/*给用户发放卡券*/
num, err = s.InsertCouponUsersByJwcodes(ctx, jwcodes, couponId)
/*给用户发放卡券*/
//return //返回jwcodes切片
return num, err
}
// 传来jwcodes字符串,解析并发放卡券
func (s *sCouponUsers) InsertCouponUsersByJwcodeStr(ctx context.Context, jwcodeStr string, couponId int) (num int, err error) {
//将字符串转换为切片
jwcodes := strings.Split(jwcodeStr, ",")
//转换成int
var jwcodesInt []int
for _, jwcode := range jwcodes {
//参数校验,检查jwcode是否为非空字符串
if jwcode == "" {
continue
}
jwcodeInt, err := strconv.Atoi(jwcode)
if err != nil {
return 0, errors.New("参数转换失败")
}
jwcodesInt = append(jwcodesInt, jwcodeInt)
}
/*给用户发放卡券*/
num, err = s.InsertCouponUsersByJwcodes(ctx, jwcodesInt, couponId)
/*给用户发放卡券*/
return
}
// 根据精网号发放用户优惠券
// 根据精网号发放用户优惠券,群发 //不被controller层调用了,但不能删除
func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) {
//去重
m := make(map[int]bool)

6
internal/service/couponusers.go

@ -19,8 +19,10 @@ type (
// 根据jwcode,优惠券id删除用户
DeleteCouponUserByJwcode(ctx context.Context, couponId int, jwcode int) (result sql.Result, err error)
// 通过excel导入精网号
InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error)
// 根据精网号发放用户优惠券
InsertJwcodeByExcel(ctx context.Context, file multipart.File, couponId int) (num int, err error)
// 传来jwcodes字符串,解析并发放卡券
InsertCouponUsersByJwcodeStr(ctx context.Context, jwcodeStr string, couponId int) (num int, err error)
// 根据精网号发放用户优惠券,群发 //不被controller层调用了,但不能删除
InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error)
// 导入满足条件的用户jwcode到redis //不上传到redis了,上传到数据库表,到时候直接从表里查
InsertJwcodesToRedisByExcel(file multipart.File) (err error)

Loading…
Cancel
Save