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

/**
 * 业态类型管理Service业务层处理
 *
 * @author YinQ
 * @date 2023-05-05
 */
@Service
public class BaseBusinessTypeServiceImpl implements IBaseBusinessTypeService {
    @Autowired
    private BaseBusinessTypeMapper baseBusinessTypeMapper;

    /**
     * 查询业态类型管理
     *
     * @param objid 业态类型管理主键
     * @return 业态类型管理
     */
    @Override
    //@DS("#header.poolName")
    public BaseBusinessType selectBaseBusinessTypeByObjid(Long objid) {
        return baseBusinessTypeMapper.selectBaseBusinessTypeByObjid(objid);
    }

    /**
     * 查询业态类型管理列表
     *
     * @param baseBusinessType 业态类型管理
     * @return 业态类型管理
     */
    @Override
    //@DS("#header.poolName")
    public List<BaseBusinessType> selectBaseBusinessTypeList(BaseBusinessType baseBusinessType) {
        return baseBusinessTypeMapper.selectBaseBusinessTypeList(baseBusinessType);
    }

    /**
     * 新增业态类型管理
     *
     * @param baseBusinessType 业态类型管理
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int insertBaseBusinessType(BaseBusinessType baseBusinessType) {
        baseBusinessType.setCreateTime(DateUtils.getNowDate());
        return baseBusinessTypeMapper.insertBaseBusinessType(baseBusinessType);
    }

    /**
     * 修改业态类型管理
     *
     * @param baseBusinessType 业态类型管理
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int updateBaseBusinessType(BaseBusinessType baseBusinessType) {
        baseBusinessType.setUpdateTime(DateUtils.getNowDate());
        return baseBusinessTypeMapper.updateBaseBusinessType(baseBusinessType);
    }

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

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

    @Override
    //@DS("#header.poolName")
    public List<BaseBusinessType> selectSubentryList(BaseMonitorInfo baseMonitorInfo) {
        BaseBusinessType baseBusinessType = new BaseBusinessType();
        List<BaseBusinessType> menuList = baseBusinessTypeMapper.selectBaseBusinessTypeList(baseBusinessType);
        return menuList;
    }

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

    @Override
    //@DS("#header.poolName")
    public List<BaseBusinessType> buildTree(List<BaseBusinessType> menus) {
        List<BaseBusinessType> returnList = new ArrayList<BaseBusinessType>();
        List<Long> tempList = menus.stream().map(BaseBusinessType::getObjid).collect(Collectors.toList());
        for (Iterator<BaseBusinessType> iterator = menus.iterator(); iterator.hasNext(); ) {
            BaseBusinessType menu = (BaseBusinessType) 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> selectBaseBusinessTypeTree() {
        List<BaseBusinessType> baseBusinessTypeList = baseBusinessTypeMapper.selectBaseBusinessTypeList(new BaseBusinessType());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (BaseBusinessType baseBusinessType : baseBusinessTypeList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(baseBusinessType.getObjid());
            ztree.setpId(baseBusinessType.getParentId());
            ztree.setName(baseBusinessType.getBusinessName());
            ztree.setTitle(baseBusinessType.getBusinessName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

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

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

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