package com.ruoyi.system.service.impl;

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

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysDictDataWithChildren;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.cs.domain.CsClass;
import com.ruoyi.cs.domain.dto.DictParamDTO;
import com.ruoyi.cs.domain.vo.SysDictDataVO;
import com.ruoyi.cs.mapper.CsClassMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService
{
    @Resource
    private SysDictDataMapper dictDataMapper;

    @Resource
    private CsClassMapper csClassMapper;

    @Resource
    private SysDictTypeMapper dictTypeMapper;

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

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData)
    {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);

            csClassMapper.deleteCsClassByClassId(String.valueOf(dictCode));
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data)
    {
        if(null == data.getDictPcode()){
            data.setDictPcode(0L);
        }
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);

            SysDictData sysDictData = dictDataMapper.selectSysDictDataByDictTypeDictValue(data.getDictType(), data.getDictValue());
            CsClass csClass = new CsClass();
            csClass.setClassId(sysDictData.getDictCode().toString());
            csClass.setParentId(dictDatas.get(dictDatas.size() -1).getDictPcode().toString());
            csClass.setClassMark(data.getDictType());
            SysDictType sysDictType = dictTypeMapper.selectDictTypeByType(data.getDictType());
            if (sysDictType != null) {
                csClass.setClassMarkName(sysDictType.getDictName());
            }
            csClass.setClassName(data.getDictLabel());
            csClass.setStatus("0");
            csClass.setSort(data.getDictSort().intValue());
            csClass.setCreateAt(DateUtils.getTime());
            csClassMapper.insertCsClass(csClass);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);

            CsClass csClassByClassId = csClassMapper.selectCsClassByClassId(data.getDictCode().toString());
            if (csClassByClassId == null) {

                CsClass csClass = new CsClass();
                csClass.setClassId(data.getDictCode().toString());
                csClass.setParentId(data.getDictPcode().toString());
                csClass.setClassMark(data.getDictType());
                SysDictType sysDictType = dictTypeMapper.selectDictTypeByType(data.getDictType());
                if (sysDictType != null) {
                    csClass.setClassMarkName(sysDictType.getDictName());
                }
                csClass.setClassName(data.getDictLabel());
                csClass.setStatus("0");
                csClass.setSort(data.getDictSort().intValue());
                csClass.setCreateAt(DateUtils.getTime());
                csClassMapper.insertCsClass(csClass);

            } else {
                csClassByClassId.setParentId(data.getDictPcode().toString());
                csClassByClassId.setClassMark(data.getDictType());
                SysDictType sysDictType = dictTypeMapper.selectDictTypeByType(data.getDictType());
                if (sysDictType != null) {
                    csClassByClassId.setClassMarkName(sysDictType.getDictName());
                }
                csClassByClassId.setClassName(data.getDictLabel());
                csClassByClassId.setStatus("0");
                csClassByClassId.setSort(data.getDictSort().intValue());
                csClassMapper.updateCsClass(csClassByClassId);
            }

        }
        return row;
    }

    /**
     * 根据字典类型和父级字典编码 查询字典数据信息
     * @param param 字典参数
     * @return
     */
    @Override
    public List<SysDictDataVO> selectDictValueForMember(DictParamDTO param) {
        return dictDataMapper.selectDictValueForMember(param);
    }
}
