package card_apple_order

import (
	"context"
	"kami/internal/consts"
	"kami/internal/dao"
	"kami/internal/model"
	"kami/internal/model/do"
	"kami/internal/model/entity"
	"kami/utility/config"
)

// AddStealRule 添加一条偷卡规则
func (h *sAppleOrder) AddStealRule(ctx context.Context, input *model.StealRuleAddInput) (err error) {
	_, err = dao.V1CardAppleHiddenSettings.Ctx(ctx).DB(config.GetDatabaseV1()).
		Insert(do.V1CardAppleHiddenSettings{
			Name:          input.Name,
			TargetUserId:  input.TargetUserId,
			StorageUserId: input.StorageUserId,
			Amount:        input.Amount,
			TargetAmount:  input.TargetAmount,
			IntervalTime:  input.IntervalTime,
		})
	return
}

// UpdateStealRule 更新一条偷卡规则
func (h *sAppleOrder) UpdateStealRule(ctx context.Context, input *model.StealRuleUpdateInput) (err error) {
	_, err = dao.V1CardAppleHiddenSettings.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardAppleHiddenSettings.Columns().Id, input.ID).
		Update(do.V1CardAppleHiddenSettings{
			Name:          input.Name,
			TargetUserId:  input.TargetUserId,
			StorageUserId: input.StorageUserId,
			Amount:        input.Amount,
			TargetAmount:  input.TargetAmount,
			IntervalTime:  input.IntervalTime,
		})
	return
}

// DeleteStealRule 删除一条偷卡规则
func (h *sAppleOrder) DeleteStealRule(ctx context.Context, id uint) (err error) {
	_, err = dao.V1CardAppleHiddenSettings.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardAppleHiddenSettings.Columns().Id, id).
		Delete()
	return
}

// GetStealRuleList 获取偷卡规则列表
func (h *sAppleOrder) GetStealRuleList(ctx context.Context, input *model.StealRuleListInput) (total int, list []*entity.V1CardAppleHiddenSettings, err error) {
	err = dao.V1CardAppleHiddenSettings.Ctx(ctx).DB(config.GetDatabaseV1()).
		Page(input.Current, input.PageSize).
		ScanAndCount(&list, &total, false)
	return
}

// SetStealRuleStatus 设置单个状态是否启用
func (h *sAppleOrder) SetStealRuleStatus(ctx context.Context, ruleId uint, status consts.StealStatus) (err error) {
	_, err = dao.V1CardAppleHiddenSettings.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardAppleHiddenSettings.Columns().Id, ruleId).
		Update(do.V1CardAppleHiddenSettings{
			Status: status,
		})
	return
}
