package com.ruoyi.nanjing.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.nanjing.mapper.TBdSubstationMapper;
import com.ruoyi.nanjing.domain.TBdSubstation;
import com.ruoyi.nanjing.service.ITBdSubstationService;
import com.ruoyi.common.core.text.Convert;

/**
 * 工位管理Service业务层处理
 * 
 * @author limy
 * @date 2021-01-22
 */
@Service
public class TBdSubstationServiceImpl implements ITBdSubstationService 
{
    @Autowired
    private TBdSubstationMapper tBdSubstationMapper;

    /**
     * 查询工位管理
     * 
     * @param ID 工位管理ID
     * @return 工位管理
     */
    @Override
    public TBdSubstation selectTBdSubstationById(Long ID)
    {
        return tBdSubstationMapper.selectTBdSubstationById(ID);
    }

    /**
     * 查询工位管理列表
     * 
     * @param tBdSubstation 工位管理
     * @return 工位管理
     */
    @Override
    public List<TBdSubstation> selectTBdSubstationList(TBdSubstation tBdSubstation)
    {
        return tBdSubstationMapper.selectTBdSubstationList(tBdSubstation);
    }

    /**
     * 新增工位管理
     * 
     * @param tBdSubstation 工位管理
     * @return 结果
     */
    @Override
    public int insertTBdSubstation(TBdSubstation tBdSubstation)
    {
        return tBdSubstationMapper.insertTBdSubstation(tBdSubstation);
    }

    /**
     * 修改工位管理
     * 
     * @param tBdSubstation 工位管理
     * @return 结果
     */
    @Override
    public int updateTBdSubstation(TBdSubstation tBdSubstation)
    {
        return tBdSubstationMapper.updateTBdSubstation(tBdSubstation);
    }

    /**
     * 删除工位管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTBdSubstationByIds(String ids)
    {
        return tBdSubstationMapper.deleteTBdSubstationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工位管理信息
     * 
     * @param ID 工位管理ID
     * @return 结果
     */
    @Override
    public int deleteTBdSubstationById(Long ID)
    {
        return tBdSubstationMapper.deleteTBdSubstationById(ID);
    }

    @Override
    public List<TBdSubstation> selectStationcode() {
        return tBdSubstationMapper.selectStationcode();
    }

    @Override
    public List<TBdSubstation> selectOrderid() {
        return tBdSubstationMapper.selectOrderid();
    }

    @Override
    public String createTable(Map map) {
        return tBdSubstationMapper.createTable(map);
    }

    @Override
    public void updateTable(Map map) {
        tBdSubstationMapper.updateTable(map);
    }

    @Override
    public void deletePara(Map map) {
        tBdSubstationMapper.deletePara(map);
    }

    @Override
    public List<Ztree> selectTbdStationTree(TBdSubstation substation) {
        List<TBdSubstation> substations= tBdSubstationMapper.selectTBdSubstationList(substation);
        List<Ztree> ztrees = initZtree(substations);
        return ztrees;
    }

    private List<Ztree> initZtree(List<TBdSubstation> substation) {
        return initZtree(substation,null);
    }

    public List<Ztree> initZtree(List<TBdSubstation> substation, List<String> roleDeptList)
    {

        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (TBdSubstation substation1 : substation)
        {

             Ztree ztree = new Ztree();
             ztree.setId(substation1.getStationID());
             ztree.setpId(substation1.getParentId());
             ztree.setName(substation1.getStationName());
             ztree.setTitle(substation1.getStationName());
             if (isCheck)
             {
                 ztree.setChecked(roleDeptList.contains(substation1.getStationID() + substation1.getStationName()));
             }
             ztrees.add(ztree);
        }
        return ztrees;
    }

}
