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.BaseMonitorInfo;
import com.hw.ems.base.domain.BaseSubentryType;
import com.hw.ems.base.domain.vo.TreeSelect;
import com.hw.ems.base.mapper.BaseSubentryTypeMapper;
import com.hw.ems.base.service.IBaseSubentryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 分项类型管理Service业务层处理
 *
 * @author YinQ
 * @date 2023-05-06
 */
@Service
public class BaseSubentryTypeServiceImpl implements IBaseSubentryTypeService {
    @Autowired
    private BaseSubentryTypeMapper baseSubentryTypeMapper;

    /**
     * 查询分项类型管理
     *
     * @param objid 分项类型管理主键
     * @return 分项类型管理
     */
    @Override
    //@DS("#header.poolName")
    public BaseSubentryType selectBaseSubentryTypeByObjid(Long objid) {
        return baseSubentryTypeMapper.selectBaseSubentryTypeByObjid(objid);
    }

    /**
     * 查询分项类型管理列表
     *
     * @param baseSubentryType 分项类型管理
     * @return 分项类型管理
     */
    @Override
    //@DS("#header.poolName")
    public List<BaseSubentryType> selectBaseSubentryTypeList(BaseSubentryType baseSubentryType) {
        return baseSubentryTypeMapper.selectBaseSubentryTypeList(baseSubentryType);
    }

    /**
     * 新增分项类型管理
     *
     * @param baseSubentryType 分项类型管理
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int insertBaseSubentryType(BaseSubentryType baseSubentryType) {
        baseSubentryType.setCreateTime(DateUtils.getNowDate());
        return baseSubentryTypeMapper.insertBaseSubentryType(baseSubentryType);
    }

    /**
     * 修改分项类型管理
     *
     * @param baseSubentryType 分项类型管理
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int updateBaseSubentryType(BaseSubentryType baseSubentryType) {
        baseSubentryType.setUpdateTime(DateUtils.getNowDate());
        return baseSubentryTypeMapper.updateBaseSubentryType(baseSubentryType);
    }

    /**
     * 批量删除分项类型管理
     *
     * @param objids 需要删除的分项类型管理主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteBaseSubentryTypeByObjids(Long[] objids) {
        return baseSubentryTypeMapper.deleteBaseSubentryTypeByObjids(objids);
    }

    /**
     * 删除分项类型管理信息
     *
     * @param objid 分项类型管理主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteBaseSubentryTypeByObjid(Long objid) {
        return baseSubentryTypeMapper.deleteBaseSubentryTypeByObjid(objid);
    }

    @Override
    //@DS("#header.poolName")
    public List<BaseSubentryType> selectSubentryList(BaseMonitorInfo baseMonitorInfo) {
        BaseSubentryType baseBuildInfo = new BaseSubentryType();
        List<BaseSubentryType> menuList = baseSubentryTypeMapper.selectBaseSubentryTypeList(baseBuildInfo);
        return menuList;
    }

    @Override
    //@DS("#header.poolName")
    public List<TreeSelect>  buildTreeSelect(List<BaseSubentryType> subentrys) {
        List<BaseSubentryType> menuTrees = buildTree(subentrys);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }
    @Override
    //@DS("#header.poolName")
    public List<BaseSubentryType> buildTree(List<BaseSubentryType> menus) {
        List<BaseSubentryType> returnList = new ArrayList<BaseSubentryType>();
        List<Long> tempList = menus.stream().map(BaseSubentryType::getObjid).collect(Collectors.toList());
        for (Iterator<BaseSubentryType> iterator = menus.iterator(); iterator.hasNext();) {
            BaseSubentryType menu = (BaseSubentryType) 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> selectBaseSubentryTypeTree() {
        List<BaseSubentryType> baseSubentryTypeList = baseSubentryTypeMapper.selectBaseSubentryTypeList(new BaseSubentryType());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (BaseSubentryType baseSubentryType : baseSubentryTypeList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(baseSubentryType.getObjid());
            ztree.setpId(baseSubentryType.getParentId());
            ztree.setName(baseSubentryType.getSubentryName());
            ztree.setTitle(baseSubentryType.getSubentryName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<BaseSubentryType> list, BaseSubentryType t) {
        // 得到子节点列表
        List<BaseSubentryType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BaseSubentryType tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<BaseSubentryType> getChildList(List<BaseSubentryType> list, BaseSubentryType t) {
        List<BaseSubentryType> tlist = new ArrayList<BaseSubentryType>();
        Iterator<BaseSubentryType> it = list.iterator();
        while (it.hasNext()) {
            BaseSubentryType n = (BaseSubentryType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getObjid().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

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