package com.ruoyi.cs.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictDataWithChildren;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.cs.domain.dto.DictParamDTO;
import com.ruoyi.cs.domain.vo.CsResourceDictDataVO;
import com.ruoyi.cs.domain.vo.CsResourceDictDataWithChildren;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.CsResourceDictDataMapper;
import com.ruoyi.cs.domain.CsResourceDictData;
import com.ruoyi.cs.service.ICsResourceDictDataService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 资源字典数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-29
 */
@Service
public class CsResourceDictDataServiceImpl implements ICsResourceDictDataService 
{
    @Resource
    private CsResourceDictDataMapper csResourceDictDataMapper;

    /**
     * 树级-根据条件分页查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<CsResourceDictDataWithChildren> selectDictDataTreeList(String dictType) {
        CsResourceDictData param = new CsResourceDictData();
        param.setDictType(dictType);
        List<CsResourceDictDataWithChildren> sysDictData = csResourceDictDataMapper.selectDictDataParentList(param);
        sysDictData = sysDictData.stream().map( parent -> {
            param.setDictPcode(parent.getDictCode());
            //查children
            List<CsResourceDictDataWithChildren> childrenList = csResourceDictDataMapper.selectDictDataParentList(param);
            //setChildren
            if(!CollectionUtils.isEmpty(childrenList)){
                parent.setChildren(childrenList);
            }else{
                parent.setChildren( new ArrayList<CsResourceDictDataWithChildren>());
            }
            return parent;
        }).collect(Collectors.toList());
        return sysDictData;
    }

    /**
     * 根据父级字典编码获取子级列表
     * @param pcode 父级字典编码
     * @return
     */
    @Override
    public List<CsResourceDictData> selectDictDataByPcode(Long pcode) {
        return csResourceDictDataMapper.selectDictDataListByPcode(pcode);
    }

    /**
     * 查询资源字典数据
     * 
     * @param dictCode 资源字典数据主键
     * @return 资源字典数据
     */
    @Override
    public CsResourceDictData selectCsResourceDictDataByDictCode(Long dictCode)
    {
        return csResourceDictDataMapper.selectCsResourceDictDataByDictCode(dictCode);
    }

    /**
     * 查询资源字典数据列表
     * 
     * @param csResourceDictData 资源字典数据
     * @return 资源字典数据
     */
    @Override
    public List<CsResourceDictData> selectCsResourceDictDataList(CsResourceDictData csResourceDictData)
    {
        return csResourceDictDataMapper.selectCsResourceDictDataList(csResourceDictData);
    }

    /**
     * 新增资源字典数据
     * 
     * @param csResourceDictData 资源字典数据
     * @return 结果
     */
    @Override
    public int insertCsResourceDictData(CsResourceDictData csResourceDictData)
    {
        csResourceDictData.setCreateTime(DateUtils.getNowDate());
        if(null == csResourceDictData.getDictPcode()){
            csResourceDictData.setDictPcode(0L);
        }
        return csResourceDictDataMapper.insertCsResourceDictData(csResourceDictData);
    }

    /**
     * 修改资源字典数据
     * 
     * @param csResourceDictData 资源字典数据
     * @return 结果
     */
    @Override
    public int updateCsResourceDictData(CsResourceDictData csResourceDictData)
    {
        csResourceDictData.setUpdateTime(DateUtils.getNowDate());
        return csResourceDictDataMapper.updateCsResourceDictData(csResourceDictData);
    }

    /**
     * 批量删除资源字典数据
     * 
     * @param dictCodes 需要删除的资源字典数据主键
     * @return 结果
     */
    @Override
    public int deleteCsResourceDictDataByDictCodes(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            CsResourceDictData data = selectCsResourceDictDataByDictCode(dictCode);
            csResourceDictDataMapper.deleteCsResourceDictDataByDictCode(dictCode);
            List<CsResourceDictData> dictDatas = csResourceDictDataMapper.selectDictDataByType(data.getDictType());
        }
        return csResourceDictDataMapper.deleteCsResourceDictDataByDictCodes(dictCodes);
    }

    /**
     * 删除资源字典数据信息
     * 
     * @param dictCode 资源字典数据主键
     * @return 结果
     */
    @Override
    public int deleteCsResourceDictDataByDictCode(Long dictCode)
    {
        return csResourceDictDataMapper.deleteCsResourceDictDataByDictCode(dictCode);
    }

    /**
     * 用户端 依据dictType 和 父Value 查子级列表
     *
     * @param param 资源字典数据
     * @return 资源字典数据集合
     */
    @Override
    public List<CsResourceDictDataVO> selectOptions(DictParamDTO param) {
        return csResourceDictDataMapper.selectOptions(param);
    }
}
