feat(prize): add reward/inventory value snapshot schema

This commit is contained in:
win 2026-02-21 22:16:10 +08:00
parent 7e8a2ebb52
commit 70e45b09ab
5 changed files with 107 additions and 52 deletions

View File

@ -32,6 +32,8 @@ func newActivityRewardSettings(db *gorm.DB, opts ...gen.DOOption) activityReward
_activityRewardSettings.UpdatedAt = field.NewTime(tableName, "updated_at") _activityRewardSettings.UpdatedAt = field.NewTime(tableName, "updated_at")
_activityRewardSettings.IssueID = field.NewInt64(tableName, "issue_id") _activityRewardSettings.IssueID = field.NewInt64(tableName, "issue_id")
_activityRewardSettings.ProductID = field.NewInt64(tableName, "product_id") _activityRewardSettings.ProductID = field.NewInt64(tableName, "product_id")
_activityRewardSettings.PriceSnapshotCents = field.NewInt64(tableName, "price_snapshot_cents")
_activityRewardSettings.PriceSnapshotAt = field.NewTime(tableName, "price_snapshot_at")
_activityRewardSettings.Weight = field.NewInt32(tableName, "weight") _activityRewardSettings.Weight = field.NewInt32(tableName, "weight")
_activityRewardSettings.Quantity = field.NewInt64(tableName, "quantity") _activityRewardSettings.Quantity = field.NewInt64(tableName, "quantity")
_activityRewardSettings.OriginalQty = field.NewInt64(tableName, "original_qty") _activityRewardSettings.OriginalQty = field.NewInt64(tableName, "original_qty")
@ -50,20 +52,22 @@ func newActivityRewardSettings(db *gorm.DB, opts ...gen.DOOption) activityReward
type activityRewardSettings struct { type activityRewardSettings struct {
activityRewardSettingsDo activityRewardSettingsDo
ALL field.Asterisk ALL field.Asterisk
ID field.Int64 // 主键ID ID field.Int64 // 主键ID
CreatedAt field.Time // 创建时间 CreatedAt field.Time // 创建时间
UpdatedAt field.Time // 更新时间 UpdatedAt field.Time // 更新时间
IssueID field.Int64 // 期IDactivity_issues.id IssueID field.Int64 // 期IDactivity_issues.id
ProductID field.Int64 // 奖品对应商品ID实物奖可填 ProductID field.Int64 // 奖品对应商品ID实物奖可填
Weight field.Int32 // 抽中权重(越大越易中) PriceSnapshotCents field.Int64 // 奖品配置时商品价格快照(分)
Quantity field.Int64 // 当前可发数量(扣减) PriceSnapshotAt field.Time // 奖品价格快照时间
OriginalQty field.Int64 // 初始配置数量 Weight field.Int32 // 抽中权重(越大越易中)
Level field.Int32 // 奖级如1=S 2=A 3=B Quantity field.Int64 // 当前可发数量(扣减)
Sort field.Int32 // 排序 OriginalQty field.Int64 // 初始配置数量
IsBoss field.Int32 // Boss 1 是 0 不是 Level field.Int32 // 奖级如1=S 2=A 3=B
DeletedAt field.Field Sort field.Int32 // 排序
MinScore field.Int64 // 最低得分/碰数要求 IsBoss field.Int32 // Boss 1 是 0 不是
DeletedAt field.Field
MinScore field.Int64 // 最低得分/碰数要求
fieldMap map[string]field.Expr fieldMap map[string]field.Expr
} }
@ -85,6 +89,8 @@ func (a *activityRewardSettings) updateTableName(table string) *activityRewardSe
a.UpdatedAt = field.NewTime(table, "updated_at") a.UpdatedAt = field.NewTime(table, "updated_at")
a.IssueID = field.NewInt64(table, "issue_id") a.IssueID = field.NewInt64(table, "issue_id")
a.ProductID = field.NewInt64(table, "product_id") a.ProductID = field.NewInt64(table, "product_id")
a.PriceSnapshotCents = field.NewInt64(table, "price_snapshot_cents")
a.PriceSnapshotAt = field.NewTime(table, "price_snapshot_at")
a.Weight = field.NewInt32(table, "weight") a.Weight = field.NewInt32(table, "weight")
a.Quantity = field.NewInt64(table, "quantity") a.Quantity = field.NewInt64(table, "quantity")
a.OriginalQty = field.NewInt64(table, "original_qty") a.OriginalQty = field.NewInt64(table, "original_qty")
@ -109,12 +115,14 @@ func (a *activityRewardSettings) GetFieldByName(fieldName string) (field.OrderEx
} }
func (a *activityRewardSettings) fillFieldMap() { func (a *activityRewardSettings) fillFieldMap() {
a.fieldMap = make(map[string]field.Expr, 13) a.fieldMap = make(map[string]field.Expr, 15)
a.fieldMap["id"] = a.ID a.fieldMap["id"] = a.ID
a.fieldMap["created_at"] = a.CreatedAt a.fieldMap["created_at"] = a.CreatedAt
a.fieldMap["updated_at"] = a.UpdatedAt a.fieldMap["updated_at"] = a.UpdatedAt
a.fieldMap["issue_id"] = a.IssueID a.fieldMap["issue_id"] = a.IssueID
a.fieldMap["product_id"] = a.ProductID a.fieldMap["product_id"] = a.ProductID
a.fieldMap["price_snapshot_cents"] = a.PriceSnapshotCents
a.fieldMap["price_snapshot_at"] = a.PriceSnapshotAt
a.fieldMap["weight"] = a.Weight a.fieldMap["weight"] = a.Weight
a.fieldMap["quantity"] = a.Quantity a.fieldMap["quantity"] = a.Quantity
a.fieldMap["original_qty"] = a.OriginalQty a.fieldMap["original_qty"] = a.OriginalQty

View File

@ -32,6 +32,9 @@ func newUserInventory(db *gorm.DB, opts ...gen.DOOption) userInventory {
_userInventory.UpdatedAt = field.NewTime(tableName, "updated_at") _userInventory.UpdatedAt = field.NewTime(tableName, "updated_at")
_userInventory.UserID = field.NewInt64(tableName, "user_id") _userInventory.UserID = field.NewInt64(tableName, "user_id")
_userInventory.ProductID = field.NewInt64(tableName, "product_id") _userInventory.ProductID = field.NewInt64(tableName, "product_id")
_userInventory.ValueCents = field.NewInt64(tableName, "value_cents")
_userInventory.ValueSource = field.NewInt32(tableName, "value_source")
_userInventory.ValueSnapshotAt = field.NewTime(tableName, "value_snapshot_at")
_userInventory.OrderID = field.NewInt64(tableName, "order_id") _userInventory.OrderID = field.NewInt64(tableName, "order_id")
_userInventory.ActivityID = field.NewInt64(tableName, "activity_id") _userInventory.ActivityID = field.NewInt64(tableName, "activity_id")
_userInventory.RewardID = field.NewInt64(tableName, "reward_id") _userInventory.RewardID = field.NewInt64(tableName, "reward_id")
@ -48,18 +51,21 @@ func newUserInventory(db *gorm.DB, opts ...gen.DOOption) userInventory {
type userInventory struct { type userInventory struct {
userInventoryDo userInventoryDo
ALL field.Asterisk ALL field.Asterisk
ID field.Int64 // 主键ID ID field.Int64 // 主键ID
CreatedAt field.Time // 创建时间 CreatedAt field.Time // 创建时间
UpdatedAt field.Time // 更新时间 UpdatedAt field.Time // 更新时间
UserID field.Int64 // 资产归属用户ID UserID field.Int64 // 资产归属用户ID
ProductID field.Int64 // 资产对应商品ID实物奖/商品) ProductID field.Int64 // 资产对应商品ID实物奖/商品)
OrderID field.Int64 // 来源订单ID ValueCents field.Int64 // 资产价值快照(分)
ActivityID field.Int64 // 来源活动ID ValueSource field.Int32 // 价值来源0未知 1奖励快照 2商品回退 3人工修复
RewardID field.Int64 // 来源奖励IDactivity_reward_settings.id ValueSnapshotAt field.Time // 资产价值快照时间
Status field.Int32 // 状态1持有 2作废 3已使用/发货 OrderID field.Int64 // 来源订单ID
ShippingNo field.String // 发货单号 ActivityID field.Int64 // 来源活动ID
Remark field.String // 备注 RewardID field.Int64 // 来源奖励IDactivity_reward_settings.id
Status field.Int32 // 状态1持有 2作废 3已使用/发货
ShippingNo field.String // 发货单号
Remark field.String // 备注
fieldMap map[string]field.Expr fieldMap map[string]field.Expr
} }
@ -81,6 +87,9 @@ func (u *userInventory) updateTableName(table string) *userInventory {
u.UpdatedAt = field.NewTime(table, "updated_at") u.UpdatedAt = field.NewTime(table, "updated_at")
u.UserID = field.NewInt64(table, "user_id") u.UserID = field.NewInt64(table, "user_id")
u.ProductID = field.NewInt64(table, "product_id") u.ProductID = field.NewInt64(table, "product_id")
u.ValueCents = field.NewInt64(table, "value_cents")
u.ValueSource = field.NewInt32(table, "value_source")
u.ValueSnapshotAt = field.NewTime(table, "value_snapshot_at")
u.OrderID = field.NewInt64(table, "order_id") u.OrderID = field.NewInt64(table, "order_id")
u.ActivityID = field.NewInt64(table, "activity_id") u.ActivityID = field.NewInt64(table, "activity_id")
u.RewardID = field.NewInt64(table, "reward_id") u.RewardID = field.NewInt64(table, "reward_id")
@ -103,12 +112,15 @@ func (u *userInventory) GetFieldByName(fieldName string) (field.OrderExpr, bool)
} }
func (u *userInventory) fillFieldMap() { func (u *userInventory) fillFieldMap() {
u.fieldMap = make(map[string]field.Expr, 11) u.fieldMap = make(map[string]field.Expr, 14)
u.fieldMap["id"] = u.ID u.fieldMap["id"] = u.ID
u.fieldMap["created_at"] = u.CreatedAt u.fieldMap["created_at"] = u.CreatedAt
u.fieldMap["updated_at"] = u.UpdatedAt u.fieldMap["updated_at"] = u.UpdatedAt
u.fieldMap["user_id"] = u.UserID u.fieldMap["user_id"] = u.UserID
u.fieldMap["product_id"] = u.ProductID u.fieldMap["product_id"] = u.ProductID
u.fieldMap["value_cents"] = u.ValueCents
u.fieldMap["value_source"] = u.ValueSource
u.fieldMap["value_snapshot_at"] = u.ValueSnapshotAt
u.fieldMap["order_id"] = u.OrderID u.fieldMap["order_id"] = u.OrderID
u.fieldMap["activity_id"] = u.ActivityID u.fieldMap["activity_id"] = u.ActivityID
u.fieldMap["reward_id"] = u.RewardID u.fieldMap["reward_id"] = u.RewardID

View File

@ -14,19 +14,21 @@ const TableNameActivityRewardSettings = "activity_reward_settings"
// ActivityRewardSettings 活动期-奖励配置 // ActivityRewardSettings 活动期-奖励配置
type ActivityRewardSettings struct { type ActivityRewardSettings struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键ID" json:"id"` // 主键ID ID int64 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键ID" json:"id"` // 主键ID
CreatedAt time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP(3);comment:创建时间" json:"created_at"` // 创建时间 CreatedAt time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP(3);comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt time.Time `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP(3);comment:更新时间" json:"updated_at"` // 更新时间 UpdatedAt time.Time `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP(3);comment:更新时间" json:"updated_at"` // 更新时间
IssueID int64 `gorm:"column:issue_id;not null;comment:期IDactivity_issues.id" json:"issue_id"` // 期IDactivity_issues.id IssueID int64 `gorm:"column:issue_id;not null;comment:期IDactivity_issues.id" json:"issue_id"` // 期IDactivity_issues.id
ProductID int64 `gorm:"column:product_id;comment:奖品对应商品ID实物奖可填" json:"product_id"` // 奖品对应商品ID实物奖可填 ProductID int64 `gorm:"column:product_id;comment:奖品对应商品ID实物奖可填" json:"product_id"` // 奖品对应商品ID实物奖可填
Weight int32 `gorm:"column:weight;not null;comment:抽中权重(越大越易中)" json:"weight"` // 抽中权重(越大越易中) PriceSnapshotCents int64 `gorm:"column:price_snapshot_cents;not null;comment:奖品配置时商品价格快照(分)" json:"price_snapshot_cents"` // 奖品配置时商品价格快照(分)
Quantity int64 `gorm:"column:quantity;not null;comment:当前可发数量(扣减)" json:"quantity"` // 当前可发数量(扣减) PriceSnapshotAt time.Time `gorm:"column:price_snapshot_at;comment:奖品价格快照时间" json:"price_snapshot_at"` // 奖品价格快照时间
OriginalQty int64 `gorm:"column:original_qty;not null;comment:初始配置数量" json:"original_qty"` // 初始配置数量 Weight int32 `gorm:"column:weight;not null;comment:抽中权重(越大越易中)" json:"weight"` // 抽中权重(越大越易中)
Level int32 `gorm:"column:level;not null;comment:奖级如1=S 2=A 3=B" json:"level"` // 奖级如1=S 2=A 3=B Quantity int64 `gorm:"column:quantity;not null;comment:当前可发数量(扣减)" json:"quantity"` // 当前可发数量(扣减)
Sort int32 `gorm:"column:sort;comment:排序" json:"sort"` // 排序 OriginalQty int64 `gorm:"column:original_qty;not null;comment:初始配置数量" json:"original_qty"` // 初始配置数量
IsBoss int32 `gorm:"column:is_boss;comment:Boss 1 是 0 不是" json:"is_boss"` // Boss 1 是 0 不是 Level int32 `gorm:"column:level;not null;comment:奖级如1=S 2=A 3=B" json:"level"` // 奖级如1=S 2=A 3=B
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` Sort int32 `gorm:"column:sort;comment:排序" json:"sort"` // 排序
MinScore int64 `gorm:"column:min_score;not null;comment:最低得分/碰数要求" json:"min_score"` // 最低得分/碰数要求 IsBoss int32 `gorm:"column:is_boss;comment:Boss 1 是 0 不是" json:"is_boss"` // Boss 1 是 0 不是
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"`
MinScore int64 `gorm:"column:min_score;not null;comment:最低得分/碰数要求" json:"min_score"` // 最低得分/碰数要求
} }
// TableName ActivityRewardSettings's table name // TableName ActivityRewardSettings's table name

View File

@ -12,17 +12,20 @@ const TableNameUserInventory = "user_inventory"
// UserInventory 用户资产(资产) // UserInventory 用户资产(资产)
type UserInventory struct { type UserInventory struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键ID" json:"id"` // 主键ID ID int64 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键ID" json:"id"` // 主键ID
CreatedAt time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP(3);comment:创建时间" json:"created_at"` // 创建时间 CreatedAt time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP(3);comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt time.Time `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP(3);comment:更新时间" json:"updated_at"` // 更新时间 UpdatedAt time.Time `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP(3);comment:更新时间" json:"updated_at"` // 更新时间
UserID int64 `gorm:"column:user_id;not null;comment:资产归属用户ID" json:"user_id"` // 资产归属用户ID UserID int64 `gorm:"column:user_id;not null;comment:资产归属用户ID" json:"user_id"` // 资产归属用户ID
ProductID int64 `gorm:"column:product_id;comment:资产对应商品ID实物奖/商品)" json:"product_id"` // 资产对应商品ID实物奖/商品) ProductID int64 `gorm:"column:product_id;comment:资产对应商品ID实物奖/商品)" json:"product_id"` // 资产对应商品ID实物奖/商品)
OrderID int64 `gorm:"column:order_id;comment:来源订单ID" json:"order_id"` // 来源订单ID ValueCents int64 `gorm:"column:value_cents;not null;comment:资产价值快照(分)" json:"value_cents"` // 资产价值快照(分)
ActivityID int64 `gorm:"column:activity_id;comment:来源活动ID" json:"activity_id"` // 来源活动ID ValueSource int32 `gorm:"column:value_source;not null;comment:价值来源0未知 1奖励快照 2商品回退 3人工修复" json:"value_source"` // 价值来源0未知 1奖励快照 2商品回退 3人工修复
RewardID int64 `gorm:"column:reward_id;comment:来源奖励IDactivity_reward_settings.id" json:"reward_id"` // 来源奖励IDactivity_reward_settings.id ValueSnapshotAt time.Time `gorm:"column:value_snapshot_at;comment:资产价值快照时间" json:"value_snapshot_at"` // 资产价值快照时间
Status int32 `gorm:"column:status;not null;default:1;comment:状态1持有 2作废 3已使用/发货" json:"status"` // 状态1持有 2作废 3已使用/发货 OrderID int64 `gorm:"column:order_id;comment:来源订单ID" json:"order_id"` // 来源订单ID
ShippingNo string `gorm:"column:shipping_no;not null;comment:发货单号" json:"shipping_no"` // 发货单号 ActivityID int64 `gorm:"column:activity_id;comment:来源活动ID" json:"activity_id"` // 来源活动ID
Remark string `gorm:"column:remark;comment:备注" json:"remark"` // 备注 RewardID int64 `gorm:"column:reward_id;comment:来源奖励IDactivity_reward_settings.id" json:"reward_id"` // 来源奖励IDactivity_reward_settings.id
Status int32 `gorm:"column:status;not null;default:1;comment:状态1持有 2作废 3已使用/发货" json:"status"` // 状态1持有 2作废 3已使用/发货
ShippingNo string `gorm:"column:shipping_no;not null;comment:发货单号" json:"shipping_no"` // 发货单号
Remark string `gorm:"column:remark;comment:备注" json:"remark"` // 备注
} }
// TableName UserInventory's table name // TableName UserInventory's table name

View File

@ -0,0 +1,30 @@
ALTER TABLE `activity_reward_settings`
ADD COLUMN `price_snapshot_cents` BIGINT NOT NULL DEFAULT 0 COMMENT '奖品配置时商品价格快照(分)' AFTER `product_id`,
ADD COLUMN `price_snapshot_at` DATETIME(3) NULL COMMENT '奖品价格快照时间' AFTER `price_snapshot_cents`;
ALTER TABLE `user_inventory`
ADD COLUMN `value_cents` BIGINT NOT NULL DEFAULT 0 COMMENT '资产价值快照(分)' AFTER `product_id`,
ADD COLUMN `value_source` TINYINT NOT NULL DEFAULT 0 COMMENT '价值来源0未知 1奖励快照 2商品回退 3人工修复' AFTER `value_cents`,
ADD COLUMN `value_snapshot_at` DATETIME(3) NULL COMMENT '资产价值快照时间' AFTER `value_source`;
UPDATE `activity_reward_settings` ars
LEFT JOIN `products` p ON p.id = ars.product_id
SET ars.price_snapshot_cents = COALESCE(p.price, 0),
ars.price_snapshot_at = NOW(3)
WHERE ars.price_snapshot_cents = 0;
UPDATE `user_inventory` ui
LEFT JOIN `activity_reward_settings` ars ON ars.id = ui.reward_id
SET ui.value_cents = ars.price_snapshot_cents,
ui.value_source = 1,
ui.value_snapshot_at = COALESCE(ars.price_snapshot_at, NOW(3))
WHERE ui.value_cents = 0
AND ui.reward_id > 0
AND ars.price_snapshot_cents > 0;
UPDATE `user_inventory` ui
LEFT JOIN `products` p ON p.id = ui.product_id
SET ui.value_cents = COALESCE(p.price, 0),
ui.value_source = 2,
ui.value_snapshot_at = NOW(3)
WHERE ui.value_cents = 0;