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

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;

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.common.util.DecimalUtil;
import com.huatek.busi.dao.TransformerPdCacheDao;
import com.huatek.busi.dto.TransformerPdCacheDto;
import com.huatek.busi.dto.monitor.BusiOnlineMonitorDataDto;
import com.huatek.busi.entity.TransformerPd;
import com.huatek.busi.entity.TransformerPdCache;
import com.huatek.busi.mapper.TransformerPdCacheMapper;
import com.huatek.busi.service.BusiCommonService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.transformer.TransformerPdCacheService;
import com.huatek.busi.vo.TransformerPdCacheVo;

/**
 * 变压器局部放电暂存数据( 类型编码 ：021001) ServiceImpl
 *
 * @author anker_cheng
 * @date Jun 26, 2019 6:32:09 PM
 */
@Component
@Service(interfaceClass = TransformerPdCacheService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerPdCacheServiceImpl implements TransformerPdCacheService {

    @Autowired
    private TransformerPdCacheMapper transformerPdCacheMapper;
    @Autowired
    private TransformerPdCacheDao transformerPdCacheDao;
    @Autowired
    private BusiCommonService busiCommonService;
    @Autowired
    private BusiEqmWarningService busiEqmWarningService;

    @Override
    public PageInfo<TransformerPdCacheVo> findTransformerPdCachePage(TransformerPdCacheDto transformerPdCacheDto) {
    	
    	PageInfo<TransformerPdCacheVo> pageList  = PageHelper.startPage(transformerPdCacheDto.getPage(), transformerPdCacheDto.getLimit())
        .doSelectPageInfo(() -> transformerPdCacheMapper.selectTransformerPdCachePage(transformerPdCacheDto));
    	pageList.getList().stream().forEach(pdCacheVo ->{
    		pdCacheVo.setRepairCount(busiCommonService.getEquipHalfYearRepairCount(ExternalTypeEnum.BYQ, pdCacheVo.getEquipmentId()));
            // 误报率
    		pdCacheVo.setFalseAlarmRate(busiEqmWarningService.getFalseAlarmRateBySensor(pdCacheVo.getSensorId(),
                DateUtil.getMonthStartDay(), DateUtil.getMonthLastDay()));
    	});
    	return pageList;
    }

    @Override
    public synchronized int saveTransformerPdCache(TransformerPd cacEntity) {
        TransformerPdCache transformerPdCache = transformerPdCacheMapper.getTransformerPdCache(cacEntity);
        if (transformerPdCache == null) {
            transformerPdCache = new TransformerPdCache();
            transformerPdCache.setCreatedTime(LocalDateTime.now());
        }
        transformerPdCache.setSiteId(cacEntity.getSiteId());
        transformerPdCache.setCacId(cacEntity.getCacId());
        transformerPdCache.setMainTreeId(cacEntity.getMainTreeId());
        transformerPdCache.setSubTreeId(cacEntity.getSubTreeId());
        transformerPdCache.setSiteEqmCode(cacEntity.getEquipmentCode());
        transformerPdCache.setSensorId(cacEntity.getSensorId());
        // transformerPdCache.setJfId(null);
        // transformerPdCache.setPeak(null);
        transformerPdCache.setModifiedTime(LocalDateTime.now());
        transformerPdCache.setDschType(cacEntity.getDschType());
        transformerPdCache.setDTime(cacEntity.getTimeStamp());
        transformerPdCache.setPhase(cacEntity.getPhase());
        transformerPdCache.setDischargeCapacity(cacEntity.getDischargeCapacity());
        transformerPdCache.setDischargePosition(cacEntity.getDischargePosition());
        transformerPdCache.setTransformerPdId(cacEntity.getId());
        // 次数+1
        transformerPdCache
            .setDichangeCount(BigDecimal.ONE.add(DecimalUtil.getVal(transformerPdCache.getDichangeCount())));
        // 保存或更新
        return transformerPdCacheDao.saveOrUpdate(transformerPdCache);
    }

    @Override
    public PageInfo<Map<String, Object>>
        findTransformerPdCachePage(BusiOnlineMonitorDataDto busiOnlineMonitorDataDto) {
        return null;
    }
}