Skip to content

Commit

Permalink
Merge pull request #28 from Seelly/master
Browse files Browse the repository at this point in the history
feature(voucher):seckillvocuher
  • Loading branch information
Seelly authored Sep 4, 2024
2 parents e909fed + 557f6e4 commit 804f640
Show file tree
Hide file tree
Showing 21 changed files with 748 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .hz
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by hz. DO NOT EDIT.

hz version: v0.9.0
hz version: v0.9.1
handlerDir: ""
modelDir: ""
routerDir: ""
31 changes: 28 additions & 3 deletions biz/dal/mysql/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,41 @@ package mysql

import (
"context"
"errors"
"gorm.io/gorm"
"xzdp/biz/model/voucher"
)

func QueryVoucherByID(ctx context.Context, id int64) ([]*voucher.Voucher, error) {
var voucherList []*voucher.Voucher
func QueryShopVoucherByShopID(ctx context.Context, id int64) ([]*voucher.SeckillVoucher, error) {
var voucherList []*voucher.SeckillVoucher

err = DB.WithContext(ctx).Where("shop_id = ?", id).Find(&voucherList).Error
err = DB.WithContext(ctx).Raw("select v.*,s.stock,s.begin_time,s.end_time from tb_voucher v left join tb_seckill_voucher s on s.voucher_id = v.id where v.shop_id = ?", id).
Find(&voucherList).Error
if err != nil {
return nil, err
}

return voucherList, nil
}

func QueryVoucherByID(ctx context.Context, id int64) (*voucher.SeckillVoucher, error) {
var seckillVoucher voucher.SeckillVoucher
err = DB.WithContext(ctx).Where("voucher_id = ?", id).Order("create_time desc").Limit(1).Find(&seckillVoucher).Error
return &seckillVoucher, err
}

func QueryVoucherOrderByVoucherID(ctx context.Context, userId int64, id int64) (*voucher.VoucherOrder, error) {
var voucherOrder voucher.VoucherOrder
if !errors.Is(DB.WithContext(ctx).Where("voucher_id = ? and user_id=?", id, userId).Limit(1).Find(&voucherOrder).Error, gorm.ErrRecordNotFound) {
return nil, errors.New("重复下单")
}
return &voucherOrder, nil
}

func UpdateVoucherStock(ctx context.Context, id int64) error {
return DB.WithContext(ctx).Model(&voucher.SeckillVoucher{}).Where("voucher_id = ?", id).Update("stock", gorm.Expr("stock - ?", 1)).Error
}

func CreateVoucherOrder(ctx context.Context, voucherOrder *voucher.VoucherOrder) error {
return DB.WithContext(ctx).Create(voucherOrder).Error
}
9 changes: 5 additions & 4 deletions biz/dal/redis/id_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
)

func NextId(ctx context.Context, prefix string) (int64, error) {
beginTimeStamp := int64(constants.BEGIN_TIMESTAMP)
now := time.Now().Unix()
dateKey := time.Now().Format("2006-01-02")

count, err := RedisClient.Incr(ctx, constants.ID_KEY+prefix+":"+dateKey).Result()
timeStamp := now - beginTimeStamp
dateKey := time.Unix(now, 0).Format("2006-01-02")
count, err := RedisClient.Incr(ctx, constants.ICRID_KEY+prefix+":"+dateKey).Result()
if err != nil {
return 0, err
}
return (now << 32) | count, nil
return (timeStamp << constants.COUNT_BIT) | count, nil
}
23 changes: 23 additions & 0 deletions biz/handler/voucher/voucher_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package voucher

import (
"context"
"errors"
"strconv"

"github.com/cloudwego/hertz/pkg/app"
Expand Down Expand Up @@ -36,3 +37,25 @@ func VoucherList(ctx context.Context, c *app.RequestContext) {

utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// SeckillVoucher .
// @router /voucher-order/seckill/:id [POST]
func SeckillVoucher(ctx context.Context, c *app.RequestContext) {
var err error
voucherString := c.Param("id")
if voucherString == "" {
utils.SendErrResponse(ctx, c, consts.StatusOK, errors.New("参数错误"))
return
}
voucherID, err := strconv.ParseInt(voucherString, 10, 64)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}
resp, err := service.NewSeckillVoucherService(ctx, c).Run(&voucherID)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}
utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}
2 changes: 1 addition & 1 deletion biz/model/blog/blog.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion biz/model/blog_comment/blog_comment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion biz/model/follow/follow.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion biz/model/image/image.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion biz/model/message/message.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion biz/model/shop/shop.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion biz/model/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 804f640

Please sign in to comment.