|
|
@ -24,71 +24,116 @@ func init() { |
|
|
|
} |
|
|
|
|
|
|
|
// 根据优惠券id查看拥有优惠券的用户
|
|
|
|
func (s *sCouponUsers) GetCouponUsersByCondition(ctx context.Context, couponId, jwcode int, name string) (users []couponusers.GetCouponUsersRes, err error) { //待解释
|
|
|
|
//从redis中获取所有数据但暂时不返回, 用于条件查询
|
|
|
|
value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d CouponUsers", couponId)) |
|
|
|
//redis中有数据
|
|
|
|
if value.String() != "" { |
|
|
|
err = json.Unmarshal(value.Bytes(), &users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("redis中数据解析失败") |
|
|
|
} |
|
|
|
} else { //redis中没有数据
|
|
|
|
//没有从redis中获取成功,从数据库中查询,并存储到redis中
|
|
|
|
/*查询所有数据*/ |
|
|
|
//在coupon_users中查询出对应的jwcode
|
|
|
|
err = dao.CouponUsers.Ctx(ctx).Fields("jwcode"). |
|
|
|
Where("coupon_id = ", couponId).Scan(&users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("根据卡券id搜索jwcode失败") |
|
|
|
} |
|
|
|
//根据jwcode查询用户信息
|
|
|
|
for i, userInfo := range users { |
|
|
|
err = dao.MemberInfo.Ctx(ctx).Fields("jwcode", "name", "deptName", "shopName"). |
|
|
|
Where("jwcode = ?", userInfo.Jwcode).Scan(&users[i]) |
|
|
|
func (s *sCouponUsers) GetCouponUsersByCondition(ctx context.Context, couponId, jwcode int, name string, pageNo, pageSize int) (users []couponusers.GetCouponUsersRes, err error) { |
|
|
|
//没有条件查询时,分页
|
|
|
|
if jwcode == 0 && name == "" { |
|
|
|
//从redis中获取当前分页数据
|
|
|
|
value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d %d-%d CouponUsers", couponId, pageNo, pageSize)) |
|
|
|
//redis中有数据
|
|
|
|
if value.String() != "" { |
|
|
|
err = json.Unmarshal(value.Bytes(), &users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("根据jwcode搜索用户信息失败") |
|
|
|
return nil, errors.New("redis中数据解析失败") |
|
|
|
} |
|
|
|
return users, err |
|
|
|
} else { //redis中没有数据
|
|
|
|
//没有从redis中获取成功,从数据库中查询,并存储到redis中
|
|
|
|
/*查询所有数据*/ |
|
|
|
//在coupon_users中查询出对应的jwcode
|
|
|
|
err = dao.CouponUsers.Ctx(ctx).Fields("jwcode"). |
|
|
|
Where("coupon_id = ", couponId).Page(pageNo, pageSize).Scan(&users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("根据卡券id搜索jwcode失败") |
|
|
|
} |
|
|
|
//根据jwcode查询用户信息
|
|
|
|
for i, userInfo := range users { |
|
|
|
err = dao.MemberInfo.Ctx(ctx).Fields("jwcode", "name", "deptName", "shopName"). |
|
|
|
Where("jwcode = ?", userInfo.Jwcode).Scan(&users[i]) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("根据jwcode搜索用户信息失败") |
|
|
|
} |
|
|
|
} |
|
|
|
/*查询所有数据*/ |
|
|
|
|
|
|
|
//将数据存入redis
|
|
|
|
UsersMarshal, _ := json.Marshal(users) |
|
|
|
_, err = g.Redis().Set(ctx, fmt.Sprintf("%d %d-%d CouponUsers", couponId, pageNo, pageSize), UsersMarshal) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("将数据存入redis失败") |
|
|
|
} |
|
|
|
} |
|
|
|
/*查询所有数据*/ |
|
|
|
|
|
|
|
//将数据存入redis
|
|
|
|
UsersMarshal, _ := json.Marshal(users) |
|
|
|
_, err = g.Redis().Set(ctx, fmt.Sprintf("%d CouponUsers", couponId), UsersMarshal) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("将数据存入redis失败") |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return users, err |
|
|
|
|
|
|
|
//有条件查询时,不在分页,全查后筛选
|
|
|
|
} else { |
|
|
|
|
|
|
|
//条件查询
|
|
|
|
if jwcode != 0 && name == "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if user.Jwcode == jwcode { |
|
|
|
result = append(result, user) |
|
|
|
//从redis中获取所有数据但暂时不返回, 用于条件查询
|
|
|
|
value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d CouponUsers", couponId)) |
|
|
|
//redis中有数据
|
|
|
|
if value.String() != "" { |
|
|
|
err = json.Unmarshal(value.Bytes(), &users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("redis中数据解析失败") |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} else if jwcode == 0 && name != "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if strings.Contains(user.Name, name) { |
|
|
|
result = append(result, user) |
|
|
|
} else { //redis中没有数据
|
|
|
|
//没有从redis中获取成功,从数据库中查询,并存储到redis中
|
|
|
|
/*查询所有数据*/ |
|
|
|
//在coupon_users中查询出对应的jwcode
|
|
|
|
err = dao.CouponUsers.Ctx(ctx).Fields("jwcode"). |
|
|
|
Where("coupon_id = ", couponId).Scan(&users) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("根据卡券id搜索jwcode失败") |
|
|
|
} |
|
|
|
//根据jwcode查询用户信息
|
|
|
|
for i, userInfo := range users { |
|
|
|
err = dao.MemberInfo.Ctx(ctx).Fields("jwcode", "name", "deptName", "shopName"). |
|
|
|
Where("jwcode = ?", userInfo.Jwcode).Scan(&users[i]) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("根据jwcode搜索用户信息失败") |
|
|
|
} |
|
|
|
} |
|
|
|
/*查询所有数据*/ |
|
|
|
|
|
|
|
//将数据存入redis
|
|
|
|
UsersMarshal, _ := json.Marshal(users) |
|
|
|
_, err = g.Redis().Set(ctx, fmt.Sprintf("%d CouponUsers", couponId), UsersMarshal) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("将数据存入redis失败") |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return result, err |
|
|
|
} else if jwcode != 0 && name != "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if user.Jwcode == jwcode && strings.Contains(user.Name, name) { |
|
|
|
result = append(result, user) |
|
|
|
|
|
|
|
//条件查询
|
|
|
|
if jwcode != 0 && name == "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if user.Jwcode == jwcode { |
|
|
|
result = append(result, user) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} else if jwcode == 0 && name != "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if strings.Contains(user.Name, name) { |
|
|
|
result = append(result, user) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} else if jwcode != 0 && name != "" { |
|
|
|
var result []couponusers.GetCouponUsersRes |
|
|
|
for _, user := range users { |
|
|
|
if user.Jwcode == jwcode && strings.Contains(user.Name, name) { |
|
|
|
result = append(result, user) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} |
|
|
|
|
|
|
|
return users, err |
|
|
|
return users, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 根据jwcode,优惠券id删除用户
|
|
|
@ -103,6 +148,16 @@ func (s *sCouponUsers) DeleteCouponUserByJwcode(ctx context.Context, couponId, j |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("删除用户失败") |
|
|
|
} |
|
|
|
|
|
|
|
//删除redis中的数据
|
|
|
|
_, err = g.Redis().Del(ctx, fmt.Sprintf("%d CouponUsers", couponId)) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.New("删除redis中的数据失败") |
|
|
|
} |
|
|
|
keys, err := g.Redis().Keys(ctx, fmt.Sprintf("%d *-* CouponUsers", couponId)) |
|
|
|
if len(keys) > 0 { |
|
|
|
_, err = g.Redis().Del(ctx, keys...) |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} |
|
|
|
|
|
|
@ -197,6 +252,16 @@ func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes [ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//删除redis中的数据
|
|
|
|
_, err = g.Redis().Del(ctx, fmt.Sprintf("%d CouponUsers", couponId)) |
|
|
|
if err != nil { |
|
|
|
return 0, errors.New("删除redis中的数据失败") |
|
|
|
} |
|
|
|
keys, err := g.Redis().Keys(ctx, fmt.Sprintf("%d *-* CouponUsers", couponId)) |
|
|
|
if len(keys) > 0 { |
|
|
|
_, err = g.Redis().Del(ctx, keys...) |
|
|
|
} |
|
|
|
|
|
|
|
return num, err //返回受影响的行数,即新增的条数
|
|
|
|
} |
|
|
|
|
|
|
@ -348,6 +413,17 @@ func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId i |
|
|
|
if err != nil { |
|
|
|
return errors.New("插入数据库失败") |
|
|
|
} |
|
|
|
|
|
|
|
//删除redis中的数据
|
|
|
|
_, err = g.Redis().Del(ctx, fmt.Sprintf("%d CouponUsers", couponId)) |
|
|
|
if err != nil { |
|
|
|
return errors.New("删除redis中的数据失败") |
|
|
|
} |
|
|
|
keys, err := g.Redis().Keys(ctx, fmt.Sprintf("%d *-* CouponUsers", couponId)) |
|
|
|
if len(keys) > 0 { |
|
|
|
_, err = g.Redis().Del(ctx, keys...) |
|
|
|
} |
|
|
|
|
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -355,3 +431,8 @@ func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId i |
|
|
|
return errors.New("该用户精网号不符合领取条件") //遍历完了所有满足条件的用户,发现不在其中,不符合条件
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 导出拥有卡券的用户列表
|
|
|
|
func (s *sCouponUsers) ExportCouponUsers() { |
|
|
|
|
|
|
|
} |