From 39670b7d3b62b8c40ec0491732495885506e826a Mon Sep 17 00:00:00 2001 From: majun <3060162534@qq.com> Date: Fri, 27 Dec 2024 00:29:19 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E7=86=9F=E6=82=89=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/.gitkeep | 0 api/v1/coupon/coupon.go | 10 +++ internal/cmd/cmd.go | 4 +- internal/controller/.gitkeep | 0 internal/controller/coupon/coupon.go | 63 +++++++++++++++ internal/dao/.gitkeep | 0 internal/dao/coupon.go | 27 +++++++ internal/dao/coupon_users.go | 27 +++++++ internal/dao/internal/coupon.go | 89 ++++++++++++++++++++++ internal/dao/internal/coupon_users.go | 83 ++++++++++++++++++++ internal/dao/internal/member_info.go | 139 ++++++++++++++++++++++++++++++++++ internal/dao/member_info.go | 27 +++++++ internal/logic/.gitkeep | 0 internal/logic/coupon/coupon.go | 53 +++++++++++++ internal/logic/logic.go | 8 ++ internal/model/.gitkeep | 0 internal/model/do/.gitkeep | 0 internal/model/do/coupon.go | 24 ++++++ internal/model/do/coupon_users.go | 20 +++++ internal/model/do/member_info.go | 49 ++++++++++++ internal/model/dto/dto_coupon.go | 15 ++++ internal/model/entity/.gitkeep | 0 internal/model/entity/coupon.go | 22 ++++++ internal/model/entity/coupon_users.go | 15 ++++ internal/model/entity/member_info.go | 47 ++++++++++++ internal/service/.gitkeep | 0 internal/service/coupon.go | 34 +++++++++ 27 files changed, 755 insertions(+), 1 deletion(-) delete mode 100644 api/v1/.gitkeep create mode 100644 api/v1/coupon/coupon.go delete mode 100644 internal/controller/.gitkeep create mode 100644 internal/controller/coupon/coupon.go delete mode 100644 internal/dao/.gitkeep create mode 100644 internal/dao/coupon.go create mode 100644 internal/dao/coupon_users.go create mode 100644 internal/dao/internal/coupon.go create mode 100644 internal/dao/internal/coupon_users.go create mode 100644 internal/dao/internal/member_info.go create mode 100644 internal/dao/member_info.go delete mode 100644 internal/logic/.gitkeep create mode 100644 internal/logic/coupon/coupon.go delete mode 100644 internal/model/.gitkeep delete mode 100644 internal/model/do/.gitkeep create mode 100644 internal/model/do/coupon.go create mode 100644 internal/model/do/coupon_users.go create mode 100644 internal/model/do/member_info.go create mode 100644 internal/model/dto/dto_coupon.go delete mode 100644 internal/model/entity/.gitkeep create mode 100644 internal/model/entity/coupon.go create mode 100644 internal/model/entity/coupon_users.go create mode 100644 internal/model/entity/member_info.go delete mode 100644 internal/service/.gitkeep create mode 100644 internal/service/coupon.go diff --git a/api/v1/.gitkeep b/api/v1/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/api/v1/coupon/coupon.go b/api/v1/coupon/coupon.go new file mode 100644 index 0000000..fe51342 --- /dev/null +++ b/api/v1/coupon/coupon.go @@ -0,0 +1,10 @@ +package coupon + +type GetCouponListReq struct { + PageNo int `v:"required#页码不能为空" dc:"页码"` + PageSize int `v:"required#页面大小不能为空" dc:"页面大小"` +} + +type GetCouponReq struct { + Id int `json:"id" v:"required#优惠券id不能为空" dc:"优惠券id"` +} diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 4c9236b..e427e6b 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -1,6 +1,7 @@ package cmd import ( + "CouponBackendGo/internal/controller/coupon" "context" "github.com/gogf/gf/v2/frame/g" @@ -16,7 +17,8 @@ var ( Func: func(ctx context.Context, parser *gcmd.Parser) (err error) { s := g.Server() s.Group("/api/coupon_backend", func(group *ghttp.RouterGroup) { - + group.POST("/get-coupon-list", coupon.Coupon().GetCouponList) + group.POST("/get-coupon", coupon.Coupon().GetCoupon) }) s.Run() return nil diff --git a/internal/controller/.gitkeep b/internal/controller/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/controller/coupon/coupon.go b/internal/controller/coupon/coupon.go new file mode 100644 index 0000000..a5be79f --- /dev/null +++ b/internal/controller/coupon/coupon.go @@ -0,0 +1,63 @@ +package coupon + +import ( + "CouponBackendGo/api/v1/coupon" + "CouponBackendGo/internal/model/dto" + "CouponBackendGo/internal/service" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/net/ghttp" +) + +type cCoupon struct{} + +func Coupon() *cCoupon { + return &cCoupon{} +} + +func (c cCoupon) GetCouponList(r *ghttp.Request) { + var req *coupon.GetCouponListReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + res, err := service.Coupon().GetCouponList(r.Context(), req.PageNo, req.PageSize) + total, err := service.Coupon().GetCouponListTotal(r.Context()) + if err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + r.Response.WriteJsonExit(dto.Result{ + Code: 200, + Message: "success", + Data: g.Map{ + "list": res, + "total": total, + }, + }) +} + +func (c cCoupon) GetCoupon(r *ghttp.Request) { + var req *coupon.GetCouponReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + res, err := service.Coupon().GetCoupon(r.Context(), req.Id) + if err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + r.Response.WriteJsonExit(dto.Result{ + Code: 200, + Message: "success", + Data: res, + }) +} diff --git a/internal/dao/.gitkeep b/internal/dao/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/dao/coupon.go b/internal/dao/coupon.go new file mode 100644 index 0000000..6755e8c --- /dev/null +++ b/internal/dao/coupon.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" +) + +// internalCouponDao is internal type for wrapping internal DAO implements. +type internalCouponDao = *internal.CouponDao + +// couponDao is the data access object for table coupon. +// You can define custom methods on it to extend its functionality as you wish. +type couponDao struct { + internalCouponDao +} + +var ( + // Coupon is globally public accessible object for table coupon operations. + Coupon = couponDao{ + internal.NewCouponDao(), + } +) + +// Fill with you ideas below. diff --git a/internal/dao/coupon_users.go b/internal/dao/coupon_users.go new file mode 100644 index 0000000..ecfee65 --- /dev/null +++ b/internal/dao/coupon_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" +) + +// internalCouponUsersDao is internal type for wrapping internal DAO implements. +type internalCouponUsersDao = *internal.CouponUsersDao + +// couponUsersDao is the data access object for table coupon_users. +// You can define custom methods on it to extend its functionality as you wish. +type couponUsersDao struct { + internalCouponUsersDao +} + +var ( + // CouponUsers is globally public accessible object for table coupon_users operations. + CouponUsers = couponUsersDao{ + internal.NewCouponUsersDao(), + } +) + +// Fill with you ideas below. diff --git a/internal/dao/internal/coupon.go b/internal/dao/internal/coupon.go new file mode 100644 index 0000000..09c0b2a --- /dev/null +++ b/internal/dao/internal/coupon.go @@ -0,0 +1,89 @@ +// ========================================================================== +// 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" +) + +// CouponDao is the data access object for table coupon. +type CouponDao 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 CouponColumns // columns contains all the column names of Table for convenient usage. +} + +// CouponColumns defines and stores column names for table coupon. +type CouponColumns struct { + Id string // + Title string // 名字 + Cover string // 封面小图片 + ImgUrl string // 放大看全图 + StartTime string // 有效期起始 + EndTime string // 有效期截止 + Deleted string // 是否删除? + CreatedAt string // 创建时间 + UpdatedAt string // 更新时间 +} + +// couponColumns holds the columns for table coupon. +var couponColumns = CouponColumns{ + Id: "id", + Title: "title", + Cover: "cover", + ImgUrl: "img_url", + StartTime: "start_time", + EndTime: "end_time", + Deleted: "deleted", + CreatedAt: "created_at", + UpdatedAt: "updated_at", +} + +// NewCouponDao creates and returns a new DAO object for table data access. +func NewCouponDao() *CouponDao { + return &CouponDao{ + group: "default", + table: "coupon", + columns: couponColumns, + } +} + +// DB retrieves and returns the underlying raw database management object of current DAO. +func (dao *CouponDao) DB() gdb.DB { + return g.DB(dao.group) +} + +// Table returns the table name of current dao. +func (dao *CouponDao) Table() string { + return dao.table +} + +// Columns returns all column names of current dao. +func (dao *CouponDao) Columns() CouponColumns { + return dao.columns +} + +// Group returns the configuration group name of database of current dao. +func (dao *CouponDao) Group() string { + return dao.group +} + +// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation. +func (dao *CouponDao) 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 *CouponDao) 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/dao/internal/coupon_users.go b/internal/dao/internal/coupon_users.go new file mode 100644 index 0000000..b0d66ef --- /dev/null +++ b/internal/dao/internal/coupon_users.go @@ -0,0 +1,83 @@ +// ========================================================================== +// 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" +) + +// CouponUsersDao is the data access object for table coupon_users. +type CouponUsersDao 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 CouponUsersColumns // columns contains all the column names of Table for convenient usage. +} + +// CouponUsersColumns defines and stores column names for table coupon_users. +type CouponUsersColumns struct { + Id string // + Jwcode string // + Time string // 领取时间 + CouponId string // 优惠券id + Code string // + State string // 0 未使用 1 已使用 +} + +// couponUsersColumns holds the columns for table coupon_users. +var couponUsersColumns = CouponUsersColumns{ + Id: "id", + Jwcode: "jwcode", + Time: "time", + CouponId: "coupon_id", + Code: "code", + State: "state", +} + +// NewCouponUsersDao creates and returns a new DAO object for table data access. +func NewCouponUsersDao() *CouponUsersDao { + return &CouponUsersDao{ + group: "default", + table: "coupon_users", + columns: couponUsersColumns, + } +} + +// DB retrieves and returns the underlying raw database management object of current DAO. +func (dao *CouponUsersDao) DB() gdb.DB { + return g.DB(dao.group) +} + +// Table returns the table name of current dao. +func (dao *CouponUsersDao) Table() string { + return dao.table +} + +// Columns returns all column names of current dao. +func (dao *CouponUsersDao) Columns() CouponUsersColumns { + return dao.columns +} + +// Group returns the configuration group name of database of current dao. +func (dao *CouponUsersDao) Group() string { + return dao.group +} + +// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation. +func (dao *CouponUsersDao) 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 *CouponUsersDao) 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/dao/internal/member_info.go b/internal/dao/internal/member_info.go new file mode 100644 index 0000000..6e3d9f5 --- /dev/null +++ b/internal/dao/internal/member_info.go @@ -0,0 +1,139 @@ +// ========================================================================== +// 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" +) + +// MemberInfoDao is the data access object for table member_info. +type MemberInfoDao 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 MemberInfoColumns // columns contains all the column names of Table for convenient usage. +} + +// MemberInfoColumns defines and stores column names for table member_info. +type MemberInfoColumns struct { + Id string // + Jwcode string // + Name string // + Sex string // 0:未知,1:男,2:女 + Avatar string // 用户头像 + Widget string // 头像挂件 + DeptId string // + DeptName string // + ShopId string // + ShopName string // + MembershipTime string // + LiveLock string // 0:解锁 1:锁住 + LocMarket string // 用户市场归属 + Level string // 等级 + LevelIcon string // 等级图标 + Star string // 星级 + AccountOwner string // + AccountOwnerText string // + Employee string // 1:员工 + Dachang string // 0:不是大厂员工 1:是大厂员工 + BoguMember string // 1:博股会员 + LearningIcon string // 学习等级icon + Mobile string // 手机号 + UserIdentity string // 0:无认证 1:红V 2:蓝V 3:黄V + UserIdentityTitle string // 用户身份头衔 + Openid string // 微信openid + UserRole string // 用户身份(1:网员 2:非网) + IsBlacklist string // 是否是黑名单用户 + IsLecturer string // 讲师 + Shenqiangshou string // 神枪手 + Huanqiu string // 环球 + Age string // 年纪 + CreatedAt string // + UpdatedAt string // +} + +// memberInfoColumns holds the columns for table member_info. +var memberInfoColumns = MemberInfoColumns{ + Id: "id", + Jwcode: "jwcode", + Name: "name", + Sex: "sex", + Avatar: "avatar", + Widget: "widget", + DeptId: "deptId", + DeptName: "deptName", + ShopId: "shopId", + ShopName: "shopName", + MembershipTime: "membership_time", + LiveLock: "live_lock", + LocMarket: "loc_market", + Level: "level", + LevelIcon: "level_icon", + Star: "star", + AccountOwner: "account_owner", + AccountOwnerText: "account_owner_text", + Employee: "employee", + Dachang: "dachang", + BoguMember: "bogu_member", + LearningIcon: "learning_icon", + Mobile: "mobile", + UserIdentity: "user_identity", + UserIdentityTitle: "user_identity_title", + Openid: "openid", + UserRole: "user_role", + IsBlacklist: "is_blacklist", + IsLecturer: "is_lecturer", + Shenqiangshou: "shenqiangshou", + Huanqiu: "huanqiu", + Age: "age", + CreatedAt: "created_at", + UpdatedAt: "updated_at", +} + +// NewMemberInfoDao creates and returns a new DAO object for table data access. +func NewMemberInfoDao() *MemberInfoDao { + return &MemberInfoDao{ + group: "default", + table: "member_info", + columns: memberInfoColumns, + } +} + +// DB retrieves and returns the underlying raw database management object of current DAO. +func (dao *MemberInfoDao) DB() gdb.DB { + return g.DB(dao.group) +} + +// Table returns the table name of current dao. +func (dao *MemberInfoDao) Table() string { + return dao.table +} + +// Columns returns all column names of current dao. +func (dao *MemberInfoDao) Columns() MemberInfoColumns { + return dao.columns +} + +// Group returns the configuration group name of database of current dao. +func (dao *MemberInfoDao) Group() string { + return dao.group +} + +// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation. +func (dao *MemberInfoDao) 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 *MemberInfoDao) 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/dao/member_info.go b/internal/dao/member_info.go new file mode 100644 index 0000000..20ed4a3 --- /dev/null +++ b/internal/dao/member_info.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" +) + +// internalMemberInfoDao is internal type for wrapping internal DAO implements. +type internalMemberInfoDao = *internal.MemberInfoDao + +// memberInfoDao is the data access object for table member_info. +// You can define custom methods on it to extend its functionality as you wish. +type memberInfoDao struct { + internalMemberInfoDao +} + +var ( + // MemberInfo is globally public accessible object for table member_info operations. + MemberInfo = memberInfoDao{ + internal.NewMemberInfoDao(), + } +) + +// Fill with you ideas below. diff --git a/internal/logic/.gitkeep b/internal/logic/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/logic/coupon/coupon.go b/internal/logic/coupon/coupon.go new file mode 100644 index 0000000..f697113 --- /dev/null +++ b/internal/logic/coupon/coupon.go @@ -0,0 +1,53 @@ +package coupon + +import ( + "CouponBackendGo/internal/dao" + "CouponBackendGo/internal/model/dto" + "CouponBackendGo/internal/service" + "context" + "github.com/gogf/gf/v2/os/gtime" +) + +type ( + sCoupon struct{} +) + +func init() { + service.RegisterCoupon(New()) +} + +func New() service.ICoupon { + return &sCoupon{} +} + +func (s *sCoupon) GetCouponList(ctx context.Context, pageNo int, pageSize int) (couponList []*dto.DtoCoupon, err error) { + err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Order("updated_at desc, end_time desc").Page(pageNo, pageSize).Scan(&couponList) + if err != nil { + return nil, err + } + return +} + +func (s *sCoupon) GetCouponListTotal(ctx context.Context) (total int, err error) { + total, err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Count() + if err != nil { + return 0, err + } + return +} + +func (s *sCoupon) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) { + err = dao.Coupon.Ctx(ctx).WherePri(id).Scan(&coupon) + coupon.Status = 0 //待使用期 + if coupon.StartTime.Unix() <= gtime.Now().Unix() { + if coupon.EndTime.Unix() <= gtime.Now().Unix() { + coupon.Status = 2 //使用期后 + } else { + coupon.Status = 1 //使用期中 + } + } + if err != nil { + return nil, err + } + return +} diff --git a/internal/logic/logic.go b/internal/logic/logic.go index 4c79103..540cd35 100644 --- a/internal/logic/logic.go +++ b/internal/logic/logic.go @@ -1 +1,9 @@ +// ========================================================================== +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ========================================================================== + package logic + +import ( + _ "CouponBackendGo/internal/logic/coupon" +) diff --git a/internal/model/.gitkeep b/internal/model/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/model/do/.gitkeep b/internal/model/do/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/model/do/coupon.go b/internal/model/do/coupon.go new file mode 100644 index 0000000..47254e0 --- /dev/null +++ b/internal/model/do/coupon.go @@ -0,0 +1,24 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package do + +import ( + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gtime" +) + +// Coupon is the golang structure of table coupon for DAO operations like Where/Data. +type Coupon struct { + g.Meta `orm:"table:coupon, do:true"` + Id interface{} // + Title interface{} // 名字 + Cover interface{} // 封面小图片 + ImgUrl interface{} // 放大看全图 + StartTime interface{} // 有效期起始 + EndTime interface{} // 有效期截止 + Deleted interface{} // 是否删除? + CreatedAt *gtime.Time // 创建时间 + UpdatedAt *gtime.Time // 更新时间 +} diff --git a/internal/model/do/coupon_users.go b/internal/model/do/coupon_users.go new file mode 100644 index 0000000..8aa0d1b --- /dev/null +++ b/internal/model/do/coupon_users.go @@ -0,0 +1,20 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package do + +import ( + "github.com/gogf/gf/v2/frame/g" +) + +// CouponUsers is the golang structure of table coupon_users for DAO operations like Where/Data. +type CouponUsers struct { + g.Meta `orm:"table:coupon_users, do:true"` + Id interface{} // + Jwcode interface{} // + Time interface{} // 领取时间 + CouponId interface{} // 优惠券id + Code interface{} // + State interface{} // 0 未使用 1 已使用 +} diff --git a/internal/model/do/member_info.go b/internal/model/do/member_info.go new file mode 100644 index 0000000..28988d6 --- /dev/null +++ b/internal/model/do/member_info.go @@ -0,0 +1,49 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package do + +import ( + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gtime" +) + +// MemberInfo is the golang structure of table member_info for DAO operations like Where/Data. +type MemberInfo struct { + g.Meta `orm:"table:member_info, do:true"` + Id interface{} // + Jwcode interface{} // + Name interface{} // + Sex interface{} // 0:未知,1:男,2:女 + Avatar interface{} // 用户头像 + Widget interface{} // 头像挂件 + DeptId interface{} // + DeptName interface{} // + ShopId interface{} // + ShopName interface{} // + MembershipTime *gtime.Time // + LiveLock interface{} // 0:解锁 1:锁住 + LocMarket interface{} // 用户市场归属 + Level interface{} // 等级 + LevelIcon interface{} // 等级图标 + Star interface{} // 星级 + AccountOwner interface{} // + AccountOwnerText interface{} // + Employee interface{} // 1:员工 + Dachang interface{} // 0:不是大厂员工 1:是大厂员工 + BoguMember interface{} // 1:博股会员 + LearningIcon interface{} // 学习等级icon + Mobile interface{} // 手机号 + UserIdentity interface{} // 0:无认证 1:红V 2:蓝V 3:黄V + UserIdentityTitle interface{} // 用户身份头衔 + Openid interface{} // 微信openid + UserRole interface{} // 用户身份(1:网员 2:非网) + IsBlacklist interface{} // 是否是黑名单用户 + IsLecturer interface{} // 讲师 + Shenqiangshou interface{} // 神枪手 + Huanqiu interface{} // 环球 + Age interface{} // 年纪 + CreatedAt *gtime.Time // + UpdatedAt *gtime.Time // +} diff --git a/internal/model/dto/dto_coupon.go b/internal/model/dto/dto_coupon.go new file mode 100644 index 0000000..dad97df --- /dev/null +++ b/internal/model/dto/dto_coupon.go @@ -0,0 +1,15 @@ +package dto + +import "github.com/gogf/gf/v2/os/gtime" + +type DtoCoupon struct { + Id uint `json:"id" orm:"db:default;table:coupon;column:id" description:"ID"` // ID + Title string `json:"title" orm:"db:default;table:coupon;column:title" description:"名字"` // 名字 + Cover string `json:"cover" orm:"db:default;table:coupon;column:cover" description:"封面小图片"` // 封面小图片 + ImgUrl string `json:"imgUrl" orm:"db:default;table:coupon;column:img_url" description:"放大看全图"` // 放大看全图 + UpdatedAt *gtime.Time `json:"updatedAt" orm:"db:default;table:coupon;column:updated_at" description:"更新时间"` // 更新时间 + StartTime *gtime.Time `json:"startTime" description:"有效期起始"` // 有效期起始 + EndTime *gtime.Time `json:"endTime" description:"有效期截止"` // 有效期截止 + Status int `json:"status" description:"卡券状态"` // 卡券状态 + Count int `json:"count" description:"持有人数"` // 持有人数 +} diff --git a/internal/model/entity/.gitkeep b/internal/model/entity/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/model/entity/coupon.go b/internal/model/entity/coupon.go new file mode 100644 index 0000000..7c90e7c --- /dev/null +++ b/internal/model/entity/coupon.go @@ -0,0 +1,22 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package entity + +import ( + "github.com/gogf/gf/v2/os/gtime" +) + +// Coupon is the golang structure for table coupon. +type Coupon struct { + Id uint `json:"id" orm:"id" description:""` // + Title string `json:"title" orm:"title" description:"名字"` // 名字 + Cover string `json:"cover" orm:"cover" description:"封面小图片"` // 封面小图片 + ImgUrl string `json:"imgUrl" orm:"img_url" description:"放大看全图"` // 放大看全图 + StartTime int `json:"startTime" orm:"start_time" description:"有效期起始"` // 有效期起始 + EndTime int `json:"endTime" orm:"end_time" description:"有效期截止"` // 有效期截止 + Deleted int `json:"deleted" orm:"deleted" description:"是否删除?"` // 是否删除? + CreatedAt *gtime.Time `json:"createdAt" orm:"created_at" description:"创建时间"` // 创建时间 + UpdatedAt *gtime.Time `json:"updatedAt" orm:"updated_at" description:"更新时间"` // 更新时间 +} diff --git a/internal/model/entity/coupon_users.go b/internal/model/entity/coupon_users.go new file mode 100644 index 0000000..8a49df3 --- /dev/null +++ b/internal/model/entity/coupon_users.go @@ -0,0 +1,15 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package entity + +// CouponUsers is the golang structure for table coupon_users. +type CouponUsers struct { + Id uint `json:"id" orm:"id" description:""` // + Jwcode int `json:"jwcode" orm:"jwcode" description:""` // + Time int `json:"time" orm:"time" description:"领取时间"` // 领取时间 + CouponId int `json:"couponId" orm:"coupon_id" description:"优惠券id"` // 优惠券id + Code string `json:"code" orm:"code" description:""` // + State int `json:"state" orm:"state" description:"0 未使用 1 已使用"` // 0 未使用 1 已使用 +} diff --git a/internal/model/entity/member_info.go b/internal/model/entity/member_info.go new file mode 100644 index 0000000..0fd0368 --- /dev/null +++ b/internal/model/entity/member_info.go @@ -0,0 +1,47 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package entity + +import ( + "github.com/gogf/gf/v2/os/gtime" +) + +// MemberInfo is the golang structure for table member_info. +type MemberInfo struct { + Id int `json:"id" orm:"id" description:""` // + Jwcode int `json:"jwcode" orm:"jwcode" description:""` // + Name string `json:"name" orm:"name" description:""` // + Sex int `json:"sex" orm:"sex" description:"0:未知,1:男,2:女"` // 0:未知,1:男,2:女 + Avatar string `json:"avatar" orm:"avatar" description:"用户头像"` // 用户头像 + Widget string `json:"widget" orm:"widget" description:"头像挂件"` // 头像挂件 + DeptId string `json:"deptId" orm:"deptId" description:""` // + DeptName string `json:"deptName" orm:"deptName" description:""` // + ShopId string `json:"shopId" orm:"shopId" description:""` // + ShopName string `json:"shopName" orm:"shopName" description:""` // + MembershipTime *gtime.Time `json:"membershipTime" orm:"membership_time" description:""` // + LiveLock int `json:"liveLock" orm:"live_lock" description:"0:解锁 1:锁住"` // 0:解锁 1:锁住 + LocMarket string `json:"locMarket" orm:"loc_market" description:"用户市场归属"` // 用户市场归属 + Level int `json:"level" orm:"level" description:"等级"` // 等级 + LevelIcon string `json:"levelIcon" orm:"level_icon" description:"等级图标"` // 等级图标 + Star int `json:"star" orm:"star" description:"星级"` // 星级 + AccountOwner string `json:"accountOwner" orm:"account_owner" description:""` // + AccountOwnerText string `json:"accountOwnerText" orm:"account_owner_text" description:""` // + Employee int `json:"employee" orm:"employee" description:"1:员工"` // 1:员工 + Dachang int `json:"dachang" orm:"dachang" description:"0:不是大厂员工 1:是大厂员工"` // 0:不是大厂员工 1:是大厂员工 + BoguMember int `json:"boguMember" orm:"bogu_member" description:"1:博股会员"` // 1:博股会员 + LearningIcon string `json:"learningIcon" orm:"learning_icon" description:"学习等级icon"` // 学习等级icon + Mobile string `json:"mobile" orm:"mobile" description:"手机号"` // 手机号 + UserIdentity int `json:"userIdentity" orm:"user_identity" description:"0:无认证 1:红V 2:蓝V 3:黄V"` // 0:无认证 1:红V 2:蓝V 3:黄V + UserIdentityTitle string `json:"userIdentityTitle" orm:"user_identity_title" description:"用户身份头衔"` // 用户身份头衔 + Openid string `json:"openid" orm:"openid" description:"微信openid"` // 微信openid + UserRole int `json:"userRole" orm:"user_role" description:"用户身份(1:网员 2:非网)"` // 用户身份(1:网员 2:非网) + IsBlacklist int `json:"isBlacklist" orm:"is_blacklist" description:"是否是黑名单用户"` // 是否是黑名单用户 + IsLecturer int `json:"isLecturer" orm:"is_lecturer" description:"讲师"` // 讲师 + Shenqiangshou int `json:"shenqiangshou" orm:"shenqiangshou" description:"神枪手"` // 神枪手 + Huanqiu int `json:"huanqiu" orm:"huanqiu" description:"环球"` // 环球 + Age uint `json:"age" orm:"age" description:"年纪"` // 年纪 + CreatedAt *gtime.Time `json:"createdAt" orm:"created_at" description:""` // + UpdatedAt *gtime.Time `json:"updatedAt" orm:"updated_at" description:""` // +} diff --git a/internal/service/.gitkeep b/internal/service/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/service/coupon.go b/internal/service/coupon.go new file mode 100644 index 0000000..7e29480 --- /dev/null +++ b/internal/service/coupon.go @@ -0,0 +1,34 @@ +// ================================================================================ +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// You can delete these comments if you wish manually maintain this interface file. +// ================================================================================ + +package service + +import ( + "CouponBackendGo/internal/model/dto" + "context" +) + +type ( + ICoupon interface { + GetCouponList(ctx context.Context, pageNo int, pageSize int) (couponList []*dto.DtoCoupon, err error) + GetCouponListTotal(ctx context.Context) (total int, err error) + GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) + } +) + +var ( + localCoupon ICoupon +) + +func Coupon() ICoupon { + if localCoupon == nil { + panic("implement not found for interface ICoupon, forgot register?") + } + return localCoupon +} + +func RegisterCoupon(i ICoupon) { + localCoupon = i +} From 9713e047bf5cad7f563899789f5b927b60796723 Mon Sep 17 00:00:00 2001 From: majun <3060162534@qq.com> Date: Fri, 27 Dec 2024 21:29:23 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E7=BC=96=E5=86=99=E5=88=A0=E3=80=81?= =?UTF-8?q?=E6=9F=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/coupon/coupon.go | 6 ++- internal/cmd/cmd.go | 1 + internal/controller/coupon/coupon.go | 21 +++++++++ internal/dao/internal/coupon_users.go | 2 + internal/logic/coupon/coupon.go | 86 +++++++++++++++++++++++++++++++---- internal/model/do/coupon_users.go | 1 + internal/model/entity/coupon_users.go | 1 + internal/service/coupon.go | 1 + 8 files changed, 110 insertions(+), 9 deletions(-) diff --git a/api/v1/coupon/coupon.go b/api/v1/coupon/coupon.go index fe51342..2ad07f3 100644 --- a/api/v1/coupon/coupon.go +++ b/api/v1/coupon/coupon.go @@ -6,5 +6,9 @@ type GetCouponListReq struct { } type GetCouponReq struct { - Id int `json:"id" v:"required#优惠券id不能为空" dc:"优惠券id"` + Id int `v:"required#优惠券id不能为空" dc:"优惠券id"` +} + +type DeleteCouponReq struct { + Id int `v:"required#优惠券id不能为空" dc:"优惠券id"` } diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index e427e6b..2a0b422 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -19,6 +19,7 @@ var ( s.Group("/api/coupon_backend", func(group *ghttp.RouterGroup) { group.POST("/get-coupon-list", coupon.Coupon().GetCouponList) group.POST("/get-coupon", coupon.Coupon().GetCoupon) + group.POST("/delete-coupon", coupon.Coupon().DeleteCoupon) }) s.Run() return nil diff --git a/internal/controller/coupon/coupon.go b/internal/controller/coupon/coupon.go index a5be79f..af7188a 100644 --- a/internal/controller/coupon/coupon.go +++ b/internal/controller/coupon/coupon.go @@ -61,3 +61,24 @@ func (c cCoupon) GetCoupon(r *ghttp.Request) { Data: res, }) } + +func (c cCoupon) DeleteCoupon(r *ghttp.Request) { + var req *coupon.DeleteCouponReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + err := service.Coupon().DeleteCoupon(r.Context(), req.Id) + if err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + r.Response.WriteJsonExit(dto.Result{ + Code: 200, + Message: "success", + }) +} diff --git a/internal/dao/internal/coupon_users.go b/internal/dao/internal/coupon_users.go index b0d66ef..bc119fb 100644 --- a/internal/dao/internal/coupon_users.go +++ b/internal/dao/internal/coupon_users.go @@ -26,6 +26,7 @@ type CouponUsersColumns struct { CouponId string // 优惠券id Code string // State string // 0 未使用 1 已使用 + Record string // 武器记录 } // couponUsersColumns holds the columns for table coupon_users. @@ -36,6 +37,7 @@ var couponUsersColumns = CouponUsersColumns{ CouponId: "coupon_id", Code: "code", State: "state", + Record: "record", } // NewCouponUsersDao creates and returns a new DAO object for table data access. diff --git a/internal/logic/coupon/coupon.go b/internal/logic/coupon/coupon.go index f697113..81807e2 100644 --- a/internal/logic/coupon/coupon.go +++ b/internal/logic/coupon/coupon.go @@ -2,9 +2,13 @@ package coupon import ( "CouponBackendGo/internal/dao" + "CouponBackendGo/internal/model/do" "CouponBackendGo/internal/model/dto" "CouponBackendGo/internal/service" "context" + "encoding/json" + "fmt" + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" ) @@ -21,13 +25,73 @@ func New() service.ICoupon { } func (s *sCoupon) GetCouponList(ctx context.Context, pageNo int, pageSize int) (couponList []*dto.DtoCoupon, err error) { + // 从Redis中获取数据 + value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize)) + if value.String() != "" { + // 如果Redis中有数据,尝试解析为Coupon列表 + err = json.Unmarshal(value.Bytes(), &couponList) //反序列化 + couponlist, cnt := UpdateCoupon(ctx, couponList) + + if cnt > 0 { + // 将修改后的卡券列表序列化并存储到Redis,更新频繁变更的数据 + couponListJson, _ := json.Marshal(couponlist) + _, err = g.Redis().Set(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize), couponListJson) + } + + if err != nil { + return nil, err + } + return couponlist, err + } + + // 如果Redis中没有数据,查询数据库 err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Order("updated_at desc, end_time desc").Page(pageNo, pageSize).Scan(&couponList) + couponList = InitCoupon(ctx, couponList) + couponList, _ = UpdateCoupon(ctx, couponList) if err != nil { return nil, err } + + // 将查询到的卡券列表序列化并存储到Redis,更新频繁变更的数据 + couponListJson, _ := json.Marshal(couponList) + _, err = g.Redis().Set(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize), couponListJson) + return } +func InitCoupon(ctx context.Context, couponList []*dto.DtoCoupon) []*dto.DtoCoupon { + for _, v := range couponList { + v.Status = 0 //待使用期 + if v.StartTime.Unix() <= gtime.Now().Unix() { + if v.EndTime.Unix() <= gtime.Now().Unix() { + v.Status = 2 //使用期后 + v.Count, _ = dao.CouponUsers.Ctx(ctx).Where("coupon_id", v.Id).Count() + } else { + v.Status = 1 //使用期中 + } + } + } + return couponList +} + +func UpdateCoupon(ctx context.Context, couponList []*dto.DtoCoupon) ([]*dto.DtoCoupon, int) { + cnt := 0 + for _, v := range couponList { + if v.Status != 2 { + if v.StartTime.Unix() > gtime.Now().Unix() { + v.Status = 0 + } else if v.EndTime.Unix() <= gtime.Now().Unix() { + v.Status = 2 + cnt++ + } else { + v.Status = 1 + } + v.Count, _ = dao.CouponUsers.Ctx(ctx).Where("coupon_id", v.Id).Count() + } + } + return couponList, cnt +} + func (s *sCoupon) GetCouponListTotal(ctx context.Context) (total int, err error) { total, err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Count() if err != nil { @@ -38,16 +102,22 @@ func (s *sCoupon) GetCouponListTotal(ctx context.Context) (total int, err error) func (s *sCoupon) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) { err = dao.Coupon.Ctx(ctx).WherePri(id).Scan(&coupon) - coupon.Status = 0 //待使用期 - if coupon.StartTime.Unix() <= gtime.Now().Unix() { - if coupon.EndTime.Unix() <= gtime.Now().Unix() { - coupon.Status = 2 //使用期后 - } else { - coupon.Status = 1 //使用期中 - } - } if err != nil { return nil, err } return } + +func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { + _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ + Deleted: 1, + }).WherePri(id).Update() + + // 软删除数据后,删除所有缓存 + keys, err := g.Redis().Keys(ctx, "*-* couponlist") + if len(keys) > 0 { + _, err = g.Redis().Del(ctx, keys...) + } + + return +} diff --git a/internal/model/do/coupon_users.go b/internal/model/do/coupon_users.go index 8aa0d1b..bc39628 100644 --- a/internal/model/do/coupon_users.go +++ b/internal/model/do/coupon_users.go @@ -17,4 +17,5 @@ type CouponUsers struct { CouponId interface{} // 优惠券id Code interface{} // State interface{} // 0 未使用 1 已使用 + Record interface{} // 武器记录 } diff --git a/internal/model/entity/coupon_users.go b/internal/model/entity/coupon_users.go index 8a49df3..19453c3 100644 --- a/internal/model/entity/coupon_users.go +++ b/internal/model/entity/coupon_users.go @@ -12,4 +12,5 @@ type CouponUsers struct { CouponId int `json:"couponId" orm:"coupon_id" description:"优惠券id"` // 优惠券id Code string `json:"code" orm:"code" description:""` // State int `json:"state" orm:"state" description:"0 未使用 1 已使用"` // 0 未使用 1 已使用 + Record string `json:"record" orm:"record" description:"武器记录"` // 武器记录 } diff --git a/internal/service/coupon.go b/internal/service/coupon.go index 7e29480..86948c7 100644 --- a/internal/service/coupon.go +++ b/internal/service/coupon.go @@ -15,6 +15,7 @@ type ( GetCouponList(ctx context.Context, pageNo int, pageSize int) (couponList []*dto.DtoCoupon, err error) GetCouponListTotal(ctx context.Context) (total int, err error) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) + DeleteCoupon(ctx context.Context, id int) (err error) } ) From 8894230c068529d0528d12e7e6f1dbeea3386512 Mon Sep 17 00:00:00 2001 From: majun <3060162534@qq.com> Date: Sat, 28 Dec 2024 13:26:02 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E7=BC=96=E5=86=99=E6=96=B0=E5=BB=BA?= =?UTF-8?q?=E5=8D=A1=E5=88=B8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/coupon/coupon.go | 9 +++++++++ internal/cmd/cmd.go | 1 + internal/controller/coupon/coupon.go | 21 +++++++++++++++++++++ internal/logic/coupon/coupon.go | 25 +++++++++++++++++++++++++ internal/service/coupon.go | 2 ++ 5 files changed, 58 insertions(+) diff --git a/api/v1/coupon/coupon.go b/api/v1/coupon/coupon.go index 2ad07f3..9530ce3 100644 --- a/api/v1/coupon/coupon.go +++ b/api/v1/coupon/coupon.go @@ -1,5 +1,7 @@ package coupon +import "github.com/gogf/gf/v2/os/gtime" + type GetCouponListReq struct { PageNo int `v:"required#页码不能为空" dc:"页码"` PageSize int `v:"required#页面大小不能为空" dc:"页面大小"` @@ -12,3 +14,10 @@ type GetCouponReq struct { type DeleteCouponReq struct { Id int `v:"required#优惠券id不能为空" dc:"优惠券id"` } + +type InsertCouponReq struct { + Title string `v:"required#优惠券名字不能为空" dc:"优惠券名字"` + Image string `v:"required#优惠券图片不能为空" dc:"优惠券图片"` + StartTime *gtime.Time `v:"required#优惠券生效时间不能为空" dc:"生效时间"` + EndTime *gtime.Time `v:"required#优惠券失效时间不能为空" dc:"失效时间"` +} diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 2a0b422..6472e48 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -20,6 +20,7 @@ var ( group.POST("/get-coupon-list", coupon.Coupon().GetCouponList) group.POST("/get-coupon", coupon.Coupon().GetCoupon) group.POST("/delete-coupon", coupon.Coupon().DeleteCoupon) + group.POST("/insert-coupon", coupon.Coupon().InsertCoupon) }) s.Run() return nil diff --git a/internal/controller/coupon/coupon.go b/internal/controller/coupon/coupon.go index af7188a..8c544b4 100644 --- a/internal/controller/coupon/coupon.go +++ b/internal/controller/coupon/coupon.go @@ -82,3 +82,24 @@ func (c cCoupon) DeleteCoupon(r *ghttp.Request) { Message: "success", }) } + +func (c cCoupon) InsertCoupon(r *ghttp.Request) { + var req *coupon.InsertCouponReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + err := service.Coupon().InsertCoupon(r.Context(), req) + if err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + r.Response.WriteJsonExit(dto.Result{ + Code: 200, + Message: "success", + }) +} diff --git a/internal/logic/coupon/coupon.go b/internal/logic/coupon/coupon.go index 81807e2..0c480b0 100644 --- a/internal/logic/coupon/coupon.go +++ b/internal/logic/coupon/coupon.go @@ -1,6 +1,7 @@ package coupon import ( + "CouponBackendGo/api/v1/coupon" "CouponBackendGo/internal/dao" "CouponBackendGo/internal/model/do" "CouponBackendGo/internal/model/dto" @@ -112,6 +113,9 @@ func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ Deleted: 1, }).WherePri(id).Update() + if err != nil { + return err + } // 软删除数据后,删除所有缓存 keys, err := g.Redis().Keys(ctx, "*-* couponlist") @@ -121,3 +125,24 @@ func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { return } + +func (s *sCoupon) InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) (err error) { + _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ + Title: req.Title, + Cover: req.Image, + ImgUrl: req.Image, + StartTime: req.StartTime.Unix(), + EndTime: req.EndTime.Unix(), + }).Insert() + if err != nil { + return err + } + + // 新增数据后,删除所有缓存 + keys, err := g.Redis().Keys(ctx, "*-* couponlist") + if len(keys) > 0 { + _, err = g.Redis().Del(ctx, keys...) + } + + return +} diff --git a/internal/service/coupon.go b/internal/service/coupon.go index 86948c7..7c266bd 100644 --- a/internal/service/coupon.go +++ b/internal/service/coupon.go @@ -6,6 +6,7 @@ package service import ( + "CouponBackendGo/api/v1/coupon" "CouponBackendGo/internal/model/dto" "context" ) @@ -16,6 +17,7 @@ type ( GetCouponListTotal(ctx context.Context) (total int, err error) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) DeleteCoupon(ctx context.Context, id int) (err error) + InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) (err error) } ) From e8d40bfb48f5064d2ff00429c8ed08c308ae8b7f Mon Sep 17 00:00:00 2001 From: majun <3060162534@qq.com> Date: Sat, 28 Dec 2024 14:03:20 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=BC=96=E5=86=99=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=8D=A1=E5=88=B8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/coupon/coupon.go | 8 ++++++++ internal/cmd/cmd.go | 1 + 2 files changed, 9 insertions(+) diff --git a/api/v1/coupon/coupon.go b/api/v1/coupon/coupon.go index 9530ce3..4904a0e 100644 --- a/api/v1/coupon/coupon.go +++ b/api/v1/coupon/coupon.go @@ -21,3 +21,11 @@ type InsertCouponReq struct { StartTime *gtime.Time `v:"required#优惠券生效时间不能为空" dc:"生效时间"` EndTime *gtime.Time `v:"required#优惠券失效时间不能为空" dc:"失效时间"` } + +type UpdateCouponReq struct { + Id int `v:"required#优惠券id不能为空" dc:"优惠券id"` + Title string `v:"required#优惠券名字不能为空" dc:"优惠券名字"` + Image string `v:"required#优惠券图片不能为空" dc:"优惠券图片"` + StartTime *gtime.Time `v:"required#优惠券生效时间不能为空" dc:"生效时间"` + EndTime *gtime.Time `v:"required#优惠券失效时间不能为空" dc:"失效时间"` +} diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 6472e48..e8da64b 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -21,6 +21,7 @@ var ( group.POST("/get-coupon", coupon.Coupon().GetCoupon) group.POST("/delete-coupon", coupon.Coupon().DeleteCoupon) group.POST("/insert-coupon", coupon.Coupon().InsertCoupon) + group.POST("/update-coupon", coupon.Coupon().UpdateCoupon) }) s.Run() return nil From 28b1d81f189f7c7a4c5e8c457ffc553708a4810d Mon Sep 17 00:00:00 2001 From: majun <3060162534@qq.com> Date: Sat, 28 Dec 2024 14:52:55 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=8D=A1=E5=88=B8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/coupon/coupon.go | 9 +-------- internal/cmd/cmd.go | 1 - internal/logic/coupon/coupon.go | 26 ++++++++++++++++++-------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/api/v1/coupon/coupon.go b/api/v1/coupon/coupon.go index 4904a0e..fa36d49 100644 --- a/api/v1/coupon/coupon.go +++ b/api/v1/coupon/coupon.go @@ -16,14 +16,7 @@ type DeleteCouponReq struct { } type InsertCouponReq struct { - Title string `v:"required#优惠券名字不能为空" dc:"优惠券名字"` - Image string `v:"required#优惠券图片不能为空" dc:"优惠券图片"` - StartTime *gtime.Time `v:"required#优惠券生效时间不能为空" dc:"生效时间"` - EndTime *gtime.Time `v:"required#优惠券失效时间不能为空" dc:"失效时间"` -} - -type UpdateCouponReq struct { - Id int `v:"required#优惠券id不能为空" dc:"优惠券id"` + Id int `dc:"优惠券id"` Title string `v:"required#优惠券名字不能为空" dc:"优惠券名字"` Image string `v:"required#优惠券图片不能为空" dc:"优惠券图片"` StartTime *gtime.Time `v:"required#优惠券生效时间不能为空" dc:"生效时间"` diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index e8da64b..6472e48 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -21,7 +21,6 @@ var ( group.POST("/get-coupon", coupon.Coupon().GetCoupon) group.POST("/delete-coupon", coupon.Coupon().DeleteCoupon) group.POST("/insert-coupon", coupon.Coupon().InsertCoupon) - group.POST("/update-coupon", coupon.Coupon().UpdateCoupon) }) s.Run() return nil diff --git a/internal/logic/coupon/coupon.go b/internal/logic/coupon/coupon.go index 0c480b0..6d3f1d3 100644 --- a/internal/logic/coupon/coupon.go +++ b/internal/logic/coupon/coupon.go @@ -127,18 +127,28 @@ func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { } func (s *sCoupon) InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) (err error) { - _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ - Title: req.Title, - Cover: req.Image, - ImgUrl: req.Image, - StartTime: req.StartTime.Unix(), - EndTime: req.EndTime.Unix(), - }).Insert() + if req.Id == 0 { + _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ + Title: req.Title, + Cover: req.Image, + ImgUrl: req.Image, + StartTime: req.StartTime.Unix(), + EndTime: req.EndTime.Unix(), + }).Insert() + } else { + _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ + Title: req.Title, + Cover: req.Image, + ImgUrl: req.Image, + StartTime: req.StartTime.Unix(), + EndTime: req.EndTime.Unix(), + }).WherePri(req.Id).Update() + } if err != nil { return err } - // 新增数据后,删除所有缓存 + // 新增&编辑数据后,删除所有缓存 keys, err := g.Redis().Keys(ctx, "*-* couponlist") if len(keys) > 0 { _, err = g.Redis().Del(ctx, keys...)