 package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.util.GenerateUtil;
import com.huatek.busi.dao.BusiOtherSubTreeDao;
import com.huatek.busi.entity.BusiOtherSubTree;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.mapper.BusiOtherSubTreeMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiOtherSubTreeService;
import com.huatek.torch.util.uuid.Uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author buck_guo
 * @date 2019/09/11
 */
@Component
@Service(interfaceClass = BusiOtherSubTreeService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOtherSubTreeServiceImpl implements BusiOtherSubTreeService {
    @Autowired
    private BusiOtherSubTreeDao busiOtherSubTreeDao;

    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;

    @Autowired
    private BusiOtherSubTreeMapper busiOtherSubTreeMapper;

    /**
     *
     */
    @Override
    public BusiOtherSubTree findByPK(String pk) {
        return busiOtherSubTreeDao.get(pk);
    }

    /**
     *
     */
    @Override
    public Boolean insertSubTreeForType(BusiSiteTree busiSiteTree) {
        String transformerId = busiSiteTree.getId();
        String transformerName = busiSiteTree.getName();
        String siteId = busiSiteTree.getSiteId();
        BusiOtherSubTree entity = createNode(siteId, "0", transformerName,
                SiteConstants.OtherTreeEnum.ROOT.getValue(), transformerId);
        createNode(siteId, entity.getId(), SiteConstants.OtherTreeEnum.DL.getName(),
                SiteConstants.OtherTreeEnum.DL.getValue(), transformerId);
        createNode(siteId, entity.getId(), SiteConstants.OtherTreeEnum.HW.getName(),
                SiteConstants.OtherTreeEnum.HW.getValue(), transformerId);
        createNode(siteId, entity.getId(), SiteConstants.OtherTreeEnum.TX.getName(),
                SiteConstants.OtherTreeEnum.TX.getValue(), transformerId);
        createNode(siteId, entity.getId(), SiteConstants.OtherTreeEnum.DLHL.getName(),
                SiteConstants.OtherTreeEnum.DLHL.getValue(), transformerId);
        return null;
    }

    /**
     *
     */
    @Override
    public Boolean insertEquipmentNode(String parentId, Integer count) {
        BusiOtherSubTree parent = findByPK(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.OtherTreeEnum.DL.getValue())) {
                name = SiteConstants.OtherTreeEnum.DL_EQM.getName();
                type = SiteConstants.OtherTreeEnum.DL_EQM.getValue();
            } else if (equipmentType.equals(SiteConstants.OtherTreeEnum.HW.getValue())) {
                name = SiteConstants.OtherTreeEnum.HW_EQM.getName();
                type = SiteConstants.OtherTreeEnum.HW_EQM.getValue();
            } else if (equipmentType.equals(SiteConstants.OtherTreeEnum.TX.getValue())) {
                name = SiteConstants.OtherTreeEnum.TX_EQM.getName();
                type = SiteConstants.OtherTreeEnum.TX_EQM.getValue();
            }
            else if (equipmentType.equals(SiteConstants.OtherTreeEnum.DLHL.getValue())) {
                name = SiteConstants.OtherTreeEnum.DLHL_EQM.getName();
                type = SiteConstants.OtherTreeEnum.DLHL_EQM.getValue();
            }

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

    /**
     *
     */
    @Override
    public Boolean updateNode(String nodeId, String newName) {
        BusiOtherSubTree entity = findByPK(nodeId);
        if (null != entity) {
            String oldName = entity.getName();
            entity.setName(newName);
            this.busiOtherSubTreeDao.updateLocal(entity);
            if (!newName.equals(oldName)) {
                this.updateSubEquipment(entity);
            }
        }
        return true;
    }

    @Override
    public Boolean updateNodeBySubEquipment(String nodeId, String newName) {
        BusiOtherSubTree entity = findByPK(nodeId);
        if (null != entity) {
            entity.setName(newName);
            this.busiOtherSubTreeDao.updateLocal(entity);
        }
        return true;
    }

    @Override
    public void updateNodeByMainEquipment(String equipmentId, String newName) {
        BusiOtherSubTree find = busiOtherSubTreeMapper.findByEquipmentIdAndType(equipmentId, "0");
        if (null != find) {
            find.setName(newName);
            this.busiOtherSubTreeDao.updateLocal(find);
        }

    }

    /**
     * 删除节点
     */
    @Override
    public Boolean deleteNode(String nodeId) {
        BusiOtherSubTree entity = findByPK(nodeId);
        if (null != entity) {
            entity.setStatus(SiteConstants.Status.DELETE.getValue());
            this.busiOtherSubTreeDao.updateLocal(entity);
        }
        return true;
    }

    private BusiOtherSubTree createNode(String siteId, String parentId, String name, String eqmType,
            String equipmentId) {
        BusiOtherSubTree entity = new BusiOtherSubTree();
        String id = Uuid.getUUID();
        entity.setId(id);
        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);
        busiOtherSubTreeDao.add(entity);
        return entity;
    }

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

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

    @Override
    public List<BusiOtherSubTree> getOtherSubTreeByType(String equipmentType) {
        return busiOtherSubTreeMapper.getOtherSubTreeByType(equipmentType);
    }
}
