package top.continew.admin.business.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.InviteRewardsRuleMapper;
import top.continew.admin.business.model.entity.InviteRewardsRule;
import top.continew.admin.business.model.query.InviteRewardsRuleQuery;
import top.continew.admin.business.model.req.InviteRewardsRuleReq;
import top.continew.admin.business.service.IInviteRewardsRuleService;
import top.continew.starter.core.validation.CheckUtils;
import top.continew.starter.extension.crud.model.resp.BasePageResp;
import top.continew.starter.extension.crud.model.resp.PageResp;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 邀请用户金币奖励规则 服务实现类
 * </p>
 *
 * @author xudm
 * @since 2024-12-15
 */
@Service
public class InviteRewardsRuleServiceImpl extends ServiceImpl<InviteRewardsRuleMapper, InviteRewardsRule> implements IInviteRewardsRuleService {

    @Override
    public BasePageResp<InviteRewardsRule> InviteRewardsRulePage(InviteRewardsRuleQuery query) {
        Page<InviteRewardsRule> page = new Page<>();
        page.setCurrent(query.getPage() != 0 ? query.getPage() : 1);
        page.setSize(query.getSize() != 0 ? query.getSize() : 10);

        InviteRewardsRule rule = new InviteRewardsRule();
        BeanUtils.copyProperties(query, rule);
        LambdaQueryWrapper<InviteRewardsRule> queryWrapper = new LambdaQueryWrapper<>();
        if (rule.getRewardsLevel() != 0) {
            queryWrapper.eq(InviteRewardsRule::getRewardsLevel, rule.getRewardsLevel());
        }
        if (rule.getGoldCoinNum() != 0) {
            queryWrapper.eq(InviteRewardsRule::getGoldCoinNum, rule.getGoldCoinNum());
        }
        if (StrUtil.isNotBlank(rule.getRewardsDescribe())) {
            queryWrapper.like(InviteRewardsRule::getRewardsDescribe, rule.getRewardsDescribe());
        }
        if (StrUtil.isNotBlank(rule.getRewardsName())) {
            queryWrapper.like(InviteRewardsRule::getRewardsName, rule.getRewardsName());
        }
        queryWrapper.orderByDesc(InviteRewardsRule::getCreatedTime);
        page = page(page, queryWrapper);
        return PageResp.build(page, InviteRewardsRule.class);
    }

    @Override
    public void delete(List<Long> ids) {
        baseMapper.deleteByIds(ids);
    }

    @Override
    public Long addRulesByLevel(InviteRewardsRuleReq req) {
        Object loginId = StpUtil.getLoginId();
        CheckUtils.throwIfNull(req, "奖励规则请求体为空");
        int maxLevel = getMaxLevel();
        InviteRewardsRule rule = new InviteRewardsRule();
        BeanUtils.copyProperties(req, rule);
        rule.setRewardsLevel(maxLevel + 1);
        rule.setCreatedBy((String) loginId);
        rule.setUpdatedBy((String) loginId);
        rule.setUpdatedTime(LocalDateTime.now());
        rule.setCreatedTime(LocalDateTime.now());
        save(rule);
        return rule.getId();
    }

    @Override
    public void updateRules(Long id, InviteRewardsRuleReq req) {
        Object loginId = StpUtil.getLoginId();
        InviteRewardsRule byId = getById(id);
        CheckUtils.throwIfNull(byId, "查询的规则为空");
        BeanUtils.copyProperties(req, byId, "id");
        byId.setUpdatedBy((String) loginId);
        byId.setUpdatedTime(LocalDateTime.now());
        updateById(byId);
    }

    @Override
    public List<InviteRewardsRule> getByIds(List<Long> ids) {
        return baseMapper.selectByIds(ids);
    }

    @Override
    public int getMaxLevel() {
        return baseMapper.getMaxLevel();
    }
}
