/**
 *
 */
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.BusiTransfromerSubTreeDao;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.entity.BusiTransfromerSubTree;
import com.huatek.busi.mapper.BusiTransfromerSubTreeMapper;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiTransfromerSubTreeService;
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: 变压器外挂设备节点树服务实现
 * @author: Arno
 * @date: Jun 26, 2019 3:06:22 PM
 * @version: 1.0
 */
@Component
@Service(interfaceClass = BusiTransfromerSubTreeService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiTransfromerSubTreeServiceImpl implements BusiTransfromerSubTreeService {

	@Autowired
	private BusiTransfromerSubTreeDao busiTransfromerSubTreeDao;


	@Autowired
	private BusiExternalEquipmentService  busiExternalEquipmentService ;

	@Autowired
	private  BusiTransfromerSubTreeMapper  busiTransfromerSubTreeMapper;

	@Override
	public Boolean insertSubTreeForType(BusiSiteTree busiSiteTree) {
		String transformerId = busiSiteTree.getId();
		String transformerName = busiSiteTree.getName();
		String siteId = busiSiteTree.getSiteId();
		BusiTransfromerSubTree entity = createNode(siteId,"0", transformerName,
				SiteConstants.TransfromerTreeEnum.ROOT.getValue(), transformerId);
		 createNode(siteId, entity.getId(), SiteConstants.TransfromerTreeEnum.YSP.getName(),
				SiteConstants.TransfromerTreeEnum.YSP.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.JF.getName(),
				SiteConstants.TransfromerTreeEnum.JF.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.TX.getName(),
				SiteConstants.TransfromerTreeEnum.TX.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.HW.getName(),
				SiteConstants.TransfromerTreeEnum.HW.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.TP.getName(),
				SiteConstants.TransfromerTreeEnum.TP.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.RX.getName(),
				SiteConstants.TransfromerTreeEnum.RX.getValue(), transformerId);
		 createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.QX.getName(),
				SiteConstants.TransfromerTreeEnum.QX.getValue(), transformerId);
		createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.JJ.getName(),
				SiteConstants.TransfromerTreeEnum.JJ.getValue(), transformerId);
		createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.TG.getName(),
            SiteConstants.TransfromerTreeEnum.TG.getValue(), transformerId);
		createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.SJZD.getName(),
            SiteConstants.TransfromerTreeEnum.SJZD.getValue(), transformerId);
		createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.RZCW.getName(),
				SiteConstants.TransfromerTreeEnum.RZCW.getValue(), transformerId);
		createNode(siteId,entity.getId(), SiteConstants.TransfromerTreeEnum.CW.getName(),
				SiteConstants.TransfromerTreeEnum.CW.getValue(), transformerId);
		return true;
	}


	@Override
	public Boolean insertEquipmentNode(String parentId, Integer count) {
		BusiTransfromerSubTree 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.TransfromerTreeEnum.YSP.getValue())){
				name = SiteConstants.TransfromerTreeEnum.YSP_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.YSP_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.JF.getValue())){
				name = SiteConstants.TransfromerTreeEnum.JF_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.JF_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.TX.getValue())){
				name = SiteConstants.TransfromerTreeEnum.TX_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.TX_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.HW.getValue())){
				name = SiteConstants.TransfromerTreeEnum.HW_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.HW_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.TP.getValue())){
				name = SiteConstants.TransfromerTreeEnum.TP_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.TP_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.RX.getValue())){
				name = SiteConstants.TransfromerTreeEnum.RX_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.RX_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.QX.getValue())){
				name = SiteConstants.TransfromerTreeEnum.QX_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.QX_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.JJ.getValue())){
				name = SiteConstants.TransfromerTreeEnum.JJ_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.JJ_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.TG.getValue())){
                name = SiteConstants.TransfromerTreeEnum.TG_EQM.getName();
                type = SiteConstants.TransfromerTreeEnum.TG_EQM.getValue();
            }else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.SJZD.getValue())){
                name = SiteConstants.TransfromerTreeEnum.SJZD_EQM.getName();
                type = SiteConstants.TransfromerTreeEnum.SJZD_EQM.getValue();
            }else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.RZCW.getValue())){
				name = SiteConstants.TransfromerTreeEnum.RZCW_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.RZCW_EQM.getValue();
			}else if(equipmentType.equals(SiteConstants.TransfromerTreeEnum.CW.getValue())) {
				name = SiteConstants.TransfromerTreeEnum.CW_EQM.getName();
				type = SiteConstants.TransfromerTreeEnum.CW_EQM.getValue();
			}

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

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

		}
		return true;
	}


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


	@Override
	public Boolean updateNodeByMainEquipment(String equipmentId, String newName) {
		BusiTransfromerSubTree find = busiTransfromerSubTreeMapper.findByEquipmentIdAndType(equipmentId, "0");
		if(null != find ){
			find.setName(newName);
			this.busiTransfromerSubTreeDao.updateLocal(find);
		}
		return true;
	}


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

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

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


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


    public List<BusiTransfromerSubTree> findByEquipmentId(String equipmentId){
         return busiTransfromerSubTreeMapper.findByEquipmentId(equipmentId);
    }
}
