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

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.mapper.BusiSubTreeMapper;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.busi.vo.BusiTreeNodeVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: BusiSubTreeServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.BusiSubTreeServiceImpl
 * @Description: 外挂设备树服务
 * @author: Arno
 * @date: Jun 27, 2019 6:35:34 PM
 * @version: 1.0
 */
@Slf4j
@Component
@Service(interfaceClass = BusiSubTreeService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiSubTreeServiceImpl implements BusiSubTreeService {
    @Autowired
    private BusiTransfromerSubTreeService busiTransfromerSubTreeService;
    @Autowired
    private BusiSensorService busiSensorService;
    @Autowired
    private BusiGisSubTreeService busiGisSubTreeService;
    @Autowired
    private BusiArresterSubTreeService busiArresterSubTreeService;
    @Autowired
    private BusiOtherSubTreeService busiOtherSubTreeService;
    @Autowired
    private BusiSubTreeMapper busiSubTreeMapper;

    @Reference(check = false)
    IBusiCableSubTreeService busiCableSubTreeService;

    @Reference(check = false)
    BusiGeneratorSubTreeService busiGeneratorSubTreeService;

    @Override
    public Boolean insertSubTree(BusiSiteTree busiSiteTree) {
        String equipmentType = busiSiteTree.getEquipmentType();
        if (equipmentType.equals(SiteConstants.SiteTreeEnum.BYQ_EQM.getValue())) {
            busiTransfromerSubTreeService.insertSubTreeForType(busiSiteTree);
        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue())) {
            busiGisSubTreeService.insertSubTreeForType(busiSiteTree);
        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue())) {
            busiArresterSubTreeService.insertSubTreeForType(busiSiteTree);
        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.CW_HL_EQM.getValue())) {
            // TODO
        } else if (Objects.equals(equipmentType, SiteConstants.SiteTreeEnum.DL_EQM.getValue())) {
            // 新增电缆子树数据
            busiCableSubTreeService.insertSubTreeForType(busiSiteTree);
        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.GEN_EQM.getValue())) {
            busiGeneratorSubTreeService.insertSubTreeForType(busiSiteTree);
        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.OTHER_EQM.getValue())) {
            busiOtherSubTreeService.insertSubTreeForType(busiSiteTree);
        }
        return true;
    }

    @Override
    public EquipmentInfoDto findEquipmentInfo(String sensorCode, String equipmentType) {
        EquipmentInfoDto info = null;
        SiteTreeEnum siteTreeEnum = SiteTreeEnum.fromValue(equipmentType);
        switch (siteTreeEnum) {
            case BLQ:
            case BLQ_HL:
            case BLQ_HL_EQM:
                info = busiSubTreeMapper.getArresterBySensor(sensorCode);
                break;
            case BYQ:
            case BYQ_EQM:
                info = busiSubTreeMapper.getTransformerBySensor(sensorCode);
                break;
            case CW:
            case CW_HL:
            case CW_HL_EQM:
                info = busiSubTreeMapper.getCwBySensor(sensorCode);
                break;
            case GIS:
            case GIS_HL:
            case GIS_HL_EQM:
                info = busiSubTreeMapper.getGisBySensor(sensorCode);
                break;
            case GEN:
            case GEN_HL:
            case GEN_EQM:
                info = busiSubTreeMapper.getGeneratorBySensor(sensorCode);
                break;
            case DL:
            case DL_HL:
            case DL_EQM:
                info = busiSubTreeMapper.getCableBySensor(sensorCode);
                break;
            case OTHER:
            case OTHER_EQM:
                info = busiSubTreeMapper.getOtherBySensor(sensorCode);
                break;
            case ROOT:
            case SITE:
                log.warn("枚举参数传参错误：equipmentType={}", equipmentType);
                break;
        }
        if (null != info) {
            info.setType(equipmentType);
        }
        log.debug("{}------->传感器编码[{}]------>设备信息[{}]", equipmentType, sensorCode, info);
        return info;
    }

    @Override
    public List<BusiTreeNodeVo> tree(String nodeId, String nodeType) {
        List<BusiTreeNodeVo> list = new ArrayList<>();
        List<BusiTreeNodeVo> findlists = new ArrayList<>();
        if (SiteConstants.SiteTreeEnum.BYQ_EQM.getValue().equals(nodeType)) {
            findlists = busiSubTreeMapper.getTransformerSubTree(nodeId, null);
        } else if (SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue().equals(nodeType)) {
            findlists = busiSubTreeMapper.getGisSubTree(nodeId, null);
        } else if (SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue().equals(nodeType)) {
            findlists = busiSubTreeMapper.getArresterSubTree(nodeId, null);
        } else if (SiteConstants.SiteTreeEnum.GEN_EQM.getValue().equals(nodeType)) {
            findlists = busiSubTreeMapper.getGeneratorSubTree(nodeId, null);
        } else if (SiteConstants.SiteTreeEnum.OTHER_EQM.getValue().equals(nodeType)) {
            findlists = busiSubTreeMapper.getOtherSubTree(nodeId, null);
        } else if (SiteConstants.SiteTreeEnum.DL_EQM.getValue().equals(nodeType)) {
            findlists = busiSubTreeMapper.getCableSubTree(nodeId, null);
        }

        if (!findlists.isEmpty()) {
            for (BusiTreeNodeVo vo : findlists) {
                // 一级菜单parentId == 0
                if (vo.getPid().equals("0")) {
                    list.add(vo);
                }
            }
            for (BusiTreeNodeVo vo : list) {
                vo.setChildren(getChild(vo.getId(), findlists));
            }
            log.debug("----->> 站点树结构 --->[{}]", JSON.toJSONString(list));
        }

        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;
    }

    @Override
    public void updateNode(String nodeId, String nodeType, String newName) {
        if (SiteConstants.SiteTreeEnum.BYQ_EQM.getValue().equals(nodeType)) {
            busiTransfromerSubTreeService.updateNode(nodeId, newName);
        } else if (SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue().equals(nodeType)) {
            busiGisSubTreeService.updateNode(nodeId, newName);
        } else if (SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue().equals(nodeType)) {
            busiArresterSubTreeService.updateNode(nodeId, newName);
        } else if (SiteConstants.SiteTreeEnum.OTHER_EQM.getValue().equals(nodeType)) {
            busiOtherSubTreeService.updateNode(nodeId, newName);
        } else if (SiteConstants.SiteTreeEnum.DL_EQM.getValue().equals(nodeType)) {
            busiCableSubTreeService.updateNode(nodeId, newName);
        } else if (SiteConstants.SiteTreeEnum.GEN_EQM.getValue().equals(nodeType)) {
            busiGeneratorSubTreeService.updateNode(nodeId, newName);
        }
    }

    @Override
    public void updateNodeBySiteTree(BusiSiteTree busiSiteTree) {
        String equipmentType = busiSiteTree.getEquipmentType();
        String id = busiSiteTree.getId();
        String name = busiSiteTree.getName();
        if (equipmentType.equals(SiteConstants.SiteTreeEnum.BYQ_EQM.getValue())) {
            busiTransfromerSubTreeService.updateNodeByMainEquipment(id, name);
        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue())) {
            busiGisSubTreeService.updateNodeByMainEquipment(id, name);
        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue())) {
            busiArresterSubTreeService.updateNodeByMainEquipment(id, name);
        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.CW_HL_EQM.getValue())) {

        } else if (equipmentType.equals(SiteConstants.SiteTreeEnum.OTHER_EQM.getValue())) {
            busiOtherSubTreeService.updateNodeByMainEquipment(id, name);
        }

    }

    @Override
    public void deleteNode(String nodeId, String nodeType) {
        String equipmentType = "";
        if (SiteConstants.SiteTreeEnum.BYQ_EQM.getValue().equals(nodeType)) {
            busiTransfromerSubTreeService.deleteNode(nodeId);
            equipmentType = SiteConstants.ExternalTypeEnum.BYQ.getValue();
        } else if (SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue().equals(nodeType)) {
            busiGisSubTreeService.deleteNode(nodeId);
            equipmentType = SiteConstants.ExternalTypeEnum.GIS.getValue();
        } else if (SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue().equals(nodeType)) {
            busiArresterSubTreeService.deleteNode(nodeId);
            equipmentType = SiteConstants.ExternalTypeEnum.BLQ.getValue();
        } else if (SiteConstants.SiteTreeEnum.OTHER_EQM.getValue().equals(nodeType)) {
            busiOtherSubTreeService.deleteNode(nodeId);
            equipmentType = SiteConstants.ExternalTypeEnum.OTHER.getValue();
        } else if (SiteConstants.SiteTreeEnum.DL_EQM.getValue().equals(nodeType)) {
            busiCableSubTreeService.deleteNode(nodeId);
            equipmentType = SiteConstants.ExternalTypeEnum.DL.getValue();
        } else if (SiteConstants.SiteTreeEnum.GEN_EQM.getValue().equals(nodeType)) {
            busiGeneratorSubTreeService.deleteNode(nodeId);
            equipmentType = SiteConstants.ExternalTypeEnum.GEN.getValue();
        }
        List<BusiSensorVo> busiSensorList = busiSensorService.querySensorByEquipMentIdAndType(nodeId, nodeType);
        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), equipmentType);
        }
    }

    @Override
    public void insertNode(String nodeId, String nodeType, String count) {
        Integer counts = Integer.valueOf(count);
        if (SiteConstants.SiteTreeEnum.BYQ_EQM.getValue().equals(nodeType)) {
            busiTransfromerSubTreeService.insertEquipmentNode(nodeId, counts);
        } else if (SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue().equals(nodeType)) {
            busiGisSubTreeService.insertEquipmentNode(nodeId, counts);
        } else if (SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue().equals(nodeType)) {
            busiArresterSubTreeService.insertEquipmentNode(nodeId, counts);
        } else if (SiteConstants.SiteTreeEnum.GEN_EQM.getValue().equals(nodeType)) {
            busiGeneratorSubTreeService.insertEquipmentNode(nodeId, counts);
        } else if (SiteConstants.SiteTreeEnum.OTHER_EQM.getValue().equals(nodeType)) {
            busiOtherSubTreeService.insertEquipmentNode(nodeId, counts);
        } else if (SiteConstants.SiteTreeEnum.DL_EQM.getValue().equals(nodeType)) {
            busiCableSubTreeService.insertEquipmentNode(nodeId, counts);
        }
    }
}