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

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.TransformerWindingTemperatureCacheDao;
import com.huatek.busi.dto.TransformerWindingTemperatureCacheDto;
import com.huatek.busi.entity.TransformerWindingTemperature;
import com.huatek.busi.entity.TransformerWindingTemperatureCache;
import com.huatek.busi.mapper.TransformerWindingTemperatureCacheMapper;
import com.huatek.busi.service.BusiCommonService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.transformer.TransformerInfraredMonitoringCacheService;
import com.huatek.busi.service.transformer.TransformerWindingTemperatureCacheService;
import com.huatek.busi.vo.TransformerWindingTemperatureCacheVo;
import com.huatek.torch.sql.beans.WherePrams;
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 java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

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

    @Autowired
    private TransformerWindingTemperatureCacheMapper transformerWindingTemperatureCacheMapper;

    @Autowired
    private TransformerWindingTemperatureCacheDao transformerWindingTemperatureCacheDao;

    @Autowired
    private BusiCommonService busiCommonService;

    @Autowired
    private BusiEqmWarningService busiEqmWarningService;

    @Override
    public PageInfo<TransformerWindingTemperatureCacheVo>
        findTransformerWindingTemperatureCachePage(TransformerWindingTemperatureCacheDto param) {
    	PageInfo<TransformerWindingTemperatureCacheVo> pageList = PageHelper.startPage(param.getPage(), param.getLimit()).doSelectPageInfo(
            () -> transformerWindingTemperatureCacheMapper.selectTransformerWindingTemperatureCachePage(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 saveTransformerWindingTemperatureCache(TransformerWindingTemperature cacEntity) {
        TransformerWindingTemperatureCache cache =
                transformerWindingTemperatureCacheDao.get(new WherePrams()
                        .where("sensor_id", cacEntity.getSensorId())
                        .orderBy("d_time desc limit 1"));
        if (cache == null) {
            cache = new TransformerWindingTemperatureCache();
            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 transformerWindingTemperatureCacheDao.saveOrUpdate(cache);
    }

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