package org.dromara.ems.base.service.impl;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.DateUtils;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.ems.base.domain.EmsBaseMonitorWorkUnit;
import org.dromara.ems.base.domain.EmsBaseWorkUnit;
import org.dromara.ems.base.domain.TreeSelects;
import org.dromara.ems.base.mapper.EmsBaseMonitorWorkUnitMapper;
import org.dromara.ems.base.mapper.EmsBaseWorkUnitMapper;
import org.dromara.ems.base.service.IEmsBaseWorkUnitService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 统计单元信息Service业务层处理
 * 
 * @author Yinq
 * @date 2024-05-09
 */
@Service
@RequiredArgsConstructor
public class EmsBaseWorkUnitServiceImpl implements IEmsBaseWorkUnitService 
{
    
    private final EmsBaseWorkUnitMapper emsBaseWorkUnitMapper;

    
    private final EmsBaseMonitorWorkUnitMapper emsBaseMonitorWorkUnitMapper;

    /**
     * 查询统计单元信息
     * 
     * @param objId 统计单元信息主键
     * @return 统计单元信息
     */
    @Override
    public EmsBaseWorkUnit selectEmsBaseWorkUnitByObjId(Long objId)
    {
        return emsBaseWorkUnitMapper.selectEmsBaseWorkUnitByObjId(objId);
    }

    /**
     * 查询统计单元信息列表
     * 
     * @param emsBaseWorkUnit 统计单元信息
     * @return 统计单元信息
     */
    @Override
    public List<EmsBaseWorkUnit> selectEmsBaseWorkUnitList(EmsBaseWorkUnit emsBaseWorkUnit)
    {
        return emsBaseWorkUnitMapper.selectEmsBaseWorkUnitList(emsBaseWorkUnit);
    }

    /**
     * 新增统计单元信息
     * 
     * @param emsBaseWorkUnit 统计单元信息
     * @return 结果
     */
    @Override
    public int insertEmsBaseWorkUnit(EmsBaseWorkUnit emsBaseWorkUnit)
    {
        emsBaseWorkUnit.setCreateTime(DateUtils.getNowDate());
        return emsBaseWorkUnitMapper.insertEmsBaseWorkUnit(emsBaseWorkUnit);
    }

    /**
     * 修改统计单元信息
     * 
     * @param emsBaseWorkUnit 统计单元信息
     * @return 结果
     */
    @Override
    public int updateEmsBaseWorkUnit(EmsBaseWorkUnit emsBaseWorkUnit)
    {
        emsBaseWorkUnit.setUpdateTime(DateUtils.getNowDate());
        return emsBaseWorkUnitMapper.updateEmsBaseWorkUnit(emsBaseWorkUnit);
    }

    /**
     * 批量删除统计单元信息
     * 
     * @param objIds 需要删除的统计单元信息主键
     * @return 结果
     */
    @Override
    public int deleteEmsBaseWorkUnitByObjIds(Long[] objIds)
    {
        for (Long objId : objIds) {
            EmsBaseWorkUnit baseWorkUnit = this.selectEmsBaseWorkUnitByObjId(objId);
            EmsBaseMonitorWorkUnit monitorWorkUnit = new EmsBaseMonitorWorkUnit();
            monitorWorkUnit.setWorkUnitCode(baseWorkUnit.getWorkUnitCode());
            List<EmsBaseMonitorWorkUnit> workUnits = emsBaseMonitorWorkUnitMapper.selectEmsBaseMonitorWorkUnitList(monitorWorkUnit);
            if (workUnits.size() > 0){
                throw new ServiceException(baseWorkUnit.getWorkUnitName() + "已关联计量设备，无法删除！");
            }
        }
        return emsBaseWorkUnitMapper.deleteEmsBaseWorkUnitByObjIds(objIds);
    }

    /**
     * 删除统计单元信息信息
     * 
     * @param objId 统计单元信息主键
     * @return 结果
     */
    @Override
    public int deleteEmsBaseWorkUnitByObjId(Long objId)
    {
        return emsBaseWorkUnitMapper.deleteEmsBaseWorkUnitByObjId(objId);
    }

    @Override
    public List<TreeSelects> selectBaseWorkUnitTreeList(EmsBaseWorkUnit baseWorkUnit) {
        List<EmsBaseWorkUnit> baseWorkUnits = selectEmsBaseWorkUnitList(baseWorkUnit);
        return buildWorkUnitTreeSelect(baseWorkUnits);
    }

    @Override
    public List<TreeSelects> buildWorkUnitTreeSelect(List<EmsBaseWorkUnit> baseWorkUnits) {
        List<EmsBaseWorkUnit> baseWorkUnitsTrees = buildWorkUnitTree(baseWorkUnits);
        return baseWorkUnitsTrees.stream().map(TreeSelects::new).collect(Collectors.toList());
    }

    @Override
    public List<EmsBaseWorkUnit> buildWorkUnitTree(List<EmsBaseWorkUnit> baseWorkUnits) {
        List<EmsBaseWorkUnit> returnList = new ArrayList<EmsBaseWorkUnit>();
        List<Long> tempList = baseWorkUnits.stream().map(EmsBaseWorkUnit::getObjId).collect(Collectors.toList());
        for (EmsBaseWorkUnit baseWorkUnit : baseWorkUnits)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(baseWorkUnit.getParentId()))
            {
                recursionFn(baseWorkUnits, baseWorkUnit);
                returnList.add(baseWorkUnit);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = baseWorkUnits;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<EmsBaseWorkUnit> list, EmsBaseWorkUnit t)
    {
        // 得到子节点列表
        List<EmsBaseWorkUnit> childList = getChildList(list, t);
        t.setChildren(childList);
        for (EmsBaseWorkUnit tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<EmsBaseWorkUnit> getChildList(List<EmsBaseWorkUnit> list, EmsBaseWorkUnit t)
    {
        List<EmsBaseWorkUnit> tlist = new ArrayList<EmsBaseWorkUnit>();
        Iterator<EmsBaseWorkUnit> it = list.iterator();
        while (it.hasNext())
        {
            EmsBaseWorkUnit n = (EmsBaseWorkUnit) it.next();
            if (ObjectUtil.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getObjId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<EmsBaseWorkUnit> list, EmsBaseWorkUnit t)
    {
        return getChildList(list, t).size() > 0;
    }
}
