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.BusiGeneratorSubTreeDao;
import com.huatek.busi.entity.BusiGeneratorSubTree;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.mapper.BusiGeneratorSubTreeMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiGeneratorSubTreeService;
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;

@Component
@Service(interfaceClass = BusiGeneratorSubTreeService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiGeneratorSubTreeServiceImpl implements BusiGeneratorSubTreeService {
    @Autowired
    private BusiGeneratorSubTreeDao busiGeneratorSubTreeDao;
    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;

    @Autowired
    private BusiGeneratorSubTreeMapper busiGeneratorSubTreeMapper;

    @Override
    public BusiGeneratorSubTree findByPK(String pk) {
        return busiGeneratorSubTreeDao.get(pk);
    }

    @Override
    public Boolean insertEquipmentNode(String parentId, Integer count) {
        BusiGeneratorSubTree 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.GeneratorTreeEnum.ZD.getValue())) {
                name = SiteConstants.GeneratorTreeEnum.ZD_EQM.getName();
                type = SiteConstants.GeneratorTreeEnum.ZD_EQM.getValue();
            } else if (equipmentType.equals(SiteConstants.GeneratorTreeEnum.JF.getValue())) {
                name = SiteConstants.GeneratorTreeEnum.JF_EQM.getName();
                type = SiteConstants.GeneratorTreeEnum.JF_EQM.getValue();
            } else if (equipmentType.equals(SiteConstants.GeneratorTreeEnum.YL.getValue())) {
                name = SiteConstants.GeneratorTreeEnum.YL_EQM.getName();
                type = SiteConstants.GeneratorTreeEnum.YL_EQM.getValue();
            }

            if (!"".equals(type)) {
                for (int i = 0; i < count; i++) {
                    BusiGeneratorSubTree entity = createNode(siteId, parentId, name, type, equipmentId);
                    this.updateSubEquipment(entity);
                }
            }
        }
        return null;
    }
    private BusiGeneratorSubTree createNode(String siteId, String parentId, String name, String eqmType,
                                           String equipmentId) {
        BusiGeneratorSubTree entity = new BusiGeneratorSubTree();
        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);
        busiGeneratorSubTreeDao.add(entity);
        return entity;
    }
    private void updateSubEquipment(BusiGeneratorSubTree entity) {
        String equipmentName = entity.getName();
        String equipmentId = entity.getId();
        String siteId = entity.getSiteId();
        busiExternalEquipmentService.updateByEquipmentId(equipmentId, siteId, equipmentName,
                SiteConstants.ExternalTypeEnum.GEN.getValue());
    }

    @Override
    public Boolean insertSubTreeForType(BusiSiteTree busiSiteTree) {
        String generatorId = busiSiteTree.getId();
        String generatorName = busiSiteTree.getName();
        String siteId = busiSiteTree.getSiteId();
        BusiGeneratorSubTree entity = createNode(siteId, "0", generatorName,
                SiteConstants.GeneratorTreeEnum.ROOT.getValue(), generatorId);
        createNode(siteId, entity.getId(), SiteConstants.GeneratorTreeEnum.ZD.getName(),
                SiteConstants.GeneratorTreeEnum.ZD.getValue(), generatorId);
        createNode(siteId,entity.getId(), SiteConstants.GeneratorTreeEnum.JF.getName(),
                SiteConstants.GeneratorTreeEnum.JF.getValue(), generatorId);
        createNode(siteId,entity.getId(), SiteConstants.GeneratorTreeEnum.YL.getName(),
                SiteConstants.GeneratorTreeEnum.YL.getValue(), generatorId);
        return true;
    }

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

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

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

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

    }

    @Override
    public List<BusiGeneratorSubTree> findByEquipmentId(String equipmentId) {

        return busiGeneratorSubTreeMapper.findByEquipmentId(equipmentId);
    }
}
