package sign_in_reward_details

import (
	"context"
	"server/internal/dao"
	"server/internal/model"
	"server/internal/model/do"
	"server/internal/service"
	"server/utility/ecode"
)

type sSignInRewardDetails struct{}

func init() {
	service.RegisterSignInRewardDetails(New())
}

func New() *sSignInRewardDetails {
	return &sSignInRewardDetails{}
}

// Create 新增签到奖励明细
func (s *sSignInRewardDetails) Create(ctx context.Context, in *model.SignInRewardDetail) (int64, error) {
	id, err := dao.SignInRewardDetails.Ctx(ctx).Data(do.SignInRewardDetails{
		RuleId:     in.RuleId,
		DayNumber:  in.DayNumber,
		RewardType: in.RewardType,
		Quantity:   in.Quantity,
		Status:     in.Status,
	}).InsertAndGetId()
	if err != nil {
		return 0, ecode.New(10001, "sign_in_reward_detail_create_failed")
	}
	return id, nil
}

// Update 编辑签到奖励明细
func (s *sSignInRewardDetails) Update(ctx context.Context, in *model.SignInRewardDetail) error {
	_, err := dao.SignInRewardDetails.Ctx(ctx).Data(do.SignInRewardDetails{
		RuleId:     in.RuleId,
		DayNumber:  in.DayNumber,
		RewardType: in.RewardType,
		Quantity:   in.Quantity,
		Status:     in.Status,
	}).Where(do.SignInRewardDetails{Id: in.Id}).Update()
	if err != nil {
		return ecode.New(10002, "sign_in_reward_detail_update_failed")
	}
	return nil
}

// Delete 删除签到奖励明细
func (s *sSignInRewardDetails) Delete(ctx context.Context, in *model.SignInRewardDetailDeleteIn) (*model.SignInRewardDetailDeleteOut, error) {
	_, err := dao.SignInRewardDetails.Ctx(ctx).Where(do.SignInRewardDetails{Id: in.Id}).Delete()
	if err != nil {
		return &model.SignInRewardDetailDeleteOut{Success: false}, ecode.New(10003, "sign_in_reward_detail_delete_failed")
	}
	return &model.SignInRewardDetailDeleteOut{Success: true}, nil
}

// Get 查询单个签到奖励明细
func (s *sSignInRewardDetails) Get(ctx context.Context, in *model.SignInRewardDetailGetIn) (*model.SignInRewardDetailGetOut, error) {
	var detail model.SignInRewardDetail
	err := dao.SignInRewardDetails.Ctx(ctx).Where(do.SignInRewardDetails{Id: in.Id}).Scan(&detail)
	if err != nil {
		return nil, ecode.New(10004, "sign_in_reward_detail_query_failed")
	}
	if detail.Id == 0 {
		return nil, ecode.New(10006, "sign_in_reward_detail_not_found")
	}
	return &model.SignInRewardDetailGetOut{SignInRewardDetail: detail}, nil
}

// List 根据 ruleId 查询签到奖励明细列表
func (s *sSignInRewardDetails) List(ctx context.Context, in *model.SignInRewardDetailListIn) (*model.SignInRewardDetailListOut, error) {
	var list []model.SignInRewardDetail
	err := dao.SignInRewardDetails.Ctx(ctx).Where(do.SignInRewardDetails{RuleId: in.RuleId}).Order("day_number asc").Scan(&list)
	if err != nil {
		return nil, ecode.New(10005, "sign_in_reward_detail_query_failed")
	}
	return &model.SignInRewardDetailListOut{List: list}, nil
}

// SetStatus 设置签到奖励明细状态
func (s *sSignInRewardDetails) SetStatus(ctx context.Context, in *model.SignInRewardDetailSetStatusIn) (*model.SignInRewardDetailSetStatusOut, error) {
	_, err := dao.SignInRewardDetails.Ctx(ctx).Data(do.SignInRewardDetails{
		Status: in.Status,
	}).Where(do.SignInRewardDetails{Id: in.Id}).Update()
	if err != nil {
		return &model.SignInRewardDetailSetStatusOut{Success: false}, ecode.New(10007, "sign_in_reward_detail_update_failed")
	}
	return &model.SignInRewardDetailSetStatusOut{Success: true}, nil
}
