/**
 * 
 */
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.BusiArresterSubTreeDao;
import com.huatek.busi.entity.BusiArresterSubTree;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.mapper.BusiArresterSubTreeMapper;
import com.huatek.busi.service.BusiArresterSubTreeService;
import com.huatek.busi.service.BusiExternalEquipmentService;
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;

/**
 * @ClassName: BusiArresterSubTreeServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.BusiArresterSubTreeServiceImpl
 * @Description: 避雷器外挂设备节点树服务
 * @author: Arno
 * @date: Jun 28, 2019 4:23:36 PM
 * @version: 1.0
 */
@Component
@Service(interfaceClass = BusiArresterSubTreeService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiArresterSubTreeServiceImpl implements BusiArresterSubTreeService {

	@Autowired
	private BusiArresterSubTreeDao busiArresterSubTreeDao;

	@Autowired
	private BusiExternalEquipmentService busiExternalEquipmentService;

	@Autowired
	private BusiArresterSubTreeMapper busiArresterSubTreeMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huatek.busi.service.BusiArresterSubTreeService#findByPK(java.lang.
	 * String)
	 */
	@Override
	public BusiArresterSubTree findByPK(String pk) {
		return busiArresterSubTreeDao.get(pk);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huatek.busi.service.BusiArresterSubTreeService#insertSubTreeForType(
	 * com.huatek.busi.entity.BusiSiteTree)
	 */
	@Override
	public Boolean insertSubTreeForType(BusiSiteTree busiSiteTree) {
		String transformerId = busiSiteTree.getId();
		String transformerName = busiSiteTree.getName();
		String siteId = busiSiteTree.getSiteId();
		BusiArresterSubTree entity = createNode(siteId, "0", transformerName,
				SiteConstants.ArresterTreeEnum.ROOT.getValue(), transformerId);
		createNode(siteId, entity.getId(), SiteConstants.ArresterTreeEnum.JY.getName(),
				SiteConstants.ArresterTreeEnum.JY.getValue(), transformerId);
		createNode(siteId, entity.getId(), SiteConstants.ArresterTreeEnum.HW.getName(),
				SiteConstants.ArresterTreeEnum.HW.getValue(), transformerId);
		createNode(siteId, entity.getId(), SiteConstants.ArresterTreeEnum.RX.getName(),
				SiteConstants.ArresterTreeEnum.RX.getValue(), transformerId);
		createNode(siteId, entity.getId(), SiteConstants.ArresterTreeEnum.QX.getName(),
				SiteConstants.ArresterTreeEnum.QX.getValue(), transformerId);
		createNode(siteId, entity.getId(), SiteConstants.ArresterTreeEnum.TP.getName(),
				SiteConstants.ArresterTreeEnum.TP.getValue(), transformerId);
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huatek.busi.service.BusiArresterSubTreeService#insertEquipmentNode(
	 * java.lang.String, java.lang.Integer)
	 */
	@Override
	public Boolean insertEquipmentNode(String parentId, Integer count) {
		BusiArresterSubTree 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.ArresterTreeEnum.JY.getValue())) {
				name = SiteConstants.ArresterTreeEnum.JY_EQM.getName();
				type = SiteConstants.ArresterTreeEnum.JY_EQM.getValue();
			} else if (equipmentType.equals(SiteConstants.ArresterTreeEnum.QX.getValue())) {
				name = SiteConstants.ArresterTreeEnum.QX.getName();
				type = SiteConstants.ArresterTreeEnum.QX_EQM.getValue();
			} else if (equipmentType.equals(SiteConstants.ArresterTreeEnum.HW.getValue())) {
				name = SiteConstants.ArresterTreeEnum.HW_EQM.getName();
				type = SiteConstants.ArresterTreeEnum.HW_EQM.getValue();
			} else if (equipmentType.equals(SiteConstants.ArresterTreeEnum.TP.getValue())) {
				name = SiteConstants.ArresterTreeEnum.TP_EQM.getName();
				type = SiteConstants.ArresterTreeEnum.TP_EQM.getValue();
			} else if (equipmentType.equals(SiteConstants.ArresterTreeEnum.RX.getValue())) {
				name = SiteConstants.ArresterTreeEnum.RX_EQM.getName();
				type = SiteConstants.ArresterTreeEnum.RX_EQM.getValue();
			}

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huatek.busi.service.BusiArresterSubTreeService#updateNode(java.lang.
	 * String, java.lang.String)
	 */
	@Override
	public Boolean updateNode(String nodeId, String newName) {
		BusiArresterSubTree entity = findByPK(nodeId);
		if (null != entity) {
			String oldName = entity.getName();
			entity.setName(newName);
			this.busiArresterSubTreeDao.updateLocal(entity);
			if (!newName.equals(oldName)) {
				this.updateSubEquipment(entity);
			}
		}
		return true;
	}

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

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

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huatek.busi.service.BusiArresterSubTreeService#deleteNode(java.lang.
	 * String)
	 */
	@Override
	public Boolean deleteNode(String nodeId) {
		BusiArresterSubTree entity = findByPK(nodeId);
		if (null != entity) {
			entity.setStatus(SiteConstants.Status.DELETE.getValue());
			this.busiArresterSubTreeDao.updateLocal(entity);
		}
		return true;
	}

	private BusiArresterSubTree createNode(String siteId, String parentId, String name, String eqmType,
			String equipmentId) {
		BusiArresterSubTree entity = new BusiArresterSubTree();
		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);
		busiArresterSubTreeDao.add(entity);
		return entity;
	}

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

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