package com.ruoyi.powerdistribution.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.powerdistribution.mapper.PdmDicMapper;
import com.ruoyi.powerdistribution.domain.PdmDic;
import com.ruoyi.powerdistribution.service.IPdmDicService;

/**
 * 配网字典Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class PdmDicServiceImpl implements IPdmDicService 
{
    @Autowired
    private PdmDicMapper pdmDicMapper;

    /**
     * 查询配网字典
     * 
     * @param id 配网字典主键
     * @return 配网字典
     */
    @Override
    public PdmDic selectPdmDicById(Long id)
    {
        return pdmDicMapper.selectPdmDicById(id);
    }

    /**
     * 查询配网字典列表
     * 
     * @param pdmDic 配网字典
     * @return 配网字典
     */
    @Override
    public List<PdmDic> selectPdmDicList(PdmDic pdmDic)
    {
        return pdmDicMapper.selectPdmDicList(pdmDic);
    }

    /**
     * 新增配网字典
     * 
     * @param pdmDic 配网字典
     * @return 结果
     */
    @Override
    public int insertPdmDic(PdmDic pdmDic)
    {
        pdmDic.setCreateTime(DateUtils.getNowDate());
        return pdmDicMapper.insertPdmDic(pdmDic);
    }

    /**
     * 修改配网字典
     * 
     * @param pdmDic 配网字典
     * @return 结果
     */
    @Override
    public int updatePdmDic(PdmDic pdmDic)
    {
        pdmDic.setUpdateTime(DateUtils.getNowDate());
        return pdmDicMapper.updatePdmDic(pdmDic);
    }

    /**
     * 批量删除配网字典
     * 
     * @param ids 需要删除的配网字典主键
     * @return 结果
     */
    @Override
    public int deletePdmDicByIds(Long[] ids)
    {
        return pdmDicMapper.deletePdmDicByIds(ids);
    }

    /**
     * 删除配网字典信息
     * 
     * @param id 配网字典主键
     * @return 结果
     */
    @Override
    public int deletePdmDicById(Long id)
    {
        return pdmDicMapper.deletePdmDicById(id);
    }
    @Override
    public List<PdmDic> getPdmDicTree( ) {
        List<PdmDic> list =getAllHrDic();
        List<PdmDic> hrDicList = new ArrayList<>();
        for (PdmDic dic : list) {
            if (dic.getParentId().equals("0")) {
                hrDicList.add(dic);
            }
        }
        for (PdmDic pdmDic : hrDicList) {
            pdmDic.setChildren(getChild(pdmDic.getDicId(), list));
        }
        return hrDicList;
    }
    List<PdmDic> getAllHrDic()
    {
        return pdmDicMapper.selectPdmDicList(new PdmDic());
    }
    /**
     * 获取字典子集
     * @param dicId 字典Id
     * @param rootHrDic 字典列表
     * @return 字典列表
     */
    private List<PdmDic> getChild(String dicId, List<PdmDic> rootHrDic) {
        List<PdmDic> childList = new ArrayList<>();
        for (PdmDic hrDic : rootHrDic) {
            if (hrDic.getParentId().equals(dicId)) {
                childList.add(hrDic);
            }
        }
        for (PdmDic hrDic : childList) {
            hrDic.setChildren(getChild(hrDic.getDicId(), rootHrDic));
        }
        if (childList.isEmpty()) {
            return Collections.emptyList();
        }
        return childList;
    }

}
