package cn.cecep.talroad.task;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.data.analyse.task.service.PollutionSourceControlService;
import cn.cecep.talroad.service.IBMainEmissionPollutionAirService;
import cn.cecep.talroad.service.IBMainEmissionPollutionWaterService;
import cn.cecep.talroad.service.analysis.ISRaSelfMonitoringDataService;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("质控实际监控值是否超过设置标准值")
@RestController
@RequestMapping("/pollutionSourceControl")
public class PollutionSourceControlController {

    @Autowired
    private PollutionSourceControlService service;
    @Autowired
    private IBMainEmissionPollutionAirService ibMainEmissionPollutionAirService;
    @Autowired
    private IBMainEmissionPollutionWaterService ibMainEmissionPollutionWaterService;
    @Autowired
    private ISRaSelfMonitoringDataService isRaSelfMonitoringDataService;

    @ApiOperation(value = "定时任务-质控实际监控值是否超过设置标准值--每小时计算一次")
    @Log(title = "质控实际监控值是否超过设置标准值", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/invoke")
    public void doProcessGasStationDataFlag(){
        //执行流程
        service.invoke();
    }

    @ApiOperation(value = "定时任务--多源排放量对比分析-每天一次")
    @Log(title = "多源排放量对比分析", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/doProcessMultiSource")
    public void doProcessMultiSource(){
        Date lastYearDate = DateUtils.addDateDays(new Date(),-1);
        // 取上一天所在年度
        String lastYear = DateUtils.parseDateToStr("yyyy",lastYearDate);
        ibMainEmissionPollutionAirService.doProcessEmissionDataByYear(lastYear);
        ibMainEmissionPollutionWaterService.doProcessEmissionDataByYear(lastYear);

    }

    @ApiOperation(value = "定时任务--多源排放量汇总手工数据-每天一次")
    @Log(title = "多源排放量汇总手工数据", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/doProcessHandleData")
    public void doProcessHandleData(@RequestParam("startDate")String startDate){
        if(StringUtils.isEmpty(startDate)) {
            Date lastDay = DateUtils.addDateDays(new Date(), -1);
            DateTime lastDayBegin = DateUtil.beginOfDay(lastDay);
            DateTime lastDayEnd = DateUtil.endOfDay(lastDay);
            isRaSelfMonitoringDataService.doProcessHandleData(lastDayBegin,lastDayEnd);
        }else{
            DateTime lastDayBegin = DateUtil.beginOfYear(new Date());
            DateTime lastDayEnd = DateUtil.endOfYear(new Date());
            isRaSelfMonitoringDataService.doProcessHandleData(lastDayBegin,lastDayEnd);
        }
    }
}
