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

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.OtherPdCacheDao;
import com.huatek.busi.dto.OtherPdCacheDto;
import com.huatek.busi.dto.TransformerPdDto;
import com.huatek.busi.dto.monitor.BusiOnlineMonitorDataDto;
import com.huatek.busi.entity.OtherPdCache;
import com.huatek.busi.entity.TransformerPd;
import com.huatek.busi.mapper.OtherPdCacheMapper;
import com.huatek.busi.mapper.TransformerPdMapper;
import com.huatek.busi.service.BusiCommonService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.Other.OtherPdCacheService;
import com.huatek.busi.vo.OtherPdCacheVo;

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

    @Autowired
    private OtherPdCacheMapper otherPdCacheMapper;
    @Autowired
    private OtherPdCacheDao otherPdCacheDao;
    @Autowired
    private BusiCommonService busiCommonService;
    @Autowired
    private BusiEqmWarningService busiEqmWarningService;
    @Autowired
    private TransformerPdMapper transformerPdMapper;

    @Override
    public PageInfo<OtherPdCacheVo> findOtherPdCachePage(OtherPdCacheDto otherPdCacheDto) {
    	PageInfo<OtherPdCacheVo> pageList =  PageHelper.startPage(otherPdCacheDto.getPage(), otherPdCacheDto.getLimit())
            .doSelectPageInfo(() -> otherPdCacheMapper.selectOtherPdCachePage(otherPdCacheDto));
        pageList.getList().stream().forEach(pdCacheVo ->{
    		pdCacheVo.setRepairCount(busiCommonService.getEquipHalfYearRepairCount(ExternalTypeEnum.OTHER, pdCacheVo.getEquipmentId()));
            // 误报率
    		pdCacheVo.setFalseAlarmRate(busiEqmWarningService.getFalseAlarmRateBySensor(pdCacheVo.getSensorId(),
                DateUtil.getMonthStartDay(), DateUtil.getMonthLastDay()));
    	});
        return pageList;
    }

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

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

	@Override
	public PageInfo<Map<String, Object>> findOtherPdCachePage(TransformerPdDto transformerPdDto) {
		// TODO Auto-generated method stub
		PageInfo<Map<String, Object>> pageInfo =
	            PageHelper.startPage(transformerPdDto.getPage(), transformerPdDto.getLimit())
	                .doSelectPageInfo(() -> transformerPdMapper.findOtherPdPage(transformerPdDto));
	        pageInfo.getList().stream().forEach(map -> {
	            // 维修次数
	            map.put("repairCount", busiCommonService.getEquipHalfYearRepairCount(ExternalTypeEnum.OTHER,
	                map.get("subEquipmentId").toString()));
	            // 误报率
	            map.put("falseAlarmRate", busiEqmWarningService.getFalseAlarmRateBySensor(map.get("sensorId").toString(),
	                DateUtil.getMonthStartDay(), DateUtil.getMonthLastDay()));
	        });
	        return pageInfo;
	}
}