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

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.GisPdCacheDao;
import com.huatek.busi.dto.GisPdCacheDto;
import com.huatek.busi.dto.monitor.BusiOnlineMonitorDataDto;
import com.huatek.busi.entity.GisPdCache;
import com.huatek.busi.entity.TransformerPd;
import com.huatek.busi.mapper.GisPdCacheMapper;
import com.huatek.busi.service.BusiCommonService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.gis.GisPdCacheService;
import com.huatek.busi.vo.GisPdCacheVo;

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

    @Autowired
    private GisPdCacheMapper gisPdCacheMapper;
    @Autowired
    private GisPdCacheDao gisPdCacheDao;
    @Autowired
    private BusiCommonService busiCommonService;
    @Autowired
    private BusiEqmWarningService busiEqmWarningService;

    @Override
    public PageInfo<GisPdCacheVo> findGisPdCachePage(GisPdCacheDto gisPdCacheDto) {
    	PageInfo<GisPdCacheVo> pageList =  PageHelper.startPage(gisPdCacheDto.getPage(), gisPdCacheDto.getLimit())
            .doSelectPageInfo(() -> gisPdCacheMapper.selectGisPdCachePage(gisPdCacheDto));
        pageList.getList().stream().forEach(pdCacheVo ->{
    		pdCacheVo.setRepairCount(busiCommonService.getEquipHalfYearRepairCount(ExternalTypeEnum.GIS, pdCacheVo.getEquipmentId()));
            // 误报率
    		pdCacheVo.setFalseAlarmRate(busiEqmWarningService.getFalseAlarmRateBySensor(pdCacheVo.getSensorId(),
                DateUtil.getMonthStartDay(), DateUtil.getMonthLastDay()));
    	});
        return pageList;
    }

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

    @Override
    public PageInfo<Map<String, Object>> findGisPdCachePage(BusiOnlineMonitorDataDto busiOnlineMonitorDataDto) {
        // TODO Auto-generated method stub
         return null;
    }
}