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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.FreeQuotaConfigMapper;
import top.continew.admin.business.model.entity.FreeQuotaConfigDO;
import top.continew.admin.business.model.query.FreeQuotaConfigQuery;
import top.continew.admin.business.model.req.FreeQuotaConfigReq;
import top.continew.admin.business.model.resp.FreeQuotaConfigResp;
import top.continew.admin.business.service.FreeQuotaConfigService;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.util.List;

/**
 * 免费额度配置业务实现
 *
 * @author jiay
 * @since 2025/11/17
 */
@Service
@RequiredArgsConstructor
public class FreeQuotaConfigServiceImpl extends BaseServiceImpl<FreeQuotaConfigMapper, FreeQuotaConfigDO, FreeQuotaConfigResp, FreeQuotaConfigResp, FreeQuotaConfigQuery, FreeQuotaConfigReq> implements FreeQuotaConfigService {

    /**
     * 获取所有启用的配置
     */
    @Override
    @Cacheable(value = "freeQuotaConfig", key = "'enabled'", unless = "#result == null || #result.isEmpty()")
    public List<FreeQuotaConfigDO> listEnabled() {
        return baseMapper.selectList(new LambdaQueryWrapper<FreeQuotaConfigDO>()
            .eq(FreeQuotaConfigDO::getIsEnabled, true)
            .orderByAsc(FreeQuotaConfigDO::getSort));
    }

    /**
     * 根据类别编码获取配置
     */
    @Override
    @Cacheable(value = "freeQuotaConfig", key = "'code_' + #categoryCode", unless = "#result == null")
    public FreeQuotaConfigDO getByCategoryCode(String categoryCode) {
        return baseMapper.selectOne(new LambdaQueryWrapper<FreeQuotaConfigDO>()
            .eq(FreeQuotaConfigDO::getCategoryCode, categoryCode)
            .eq(FreeQuotaConfigDO::getIsEnabled, true)
            .last("LIMIT 1"));
    }

    /**
     * 根据计费类型获取配置
     */
    @Override
    @Cacheable(value = "freeQuotaConfig", key = "'chargeType_' + #chargeType", unless = "#result == null")
    public FreeQuotaConfigDO getByChargeType(Integer chargeType) {
        return baseMapper.selectOne(new LambdaQueryWrapper<FreeQuotaConfigDO>()
            .eq(FreeQuotaConfigDO::getChargeType, chargeType)
            .eq(FreeQuotaConfigDO::getIsEnabled, true)
            .last("LIMIT 1"));
    }

    /**
     * 新增后清除缓存
     */
    @Override
    @CacheEvict(value = "freeQuotaConfig", allEntries = true)
    public Long add(FreeQuotaConfigReq req) {
        return super.add(req);
    }

    /**
     * 更新后清除缓存
     */
    @Override
    @CacheEvict(value = "freeQuotaConfig", allEntries = true)
    public void update(FreeQuotaConfigReq req, Long id) {
        super.update(req, id);
    }

    /**
     * 删除后清除缓存
     */
    @Override
    @CacheEvict(value = "freeQuotaConfig", allEntries = true)
    public void delete(List<Long> ids) {
        super.delete(ids);
    }
}
