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.IssueID = field.NewInt64(tableName, "issue_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.Quantity = field.NewInt64(tableName, "quantity")
_activityRewardSettings.OriginalQty = field.NewInt64(tableName, "original_qty")
@ -56,6 +58,8 @@ type activityRewardSettings struct {
UpdatedAt field.Time // 更新时间
IssueID field.Int64 // 期IDactivity_issues.id
ProductID field.Int64 // 奖品对应商品ID实物奖可填
PriceSnapshotCents field.Int64 // 奖品配置时商品价格快照(分)
PriceSnapshotAt field.Time // 奖品价格快照时间
Weight field.Int32 // 抽中权重(越大越易中)
Quantity field.Int64 // 当前可发数量(扣减)
OriginalQty field.Int64 // 初始配置数量
@ -85,6 +89,8 @@ func (a *activityRewardSettings) updateTableName(table string) *activityRewardSe
a.UpdatedAt = field.NewTime(table, "updated_at")
a.IssueID = field.NewInt64(table, "issue_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.Quantity = field.NewInt64(table, "quantity")
a.OriginalQty = field.NewInt64(table, "original_qty")
@ -109,12 +115,14 @@ func (a *activityRewardSettings) GetFieldByName(fieldName string) (field.OrderEx
}
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["created_at"] = a.CreatedAt
a.fieldMap["updated_at"] = a.UpdatedAt
a.fieldMap["issue_id"] = a.IssueID
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["quantity"] = a.Quantity
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.UserID = field.NewInt64(tableName, "user_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.ActivityID = field.NewInt64(tableName, "activity_id")
_userInventory.RewardID = field.NewInt64(tableName, "reward_id")
@ -54,6 +57,9 @@ type userInventory struct {
UpdatedAt field.Time // 更新时间
UserID field.Int64 // 资产归属用户ID
ProductID field.Int64 // 资产对应商品ID实物奖/商品)
ValueCents field.Int64 // 资产价值快照(分)
ValueSource field.Int32 // 价值来源0未知 1奖励快照 2商品回退 3人工修复
ValueSnapshotAt field.Time // 资产价值快照时间
OrderID field.Int64 // 来源订单ID
ActivityID field.Int64 // 来源活动ID
RewardID field.Int64 // 来源奖励IDactivity_reward_settings.id
@ -81,6 +87,9 @@ func (u *userInventory) updateTableName(table string) *userInventory {
u.UpdatedAt = field.NewTime(table, "updated_at")
u.UserID = field.NewInt64(table, "user_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.ActivityID = field.NewInt64(table, "activity_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() {
u.fieldMap = make(map[string]field.Expr, 11)
u.fieldMap = make(map[string]field.Expr, 14)
u.fieldMap["id"] = u.ID
u.fieldMap["created_at"] = u.CreatedAt
u.fieldMap["updated_at"] = u.UpdatedAt
u.fieldMap["user_id"] = u.UserID
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["activity_id"] = u.ActivityID
u.fieldMap["reward_id"] = u.RewardID

View File

@ -19,6 +19,8 @@ type ActivityRewardSettings struct {
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
ProductID int64 `gorm:"column:product_id;comment:奖品对应商品ID实物奖可填" json:"product_id"` // 奖品对应商品ID实物奖可填
PriceSnapshotCents int64 `gorm:"column:price_snapshot_cents;not null;comment:奖品配置时商品价格快照(分)" json:"price_snapshot_cents"` // 奖品配置时商品价格快照(分)
PriceSnapshotAt time.Time `gorm:"column:price_snapshot_at;comment:奖品价格快照时间" json:"price_snapshot_at"` // 奖品价格快照时间
Weight int32 `gorm:"column:weight;not null;comment:抽中权重(越大越易中)" json:"weight"` // 抽中权重(越大越易中)
Quantity int64 `gorm:"column:quantity;not null;comment:当前可发数量(扣减)" json:"quantity"` // 当前可发数量(扣减)
OriginalQty int64 `gorm:"column:original_qty;not null;comment:初始配置数量" json:"original_qty"` // 初始配置数量

View File

@ -17,6 +17,9 @@ type UserInventory struct {
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
ProductID int64 `gorm:"column:product_id;comment:资产对应商品ID实物奖/商品)" json:"product_id"` // 资产对应商品ID实物奖/商品)
ValueCents int64 `gorm:"column:value_cents;not null;comment:资产价值快照(分)" json:"value_cents"` // 资产价值快照(分)
ValueSource int32 `gorm:"column:value_source;not null;comment:价值来源0未知 1奖励快照 2商品回退 3人工修复" json:"value_source"` // 价值来源0未知 1奖励快照 2商品回退 3人工修复
ValueSnapshotAt time.Time `gorm:"column:value_snapshot_at;comment:资产价值快照时间" json:"value_snapshot_at"` // 资产价值快照时间
OrderID int64 `gorm:"column:order_id;comment:来源订单ID" json:"order_id"` // 来源订单ID
ActivityID int64 `gorm:"column:activity_id;comment:来源活动ID" json:"activity_id"` // 来源活动ID
RewardID int64 `gorm:"column:reward_id;comment:来源奖励IDactivity_reward_settings.id" json:"reward_id"` // 来源奖励IDactivity_reward_settings.id

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;