This commit is contained in:
ziming 2025-05-21 10:27:52 +08:00
parent 0dd2d8d3d6
commit 12e8951c4f
10 changed files with 212 additions and 13 deletions

23
gorm.sh
View File

@ -112,9 +112,7 @@ import (
)
type ${table_capitalized}Repo interface {
// Create 创建 ${table_capitalized}
Create(ctx context.Context, req *bo.${table_capitalized}Bo) (*bo.${table_capitalized}Bo, error)
// GetByID 根据 ID 获取 ${table_capitalized}
GetByID(ctx context.Context,id int32) (*bo.${table_capitalized}Bo, error)
}
EOL
@ -133,6 +131,7 @@ import (
"voucher/internal/biz/repo"
"voucher/internal/biz/bo"
"voucher/internal/data"
"gorm.io/gorm"
err2 "voucher/api/err"
)
@ -143,34 +142,34 @@ type ${table_capitalized}RepoImpl struct {
}
// New${table_capitalized}RepoImpl .
func New${table_capitalized}RepoImpl() repo.${table_capitalized}Repo {
return &${table_capitalized}RepoImpl{}
func New${table_capitalized}RepoImpl(db *data.Db) repo.${table_capitalized}Repo {
return &${table_capitalized}RepoImpl{db:db}
}
func (r *${table_capitalized}RepoImpl) DB(ctx context.Context) *gorm.DB {
return p.db.DB(ctx).WithContext(ctx).Model(model.${table_capitalized}{})
func (this *${table_capitalized}RepoImpl) DB(ctx context.Context) *gorm.DB {
return this.db.DB(ctx).WithContext(ctx).Model(model.${table_capitalized}{})
}
func (r *${table_capitalized}RepoImpl) Create(ctx context.Context, req *bo.${table_capitalized}Bo) (*bo.${table_capitalized}Bo, error) {
func (this *${table_capitalized}RepoImpl) Create(ctx context.Context, req *bo.${table_capitalized}Bo) (*bo.${table_capitalized}Bo, error) {
// todo 待实现
return nil, nil
}
// GetByID 根据 ID 获取 ${table_capitalized}
func (r *${table_capitalized}RepoImpl) GetByID(ctx context.Context,id int32) (*bo.${table_capitalized}Bo, error) {
func (this *${table_capitalized}RepoImpl) GetByID(ctx context.Context,id int32) (*bo.${table_capitalized}Bo, error) {
var item model.${table_capitalized}
tx := p.DB(ctx).Where(model.${table_capitalized}{ID: id}).First(&item)
tx := this.DB(ctx).Where(model.${table_capitalized}{ID: id}).First(&item)
if tx.Error != nil {
return nil, fmt.Errorf("b fail %w", tx.Error)
return nil, tx.Error
}
if tx.RowsAffected == 0 {
return nil, err2.ErrorDbNotFound("数据不存在")
return nil, gorm.ErrRecordNotFound
}
return r.ToBo(&item), nil
return this.ToBo(&item), nil
}
EOL

View File

@ -13,7 +13,7 @@ type ProductBo struct {
BatchName string
BatchNo string
MchId string
MiniMum int32
MiniMum int32 // 使用券金额门槛
Channel vo.Channel
AvailableType vo.AvailableType
AvailableDays uint32

View File

@ -0,0 +1,16 @@
package bo
import (
"time"
"voucher/internal/biz/vo"
)
// UseLogBo 领域实体Bo结构字段和模型字段保持一致
type UseLogBo struct {
ID uint64
OrderNo string
Amount int64
Type vo.UseLogType
UseTime *time.Time
CreateTime *time.Time
}

View File

@ -0,0 +1,13 @@
package repo
import (
"context"
"time"
"voucher/internal/biz/bo"
)
type UseLogRepo interface {
Create(ctx context.Context, req *bo.UseLogBo) (*bo.UseLogBo, error)
GetByID(ctx context.Context, id uint64) (*bo.UseLogBo, error)
GetByUseTimeOrder(ctx context.Context, orderNo string, useTime *time.Time) (*bo.UseLogBo, error)
}

View File

@ -0,0 +1,36 @@
package vo
import (
"fmt"
)
type UseLogType uint8
const (
UseLogTypeUsed UseLogType = iota + 1
UseLogTypeRefund
)
var UseLogTypeMap = map[UseLogType]string{
UseLogTypeUsed: "核销",
UseLogTypeRefund: "退款",
}
func (s UseLogType) GetText() string {
if t, ok := UseLogTypeMap[s]; ok {
return t
}
return "未知类型"
}
func (s UseLogType) String() string {
return fmt.Sprintf("%d", s)
}
func (s UseLogType) GetValue() uint8 {
return uint8(s)
}
func (s UseLogType) IsUsed() bool {
return s == UseLogTypeUsed
}

View File

@ -28,6 +28,7 @@ type VoucherBiz struct {
DingMixRepo mixrepos.DingMixRepo
CmbMixRepo mixrepos.CmbMixRepo
KxMixRepo mixrepos.KxMixRepo
UseLogRepo repo.UseLogRepo
}
func NewVoucherBiz(
@ -44,6 +45,7 @@ func NewVoucherBiz(
DingMixRepo mixrepos.DingMixRepo,
CmbMixRepo mixrepos.CmbMixRepo,
KxMixRepo mixrepos.KxMixRepo,
UseLogRepo repo.UseLogRepo,
) *VoucherBiz {
return &VoucherBiz{
bc: bc,
@ -59,6 +61,7 @@ func NewVoucherBiz(
DingMixRepo: DingMixRepo,
CmbMixRepo: CmbMixRepo,
KxMixRepo: KxMixRepo,
UseLogRepo: UseLogRepo,
}
}

View File

@ -2,8 +2,11 @@ package biz
import (
"context"
"errors"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"gorm.io/gorm"
"time"
errPb "voucher/api/err"
"voucher/internal/biz/bo"
"voucher/internal/biz/vo"
@ -30,6 +33,32 @@ func (v *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req *
if err = v.used(ctx, order); err != nil {
return err
}
if req.PlainText.ConsumeInformation.ConsumeTime != "" {
inputFormat := time.RFC3339
useTime, err2 := time.Parse(inputFormat, req.PlainText.ConsumeInformation.ConsumeTime)
if err2 != nil {
return err2
}
useLog, err2 := v.UseLogRepo.GetByUseTimeOrder(ctx, order.OrderNo, &useTime)
if err2 != nil && !errors.Is(err2, gorm.ErrRecordNotFound) {
return err2
}
if useLog == nil {
_, err = v.UseLogRepo.Create(ctx, &bo.UseLogBo{
OrderNo: order.OrderNo,
Amount: req.PlainText.ConsumeInformation.ConsumeAmount,
Type: vo.UseLogTypeUsed,
UseTime: &useTime,
})
if err != nil {
return err
}
}
}
return v.wxToBBUse(ctx, order, req)
} else if req.PlainText.Status.IsExpired() {

View File

@ -0,0 +1,26 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (
"time"
)
const TableNameUseLog = "use_log"
// UseLog mapped from table <user_log>
type UseLog struct {
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
OrderNo string `gorm:"column:order_no;not null" json:"order_no"`
Amount int64 `gorm:"column:amount;not null;comment:核销金额" json:"amount"`
Type uint8 `gorm:"column:type;not null;comment:1:核销 2:退款" json:"type"`
UseTime *time.Time `gorm:"column:use_time;not null" json:"use_time"`
CreateTime *time.Time `gorm:"column:create_time;not null" json:"create_time"`
}
// TableName UseLog's table name
func (*UseLog) TableName() string {
return TableNameUseLog
}

View File

@ -10,4 +10,5 @@ var ProviderRepoImplSet = wire.NewSet(
NewProductRepoImpl,
NewOrderNotifyRepoImpl,
NewWechatNotifyRegisterTagRepoImpl,
NewUseLogRepoImpl,
)

View File

@ -0,0 +1,76 @@
package repoimpl
import (
"context"
"gorm.io/gorm"
"time"
"voucher/internal/biz/bo"
"voucher/internal/biz/repo"
"voucher/internal/data"
"voucher/internal/data/model"
)
// UseLogRepoImpl .
type UseLogRepoImpl struct {
Base[model.UseLog, bo.UseLogBo]
db *data.Db
}
// NewUseLogRepoImpl .
func NewUseLogRepoImpl(db *data.Db) repo.UseLogRepo {
return &UseLogRepoImpl{db: db}
}
func (this *UseLogRepoImpl) DB(ctx context.Context) *gorm.DB {
return this.db.DB(ctx).WithContext(ctx).Model(model.UseLog{})
}
func (this *UseLogRepoImpl) Create(ctx context.Context, req *bo.UseLogBo) (*bo.UseLogBo, error) {
now := time.Now()
info := &model.UseLog{
OrderNo: req.OrderNo,
Type: req.Type.GetValue(),
Amount: req.Amount,
UseTime: req.UseTime,
CreateTime: &now,
}
if err := this.DB(ctx).Create(info).Error; err != nil {
return nil, err
}
return this.ToBo(info), nil
}
func (this *UseLogRepoImpl) GetByID(ctx context.Context, id uint64) (*bo.UseLogBo, error) {
var item model.UseLog
tx := this.DB(ctx).Where(model.UseLog{ID: id}).First(&item)
if tx.Error != nil {
return nil, tx.Error
}
if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}
return this.ToBo(&item), nil
}
func (this *UseLogRepoImpl) GetByUseTimeOrder(ctx context.Context, orderNo string, useTime *time.Time) (*bo.UseLogBo, error) {
var item model.UseLog
tx := this.DB(ctx).Where(model.UseLog{OrderNo: orderNo, UseTime: useTime}).First(&item)
if tx.Error != nil {
return nil, tx.Error
}
if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}
return this.ToBo(&item), nil
}