package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.*;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.util.GenerateUtil;
import com.huatek.busi.dao.BusiOtherSubTreeDao;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.BusiCableSubTreeMapper;
import com.huatek.busi.service.*;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.apache.yetus.audience.InterfaceAudience;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
@Service(interfaceClass = IBusiCableSubTreeService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiCableSubTreeServiceImpl extends BaseDao<BusiCableSubTree, String> implements IBusiCableSubTreeService {

    @Resource
    private BusiCableSubTreeMapper busiCableSubTreeMapper;

    @Reference(check = false)
    BusiExternalEquipmentService busiExternalEquipmentService;

    @Override
    public Boolean insertSubTreeForType(BusiSiteTree busiSiteTree) {
        // 电缆编号
        String cableId = busiSiteTree.getId();
        // 电缆名称
        String cableName = busiSiteTree.getName();
        // 站点编号
        String siteId = busiSiteTree.getSiteId();

        BusiCableSubTree busiCableSubTree = createNode(siteId,"0", cableName,
                SiteConstants.CableTreeEnum.ROOT.getValue(), cableId);
        createNode(siteId, busiCableSubTree.getId(), SiteConstants.CableTreeEnum.CABLE_PARTIAL_DISCHARGE.getName(),
                SiteConstants.CableTreeEnum.CABLE_PARTIAL_DISCHARGE.getValue(), cableId);
        createNode(siteId,busiCableSubTree.getId(), SiteConstants.CableTreeEnum.CABLE_CIRCULATION.getName(),
                SiteConstants.CableTreeEnum.CABLE_CIRCULATION.getValue(), cableId);
//        createNode(siteId,busiCableSubTree.getId(), SiteConstants.CableTreeEnum.CABLE_TRENCH.getName(),
//                SiteConstants.CableTreeEnum.CABLE_TRENCH.getValue(), cableId);
        createNode(siteId,busiCableSubTree.getId(), SiteConstants.CableTreeEnum.HW.getName(),
                SiteConstants.CableTreeEnum.HW.getValue(), cableId);
        createNode(siteId,busiCableSubTree.getId(), SiteConstants.CableTreeEnum.CW.getName(),
                SiteConstants.CableTreeEnum.CW.getValue(), cableId);
        return true;
    }

    @Override
    public List<BusiCableSubTree> findByEquipmentId(String equipmentId) {
        return busiCableSubTreeMapper.findByEquipmentId(equipmentId);
    }

    @Override
    public List<BusiCableSubTree> getCableSubTreeByType(String equipmentType){
        return busiCableSubTreeMapper.getCableSubTreeByType(equipmentType);
    }

    @Override
    public Boolean updateNode(String nodeId, String newName){
        BusiCableSubTree entity = this.get(nodeId);
        if (null != entity) {
            entity.setName(newName);
            this.updateLocal(entity);

            String oldName = entity.getName();
            if (!newName.equals(oldName)) {
                String equipmentName = entity.getName();
                String equipmentId = entity.getId();
                String siteId = entity.getSiteId();
                busiExternalEquipmentService.updateByEquipmentId(equipmentId, siteId, equipmentName,
                        SiteConstants.ExternalTypeEnum.DL.getValue());
            }
        }
        return true;
    }

    @Override
    public Boolean deleteNode(String nodeId){
        BusiCableSubTree entity = this.get(nodeId);
        if (null != entity) {
            entity.setStatus(SiteConstants.Status.DELETE.getValue());
            this.updateLocal(entity);
        }
        return true;
    }

    @Override
    public Boolean insertEquipmentNode(String parentId, Integer count){
        BusiCableSubTree parent = this.get(parentId);
        if (null != parent && count > 0) {
            String equipmentId = parent.getEquipmentId();
            String equipmentType = parent.getEquipmentType() == null ? "" : parent.getEquipmentType();
            String name = "";
            String type = "";
            String siteId = parent.getSiteId();
            if (equipmentType.equals(SiteConstants.CableTreeEnum.CABLE_PARTIAL_DISCHARGE.getValue())) {
                name = SiteConstants.CableTreeEnum.DLJF_EQM.getName();
                type = SiteConstants.CableTreeEnum.DLJF_EQM.getValue();
            } else if (equipmentType.equals(SiteConstants.CableTreeEnum.CABLE_CIRCULATION.getValue())) {
                name = SiteConstants.CableTreeEnum.DLHL_EQM.getName();
                type = SiteConstants.CableTreeEnum.DLHL_EQM.getValue();
            }
//            else if (equipmentType.equals(SiteConstants.CableTreeEnum.CABLE_TRENCH.getValue())) {
//                name = SiteConstants.CableTreeEnum.DLG_EQM.getName();
//                type = SiteConstants.CableTreeEnum.DLG_EQM.getValue();
//            }
            else if (equipmentType.equals(SiteConstants.CableTreeEnum.HW.getValue())) {
                name = SiteConstants.CableTreeEnum.HW_EQM.getName();
                type = SiteConstants.CableTreeEnum.HW_EQM.getValue();
            } else if (equipmentType.equals(SiteConstants.CableTreeEnum.CW.getValue())) {
                name = SiteConstants.CableTreeEnum.CW_EQM.getName();
                type = SiteConstants.CableTreeEnum.CW_EQM.getValue();
            }

            if (!"".equals(type)) {
                for (int i = 0; i < count; i++) {
                    BusiCableSubTree entity = createNode(siteId, parentId, name, type, equipmentId);
                    this.updateSubEquipment(entity);
                }
            }
        }
        return null;
    }

    private BusiCableSubTree createNode(String siteId, String parentId, String name, String eqmType, String equipmentId) {
        BusiCableSubTree entity = new BusiCableSubTree();
        entity.setId(Uuid.getUUID());
        entity.setParentId(parentId);
        entity.setName(name);
        entity.setStatus(SiteConstants.Status.NORMAL.getValue());
        entity.setEquipmentId(equipmentId);
        entity.setEquipmentType(eqmType);
        entity.setSystemCode(GenerateUtil.generateCode(17));
        entity.setCreateTime(LocalDateTime.now());
        entity.setSiteId(siteId);
        this.add(entity);
        return entity;
    }

    @Override
    public BusiCableSubTree findByPK(String pk) {
        return this.get(pk);
    }

    private void updateSubEquipment(BusiCableSubTree entity) {
        String equipmentName = entity.getName();
        String equipmentId = entity.getId();
        String siteId = entity.getSiteId();
        busiExternalEquipmentService.updateByEquipmentId(equipmentId, siteId, equipmentName,
                SiteConstants.ExternalTypeEnum.DL.getValue());
    }
}
