package com.sckw.system.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sckw.core.model.base.BaseModel;
import com.sckw.system.dao.SysDictDao;
import com.sckw.system.model.SysDict;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 字典服务层（MyBatis-Plus 版本）
 * @author system
 * @date 2024
 */
@Repository
public class SysDictRepository extends ServiceImpl<SysDictDao, SysDict> {


    /**
     * 根据条件查询唯一字典
     */
    public SysDict selectUniqueDict(Long dictTypeId,String label, String type, String parentId) {
        return this.getOne(Wrappers.<SysDict>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(Objects.nonNull(dictTypeId),SysDict::getDictId, dictTypeId)
                .eq(StringUtils.isNotBlank(label),SysDict::getLabel, label)
                .eq(StringUtils.isNotBlank( type),SysDict::getType, type)
                .eq(StringUtils.isNotBlank(parentId),SysDict::getParentId, parentId)
                .last("limit 1"));
    }

    /**
     * 逻辑删除
     */
    public int logicDeleteById(Long id) {
        return this.baseMapper.deleteById(id);
    }

    /**
     * 根据主键查询
     */
    public SysDict selectById(Long id) {
        return this.getById(id);
    }

    /**
     * 更新字典
     */
    public Boolean update(SysDict entity) {
        return updateById(entity);
    }

    /**
     * 分页查询字典（包含父级名称）
     */
    public IPage<SysDict> selectPage(int pageNum, int pageSize, String type, String parentId, String label) {
        Page<SysDict> page = new Page<>(pageNum, pageSize);
        
        // 使用自定义 SQL 查询（包含父级名称）
        LambdaQueryWrapper<SysDict> wrapper = Wrappers.<SysDict>lambdaQuery()
                .eq(SysDict::getDelFlag, 0)
                .eq(StringUtils.isNotBlank(type), SysDict::getType, type)
                .eq(StringUtils.isNotBlank(parentId), SysDict::getParentId, parentId)
                .like(StringUtils.isNotBlank(label), SysDict::getLabel, label)
                .orderByDesc(BaseModel::getCreateTime)
                .orderByAsc(SysDict::getSort);
        return page(page,wrapper);
    }

    /**
     * 统计字典数量
     */
    public long count(String type) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDict::getDelFlag, 0);
        
        if (StringUtils.isNotBlank(type)) {
            wrapper.eq(SysDict::getType, type);
        }
        
        return this.count(wrapper);
    }

    /**
     * 查询上级字典下拉列表
     */
    public List<SysDict> searchParentDictForSelect(String keyword) {
        return list(Wrappers.<SysDict>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(BaseModel::getStatus,0)
                .like (StringUtils.isNotBlank( keyword), SysDict::getLabel, keyword));
    }

    /**
     * 快捷更改字典状态
     */
    public Boolean updateStatus(Long id, Integer status) {
        SysDict sysDict = new SysDict();
        sysDict.setId(id);
        sysDict.setStatus(status);
        return updateById(sysDict);
    }

    public List<SysDict> queryByIds(Set<String> parentIds) {
        return list(Wrappers.<SysDict>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(BaseModel::getStatus,0)
                .in(SysDict::getId, parentIds));
    }

    public List<SysDict> selectByType(String testDictType) {
        return list(Wrappers.<SysDict>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(BaseModel::getStatus,0)
                .eq(SysDict::getType, testDictType));
    }
}
