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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.huatek.busi.common.BusiConstant.EqType;
import com.huatek.busi.common.BusiConstant.GzType;
import com.huatek.busi.common.BusiConstant.TemperPointType;
import com.huatek.busi.common.CacMonitorTypeCode;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.TemperIncreaseRateCacheDao;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.entity.BusiEqmWarning;
import com.huatek.busi.entity.TemperIncreaseRateCache;
import com.huatek.busi.mapper.TemperIncreaseRateCacheMapper;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.temperature.BusiTemperPointService;
import com.huatek.busi.service.temperature.TemperIncreaseRateCacheService;
import com.huatek.busi.vo.BusiTemperPointVo;
import com.huatek.busi.vo.CwgTemperatureStatisticsVo;
import com.huatek.busi.vo.warning.BusiWcgVo;

@Component
@Service(interfaceClass = TemperIncreaseRateCacheService.class)
@Transactional(rollbackFor = Exception.class)
public class TemperIncreaseRateCacheServiceImpl implements TemperIncreaseRateCacheService {

    @Autowired
    private TemperIncreaseRateCacheMapper temperIncreaseRateCacheMapper;
    @Autowired
    private TemperIncreaseRateCacheDao temperIncreaseRateCacheDao;

    @Autowired
    private BusiTemperPointService busiTemperPointService;
    @Autowired
    private BusiEqmWarningService busiEqmWarningService;
    @Autowired
    private BusiSubTreeService busiSubTreeService;

    @Override
    public void saveTemperIncreaseRateCache(CwgTemperatureStatisticsVo item, LocalDateTime dTime) {
        // 删除记录
        temperIncreaseRateCacheMapper.deleteItem(item.getSensorCode(), dTime);
        // 保存新记录
        TemperIncreaseRateCache entity = new TemperIncreaseRateCache();
        entity.setSiteId(item.getSiteId());
        entity.setSensorId(item.getSensorCode());
        entity.setMainTreeId(item.getMainTreeId());
        entity.setSubTreeId(item.getSubTreeId());
        entity.setDTime(dTime);
        entity.setAverageTemper(DecimalUtil.toStr(item.getTmp()));
        entity.setIncreaseRate("0");
        entity.setCreateTime(LocalDateTime.now());
        // 上一条记录
        TemperIncreaseRateCache oldItem = temperIncreaseRateCacheMapper.getNearlyItem(item.getSensorCode(), dTime);
        if (oldItem != null) {
            entity.setIncreaseRate(DecimalUtil.calIncrease(item.getTmp(), oldItem.getAverageTemper()));
        }
        // 保存
        temperIncreaseRateCacheDao.saveOrUpdate(entity);
        // 温度增长率告警
        saveGrowthRateWarning(item, dTime, entity);
    }

    @Override
    public void saveGrowthRateWarning(CwgTemperatureStatisticsVo item, LocalDateTime dTime, TemperIncreaseRateCache entity) {
        BusiTemperPointVo busiTemperPointVo = busiTemperPointService.getBusiTemperPointVoBySensor(item.getSensorCode());
        EquipmentInfoDto equipmentInfo =
            busiSubTreeService.findEquipmentInfo(item.getSensorCode(), SiteTreeEnum.CW.getValue());
        if (busiTemperPointVo == null || equipmentInfo == null
            || StringUtils.isEmpty(busiTemperPointVo.getGrowRate())) {
            return;
        }
        String phase = "";
        TemperPointType pointType = TemperPointType.from(busiTemperPointVo.getPosition());
        if (pointType != null) {
            phase = pointType.getName();
        }
        BigDecimal thresholdValue = DecimalUtil.getVal(busiTemperPointVo.getGrowRate());
        BigDecimal growthRate = DecimalUtil.getVal(entity.getIncreaseRate());
        if (isWarning(growthRate, thresholdValue)) {
            // 保存告警
            BusiEqmWarning busiEqmWarning =
                busiEqmWarningService.saveBusiEqmWarning(equipmentInfo, EqType.EQ_TYPE4, GzType.GZ_TYPE10,
                    CacMonitorTypeCode.CWG.getCode(), dTime, formateCwWarningMsg(phase, entity.getIncreaseRate()),"1");
            // 保存告警详情
            BusiWcgVo vo = new BusiWcgVo();
            vo.setEquipmentName(equipmentInfo.getSubEquipmentName());
            vo.setCollectValue(entity.getAverageTemper());
            vo.setAlarmValue(entity.getAverageTemper());
            vo.setThresholdValue(busiTemperPointVo.getGrowRate());
            vo.setEquipmentStatus("");
            vo.setCollectDate(dTime);
            vo.setDayIncreaseRate("");
            vo.setMonthIncreaseRate("");
            vo.setPhase(phase);
            busiEqmWarningService.saveBusiEqmWarningInfo(busiEqmWarning, JSON.toJSONString(vo));
        }
    }

    private static boolean isWarning(BigDecimal growthRate, BigDecimal thresholdValue) {
        return growthRate.compareTo(thresholdValue) > 0;
    }

    private String formateCwWarningMsg(String phase, String tmp) {
        return String.format("%s测温柜：%s%%增长率告警", phase, tmp);
    }
}
