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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

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.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.util.DateUtil;
import com.huatek.busi.dao.TransformerInfraredMonitoringCacheDao;
import com.huatek.busi.dto.TransformerInfraredMonitoringCacheDto;
import com.huatek.busi.entity.TransformerInfraredMonitoringCache;
import com.huatek.busi.entity.TransformerInfraredTemperature;
import com.huatek.busi.mapper.TransformerInfraredMonitoringCacheMapper;
import com.huatek.busi.service.BusiCommonService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.transformer.TransformerInfraredMonitoringCacheService;
import com.huatek.busi.vo.TransformerInfraredMonitoringCacheVo;

/**
 * 变压器红外监测暂存数据 ServiceImpl
 *
 * @author anker_cheng
 * @date Jun 28, 2019 1:21:03 PM
 */
@Component
@Service(interfaceClass = TransformerInfraredMonitoringCacheService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerInfraredMonitoringCacheServiceImpl implements TransformerInfraredMonitoringCacheService {

    @Autowired
    private TransformerInfraredMonitoringCacheMapper transformerInfraredMonitoringCacheMapper;
    @Autowired
    private TransformerInfraredMonitoringCacheDao transformerInfraredMonitoringCacheDao;
    @Autowired
    private BusiCommonService busiCommonService;
    @Autowired
    private BusiEqmWarningService busiEqmWarningService;

    @Override
    public PageInfo<TransformerInfraredMonitoringCacheVo>
        findTransformerInfraredMonitoringCachePage(TransformerInfraredMonitoringCacheDto param) {
    	PageInfo<TransformerInfraredMonitoringCacheVo> pageList = PageHelper.startPage(param.getPage(), param.getLimit()).doSelectPageInfo(
            () -> transformerInfraredMonitoringCacheMapper.selectTransformerInfraredMonitoringCachePage(param));    	
    	pageList.getList().stream().forEach(sf6MonitorVo ->{
   		 //维修次数
   		 sf6MonitorVo.setRepairCount(busiCommonService.getEquipHalfYearRepairCount(ExternalTypeEnum.BYQ, sf6MonitorVo.getEquipmentId()));
   		 //误报率
   		 sf6MonitorVo.setFalseAlarmRate(busiEqmWarningService.getFalseAlarmRateBySensor(sf6MonitorVo.getSensorId(), DateUtil.getMonthStartDay(), DateUtil.getMonthLastDay()));  
    	});
    	return pageList;
    }

    @Override
    public synchronized int saveTransformerInfraredMonitoringCache(TransformerInfraredTemperature cacEntity) {
        TransformerInfraredMonitoringCache cache =
            transformerInfraredMonitoringCacheMapper.getTransformerInfraredMonitoringCache(cacEntity);
        if (cache == null) {
            cache = new TransformerInfraredMonitoringCache();
            cache.setCreatedTime(LocalDateTime.now());
        }
        cache.setType(cacEntity.getCacMonitorTypeCode());
        cache.setSiteId(cacEntity.getSiteId());
        cache.setMainTreeId(cacEntity.getMainTreeId());
        cache.setSubTreeId(cacEntity.getSubTreeId());
        cache.setModifiedTime(LocalDateTime.now());
        cache.setEqmId(cacEntity.getEquipmentId());
        cache.setSiteEqmCode(cacEntity.getEquipmentCode());
        cache.setPhase(cacEntity.getPhase());
        cache.setSensorId(cacEntity.getSensorId());
        cache.setRunState(cacEntity.getRunState());
        cache.setDTime(cacEntity.getTimeStamp());
        cache.setHTemperature(transformData(cacEntity.getMaxTmp()));
        cache.setLTemperature(transformData(cacEntity.getMinTmp()));
        cache.setAvgTmp(transformData(cacEntity.getAvgTmp()));
        return transformerInfraredMonitoringCacheDao.saveOrUpdate(cache);
    }

    private static String transformData(String value) {
        if (StringUtils.isNotBlank(value)) {
            return new BigDecimal(value).setScale(2, RoundingMode.HALF_DOWN).toString();
        }
        return value;
    }
}