/**
 * 
 */
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.BusiGisSubTreeDao;
import com.huatek.busi.entity.BusiGisSubTree;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.mapper.BusiGisSubTreeMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiGisSubTreeService;
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: BusiTransfromerSubTreeServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.
 *                 BusiTransfromerSubTreeServiceImpl
 * @Description: GIS外挂设备节点树服务实现
 * @author: Arno
 * @date: Jun 26, 2019 3:06:22 PM
 * @version: 1.0
 */
@Component
@Service(interfaceClass = BusiGisSubTreeService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiGisSubTreeServiceImpl implements  BusiGisSubTreeService {

	@Autowired
	private BusiGisSubTreeDao busiGisSubTreeDao;
	
	@Autowired
	private BusiExternalEquipmentService  busiExternalEquipmentService ;
	
	
	@Autowired
	private BusiGisSubTreeMapper busiGisSubTreeMapper;

	@Override
	public Boolean insertSubTreeForType(BusiSiteTree busiSiteTree) {
		String transformerId = busiSiteTree.getId();
		String transformerName = busiSiteTree.getName();
		String siteId = busiSiteTree.getSiteId();
		BusiGisSubTree entity = createNode(siteId,"0", transformerName,
				SiteConstants.GisTreeEnum.ROOT.getValue(), transformerId);
		 createNode(siteId, entity.getId(), SiteConstants.GisTreeEnum.JF.getName(),
				SiteConstants.GisTreeEnum.JF.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.GisTreeEnum.SF6.getName(),
				SiteConstants.GisTreeEnum.SF6.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.GisTreeEnum.JXTX.getName(),
				SiteConstants.GisTreeEnum.JXTX.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.GisTreeEnum.QX.getName(),
				SiteConstants.GisTreeEnum.QX.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.GisTreeEnum.HW.getName(),
				SiteConstants.GisTreeEnum.HW.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.GisTreeEnum.TP.getName(),
				SiteConstants.GisTreeEnum.TP.getValue(), transformerId);
		createNode(siteId,entity.getId(), SiteConstants.GisTreeEnum.HJJC.getName(),
				SiteConstants.GisTreeEnum.HJJC.getValue(), transformerId);

		return true;
	}


	@Override
	public Boolean insertEquipmentNode(String parentId, Integer count) {
		BusiGisSubTree 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.GisTreeEnum.JF.getValue())){
				name = SiteConstants.GisTreeEnum.JF_EQM.getName();
				type = SiteConstants.GisTreeEnum.JF_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.GisTreeEnum.SF6.getValue())){
				name = SiteConstants.GisTreeEnum.SF6_EQM.getName();
				type = SiteConstants.GisTreeEnum.SF6_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.GisTreeEnum.JXTX.getValue())){
				name = SiteConstants.GisTreeEnum.JXTX_EQM.getName();
				type = SiteConstants.GisTreeEnum.JXTX_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.GisTreeEnum.QX.getValue())){
				name = SiteConstants.GisTreeEnum.QX.getName();
				type = SiteConstants.GisTreeEnum.QX_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.GisTreeEnum.HW.getValue())){
				name = SiteConstants.GisTreeEnum.HW_EQM.getName();
				type = SiteConstants.GisTreeEnum.HW_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.GisTreeEnum.TP.getValue())){
				name = SiteConstants.GisTreeEnum.TP_EQM.getName();
				type = SiteConstants.GisTreeEnum.TP_EQM.getValue();
			} else if(equipmentType.equals(SiteConstants.GisTreeEnum.HJJC.getValue())){
				name = SiteConstants.GisTreeEnum.HJJC_EQM.getName();
				type = SiteConstants.GisTreeEnum.HJJC_EQM.getValue();
			}
			if(!"".equals(type)){
				for(int i= 0; i< count ; i++){
					BusiGisSubTree busiGisSubTree = createNode(siteId, parentId, name,type, equipmentId);
					this.updateSubEquipment(busiGisSubTree);
				}
			}
		}
		return true;
	}
	
	@Override
	public Boolean updateNode(String nodeId, String newName) {
		BusiGisSubTree entity = findByPK(nodeId);
		if(null != entity){
			String oldName = entity.getName();
			entity.setName(newName);
			this.busiGisSubTreeDao.updateLocal(entity);
			if(!newName.equals(oldName)){
				this.updateSubEquipment(entity);
			}
		}
		return true;
	}

	
	

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

	}
	
	
	


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


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

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

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

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


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

}
