package com.hw.ems.base.service.impl;

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

import com.hw.common.core.domain.Ztree;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.core.utils.StringUtils;
import com.hw.ems.base.domain.BaseBuildInfo;
import com.hw.ems.base.domain.BaseMonitorInfo;
import com.hw.ems.base.domain.vo.TreeSelect;
import com.hw.ems.base.mapper.BaseBuildInfoMapper;
import com.hw.ems.base.service.IBaseBuildInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 建筑信息管理Service业务层处理
 *
 * @author YinQ
 * @date 2023-05-05
 */
@Service
public class BaseBuildInfoServiceImpl implements IBaseBuildInfoService {
    @Autowired
    private BaseBuildInfoMapper baseBuildInfoMapper;

    /**
     * 查询建筑信息管理
     *
     * @param objid 建筑信息管理主键
     * @return 建筑信息管理
     */
    @Override
    //@DS("#header.poolName")
    public BaseBuildInfo selectBaseBuildInfoByObjid(Long objid) {
        return baseBuildInfoMapper.selectBaseBuildInfoByObjid(objid);
    }

    /**
     * 查询建筑信息管理列表
     *
     * @param baseBuildInfo 建筑信息管理
     * @return 建筑信息管理
     */
    @Override
    //@DS("#header.poolName")
    public List<BaseBuildInfo> selectBaseBuildInfoList(BaseBuildInfo baseBuildInfo) {
        return baseBuildInfoMapper.selectBaseBuildInfoList(baseBuildInfo);
    }

    /**
     * 新增建筑信息管理
     *
     * @param baseBuildInfo 建筑信息管理
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int insertBaseBuildInfo(BaseBuildInfo baseBuildInfo) {
        baseBuildInfo.setCreateTime(DateUtils.getNowDate());
        return baseBuildInfoMapper.insertBaseBuildInfo(baseBuildInfo);
    }

    /**
     * 修改建筑信息管理
     *
     * @param baseBuildInfo 建筑信息管理
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int updateBaseBuildInfo(BaseBuildInfo baseBuildInfo) {
        baseBuildInfo.setUpdateTime(DateUtils.getNowDate());
        return baseBuildInfoMapper.updateBaseBuildInfo(baseBuildInfo);
    }

    /**
     * 批量删除建筑信息管理
     *
     * @param objids 需要删除的建筑信息管理主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteBaseBuildInfoByObjids(Long[] objids) {
        return baseBuildInfoMapper.deleteBaseBuildInfoByObjids(objids);
    }

    /**
     * 删除建筑信息管理信息
     *
     * @param objid 建筑信息管理主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteBaseBuildInfoByObjid(Long objid) {
        return baseBuildInfoMapper.deleteBaseBuildInfoByObjid(objid);
    }

    @Override
    //@DS("#header.poolName")
    public List<BaseBuildInfo> selectBuildList(BaseMonitorInfo baseMonitorInfo) {
        BaseBuildInfo baseBuildInfo = new BaseBuildInfo();
        List<BaseBuildInfo> menuList = baseBuildInfoMapper.selectBaseBuildInfoList(baseBuildInfo);
        return menuList;
    }

    @Override
    //@DS("#header.poolName")
    public List<TreeSelect> buildTreeSelect(List<BaseBuildInfo> builds) {
        List<BaseBuildInfo> menuTrees = buildTree(builds);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    //@DS("#header.poolName")
    public List<BaseBuildInfo> buildTree(List<BaseBuildInfo> menus) {
        List<BaseBuildInfo> returnList = new ArrayList<BaseBuildInfo>();
        List<Long> tempList = menus.stream().map(BaseBuildInfo::getObjid).collect(Collectors.toList());
        for (Iterator<BaseBuildInfo> iterator = menus.iterator(); iterator.hasNext(); ) {
            BaseBuildInfo menu = (BaseBuildInfo) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }

    @Override
    //@DS("#header.poolName")
    public List<Ztree> selectBaseBuildInfoTree() {
        List<BaseBuildInfo> baseBuildInfoList = baseBuildInfoMapper.selectBaseBuildInfoList(new BaseBuildInfo());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (BaseBuildInfo baseBuildInfo : baseBuildInfoList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(baseBuildInfo.getObjid());
            ztree.setpId(baseBuildInfo.getParentId());
            ztree.setName(baseBuildInfo.getBuildName());
            ztree.setTitle(baseBuildInfo.getBuildName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

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

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

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