package activity

import (
	"context"
	"time"

	"bindbox-game/internal/repository/mysql/model"
)

// ModifyIssueReward 修改期奖励
// 参数: rewardID 奖励ID, in 修改输入
// 返回: 错误信息
func (s *service) ModifyIssueReward(ctx context.Context, rewardID int64, in ModifyRewardInput) error {
	item, err := s.readDB.ActivityRewardSettings.WithContext(ctx).Where(s.readDB.ActivityRewardSettings.ID.Eq(rewardID)).First()
	if err != nil {
		return err
	}
	if in.ProductID != nil {
		item.ProductID = *in.ProductID
	}
	if in.Weight != nil {
		item.Weight = int32(*in.Weight)
	}
	if in.Quantity != nil {
		item.Quantity = *in.Quantity
	}
	if in.OriginalQty != nil {
		item.OriginalQty = *in.OriginalQty
	}
	if in.Level != nil {
		item.Level = *in.Level
	}
	if in.Sort != nil {
		item.Sort = *in.Sort
	}
	if in.IsBoss != nil {
		item.IsBoss = *in.IsBoss
	}
	if in.MinScore != nil {
		item.MinScore = *in.MinScore
	}
	item.UpdatedAt = time.Now()
	return s.writeDB.ActivityRewardSettings.WithContext(ctx).Save(item)
}

// DeleteIssueReward 删除期奖励
// 参数: rewardID 奖励ID
// 返回: 错误信息
func (s *service) DeleteIssueReward(ctx context.Context, rewardID int64) error {
	_, err := s.writeDB.ActivityRewardSettings.WithContext(ctx).Where(s.writeDB.ActivityRewardSettings.ID.Eq(rewardID)).Delete(&model.ActivityRewardSettings{})
	return err
}
