package steal_rule

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

func (r *sStealRule) Create(ctx context.Context, input *model.MerchantHiddenConfigInput) (err error) {
	_, err = dao.V1MerchantHiddenConfig.Ctx(ctx).DB(config.GetDatabaseV1()).Insert(do.V1MerchantHiddenConfig{
		MerchantUid:      input.MerchantUid,
		RoadUid:          input.RoadUid,
		Amount:           input.Amount,
		FaceAmount:       input.FaceAmount,
		DelayDurationMin: input.DelayDurationMin,
		DelayDurationMax: input.DelayDurationMax,
		Enable:           input.Enable,
		Strategy:         input.Strategy,
		Name:             input.Name,
		AmountRule:       input.AmountRule,
		ExtraReturnInfo:  input.ExtraReturnInfo,
	})
	return
}

func (r *sStealRule) Update(ctx context.Context, id uint, input *model.MerchantHiddenConfigInput) (err error) {
	_, err = dao.V1MerchantHiddenConfig.Ctx(ctx).DB(config.GetDatabaseV1()).Where(dao.V1MerchantHiddenConfig.Columns().Id, id).Update(do.V1MerchantHiddenConfig{
		Amount:           input.Amount,
		FaceAmount:       input.FaceAmount,
		DelayDurationMin: input.DelayDurationMin,
		DelayDurationMax: input.DelayDurationMax,
		Enable:           input.Enable,
		Strategy:         input.Strategy,
		RoadUid:          input.RoadUid,
		MerchantUid:      input.MerchantUid,
		Name:             input.Name,
		AmountRule:       input.AmountRule,
		ExtraReturnInfo:  input.ExtraReturnInfo,
	})
	return
}

// UpdateStatus 更新某条记录的状态
func (r *sStealRule) UpdateStatus(ctx context.Context, id uint, status int) (err error) {
	_, err = dao.V1MerchantHiddenConfig.Ctx(ctx).
		DB(config.GetDatabaseV1()).
		Where(dao.V1MerchantHiddenConfig.Columns().Id, id).Update(do.V1MerchantHiddenConfig{
		Enable: status,
	})
	return
}

// Delete 删除
func (r *sStealRule) Delete(ctx context.Context, id uint) (err error) {
	_, err = dao.V1MerchantHiddenConfig.Ctx(ctx).DB(config.GetDatabaseV1()).Where(dao.V1MerchantHiddenConfig.Columns().Id, id).Delete()
	return
}

func (r *sStealRule) List(ctx context.Context, input *model.MerchantHiddenConfigList) (list []*model.MerchantHiddenConfigOutput, total int, err error) {
	list = make([]*model.MerchantHiddenConfigOutput, 0)
	total = 0
	err = dao.V1MerchantHiddenConfig.Ctx(ctx).DB(config.GetDatabaseV1()).Page(input.Current, input.PageSize).
		OrderDesc(dao.V1MerchantHiddenConfig.Columns().UpdatedAt).
		ScanAndCount(&list, &total, false)
	return
}

func (r *sStealRule) Record(ctx context.Context, input *model.MerchantHiddenRecordList) (list []*entity.V1MerchantHiddenRecord, total int, err error) {
	list = make([]*entity.V1MerchantHiddenRecord, 0)
	total = 0
	err = dao.V1MerchantHiddenRecord.Ctx(ctx).DB(config.GetDatabaseV1()).Page(input.Current, input.PageSize).
		OrderDesc(dao.V1MerchantHiddenRecord.Columns().Id).
		ScanAndCount(&list, &total, false)
	return
}
