|
|
@ -6,7 +6,6 @@ import ( |
|
|
|
"CouponBackendGo/internal/service" |
|
|
|
"context" |
|
|
|
"database/sql" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
|
"github.com/gogf/gf/v2/os/gtime" |
|
|
@ -201,7 +200,7 @@ func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes [ |
|
|
|
return num, err //返回受影响的行数,即新增的条数
|
|
|
|
} |
|
|
|
|
|
|
|
// 导入满足条件的用户jwcode到redis
|
|
|
|
// 导入满足条件的用户jwcode到redis //不上传到redis了,上传到数据库表,到时候直接从表里查
|
|
|
|
func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err error) { |
|
|
|
var ctx g.Ctx |
|
|
|
//打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容
|
|
|
@ -231,7 +230,7 @@ func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err err |
|
|
|
} |
|
|
|
|
|
|
|
// 将每行的第一列转换为int,并添加到切片中
|
|
|
|
var jwcodes []int |
|
|
|
//var jwcodes []int
|
|
|
|
for i, row := range rows { |
|
|
|
//跳过第一行
|
|
|
|
if i == 0 { |
|
|
@ -254,33 +253,34 @@ func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err err |
|
|
|
return errors.New("参数转换失败") |
|
|
|
} |
|
|
|
|
|
|
|
jwcodes = append(jwcodes, jwcode) |
|
|
|
// 判断jwcode是否在表coupon_qualified_users中
|
|
|
|
count, err := g.DB().Model(ctx, "coupon_qualified_users").Where("jwcode = ?", jwcode).Count() |
|
|
|
if err != nil { |
|
|
|
return errors.New("检索数据库中是否已存在数据失败") |
|
|
|
} |
|
|
|
if count == 0 { //不存在,插入
|
|
|
|
_, err = g.DB().Model(ctx, "coupon_qualified_users").Insert(g.Map{ |
|
|
|
"jwcode": jwcode, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
//jwcodes = append(jwcodes, jwcode)
|
|
|
|
/*参数校验*/ |
|
|
|
} |
|
|
|
|
|
|
|
//将jwcodes存入redis
|
|
|
|
MarshalJwcodes, _ := json.Marshal(jwcodes) |
|
|
|
_, err = g.Redis().Set(ctx, "EligibleJwcodes", MarshalJwcodes) |
|
|
|
if err != nil { |
|
|
|
return errors.New("存入redis失败") |
|
|
|
} |
|
|
|
return //返回nil表示成功
|
|
|
|
} |
|
|
|
|
|
|
|
// 判断某用户能否抽到卡券
|
|
|
|
func (s *sCouponUsers) IsEligibleUser(ctx context.Context, jwcode int, couponIds []int) (img string, err error) { |
|
|
|
/*从redis中获取符合条件的精网号EligibleJwcodes*/ |
|
|
|
redisResult, err := g.Redis().Get(ctx, "EligibleJwcodes") |
|
|
|
if err != nil { |
|
|
|
return "", errors.New("从redis中获取数据失败") |
|
|
|
} |
|
|
|
//从数据库中获取符合条件的精网号EligibleJwcodes
|
|
|
|
var EligibleJwcodes []int |
|
|
|
err = json.Unmarshal(redisResult.Bytes(), &EligibleJwcodes) |
|
|
|
err = g.DB().Model(ctx, "coupon_qualified_users").Fields("jwcode").Scan(&EligibleJwcodes) |
|
|
|
if err != nil { |
|
|
|
return "", errors.New("redis中数据解析失败") |
|
|
|
return "", errors.New("从数据库中获取符合条件的精网号失败") |
|
|
|
} |
|
|
|
if len(EligibleJwcodes) == 0 { |
|
|
|
return "", errors.New("redis中数据为空") |
|
|
|
return "", errors.New("核验数据库(满足条件)中数据为空") |
|
|
|
} |
|
|
|
/*从redis中获取符合条件的精网号EligibleJwcodes*/ |
|
|
|
|
|
|
@ -320,20 +320,16 @@ func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId i |
|
|
|
} |
|
|
|
|
|
|
|
//不存在 查看是否满足条件,满足就添加,不满足就返回
|
|
|
|
/*从redis中获取符合条件的精网号EligibleJwcodes*/ |
|
|
|
redisResult, err := g.Redis().Get(ctx, "EligibleJwcodes") |
|
|
|
if err != nil { |
|
|
|
return errors.New("从redis中获取数据失败") |
|
|
|
} |
|
|
|
|
|
|
|
//从数据库中获取符合条件的精网号EligibleJwcodes
|
|
|
|
var EligibleJwcodes []int |
|
|
|
err = json.Unmarshal(redisResult.Bytes(), &EligibleJwcodes) |
|
|
|
err = g.DB().Model(ctx, "coupon_qualified_users").Fields("jwcode").Scan(&EligibleJwcodes) |
|
|
|
if err != nil { |
|
|
|
return errors.New("redis中数据解析失败") |
|
|
|
return errors.New("从数据库中获取符合条件的精网号失败") |
|
|
|
} |
|
|
|
if len(EligibleJwcodes) == 0 { |
|
|
|
return errors.New("redis中数据为空") |
|
|
|
return errors.New("核验数据库(满足条件)中数据为空") |
|
|
|
} |
|
|
|
/*从redis中获取符合条件的精网号EligibleJwcodes*/ |
|
|
|
|
|
|
|
//检查jwcode是否在EligibleJwcodes中
|
|
|
|
for _, EligibleJwcode := range EligibleJwcodes { |
|
|
|