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

import cn.dev33.satoken.stp.StpUtil;
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.CoinSpeedUpgradesRulesMapper;
import top.continew.admin.business.model.entity.CoinSpeedUpgradesRules;
import top.continew.admin.business.model.entity.TaskInfo;
import top.continew.admin.business.model.query.CoinSpeedUpgradesRulesQuery;
import top.continew.admin.business.model.req.CoinSpeedUpgradesRulesReq;
import top.continew.admin.business.service.ICoinSpeedUpgradesRulesService;
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;

/**
 * <p>
 * 金币产出速率升级规则 服务实现类
 * </p>
 *
 * @author xudm
 * @since 2024-12-15
 */
@Service
public class CoinSpeedUpgradesRulesServiceImpl extends ServiceImpl<CoinSpeedUpgradesRulesMapper, CoinSpeedUpgradesRules> implements ICoinSpeedUpgradesRulesService {
    @Override
    public Long addCoinSpeedUpgradesRules(CoinSpeedUpgradesRulesReq req) {
        Object loginId = StpUtil.getLoginId();
        CoinSpeedUpgradesRules rules = new CoinSpeedUpgradesRules();
        BeanUtils.copyProperties(req, rules);
        rules.setCreatedBy((String) loginId);
        rules.setCreatedTime(LocalDateTime.now());
        rules.setUpdatedTime(LocalDateTime.now());
        rules.setUpdatedBy((String) loginId);
        save(rules);
        return rules.getId();
    }

    @Override
    public void updateCoinSpeedUpgradesRules(Long id, CoinSpeedUpgradesRulesReq req) {
        Object loginId = StpUtil.getLoginId();
        CoinSpeedUpgradesRules rules = getById(id);
        CheckUtils.throwIfNull(rules, "推进器数据为空");
        BeanUtils.copyProperties(req, rules);
        rules.setUpdatedBy(String.valueOf(loginId));
        rules.setUpdatedTime(LocalDateTime.now());
        updateById(rules);
    }

    @Override
    public BasePageResp<CoinSpeedUpgradesRules> getCoinSpeedUpgradesRulesPage(CoinSpeedUpgradesRulesQuery query) {
        Page<CoinSpeedUpgradesRules> page = new Page<>();
        page.setCurrent(query.getPage() != 0 ? query.getPage() : 1);
        page.setSize(query.getSize() != 0 ? query.getSize() : 10);
        LambdaQueryWrapper<CoinSpeedUpgradesRules> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(CoinSpeedUpgradesRules::getLevel);
        page = page(page, queryWrapper);
        return PageResp.build(page, CoinSpeedUpgradesRules.class);
    }
}
