/**
 * 
 */
package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dao.BusiExternalEquipmentDao;
import com.huatek.busi.dto.BusiExternalEquipmentDto;
import com.huatek.busi.entity.BusiExternalEquipment;
import com.huatek.busi.mapper.BusiExternalEquipmentMapper;
import com.huatek.busi.service.BusiArresterSubTreeService;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiGisSubTreeService;
import com.huatek.busi.service.BusiTransfromerSubTreeService;
import com.huatek.busi.util.SpringUtils;
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.util.List;
import java.util.Objects;

/**
 * @ClassName: BusExternalEquipmentServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.BusExternalEquipmentServiceImpl
 * @Description: 站点外挂设备信息服务
 * @author: Arno
 * @date: Jul 1, 2019 3:28:23 PM
 * @version: 1.0
 */
@Component
@Service(interfaceClass = BusiExternalEquipmentService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiExternalEquipmentServiceImpl implements BusiExternalEquipmentService {

	@Autowired
	private BusiExternalEquipmentDao busiExternalEquipmentDao;

	@Autowired
	private BusiExternalEquipmentMapper busiExternalEquipmentMapper;

	@Autowired
	private BusiTransfromerSubTreeService busiTransfromerSubTreeService;

	@Autowired
	private BusiGisSubTreeService busiGisSubTreeService;

	@Autowired
	private BusiArresterSubTreeService busiArresterSubTreeService;

	@Override
	public BusiExternalEquipment insertBusiExternalEquipment(BusiExternalEquipment entity) {
		entity.setId(Uuid.getUUID());
		busiExternalEquipmentDao.add(entity);
		return entity;
	}

	@Override
	public BusiExternalEquipment updateBusiExternalEquipment(BusiExternalEquipmentDto entityDto) {
		//BusiExternalEquipment entity = new BusiExternalEquipment();
		//BeanUtils.copyPropertiesWithIgnoreNull(entityDto, entity);
		BusiExternalEquipment find = busiExternalEquipmentMapper.getByEquipmentId(entityDto.getEquipmentId());
		if (null != find) {
			String name = find.getEquipmentName();
			find.setEquipmentName(entityDto.getEquipmentName());
			find.setEquipmentBrand(entityDto.getEquipmentBrand());
			find.setManufactor(entityDto.getManufactor());
			find.setInstallLocation(entityDto.getInstallLocation());
			// find.setMark(entity.getMark());
			find.setProductionDate(entityDto.getProductionDate());
			find.setUseDate(entityDto.getUseDate());
			busiExternalEquipmentDao.updateLocal(find);
			if (!entityDto.getEquipmentName().equals(name)) {
				this.updateSubTreeName(find);
			}
		}
		return find;
	}

	@Override
	public BusiExternalEquipment getByEquipmentId(String equipmentId) {
		//return busiExternalEquipmentDao.get(equipmentId);
		BusiExternalEquipment find = busiExternalEquipmentMapper.getByEquipmentId(equipmentId);
		return find;
	}

	@Override
	public BusiExternalEquipment getByPK(String pk) {
		return busiExternalEquipmentDao.get(pk);
	}

	@Override
	public void updateByEquipmentId(String equipmentId, String siteId, String equipmentName, String externalType) {
		BusiExternalEquipment find = this.getByEquipmentId(equipmentId);
		if (null != find) {
			find.setEquipmentName(equipmentName);
			busiExternalEquipmentDao.updateLocal(find);
		} else {
			find = new BusiExternalEquipment();
			find.setSiteId(siteId);
			find.setEquipmentId(equipmentId);
			find.setEquipmentName(equipmentName);
			find.setExternalType(externalType);
			find.setId(Uuid.getUUID());
			busiExternalEquipmentDao.add(find);
		}
	}

	private void updateSubTreeName(BusiExternalEquipment entity) {
		String equipmentName = entity.getEquipmentName();
		String equipmentId = entity.getEquipmentId();
		String externalType = entity.getExternalType();
		if (externalType.equals(SiteConstants.ExternalTypeEnum.BYQ.getValue())) {
			busiTransfromerSubTreeService.updateNodeBySubEquipment(equipmentId, equipmentName);
		} else if (externalType.equals(SiteConstants.ExternalTypeEnum.GIS.getValue())) {
			busiGisSubTreeService.updateNodeBySubEquipment(equipmentId, equipmentName);
		} else if (externalType.equals(SiteConstants.ExternalTypeEnum.CW.getValue())) {

		} else if (externalType.equals(SiteConstants.ExternalTypeEnum.BLQ.getValue())) {
			busiArresterSubTreeService.updateNodeBySubEquipment(equipmentId, equipmentName);
		}
	}
	/**
     * 根据站点ID获取外挂设备信息
     * @return
     */
    @Override
    public List<BusiExternalEquipment> queryBusiExternalEquipment() {
        String siteId = SpringUtils.getDefaultSiteId();
        return busiExternalEquipmentMapper.queryBusiExternalEquipment(siteId);
    }

	@Override
	public BusiExternalEquipment findBySensorCode(String sensorCode, String type) {
	    if(type.equals(SiteConstants.ExternalTypeEnum.BYQ.getValue())){
	    	return busiExternalEquipmentMapper.selectTransformerBySensorCode(sensorCode);
	    }else if(type.equals(SiteConstants.ExternalTypeEnum.GIS.getValue())){
	    	return busiExternalEquipmentMapper.selectGisBySensorCode(sensorCode);
	    }else if(type.equals(SiteConstants.ExternalTypeEnum.BLQ.getValue())){
	    	return busiExternalEquipmentMapper.selectArresterBySensorCode(sensorCode);
	    }else if(type.equals(SiteConstants.ExternalTypeEnum.GEN.getValue())){
			return busiExternalEquipmentMapper.selectGeneratorBySensorCode(sensorCode);
		}else if(type.equals(SiteConstants.ExternalTypeEnum.DL.getValue())){
			return busiExternalEquipmentMapper.selectCableBySensorCode(sensorCode);
		} else if(type.equals(SiteConstants.ExternalTypeEnum.OTHER.getValue())){
            return busiExternalEquipmentMapper.selectOtherBySensorCode(sensorCode);
        }
		return null;
	}
    
	@Override
	public BusiExternalEquipment findByMainEqmId(String mainEqmId, String type) {
	    if(type.equals(SiteConstants.ExternalTypeEnum.BYQ.getValue())){
	    	return busiExternalEquipmentMapper.selectTransformerByMainEqmId(mainEqmId);
	    }else if(type.equals(SiteConstants.ExternalTypeEnum.GIS.getValue())){
	    	return busiExternalEquipmentMapper.selectGisByMainEqmId(mainEqmId);
	    }else   if(type.equals(SiteConstants.ExternalTypeEnum.BLQ.getValue())){
	    	return busiExternalEquipmentMapper.selectArresterByMainEqmId(mainEqmId);
	    }
		return null;
	}

    @Override
    public List<BusiExternalEquipment> queryBusiExternalEquipmentByCondition(String mainEqmId, String externalType) {

        String type="";
        if(externalType.equals(SiteConstants.SiteTreeEnum.BYQ_EQM.getValue())){
            type=SiteConstants.ExternalTypeEnum.BYQ.getValue();
        }else if(externalType.equals(SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue())){
            type=SiteConstants.ExternalTypeEnum.GIS.getValue();
        }else if(externalType.equals(SiteConstants.SiteTreeEnum.CW_HL_EQM.getValue())){
            type=SiteConstants.ExternalTypeEnum.CW.getValue();
        }else if(externalType.equals(SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue())){
            type=SiteConstants.ExternalTypeEnum.BLQ.getValue();
      
        }else if(externalType.equals(SiteConstants.SiteTreeEnum.OTHER_EQM.getValue())){
            type=SiteConstants.ExternalTypeEnum.OTHER.getValue();
        }else if(Objects.equals(externalType, SiteConstants.SiteTreeEnum.DL_EQM.getValue())){
			type=SiteConstants.ExternalTypeEnum.DL.getValue();
		}
        return busiExternalEquipmentMapper.queryBusiExternalEquipmentByCondition(mainEqmId,type);
    }
}
