/**
 *
 */
package com.huatek.busi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.SiteConstants.ArresterTreeEnum;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.SiteConstants.GisTreeEnum;
import com.huatek.busi.common.SiteConstants.TransfromerTreeEnum;
import com.huatek.busi.common.util.GenerateUtil;
import com.huatek.busi.dao.BusiSiteDao;
import com.huatek.busi.dao.BusiSiteTreeDao;
import com.huatek.busi.dto.BusiSiteTreeDto;
import com.huatek.busi.dto.BusiSiteTreeRootNodeDto;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.BusiSiteTreeMapper;
import com.huatek.busi.service.*;
import com.huatek.busi.service.arrester.BusiOfflineArresterBrandService;
import com.huatek.busi.service.generatro.BusiOfflineGeneratorBrandService;
import com.huatek.busi.service.gis.BusiOfflineGisBrandService;
import com.huatek.busi.service.param.BusiParamAppendixService;
import com.huatek.busi.service.param.BusiParamArresterService;
import com.huatek.busi.service.param.BusiParamGisService;
import com.huatek.busi.service.param.BusiParamTransformerService;
import com.huatek.busi.service.temperature.BusiOfflineSwitchgearBrandService;
import com.huatek.busi.service.transformer.BusiOfflineTransformerBrandService;
import com.huatek.busi.vo.*;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName: BusiSiteTreeServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.BusiSiteTreeServiceImpl
 * @Description: 站点树服务实现
 * @author: Arno
 * @date: Jun 21, 2019 10:23:37 AM
 * @version: 1.0
 */
@Slf4j
@Component
@Service(interfaceClass = BusiSiteTreeService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiSiteTreeServiceImpl implements BusiSiteTreeService {
	@Autowired
	private BusiSubTreeService busiSubTreeService;
	@Autowired
	private BusiOfflineTransformerBrandService busiOfflineTransformerBrandService;
	@Autowired
	private BusiOfflineGisBrandService busiOfflineGisBrandService;
	@Autowired
	private BusiOfflineSwitchgearBrandService busiOfflineSwitchgearBrandService;
	@Autowired
	private BusiOfflineArresterBrandService busiOfflineArresterBrandService;
	@Reference(check = false)
	IBusiOfflineCableBrandService busiOfflineCableBrandService;
	@Autowired
	private BusiParamGisService busiParamGisService;

	@Autowired
	private BusiParamArresterService busiParamArresterService;

	@Autowired
	private BusiParamAppendixService busiParamAppendixService;



	@Autowired
	private BusiParamTransformerService busiParamTransformerService;
//	@Autowired
//	private TransformerRunStatusThresholdService transformerRunStatusThresholdService;

	@Autowired
    private BusiArresterSubTreeService busiArresterSubTreeService;

	@Autowired
    private BusiGisSubTreeService busiGisSubTreeService;

	@Autowired
    private BusiTransfromerSubTreeService busiTransfromerSubTreeService;

	@Autowired
    private	BusiOtherSubTreeService busiOtherSubTreeService;

	@Autowired
	private BusiSiteDao busiSiteDao;

	@Autowired
	private BusiSiteTreeDao busiSiteTreeDao;

	@Autowired
	private BusiSiteTreeMapper busiSiteTreeMapper;

	@Autowired
    private BusiSensorService busiSensorService;

	@Autowired
	private BusiOfflineGeneratorBrandService busiOfflineGeneratorBrandService;

	@Reference(check = false)
	IBusiCableSubTreeService busiCableSubTreeService;

	@Override
	public BusiSiteTree findByPK(String pk) {
		return busiSiteTreeDao.get(pk);
	}

	@Override
	public Boolean insertSiteNode(String siteId, String nodeName) {
		BusiSiteTree busiSiteTree = createBusiTreeNode(nodeName, "1", SiteConstants.SiteTreeEnum.SITE.getValue(),
				siteId);
		return busiSiteTreeDao.add(busiSiteTree) > 0;
	}


	@Override
	public Boolean updateSiteTreeName(String siteId, String newName) {
		List<BusiSiteTree> list = busiSiteTreeMapper.findByEquipmentTypeAndSiteId(siteId, SiteConstants.SiteTreeEnum.SITE.getValue());
		if(null != list && !list.isEmpty()){
			list.stream().forEach(tree -> {
				tree.setName(newName);
				busiSiteTreeDao.updateLocal(tree);
			});
		}
		return true;
	}

	@Override
	public Boolean insertSiteTypeNode(String siteNodeId, Map<String, Object> equipmentMap) {
		BusiSiteTree parent = this.busiSiteTreeDao.get(siteNodeId);
		String siteId = parent.getSiteId();

		Integer transfromCount = getEquipmentCount(SiteConstants.SiteTreeEnum.BYQ.getValue(), equipmentMap);
		Integer gisCount = getEquipmentCount(SiteConstants.SiteTreeEnum.GIS.getValue(), equipmentMap);
		Integer cwCount = getEquipmentCount(SiteConstants.SiteTreeEnum.CW.getValue(), equipmentMap);
		Integer blqCount = getEquipmentCount(SiteConstants.SiteTreeEnum.BLQ.getValue(), equipmentMap);
		Integer genCount = getEquipmentCount(SiteConstants.SiteTreeEnum.GEN.getValue(), equipmentMap);
		// 电缆
		Integer dlCount = getEquipmentCount(SiteConstants.SiteTreeEnum.DL.getValue(), equipmentMap);
		Integer otherCount = getEquipmentCount(SiteConstants.SiteTreeEnum.OTHER.getValue(), equipmentMap);

		if (transfromCount > 0) {
			createNode(transfromCount, siteNodeId, SiteConstants.SiteTreeEnum.BYQ.getValue(), siteId);
		}
		if (gisCount > 0) {
			createNode(gisCount, siteNodeId, SiteConstants.SiteTreeEnum.GIS.getValue(), siteId);
		}
		if (cwCount > 0) {
			createNode(cwCount, siteNodeId, SiteConstants.SiteTreeEnum.CW.getValue(), siteId);
		}
		if (blqCount > 0) {
			createNode(blqCount, siteNodeId, SiteConstants.SiteTreeEnum.BLQ.getValue(), siteId);
		}
		if (genCount > 0) {
			createNode(genCount, siteNodeId, SiteConstants.SiteTreeEnum.GEN.getValue(), siteId);
		}
		if (otherCount > 0) {
            createNode(otherCount, siteNodeId, SiteConstants.SiteTreeEnum.OTHER.getValue(), siteId);
        }
		// 电缆
		if(dlCount > 0){
			createNode(dlCount, siteNodeId, SiteConstants.SiteTreeEnum.DL.getValue(), siteId);
		}

		return true;
	}

	@Override
	public Boolean insertSiteEquipmentNode(String parentId, Integer count) {
		BusiSiteTree parent = this.busiSiteTreeDao.get(parentId);
		if (null != parent) {
			String siteId = parent.getSiteId();
			for (int i = 0; i < count; i++) {
				if (parent.getEquipmentType().equals(SiteConstants.SiteTreeEnum.GIS_HL.getValue())) {
					BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getName(),
							parent.getId(), SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue(), siteId);
					busiSiteTreeDao.add(tmp);
					this.updateEquipmentByTree(tmp);
					busiSubTreeService.insertSubTree(tmp);
					//初始化GIS分析参数
					busiParamGisService.initBusiParams(null, siteId, tmp.getId());
				} else if (parent.getEquipmentType().equals(SiteConstants.SiteTreeEnum.CW_HL.getValue())) {
					BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.CW_HL_EQM.getName(),
							parent.getId(), SiteConstants.SiteTreeEnum.CW_HL_EQM.getValue(), siteId);
					busiSiteTreeDao.add(tmp);
					this.updateEquipmentByTree(tmp);
					busiSubTreeService.insertSubTree(tmp);
				} else if (parent.getEquipmentType().equals(SiteConstants.SiteTreeEnum.BLQ_HL.getValue())) {
					BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getName(),
							parent.getId(), SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue(), siteId);
					busiSiteTreeDao.add(tmp);
					this.updateEquipmentByTree(tmp);
					busiSubTreeService.insertSubTree(tmp);
					//初始化避雷器分析参数
					busiParamArresterService.initBusiParams(null, siteId, tmp.getId());
				}else if(Objects.equals(parent.getEquipmentType(), SiteConstants.SiteTreeEnum.DL_HL.getValue())){
					BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.DL_EQM.getName(),
							parent.getId(), SiteConstants.SiteTreeEnum.DL_EQM.getValue(), siteId);
					busiSiteTreeDao.add(tmp);
					// TODO 更新电缆标牌信息
					this.updateEquipmentByTree(tmp);
					// TODO 初始化电缆分析参数
				}
				else if(Objects.equals(parent.getEquipmentType(), SiteConstants.SiteTreeEnum.GEN_HL.getValue())){
					BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.GEN_EQM.getName(),
							parent.getId(), SiteConstants.SiteTreeEnum.GEN_EQM.getValue(), siteId);
					busiSiteTreeDao.add(tmp);
					// TODO 更新发电机标牌信息
					this.updateEquipmentByTree(tmp);
					// TODO 初始化发电机分析参数
				}
//				else if (parent.getEquipmentType().equals(SiteConstants.SiteTreeEnum.OTHER_HL.getValue())) {
//                        BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.OTHER_HL_EQM.getName(),
//                            parent.getId(), SiteConstants.SiteTreeEnum.OTHER_HL_EQM.getValue(), siteId);
//                    busiSiteTreeDao.add(tmp);
//                    this.updateEquipmentByTree(tmp);
//                    busiSubTreeService.insertSubTree(tmp);
//				}
			}
		}
		return true;
	}

	@Override
	public Boolean updateNode(BusiSiteTreeDto  busiSiteTreeDto) {
		BusiSiteTree find = busiSiteTreeDao.get(busiSiteTreeDto.getId());
		if (null != find) {
//			String id = find.getId();
			String oldName = find.getName();
//			busiSiteTreeDto.setId(id);
//			busiSiteTreeDto.setName(busiSiteTreeDto.getName());
//			busiSiteTreeMapper.updateTree(busiSiteTreeDto);
			find.setName(busiSiteTreeDto.getName());
			busiSiteTreeDao.updateLocal(find);
			if (!oldName.equals(busiSiteTreeDto.getName())) {
				this.updateEquipmentByTree(find);
			}
			if(find.getEquipmentType().equals(SiteConstants.SiteTreeEnum.SITE.getValue())){
				this.updateSite(find);
			}
		}
		return true;
	}

	// 修改站点名称
	public void updateSite(BusiSiteTree find) {
		BusiSite busiSite = busiSiteDao.get(find.getSiteId());
		busiSite.setSiteName(find.getName());
		busiSiteDao.updateLocal(busiSite);
	}

	@Override
	public Boolean updateNodeFromEquipment(String nodeId, String newName) {
		BusiSiteTree find = this.busiSiteTreeDao.get(nodeId);
		if (null != find) {
			String oldName = find.getName();
			if (!oldName.equals(newName)) {
				find.setName(newName);
				busiSiteTreeDao.updateLocal(find);
			}
		}
		return true;
	}


	public void deleteInfo(String equipmentId,String equipmentType)
	{
	    List<BusiSensorVo> busiSensorList = busiSensorService.querySensorByEquipMentIdAndType(equipmentId, equipmentType);

	    String type="";
	    if(SiteConstants.SiteTreeEnum.BYQ_EQM.getValue().equals(equipmentType)){
            type = SiteConstants.ExternalTypeEnum.BYQ.getValue();

        }else if(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue().equals(equipmentType)){
            type = SiteConstants.ExternalTypeEnum.GIS.getValue();

        }else if(SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue().equals(equipmentType)){
            type = equipmentType = SiteConstants.ExternalTypeEnum.BLQ.getValue();
        }else if(SiteConstants.SiteTreeEnum.OTHER_EQM.getValue().equals(equipmentType)){
            type = equipmentType = SiteConstants.ExternalTypeEnum.OTHER.getValue();
        }else if(Objects.equals(equipmentType, SiteConstants.SiteTreeEnum.DL_EQM.getValue())){
			type = SiteConstants.ExternalTypeEnum.DL.getValue();
		}else if(Objects.equals(equipmentType, SiteConstants.SiteTreeEnum.GEN_EQM.getValue())){
			type = SiteConstants.ExternalTypeEnum.GEN.getValue();
		}


        if(!busiSensorList.isEmpty())
        {
            StringBuffer sb = new StringBuffer();
            for(BusiSensorVo busiSensorVo:busiSensorList)
            {
                sb.append(busiSensorVo.getId()).append(",");
            }
            String ids = sb.toString();
            busiSensorService.delete(ids.substring(0,ids.length()-1), type);
        }
	}

	@Override
	public Boolean deleteNode(String nodeId) {
		List<BusiSiteTree> list = getChildrens(nodeId);
		if (!list.isEmpty()) {
			list.forEach(tree -> {
				tree.setStatus(SiteConstants.Status.DELETE.getValue());
//				busiSiteTreeDao.updateLocal(tree);
				busiSiteTreeMapper.isDelete(tree);
			});
		}
		BusiSiteTree find = this.busiSiteTreeDao.get(nodeId);

		if(null != find ){
		    if("1".equals(find.getParentId()))
	        {
	            String siteId = find.getSiteId();
	            BusiSite site = new BusiSite();
	            site.setId(siteId);
	            site.setSiteName(find.getName());
	            site.setStatus(SiteConstants.Status.DELETE.getValue());
	            busiSiteDao.updateLocal(site);
	        }else
	        {
	            String equipmentType = find.getEquipmentType();
	            if(equipmentType.equals(SiteConstants.SiteTreeEnum.BYQ_EQM.getValue())){
//	              busiOfflineTransformerBrandService.deleteByMainEquipmentId(find.getId());

	                //根据nodeId获取子树信息
	                List<BusiTransfromerSubTree> transformerList = busiTransfromerSubTreeService.findByEquipmentId(find.getId());
	                //遍历子树获取子树下的sensor
	                if(!transformerList.isEmpty())
	                {
	                    for(BusiTransfromerSubTree busiTransfromerSubTree:transformerList)
	                    {
	                        deleteInfo(busiTransfromerSubTree.getId(), equipmentType);
	                    }
	                }
	            }else if(equipmentType.equals(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue())){
	                //根据nodeId获取子树信息
	                List<BusiGisSubTree> gisList = busiGisSubTreeService.findByEquipmentId(find.getId());
	                //遍历子树获取子树下的sensor
	                if(!gisList.isEmpty())
	                {
	                    for(BusiGisSubTree BusiGisSubTree:gisList)
	                    {
	                        deleteInfo(BusiGisSubTree.getId(), equipmentType);
	                    }
	                }
	            }else if(equipmentType.equals(SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue())){
	                //根据nodeId获取子树信息
	                List<BusiArresterSubTree> gisList = busiArresterSubTreeService.findByEquipmentId(find.getId());
	                //遍历子树获取子树下的sensor
	                if(!gisList.isEmpty())
	                {
	                    for(BusiArresterSubTree busiArresterSubTree:gisList)
	                    {
	                        deleteInfo(busiArresterSubTree.getId(), equipmentType);
	                    }
	                }
	            }else if(equipmentType.equals(SiteConstants.SiteTreeEnum.OTHER_EQM.getValue())){
	                //根据nodeId获取子树信息
	                List<BusiOtherSubTree> otherList = busiOtherSubTreeService.findByEquipmentId(find.getId());
	                //遍历子树获取子树下的sensor
	                if(!otherList.isEmpty())
	                {
	                    for(BusiOtherSubTree busiOtherSubTree:otherList)
	                    {
	                        deleteInfo(busiOtherSubTree.getId(), equipmentType);
	                    }
	                }
	            }else if(Objects.equals(equipmentType, SiteConstants.SiteTreeEnum.DL_EQM.getValue())){
	            	//根据nodeId获取子树信息
					List<BusiCableSubTree> busiCableSubTreeList = busiCableSubTreeService.findByEquipmentId(find.getId());
					if(CollectionUtil.isNotEmpty(busiCableSubTreeList)){
						busiCableSubTreeList.stream().forEach(item -> deleteInfo(item.getId(), equipmentType));
					}
				}
	        }
		}
		return true;
	}

	@Override
	public List<BusiSiteTree> getChildrens(String nodeId) {
		List<BusiSiteTree> list = new ArrayList<>();
		BusiSiteTree find = this.busiSiteTreeDao.get(nodeId);
		list.add(find);
		list = getChiildNodes(list, find);
		return list;
	}



	/**
	 * 根据系统编码查询站点
	 *
	 * @param
	 * @return
	 */
	@Override
	public BusiSiteTree geBusiSiteTreeBysystemCode(String systemCode) {
		return busiSiteTreeMapper.geBusiSiteTreeBysystemCode(systemCode);
	}

	/**
	 * 根据站点ID查询站点节点数据
	 */
	@Override
	public List<BusiSiteTree> findListBySiteId(String siteId) {
		return busiSiteTreeMapper.findListBySiteId(siteId);
	}

	/**
	 * 递归获取某个id下面的所有子节点
	 */
	private List<BusiSiteTree> getChiildNodes(List<BusiSiteTree> list, BusiSiteTree busiSiteTree) {
		List<BusiSiteTree> childrens = busiSiteTreeMapper.getChildrens(busiSiteTree.getId());
		for (BusiSiteTree d : childrens) {
			list.add(d);
			getChiildNodes(list, d);
		}
		return list;
	}

	public void createNode(Integer count, String parentId, String equipmentType, String siteId) {
		String nodeName = "";
		if (equipmentType.equals(SiteConstants.SiteTreeEnum.BYQ.getValue())) {
			nodeName = SiteConstants.SiteTreeEnum.BYQ.getName();
		} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.GIS.getValue())) {
			nodeName = SiteConstants.SiteTreeEnum.GIS.getName();
		} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.CW.getValue())) {
			nodeName = SiteConstants.SiteTreeEnum.CW.getName();
		} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.BLQ.getValue())) {
			nodeName = SiteConstants.SiteTreeEnum.BLQ.getName();
		}  else if (equipmentType.equals(SiteConstants.SiteTreeEnum.GEN.getValue())) {
			nodeName = SiteConstants.SiteTreeEnum.GEN.getName();
		}else if (equipmentType.equals(SiteConstants.SiteTreeEnum.OTHER.getValue())) {
            nodeName = SiteConstants.SiteTreeEnum.OTHER.getName();
        }else if(Objects.equals(equipmentType, SiteConstants.SiteTreeEnum.DL.getValue())){
			nodeName = SiteConstants.SiteTreeEnum.DL.getName();
		}

		BusiSiteTree find = this.findByEquipmentType(parentId, equipmentType);
		if (null == find) {
			find = createBusiTreeNode(nodeName, parentId, equipmentType, siteId);
			busiSiteTreeDao.add(find);
		}

		for (int i = 0; i < count; i++) {
			if (equipmentType.equals(SiteConstants.SiteTreeEnum.BYQ.getValue())) {
				BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.BYQ_EQM.getName(), find.getId(),
						SiteConstants.SiteTreeEnum.BYQ_EQM.getValue(), siteId);
				busiSiteTreeDao.add(tmp);

//				/**
//				 * 根据主设备ID，新增三种状态下的运行工况阈值
//				 * 传入主设备ID,站点ID作为参数
//				 */
//				transformerRunStatusThresholdService.insertTransformerRunStatusThreshold(tmp.getId(),tmp.getSiteId());

				this.updateEquipmentByTree(tmp);
				busiSubTreeService.insertSubTree(tmp);
				//初始化变压器分析参数
				busiParamTransformerService.initBusiParams(null, siteId, tmp.getId());
				//初始化附件分析参数
				busiParamAppendixService.initBusiParams(null, siteId, tmp.getId());
			} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.GIS.getValue())) {
				BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.GIS_HL.getName(), find.getId(),
						SiteConstants.SiteTreeEnum.GIS_HL.getValue(), siteId);
				busiSiteTreeDao.add(tmp);
			} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.CW.getValue())) {
				BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.CW_HL.getName(), find.getId(),
						SiteConstants.SiteTreeEnum.CW_HL.getValue(), siteId);
				busiSiteTreeDao.add(tmp);
			} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.BLQ.getValue())) {
				BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.BLQ_HL.getName(), find.getId(),
						SiteConstants.SiteTreeEnum.BLQ_HL.getValue(), siteId);
				busiSiteTreeDao.add(tmp);
			}else if (equipmentType.equals(SiteConstants.SiteTreeEnum.GEN.getValue())) {
				BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.GEN_EQM.getName(), find.getId(),
						SiteConstants.SiteTreeEnum.GEN_EQM.getValue(), siteId);
				busiSiteTreeDao.add(tmp);
				this.updateEquipmentByTree(tmp);
				busiSubTreeService.insertSubTree(tmp);


			} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.OTHER.getValue())) {
                BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.OTHER_EQM.getName(), find.getId(),
                        SiteConstants.SiteTreeEnum.OTHER_EQM.getValue(), siteId);
                busiSiteTreeDao.add(tmp);
                this.updateEquipmentByTree(tmp);
                busiSubTreeService.insertSubTree(tmp);
            }else if(Objects.equals(equipmentType, SiteConstants.SiteTreeEnum.DL.getValue())){
				BusiSiteTree tmp = createBusiTreeNode(SiteConstants.SiteTreeEnum.DL_EQM.getName(), find.getId(),
						SiteConstants.SiteTreeEnum.DL_EQM.getValue(), siteId);
				busiSiteTreeDao.add(tmp);
				//TODO 更新电缆标牌信息
				this.updateEquipmentByTree(tmp);
				busiSubTreeService.insertSubTree(tmp);
				//TODO 初始化电缆分析参数

			}
		}
	}

	/**
	 * @Title: createBusiTreeNode
	 * @Description: 创建树节点
	 * @createDate: Jun 24, 2019 3:14:13 PM
	 * @param nodeName
	 *            节点名称
	 * @param parentId
	 *            父节点ID
	 * @param equipmentType
	 *            节点类型
	 * @param siteId
	 *            站点ID
	 * @return BusiSiteTree
	 */
	public BusiSiteTree createBusiTreeNode(String nodeName, String parentId, String equipmentType, String siteId) {
		BusiSiteTree busiSiteTree = new BusiSiteTree();
		String id = Uuid.getUUID();
		busiSiteTree.setId(id);
		busiSiteTree.setParentId(parentId);
		busiSiteTree.setName(nodeName);
		busiSiteTree.setStatus(SiteConstants.Status.NORMAL.getValue());
		busiSiteTree.setSiteId(siteId);
		busiSiteTree.setEquipmentType(equipmentType);
		busiSiteTree.setSystemCode(GenerateUtil.generateCode(17));
		busiSiteTree.setCreateTime(LocalDateTime.now());
		return busiSiteTree;
	}

	public BusiSiteTree findByEquipmentType(String parentId, String equipmentType) {
		return busiSiteTreeMapper.findByEquipmentType(parentId, equipmentType);
	}

	/**
	 * @Title: getEquipmentCount
	 * @Description: 获取设备数量
	 * @createDate: Jun 24, 2019 2:04:08 PM
	 * @param
	 * @param
	 * 			@return Integer @throws
	 */
	public Integer getEquipmentCount(String equipmentType, Map<String, Object> equipmentMap) {
		Integer count = 0;
		if (null != equipmentMap && !equipmentMap.isEmpty()) {
			count = equipmentMap.get(equipmentType) == null || "".equals(equipmentMap.get(equipmentType).toString()) ? 0
					: Integer.valueOf(equipmentMap.get(equipmentType).toString());
		}
		return count;
	}

	/**
	 * 导出站点Sensior设备信息
	 */
	@Override
	public List<SensiorIdExportVo> exportSensiorData(String siteId) {
		List<SensiorIdExportVo> list = busiSiteTreeMapper.selectSensorListBySiteId(siteId);
		if (list.isEmpty()) {
			list = new ArrayList<>();
			list.add(new SensiorIdExportVo());
		} else {
			list.stream().forEach(sensorItem ->{
				if (sensorItem.getType().equals(ExternalTypeEnum.BYQ.getValue())) {
					sensorItem.setPendantType(TransfromerTreeEnum.getName(sensorItem.getPendantType()));
				}
				if (sensorItem.getType().equals(ExternalTypeEnum.GIS.getValue())) {
					sensorItem.setPendantType(GisTreeEnum.getName(sensorItem.getPendantType()));
				}
				if (sensorItem.getType().equals(ExternalTypeEnum.BLQ.getValue())) {
					sensorItem.setPendantType(ArresterTreeEnum.getName(sensorItem.getPendantType()));
				}
				if(Objects.equals(sensorItem.getType(), ExternalTypeEnum.DL.getValue())){
					sensorItem.setPendantType(SiteConstants.CableTreeEnum.getName(sensorItem.getPendantType()));
				}
				if(Objects.equals(sensorItem.getType(), ExternalTypeEnum.GEN.getValue())){
					sensorItem.setPendantType(SiteConstants.GeneratorTreeEnum.getName(sensorItem.getPendantType()));
				}
				if(Objects.equals(sensorItem.getType(), ExternalTypeEnum.CW.getValue())){
					sensorItem.setPendantType("开关柜测温");
				}
				if(Objects.equals(sensorItem.getType(), ExternalTypeEnum.OTHER.getValue())){
					sensorItem.setPendantType(SiteConstants.OtherTreeEnum.getName(sensorItem.getPendantType()));
				}
			});
		}
		return list;
	}

	@Override
	public List<BusiTreeNodeVo> siteTreeForCalculate(String parentId) {
		List<BusiTreeNodeVo> list = new ArrayList<>();
		// 查询除GIS 211外的其他所有设备
		List<BusiTreeNodeVo> listWithoutGis = busiSiteTreeMapper.findListWithoutGis(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue());
		// 查询除GIS 中的断路器及开关
		List<BusiTreeNodeVo> listGis = busiSiteTreeMapper.findGisMainEquipmentList(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue());
		// 合并两个list
		listWithoutGis.addAll(listGis);
		// 先找到所有的一级
		for (BusiTreeNodeVo vo : listWithoutGis) { // 一级菜单parentId == 0
			if (vo.getPid().equals("0")) {
				list.add(vo);
			}
		}
		for (BusiTreeNodeVo vo : list) {
			vo.setChildren(getChild(vo.getId(), listWithoutGis));
		}

		return list;
	}

	@Override
	public List<BusiTreeNodeVo> siteTree(String parentId) {
		List<BusiTreeNodeVo> list = new ArrayList<>();
		List<BusiTreeNodeVo> findlists = busiSiteTreeMapper.findlist2();
		// 先找到所有的一级
		for (BusiTreeNodeVo vo : findlists) { // 一级菜单parentId == 0
			if (vo.getPid().equals("0")) {
				list.add(vo);
			}
		}
		for (BusiTreeNodeVo vo : list) {
			vo.setChildren(getChild(vo.getId(), findlists));
		}

		return list;
	}

	private List<BusiTreeNodeVo> getChild(String id, List<BusiTreeNodeVo> rootMenu) {
		// 子集合
		List<BusiTreeNodeVo> childList = new ArrayList<>();
		for (BusiTreeNodeVo child : rootMenu) {
			// 遍历所有节点，将父id与传过来的id比较
			if (child.getPid().equals(id)) {
				childList.add(child);
			}
		}
		// 把子集合再循环一遍
		for (BusiTreeNodeVo child : childList) {
			// 递归
			child.setChildren(getChild(child.getId(), rootMenu));
		}
		// 递归退出条件
		if (childList.size() == 0) {
			return null;
		}
		return childList;
	}

	/**
	 * @Title: updateEquipmentByTree @Description: 跟新主设备名称信息 @createDate: Jul 17,
	 * 2019 4:33:31 PM @param find 树节点信息 @return void @throws
	 */
	private void updateEquipmentByTree(BusiSiteTree find) {
		String equipmentType = find.getEquipmentType();
		String name = find.getName();
		String id = find.getId();
		String siteId = find.getSiteId();
		// 更新设备名称
		if (equipmentType.equals(SiteConstants.SiteTreeEnum.BYQ_EQM.getValue())) {
			busiOfflineTransformerBrandService.saveOrUpdateBrand(siteId, id, name);
		} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue())) {
			busiOfflineGisBrandService.saveOrUpdateBrand(siteId, id, name);
		} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.CW_HL_EQM.getValue())) {
			busiOfflineSwitchgearBrandService.saveOrUpdateBrand(siteId, id, name);
		} else if (equipmentType.equals(SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue())) {
			busiOfflineArresterBrandService.saveOrUpdateBrand(siteId, id, name);
		}else if(Objects.equals(equipmentType, SiteConstants.SiteTreeEnum.DL_EQM.getValue())){
			busiOfflineCableBrandService.saveOrUpdateBrand(siteId, id, name);
		}else if(Objects.equals(equipmentType, SiteConstants.SiteTreeEnum.GEN_EQM.getValue())){
			busiOfflineGeneratorBrandService.saveOrUpdateBrand(siteId, id, name);
		}
//		else if (equipmentType.equals(SiteConstants.SiteTreeEnum.OTHER_HL_EQM.getValue())) {
//            busiOfflineArresterBrandService.saveOrUpdateBrand(siteId, id, name);
//        }

		busiSubTreeService.updateNodeBySiteTree(find);
	}

    @Override
    public List<BusiTreeNodeVo> findMainEquipmentList(String[] equipmentTypes) {
        return busiSiteTreeMapper.findMainEquipmentList(equipmentTypes);
    }

	// 查询GIS信息，根据类型过滤，只取断路器及开关
	@Override
	public List<BusiTreeNodeVo> findGisMainEquipmentList(String equipmentTypes) {
		return busiSiteTreeMapper.findGisMainEquipmentList(equipmentTypes);
	}

	@Override
	public BusiSiteTree findByEquType(String equType){
		return busiSiteTreeMapper.findByEquType(equType);
	}

	@Override
	public BusiTreeNodeVo findByNameAndEquType(String equName, String eqType){
		return busiSiteTreeMapper.findByNameAndEquType(equName, eqType);
	}

	@Override
	public BusiTreeNodeVo findByIdAndEquType(String id, String eqType){
		return busiSiteTreeMapper.findByIdAndEquType(id, eqType);
	}

	@Override
	public List<BusiSiteTree> findByParentId(String id){
		return busiSiteTreeMapper.findByParentId(id);
	}

	@Override
	public List<BusiSiteTree> findByParentIds(List<String> ids){
		return busiSiteTreeMapper.findByParentIds(ids);
	}

	@Override
	public BusiTreeNodeVo findByEquNumAndequNameAndEquType(String systemCode, String equName, String eqType){
		return busiSiteTreeMapper.findByEquNumAndequNameAndEquType(systemCode, equName, eqType);
	}

	@Override
	public List<BusiTreeNodeVo> findAll(){
		return busiSiteTreeMapper.findAll();
	}

	@Override
	public List<BusiTreeNodeVo> findByEquTypes(String equType){
		return busiSiteTreeMapper.findByEquTypes(equType);
	}

	@Override
	public List<BusiTreeNodeVo> findThirdLevelByEquTypes(String equType,String equId){
		return busiSiteTreeMapper.findThirdLevelByEquTypes(equType,equId);
	}

	@Override
	public List<TransformerTmpVo> findByTransformerBySiteId(String siteId) {
		return busiSiteTreeMapper.selectByTransformerBySiteId(siteId);
	}

    @Override
    public String getDefaultSiteId() {
        List<String> siteIds = busiSiteTreeMapper.getSiteIds();
        if(CollectionUtils.isEmpty(siteIds)) {
            return null;
        }
        if(siteIds.size() > 1) {
            log.warn("找到了多个站点ID");
        }
         return siteIds.get(0);
    }

	@Override
	public BusiEquipmentVo findBySensorCode(String sensorCode, String type) {
		// TODO Auto-generated method stub
		if(type.equals(SiteConstants.ExternalTypeEnum.BYQ.getValue())){
	    	return busiSiteTreeMapper.selectTransformerBySensorCode(sensorCode);
	    }else if(type.equals(SiteConstants.ExternalTypeEnum.GIS.getValue())){
	    	return busiSiteTreeMapper.selectGisBySensorCode(sensorCode);
	    }else   if(type.equals(SiteConstants.ExternalTypeEnum.BLQ.getValue())){
	    	return busiSiteTreeMapper.selectArresterBySensorCode(sensorCode);
	    }
		return null;
	}

	@Override
	public List<TransformerTmpVo> selectByTransformerAndBindRelationBySiteId(String siteId,String equipmentId) {
		// TODO Auto-generated method stub
		return busiSiteTreeMapper.selectByTransformerAndBindRelationBySiteId(siteId,equipmentId);
	}

	@Override
	public List<BusiTreeNodeVo> inputDataTree(String parentId) {
		// TODO Auto-generated method stub
		List<BusiTreeNodeVo> list = new ArrayList<>();
		List<BusiTreeNodeVo> findlists = busiSiteTreeMapper.findInputDataTree();
		// 先找到所有的一级
		for (BusiTreeNodeVo vo : findlists) { // 一级菜单parentId == 0
			if (vo.getPid().equals("0")) {
				list.add(vo);
			}
		}
		for (BusiTreeNodeVo vo : list) {
			vo.setChildren(getChild(vo.getId(), findlists));
		}

		return list;
	}

	@Override
	public BusiSiteTreeRootNodeDto getSiteEquipmentNodeBySiteIdAndEquType(String siteId, String equType){
		return busiSiteTreeMapper.getSiteEquipmentNodeBySiteIdAndEquType(siteId, equType);
	}

	@Override
	public BusiSiteTree findByMainEquipmentIdAndSiteId(String mainEquipmentId, String siteId){
		return busiSiteTreeMapper.findByMainEquipmentIdAndSiteId(mainEquipmentId, siteId);
	}
}
