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

import com.jeesharp.business.modules.common.enums.DisabledEnabledEnum;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.setting.entity.CmsHomeQuickGrid;

import com.jeesharp.business.modules.setting.mapper.HomeQuickGridMapper;
import com.jeesharp.business.modules.setting.service.HomeQuickGridService;
import com.jeesharp.business.modules.setting.vo.HomeQuickGridQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 小程序首页快捷宫格领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-15
 */
@Service
@Transactional(readOnly = true)
public class HomeQuickGridServiceImpl extends CrudService<HomeQuickGridMapper, CmsHomeQuickGrid, HomeQuickGridQueryVO> implements HomeQuickGridService {

    private static final int SHELL_MAX = 8;

    @Override
    public List<CmsHomeQuickGrid> listEnabledForShell(int limit) {
        int n = limit > 0 ? Math.min(limit, SHELL_MAX) : SHELL_MAX;
        LambdaQueryWrapper<CmsHomeQuickGrid> w = new LambdaQueryWrapper<>();
        w.eq(CmsHomeQuickGrid::getDelFlag, "0");
        w.eq(CmsHomeQuickGrid::getStatus, DisabledEnabledEnum.ENABLED.getValue());
        w.orderByDesc(CmsHomeQuickGrid::getSortOrder);
        w.orderByDesc(CmsHomeQuickGrid::getCreateDate);
        w.last("LIMIT " + n);
        return list(w);
    }

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

}
