/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.annotation.DataFilter;
import io.renren.modules.sys.dao.SysPlantDao;
import io.renren.modules.sys.entity.SysPlantEntity;
import io.renren.modules.sys.service.SysPlantService;
import org.springframework.stereotype.Service;

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


@Service("sysPlantService")
public class SysPlantServiceImpl extends ServiceImpl<SysPlantDao, SysPlantEntity> implements SysPlantService {

    @Override
//    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public List<SysPlantEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }

    @Override
    public List<Long> queryPlantIdList(Long parentId) {
        return baseMapper.queryPlantIdList(parentId);
    }

    @Override
    public List<Long> getSubPlantIdList(Long PlantId) {
        //装置及子装置ID列表
        List<Long> PlantIdList = new ArrayList<>();

        //获取子装置ID
        List<Long> subIdList = queryPlantIdList(PlantId);
        getPlantTreeList(subIdList, PlantIdList);

        return PlantIdList;
    }

    /**
     * 递归
     */
    private void getPlantTreeList(List<Long> subIdList, List<Long> PlantIdList) {
        for (Long PlantId : subIdList) {
            List<Long> list = queryPlantIdList(PlantId);
            if (list.size() > 0) {
                getPlantTreeList(list, PlantIdList);
            }

            PlantIdList.add(PlantId);
        }
    }
}
