58 lines
1.5 KiB
Go
Executable File

package activity
import (
"context"
"time"
"bindbox-game/internal/repository/mysql/dao"
"bindbox-game/internal/repository/mysql/model"
)
// CreateIssueRewards 批量创建期奖励
// 参数: issueID 期ID, rewards 奖励创建输入数组
// 返回: 错误信息
func (s *service) CreateIssueRewards(ctx context.Context, issueID int64, rewards []CreateRewardInput) error {
return s.writeDB.Transaction(func(tx *dao.Query) error {
productIDs := make(map[int64]struct{})
for _, r := range rewards {
if r.ProductID > 0 {
productIDs[r.ProductID] = struct{}{}
}
}
productPriceMap := make(map[int64]int64)
if len(productIDs) > 0 {
ids := make([]int64, 0, len(productIDs))
for id := range productIDs {
ids = append(ids, id)
}
products, err := tx.Products.WithContext(ctx).Where(tx.Products.ID.In(ids...)).Find()
if err != nil {
return err
}
for _, p := range products {
productPriceMap[p.ID] = p.Price
}
}
for _, r := range rewards {
item := &model.ActivityRewardSettings{
IssueID: issueID,
ProductID: r.ProductID,
PriceSnapshotCents: productPriceMap[r.ProductID],
PriceSnapshotAt: time.Now(),
Weight: r.Weight,
Quantity: r.Quantity,
OriginalQty: r.OriginalQty,
Level: r.Level,
Sort: r.Sort,
IsBoss: r.IsBoss,
MinScore: r.MinScore,
}
if err := tx.ActivityRewardSettings.WithContext(ctx).Create(item); err != nil {
return err
}
}
return nil
})
}