package com.jeesharp.business.modules.setting.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.setting.entity.CmsSiteIcon;
import com.jeesharp.business.modules.setting.enums.SiteIconStatusEnum;
import com.jeesharp.business.modules.setting.mapper.SiteIconMapper;
import com.jeesharp.business.modules.setting.service.SiteIconService;
import com.jeesharp.business.modules.setting.vo.SiteIconQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 站点图标领域服务实现，继承框架 {@code CrudService}，并提供按分类拉取启用项等查询。
 *
 * @author jeesharp
 * @since 2026-05-15
 */
@Service
@Transactional(readOnly = true)
public class SiteIconServiceImpl extends CrudService<SiteIconMapper, CmsSiteIcon, SiteIconQueryVO> implements SiteIconService {

    @Override
    public List<CmsSiteIcon> listEnabledByCategory(String category) {
        if (StrUtil.isBlank(category)) {
            return List.of();
        }
        LambdaQueryWrapper<CmsSiteIcon> w = new LambdaQueryWrapper<>();
        w.eq(CmsSiteIcon::getDelFlag, "0");
        w.eq(CmsSiteIcon::getStatus, SiteIconStatusEnum.ENABLED.getValue());
        w.eq(CmsSiteIcon::getCategory, category.trim());
        w.orderByDesc(CmsSiteIcon::getSortOrder);
        w.orderByDesc(CmsSiteIcon::getCreateDate);
        return list(w);
    }

    @Override
    public List<CmsSiteIcon> listEnabledByCategoryAndIconCodePrefix(String category, String iconCodePrefix) {
        if (StrUtil.isBlank(category) || StrUtil.isBlank(iconCodePrefix)) {
            return List.of();
        }
        LambdaQueryWrapper<CmsSiteIcon> w = new LambdaQueryWrapper<>();
        w.eq(CmsSiteIcon::getDelFlag, "0");
        w.eq(CmsSiteIcon::getStatus, SiteIconStatusEnum.ENABLED.getValue());
        w.eq(CmsSiteIcon::getCategory, category.trim());
        w.likeRight(CmsSiteIcon::getIconCode, iconCodePrefix.trim());
        w.orderByDesc(CmsSiteIcon::getSortOrder);
        w.orderByDesc(CmsSiteIcon::getCreateDate);
        return list(w);
    }

    @Override
    public long countByIconCodeExcludeId(String iconCode, String excludeId) {
        if (StrUtil.isBlank(iconCode)) {
            return 0L;
        }
        LambdaQueryWrapper<CmsSiteIcon> w = new LambdaQueryWrapper<>();
        w.eq(CmsSiteIcon::getDelFlag, "0");
        w.eq(CmsSiteIcon::getIconCode, iconCode.trim());
        if (StrUtil.isNotBlank(excludeId)) {
            w.ne(CmsSiteIcon::getId, excludeId.trim());
        }
        return getBaseMapper().selectCount(w);
    }

}
