package com.huatek.busi.service.impl.transformer;

import java.math.RoundingMode;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.SiteConstants.IcgeType;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.TransformerIcgeCacheDao;
import com.huatek.busi.dto.TransformerIcgeCacheDto;
import com.huatek.busi.dto.TransformerIcgeThresholdDto;
import com.huatek.busi.entity.BusiExternalEquipment;
import com.huatek.busi.entity.TransformerIcge;
import com.huatek.busi.entity.TransformerIcgeCache;
import com.huatek.busi.mapper.TransformerIcgeCacheMapper;
import com.huatek.busi.service.BusiCommonService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.threshold.TransformerIcgeThresholdService;
import com.huatek.busi.service.transformer.TransformerIcgeCacheService;
import com.huatek.busi.vo.BusiEquipmentVo;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.busi.vo.TransformerIcgeCacheVo;
import com.huatek.busi.vo.TransformerIcgeThresholdVo;
import com.huatek.busi.vo.warning.BusiIcgeVo;

/**
 * 变压器铁芯接地电流暂存数据(类型编码 ：021004) ServiceImpl
 *
 * @author anker_cheng
 * @date Jun 27, 2019 6:53:48 PM
 */
@Component
@Service(interfaceClass = TransformerIcgeCacheService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerIcgeCacheServiceImpl implements TransformerIcgeCacheService {

	@Autowired
	private TransformerIcgeCacheMapper transformerIcgeCacheMapper;
	@Autowired
	private TransformerIcgeCacheDao transformerIcgeCacheDao;
    @Autowired
    private BusiCommonService busiCommonService;
    @Autowired
    private BusiEqmWarningService busiEqmWarningService;
    @Reference(check = false)
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Reference(check = false)
    private BusiSensorService busiSensorService;
    @Reference(check = false)
    private BusiSiteTreeService busiSiteTreeService;
    @Reference(check = false)
    private TransformerIcgeThresholdService transformerIcgeThresholdService;

	@Override
	public Page<TransformerIcgeCacheVo> findTransformerIcgeCachePage(TransformerIcgeCacheDto transformerIcgeCacheDto) {
		if (transformerIcgeCacheDto.getPage() != null && transformerIcgeCacheDto.getLimit() != null) {
			PageHelper.startPage(transformerIcgeCacheDto.getPage(), transformerIcgeCacheDto.getLimit());
		}
		Page<TransformerIcgeCacheVo> transformerIcgeCaches = transformerIcgeCacheMapper
				.selectTransformerIcgeCachePage(transformerIcgeCacheDto);
		return transformerIcgeCaches;
	}

	@Override
	public synchronized int saveTransformerIcgeCache(TransformerIcge cacEntity) {
		TransformerIcgeCache transformerIcgeCache = transformerIcgeCacheMapper
				.selectByTreeId(cacEntity.getMainTreeId(), cacEntity.getSubTreeId(), cacEntity.getType());
		if (transformerIcgeCache == null) {
			transformerIcgeCache = new TransformerIcgeCache();
		}
		transformerIcgeCache.setMainTreeId(cacEntity.getMainTreeId());
		transformerIcgeCache.setSubTreeId(cacEntity.getSubTreeId());
		transformerIcgeCache.setDTime(cacEntity.getTimeStamp());
		transformerIcgeCache.setClamp(transforeData(cacEntity.getTotalCoreCurrent()));
		transformerIcgeCache.setPhase(cacEntity.getPhase());
		transformerIcgeCache.setTotalCoreCurrent(transforeData(cacEntity.getTotalCoreCurrent()));
		transformerIcgeCache.setSensorId(cacEntity.getSensorId());
		transformerIcgeCache.setSiteId(cacEntity.getSiteId());
		transformerIcgeCache.setType(cacEntity.getType());
		transformerIcgeCache.setRunStatus(cacEntity.getRunState());
		return transformerIcgeCacheDao.saveOrUpdate(transformerIcgeCache);
	}

	private static String transforeData(String value) {
		return DecimalUtil.getVal(value).setScale(2, RoundingMode.HALF_DOWN).toString();
	}

	@Override
	public TransformerIcgeCache findByTreeId(String mainTreeId, String subTreeId, String type) {
		return transformerIcgeCacheMapper.selectByTreeId(mainTreeId, subTreeId, type);
	}
	
	/**
	 * 铁芯、夹件实时数据
	 */
	@Override
	public BusiIcgeVo getFirstTransformerIcgeCacheInfo(TransformerIcgeCacheDto transformerIcgeCacheDto) {
		// TODO Auto-generated method stub
		BusiIcgeVo busiIcgeVoInfo = transformerIcgeCacheMapper.getFirstTransformerIcgeCacheInfo(transformerIcgeCacheDto);
		
		
		
		BusiExternalEquipment externalEquipment = busiExternalEquipmentService.findBySensorCode(transformerIcgeCacheDto.getSensorId(), ExternalTypeEnum.BYQ.getValue());
		if (ObjectUtils.isNotEmpty(externalEquipment)) {
			if (ObjectUtils.isEmpty(busiIcgeVoInfo)) {
				busiIcgeVoInfo = new BusiIcgeVo();
			}  else {
				//阈值
				TransformerIcgeThresholdDto transformerIcgeThresholdDto = new TransformerIcgeThresholdDto();
				if (StringUtils.isNotEmpty(busiIcgeVoInfo.getEquipmentStatus())) {
					transformerIcgeThresholdDto.setSensorCode(transformerIcgeCacheDto.getSensorId());
					transformerIcgeThresholdDto.setRunState(busiIcgeVoInfo.getEquipmentStatus());
					TransformerIcgeThresholdVo vo =
				            transformerIcgeThresholdService.getTransformerIcgeThresholdVo(transformerIcgeThresholdDto);
					if (ObjectUtils.isNotEmpty(vo)) {
						if (transformerIcgeCacheDto.getType().equals(IcgeType.TX.getValue())) {
							busiIcgeVoInfo.setThresholdValue(vo.getTxDl());
						} else {
							busiIcgeVoInfo.setThresholdValue(vo.getJjDl());
						}
					}
				}
			}
			BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, transformerIcgeCacheDto.getSensorId());
			busiIcgeVoInfo.setExternalType(externalEquipment.getExternalType());
	        //设备名称
			//监测装置名称
	        BusiEquipmentVo equipmentVo = busiSiteTreeService.findBySensorCode(transformerIcgeCacheDto.getSensorId(), ExternalTypeEnum.BYQ.getValue());
	        busiIcgeVoInfo.setEquipmentName(equipmentVo.getEquipmentName());
	        busiIcgeVoInfo.setMainEquipmentName(equipmentVo.getMainEquipmentName());
			if (ObjectUtils.isNotEmpty(busiSensorVo)) {
		        //标牌
				busiIcgeVoInfo.setBrand(busiSensorVo.getSensorBrand());
		        //生产厂家
				busiIcgeVoInfo.setManufactor(busiSensorVo.getManufactor());
		        //安装位置
				busiIcgeVoInfo.setInstallLocation(busiSensorVo.getPositionDes());
		        //投运时间
				busiIcgeVoInfo.setUseDate(busiSensorVo.getUseDate());
			}
			//收集时间
			busiIcgeVoInfo.setCollectDate(busiIcgeVoInfo.getDTime());
		}
		//维修次数
		busiIcgeVoInfo.setRepairCount(busiCommonService.getEquipHalfYearRepairCount(ExternalTypeEnum.BYQ, transformerIcgeCacheDto.getSubTreeId()));
        // 误报率
		busiIcgeVoInfo.setFalseAlarmRate(busiEqmWarningService.getFalseAlarmRateByMainTreeId(transformerIcgeCacheDto.getMainTreeId(), null, null));
		return busiIcgeVoInfo;
	}

}