package com.cool.store.dao.dict;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.cool.store.entity.dict.SysDictTableDO;
import com.cool.store.mapper.dict.SysDictTableMapper;
import com.cool.store.request.dict.DictTableQueryRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * <p>
 * 字典表DAO
 * </p>
 *
 * @author wangff
 * @since 2025/9/25
 */
@Repository
@RequiredArgsConstructor
public class SysDictTableDAO {

    private final SysDictTableMapper dictTableMapper;


    /**
     * 列表查询
     * @param dto 字典表查询DTO
     * @return 字典表信息表列表
     */
    public List<SysDictTableDO> getList(DictTableQueryRequest dto) {
        return dictTableMapper.selectList(dto);
    }

    /**
     * 根据id查询
     * @param id id
     * @return 字典表信息表
     */
    public SysDictTableDO getById(Long id) {
        return dictTableMapper.selectById(id);
    }

    /**
     * 根据id查询
     * @param ids id列表
     * @return 字典表列表
     */
    public List<SysDictTableDO> getByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return dictTableMapper.selectByIds(ids);
    }

    /**
     * 根据字典表id查询所属字典分组id
     * @param dictTableId 字典表id
     * @return 字典分组id
     */
    public Long getGroupIdByTableId(Long dictTableId) {
        SysDictTableDO tableInfoDO = getById(dictTableId);
        if (Objects.nonNull(tableInfoDO)) {
            return tableInfoDO.getDictGroupId();
        }
        return null;
    }

    /**
     * 根据分组id查询字典表
     * @param groupIds 字典分组id列表
     * @return 字典表VO对象列表
     */
    public List<SysDictTableDO> getListByGroupIds(List<Long> groupIds, Integer openStatus) {
        if (CollectionUtil.isEmpty(groupIds)) {
            return Collections.emptyList();
        }
        return dictTableMapper.selectByGroupIds(groupIds, openStatus);
    }

    /**
     * 过滤存在启用字典表的字典分组id
     * @param groupIds 字典分组id
     * @return 字典分组id列表
     */
    public Set<Long> filterEnableTableByGroupIds(List<Long> groupIds) {
        if (CollectionUtil.isEmpty(groupIds)) {
            return Collections.emptySet();
        }
        List<Map<String, Object>> maps = dictTableMapper.filterEnableTableByGroupIds(groupIds);
        return CollStreamUtil.toSet(maps, v -> MapUtils.getLong(v, "dict_group_id"));
    }

    /**
     * 根据分组id删除
     * @param groupIds 分组id列表
     */
    public void deleteByGroupIds(List<Long> groupIds) {
        if (CollectionUtil.isEmpty(groupIds)) {
            return;
        }
        dictTableMapper.deleteByGroupIds(groupIds);
    }

    /**
     * 根据字典表编码查询
     * @param tableCode 字典表编码
     * @return 字典表
     */
    public SysDictTableDO getByTableCode(String tableCode) {
        return dictTableMapper.selectByTableCode(tableCode);
    }

    /**
     * 根据字典表编码查询启用状态的字典表
     * @param tableCodes 字典表编码
     * @return 字典表列表
     */
    public List<SysDictTableDO> getOpenTableListByTableCodes(List<String> tableCodes) {
        if (CollectionUtils.isEmpty(tableCodes)) {
            return Collections.emptyList();
        }
        return dictTableMapper.selectOpenTableListByTableCodes(tableCodes);
    }
}
