package com.bonus.sgzb.base.service.impl;

import com.bonus.sgzb.base.domain.MaHouse;
import com.bonus.sgzb.base.domain.vo.TreeSelect;
import com.bonus.sgzb.base.mapper.MaHouseMapper;
import com.bonus.sgzb.base.service.IhouseService;
import com.bonus.sgzb.common.core.constant.UserConstants;
import com.bonus.sgzb.common.core.exception.ServiceException;
import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class MaHouseServiceImpl implements IhouseService {

    @Resource
    private MaHouseMapper houseMapper;

    /**
     * 查询仓库管理ma_house_info
     *
     * @param houseId 仓库管理ma_house_info主键
     * @return 仓库管理ma_house_info
     */
    @Override
    public MaHouse selectMaHouseByHouseId(Long houseId)
    {
        return houseMapper.selectMaHouseByHouseId(houseId);
    }


    /**
     * 校验工程项目名称
     *
     * @param house 仓库信息
     * @return 结果
     */
    @Override
    public boolean checkHouseNameUnique(MaHouse house) {
        Long houseId = StringUtils.isNull(house.getHouseId()) ? -1L : house.getHouseId();
        MaHouse info = houseMapper.checkHouseNameUnique(house.getHouseName());
        if (StringUtils.isNotNull(info) && info.getHouseId().longValue() != houseId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增保存仓库信息
     *
     * @param house 仓库信息
     * @return 结果
     */
    @Override
    public int insertHouse(MaHouse house) {
        MaHouse info = houseMapper.selectHouseById(house.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (house.getParentId() != 0 && !UserConstants.DEPT_NORMAL.equals(info.getStatus()))
        {
            throw new ServiceException("仓库货架停用，不允许新增");
        }
        house.setCreateBy(SecurityUtils.getUsername());
        return houseMapper.insertHouse(house);
    }

    /**
     * 修改仓库信息
     *
     * @param house 仓库信息
     * @return 结果
     */
    @Override
    public int updatetHouse(MaHouse house) {
        return houseMapper.updateHouse(house);
    }

    /**
     * 删除仓库货架信息
     * @param houseId
     * @return
     */
    @Override
    public int deleteHouseById(Long houseId) {
        return houseMapper.deleteHouseById(houseId);
    }

    /**
     * 查询仓库列表
     * @param house
     * @return
     */
    @Override
    public List<MaHouse> selectHouseList(MaHouse house) {
        return houseMapper.selectHouseList(house);
    }

    /**
     * 是否存在子仓库
     * @param houseId
     * @return
     */
    @Override
    public boolean hasChildByHouseId(Long houseId) {
        Integer result = houseMapper.hasChildByHouseId(houseId);
        return result > 0;
    }

    /**
     * 查询货架列表组织树
     * @param house 货架信息
     * @return 结果
     */
    @Override
    public List<TreeSelect> getHouseTree(MaHouse house) {
        List<MaHouse> houseList = houseMapper.selectHouseList(house);
        return buildDeptTreeSelect(houseList);
    }







    /**
     * 构建前端所需要树结构
     *
     * @param maHouseList 部门列表
     * @return 树结构列表
     */
    @Override
    public List<MaHouse> buildDeptTree(List<MaHouse> maHouseList)
    {
        List<MaHouse> returnList = new ArrayList<MaHouse>();
        List<Long> tempList = maHouseList.stream().map(MaHouse::getHouseId).collect(Collectors.toList());
        for (MaHouse maHouse : maHouseList)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(maHouse.getParentId()))
            {
                recursionFn(maHouseList, maHouse);
                returnList.add(maHouse);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = maHouseList;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<MaHouse> depts)
    {
        List<MaHouse> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


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



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


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