Browse Source

12.28日下午增加请求校验, 删除一些非必须的redis

ljk
lijikun 6 months ago
parent
commit
15f102b89b
  1. 12
      internal/controller/couponusers/couponUsers.go
  2. 134
      internal/logic/couponusers/couponUsers.go
  3. 1
      internal/service/couponusers.go

12
internal/controller/couponusers/couponUsers.go

@ -178,13 +178,6 @@ func (c *CouponUsers) IsEligibleUser(r *ghttp.Request) {
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()))
@ -199,13 +192,8 @@ func (c *CouponUsers) IssueCouponUser(r *ghttp.Request) {
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()))

134
internal/logic/couponusers/couponUsers.go

@ -8,7 +8,6 @@ import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/xuri/excelize/v2"
@ -27,83 +26,42 @@ func init() {
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("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)
/*查询所有数据*/
//在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("将数据存入redis失败")
return nil, errors.New("根据jwcode搜索用户信息失败")
}
}
/*查询所有数据*/
return users, err
} else { //有条件查询时,不在分页,全查后筛选
//有条件查询时,不在分页,全查后筛选
} else {
//从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])
if err != nil {
return nil, errors.New("根据jwcode搜索用户信息失败")
}
}
/*查询所有数据*/
//将数据存入redis
UsersMarshal, _ := json.Marshal(users)
_, err = g.Redis().Set(ctx, fmt.Sprintf("%d CouponUsers", couponId), UsersMarshal)
/*查询所有数据*/
//在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("将数据存入redis失败")
return nil, errors.New("根据jwcode搜索用户信息失败")
}
}
/*查询所有数据*/
//条件查询
if jwcode != 0 && name == "" {
@ -148,16 +106,6 @@ 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
}
@ -181,9 +129,7 @@ func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int,
if len(GetSheetMap) == 0 {
return nil, errors.New("没有工作表")
}
// 读取第一个工作表
//sheetName := GetSheetMap[1]
//rows, err := f.GetRows(f.GetSheetName(1))
rows, err := f.GetRows("Sheet1")
if err != nil {
return nil, err
@ -252,16 +198,6 @@ 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 //返回受影响的行数,即新增的条数
}
@ -289,8 +225,6 @@ func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err err
}
// 读取第一个工作表
//sheetName := GetSheetMap[1]
//rows, err := f.GetRows(f.GetSheetName(1))
rows, err := f.GetRows("Sheet1")
if err != nil {
return err
@ -413,17 +347,6 @@ 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
}
}
@ -432,6 +355,7 @@ func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId i
}
/*未编写*/
// 导出拥有卡券的用户列表
func (s *sCouponUsers) ExportCouponUsers() {

1
internal/service/couponusers.go

@ -28,6 +28,7 @@ type (
IsEligibleUser(ctx context.Context, jwcode int, couponIds []int) (img string, err error)
// 给单个用户发放卡券
IssueCouponToUser(ctx context.Context, jwcode int, couponId int) (err error)
/*未编写*/
// 导出拥有卡券的用户列表
ExportCouponUsers()
}

Loading…
Cancel
Save