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

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

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.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.TemperIncreaseRateDao;
import com.huatek.busi.entity.TemperIncreaseRate;
import com.huatek.busi.entity.TemperIncreaseRateCache;
import com.huatek.busi.mapper.TemperIncreaseRateMapper;
import com.huatek.busi.service.temperature.TemperIncreaseRateCacheService;
import com.huatek.busi.service.temperature.TemperIncreaseRateService;
import com.huatek.busi.vo.CwgTemperatureStatisticsVo;

@Component
@Service(interfaceClass = TemperIncreaseRateService.class)
@Transactional(rollbackFor = Exception.class)
public class TemperIncreaseRateServiceImpl implements TemperIncreaseRateService {

    @Autowired
    private TemperIncreaseRateMapper temperIncreaseRateMapper;
    @Autowired
    private TemperIncreaseRateDao temperIncreaseRateDao;
    @Autowired
    private TemperIncreaseRateCacheService temperIncreaseRateCacheService;

    @Override
    public void saveTemperIncreaseRate(CwgTemperatureStatisticsVo item, LocalDate dTime) {
        // 删除记录
        temperIncreaseRateMapper.deleteItem(item.getSensorCode(), dTime);
        // 保存新记录
        TemperIncreaseRate entity = new TemperIncreaseRate();
        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.setDailyGrowthRate("0");
        entity.setMonthlyGrowthRate("0");
        entity.setMonthAverageTemper("0");
        entity.setCreateTime(LocalDateTime.now());
        // 上一条记录
        TemperIncreaseRate oldItem = temperIncreaseRateMapper.getNearlyItem(item.getSensorCode(), dTime.minusDays(1));
        if (oldItem != null) {
            entity.setDailyGrowthRate(DecimalUtil.calIncrease(item.getTmp(), oldItem.getAverageTemper()));
        }
        // 月增长率
        BigDecimal monthAvgTemper = temperIncreaseRateMapper.getMonthAvgTemper(item.getSensorCode(),
            getMonthStartDay(dTime), getMonthEndDay(dTime));
        entity.setMonthlyGrowthRate(monthAvgTemper == null ? null : monthAvgTemper.toString());
        // 保存
        temperIncreaseRateDao.saveOrUpdate(entity);
        // 温度增长率告警
        TemperIncreaseRateCache temperIncreaseRateCache = new TemperIncreaseRateCache();
        if (entity != null) {
            temperIncreaseRateCache.setAverageTemper(entity.getAverageTemper());
            temperIncreaseRateCache.setIncreaseRate(entity.getDailyGrowthRate());
        }
        temperIncreaseRateCacheService.saveGrowthRateWarning(item, dTime.atStartOfDay(), temperIncreaseRateCache);
    }

    private LocalDate getMonthStartDay(LocalDate day) {
        return LocalDate.of(day.getYear(), day.getMonth(), 1);
    }

    private LocalDate getMonthEndDay(LocalDate day) {
        return LocalDate.of(day.getYear(), day.getMonth(), day.getDayOfMonth());
    }

    @Override
    public TemperIncreaseRate getTemperIncreaseRateBySensorCode(String sensorCode) {
        return temperIncreaseRateMapper.getTemperIncreaseRateBySensorCode(sensorCode);
    }

    @Override
    public  String getDailyGrowthRate(String sensorCode,LocalDate day){
        return temperIncreaseRateMapper.getDailyGrowthRate(sensorCode, day);
    }
}
