diff --git a/api/v1/couponusers/couponUsers.go b/api/v1/couponusers/couponUsers.go index 8b2823f..cfd499e 100644 --- a/api/v1/couponusers/couponUsers.go +++ b/api/v1/couponusers/couponUsers.go @@ -37,3 +37,7 @@ type AddRecordReq struct { CouponId int `json:"couponId" dc:"卡券id"` Name string `json:"name" dc:"选择武器名称"` } + +type InsertCouponUserRes struct { + Jwcode int `json:"jwcode" dc:"精网号"` +} diff --git a/hack/config.yaml b/hack/config.yaml index 03b6e23..e977e0f 100644 --- a/hack/config.yaml +++ b/hack/config.yaml @@ -6,7 +6,7 @@ gfcli: dao: - link: "mysql:live:p4jMAMShNM8HTrbX@tcp(39.101.133.168:3306)/live?charset=utf8mb4&parseTime=True&loc=Local" group: "default" - tables: "coupon, coupon_users, member_info" + tables: "coupon, coupon_users, member_info, coupon_qualified_users" descriptionTag: true docker: diff --git a/internal/controller/couponusers/couponUsers.go b/internal/controller/couponusers/couponUsers.go index 3ad5ff1..64f7aaf 100644 --- a/internal/controller/couponusers/couponUsers.go +++ b/internal/controller/couponusers/couponUsers.go @@ -138,7 +138,8 @@ func (c *CouponUsers) InsertJwcodesToRedisByExcel(r *ghttp.Request) { r.Response.WriteJsonExit(dto.Error(err.Error())) } //成功处理 - r.Response.WriteJsonExit(dto.SuccessWithMsg("导入redis成功")) + //r.Response.WriteJsonExit(dto.SuccessWithMsg("导入redis成功")) + r.Response.WriteJsonExit(dto.SuccessWithMsg("导入coupon_qualified_users表成功")) } diff --git a/internal/dao/coupon_qualified_users.go b/internal/dao/coupon_qualified_users.go new file mode 100644 index 0000000..6dc9dec --- /dev/null +++ b/internal/dao/coupon_qualified_users.go @@ -0,0 +1,27 @@ +// ================================================================================= +// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish. +// ================================================================================= + +package dao + +import ( + "CouponBackendGo/internal/dao/internal" +) + +// internalCouponQualifiedUsersDao is internal type for wrapping internal DAO implements. +type internalCouponQualifiedUsersDao = *internal.CouponQualifiedUsersDao + +// couponQualifiedUsersDao is the data access object for table coupon_qualified_users. +// You can define custom methods on it to extend its functionality as you wish. +type couponQualifiedUsersDao struct { + internalCouponQualifiedUsersDao +} + +var ( + // CouponQualifiedUsers is globally public accessible object for table coupon_qualified_users operations. + CouponQualifiedUsers = couponQualifiedUsersDao{ + internal.NewCouponQualifiedUsersDao(), + } +) + +// Fill with you ideas below. diff --git a/internal/dao/internal/coupon_qualified_users.go b/internal/dao/internal/coupon_qualified_users.go new file mode 100644 index 0000000..687b206 --- /dev/null +++ b/internal/dao/internal/coupon_qualified_users.go @@ -0,0 +1,75 @@ +// ========================================================================== +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ========================================================================== + +package internal + +import ( + "context" + + "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/frame/g" +) + +// CouponQualifiedUsersDao is the data access object for table coupon_qualified_users. +type CouponQualifiedUsersDao struct { + table string // table is the underlying table name of the DAO. + group string // group is the database configuration group name of current DAO. + columns CouponQualifiedUsersColumns // columns contains all the column names of Table for convenient usage. +} + +// CouponQualifiedUsersColumns defines and stores column names for table coupon_qualified_users. +type CouponQualifiedUsersColumns struct { + Id string // + Jwcode string // +} + +// couponQualifiedUsersColumns holds the columns for table coupon_qualified_users. +var couponQualifiedUsersColumns = CouponQualifiedUsersColumns{ + Id: "id", + Jwcode: "jwcode", +} + +// NewCouponQualifiedUsersDao creates and returns a new DAO object for table data access. +func NewCouponQualifiedUsersDao() *CouponQualifiedUsersDao { + return &CouponQualifiedUsersDao{ + group: "default", + table: "coupon_qualified_users", + columns: couponQualifiedUsersColumns, + } +} + +// DB retrieves and returns the underlying raw database management object of current DAO. +func (dao *CouponQualifiedUsersDao) DB() gdb.DB { + return g.DB(dao.group) +} + +// Table returns the table name of current dao. +func (dao *CouponQualifiedUsersDao) Table() string { + return dao.table +} + +// Columns returns all column names of current dao. +func (dao *CouponQualifiedUsersDao) Columns() CouponQualifiedUsersColumns { + return dao.columns +} + +// Group returns the configuration group name of database of current dao. +func (dao *CouponQualifiedUsersDao) Group() string { + return dao.group +} + +// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation. +func (dao *CouponQualifiedUsersDao) Ctx(ctx context.Context) *gdb.Model { + return dao.DB().Model(dao.table).Safe().Ctx(ctx) +} + +// Transaction wraps the transaction logic using function f. +// It rollbacks the transaction and returns the error from function f if it returns non-nil error. +// It commits the transaction and returns nil if function f returns nil. +// +// Note that, you should not Commit or Rollback the transaction in function f +// as it is automatically handled by this function. +func (dao *CouponQualifiedUsersDao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) { + return dao.Ctx(ctx).Transaction(ctx, f) +} diff --git a/internal/logic/couponusers/couponUsers.go b/internal/logic/couponusers/couponUsers.go index a766cb0..9d2dba2 100644 --- a/internal/logic/couponusers/couponUsers.go +++ b/internal/logic/couponusers/couponUsers.go @@ -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,39 +253,39 @@ func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err err return errors.New("参数转换失败") } - jwcodes = append(jwcodes, jwcode) + // 判断jwcode是否在表coupon_qualified_users中 + count, err := dao.CouponQualifiedUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Count() + if err != nil { + return errors.New("检索数据库中是否已存在数据失败") + } + if count == 0 { //不存在,插入 + _, err = dao.CouponQualifiedUsers.Ctx(ctx).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中获取数据失败") - } - var EligibleJwcodes []int - err = json.Unmarshal(redisResult.Bytes(), &EligibleJwcodes) + //从数据库中获取符合条件的精网号EligibleJwcodes + var EligibleJwcodes []couponusers.InsertCouponUserRes + err = dao.CouponQualifiedUsers.Ctx(ctx).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 { - if EligibleJwcode == jwcode { + if EligibleJwcode.Jwcode == jwcode { //存在,有资格,判断是否抽取过 for _, couponId := range couponIds { count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count() @@ -320,24 +319,20 @@ 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中获取数据失败") - } - var EligibleJwcodes []int - err = json.Unmarshal(redisResult.Bytes(), &EligibleJwcodes) + + //从数据库中获取符合条件的精网号EligibleJwcodes + var EligibleJwcodes []couponusers.InsertCouponUserRes + err = dao.CouponQualifiedUsers.Ctx(ctx).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 { - if EligibleJwcode == jwcode { + if EligibleJwcode.Jwcode == jwcode { //存在,可以插入 _, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{ "jwcode": jwcode, diff --git a/internal/model/do/coupon_qualified_users.go b/internal/model/do/coupon_qualified_users.go new file mode 100644 index 0000000..cf7dec6 --- /dev/null +++ b/internal/model/do/coupon_qualified_users.go @@ -0,0 +1,16 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package do + +import ( + "github.com/gogf/gf/v2/frame/g" +) + +// CouponQualifiedUsers is the golang structure of table coupon_qualified_users for DAO operations like Where/Data. +type CouponQualifiedUsers struct { + g.Meta `orm:"table:coupon_qualified_users, do:true"` + Id interface{} // + Jwcode interface{} // +} diff --git a/internal/model/entity/coupon_qualified_users.go b/internal/model/entity/coupon_qualified_users.go new file mode 100644 index 0000000..2e37dc1 --- /dev/null +++ b/internal/model/entity/coupon_qualified_users.go @@ -0,0 +1,11 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package entity + +// CouponQualifiedUsers is the golang structure for table coupon_qualified_users. +type CouponQualifiedUsers struct { + Id uint `json:"id" orm:"id" description:""` // + Jwcode int `json:"jwcode" orm:"jwcode" description:""` // +} diff --git a/internal/service/couponusers.go b/internal/service/couponusers.go index 02a20bc..438a2e5 100644 --- a/internal/service/couponusers.go +++ b/internal/service/couponusers.go @@ -22,7 +22,7 @@ type ( InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) // 根据精网号发放用户优惠券 InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) - // 导入满足条件的用户jwcode到redis + // 导入满足条件的用户jwcode到redis //不上传到redis了,上传到数据库表,到时候直接从表里查 InsertJwcodesToRedisByExcel(file multipart.File) (err error) // 判断某用户能否抽到卡券 IsEligibleUser(ctx context.Context, jwcode int, couponIds []int) (img string, err error)