Browse Source

编写删、查接口

majun
majun 6 months ago
parent
commit
9713e047bf
  1. 6
      api/v1/coupon/coupon.go
  2. 1
      internal/cmd/cmd.go
  3. 21
      internal/controller/coupon/coupon.go
  4. 2
      internal/dao/internal/coupon_users.go
  5. 86
      internal/logic/coupon/coupon.go
  6. 1
      internal/model/do/coupon_users.go
  7. 1
      internal/model/entity/coupon_users.go
  8. 1
      internal/service/coupon.go

6
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"`
}

1
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

21
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",
})
}

2
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.

86
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
}

1
internal/model/do/coupon_users.go

@ -17,4 +17,5 @@ type CouponUsers struct {
CouponId interface{} // 优惠券id
Code interface{} //
State interface{} // 0 未使用 1 已使用
Record interface{} // 武器记录
}

1
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:"武器记录"` // 武器记录
}

1
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)
}
)

Loading…
Cancel
Save