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.core.web.domain.AjaxResults;
import cn.cecep.talroad.service.analysis.ISRaFactoryTransferRateAnalysisService;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @Title TransferRateTaskController
 * @Description:
 * @author: Gaohaoyun
 * @date: 2023/6/10 10:23
 */
@Api(tags = "数据传输率定时任务")
@RestController
@RequestMapping("/transferRateTask")
public class TransferRateTaskController {

    @Autowired
    private ISRaFactoryTransferRateAnalysisService isRaFactoryTransferRateAnalysisService;


//    @ApiOperation(value = "触发数据传输率定时任务-企业日任务", notes = "触发数据传输率定时任务-企业日任务")
//    @GetMapping("/generateFactoryDayRecord")
//    public AjaxResults<Void> generateFactoryDayRecord(@RequestParam(name = "statisticsDate", required = false) String statisticsDate) {
//        LocalDate date;
//        if(StrUtil.isNotBlank(statisticsDate)){
//            date = LocalDate.parse(statisticsDate, DateTimeFormatter.ofPattern("yyyyMMdd"));
//        }else{
//            date = LocalDate.now().plusDays(-1L);
//        }
//        isRaFactoryTransferRateAnalysisService.generateFactoryDayRecord(date);
//        return AjaxResults.success();
//    }

    @ApiOperation(value = "触发数据传输率定时任务2-企业日任务", notes = "触发数据传输率定时任务2-企业日任务")
    @GetMapping("/generateFactoryDayRecord2")
    public AjaxResults<Void> generateFactoryDayRecord2(@RequestParam(name = "statisticsDate", required = false) String statisticsDate) {
        LocalDate date;
        if(StrUtil.isNotBlank(statisticsDate)){
            date = LocalDate.parse(statisticsDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }else{
            date = LocalDate.now().plusDays(-1L);
        }
        isRaFactoryTransferRateAnalysisService.generateFactoryDayRecord2(date);
        return AjaxResults.success();
    }

    @ApiOperation(value = "触发数据传输率定时任务-企业月任务", notes = "触发数据传输率定时任务-企业月任务")
    @GetMapping("/generateFactoryMonthRecord")
    public AjaxResults<Void> generateFactoryMonthRecord(@ApiParam(value="生成日期格式为yyyyMM") @RequestParam(name = "statisticsDate", required = false) String statisticsDate) {
        if(StringUtils.isEmpty(statisticsDate)){
            statisticsDate = DateUtils.parseDateToStr("yyyy-MM",DateUtils.addDateMonths(new Date(),-1));
        }
        isRaFactoryTransferRateAnalysisService.generateFactoryMonthRecord(statisticsDate);
        return AjaxResults.success();
    }

    @ApiOperation(value = "触发数据传输率定时任务-企业季度任务", notes = "触发数据传输率定时任务-企业季度任务")
    @GetMapping("/generateFactorySeasonRecord")
    public AjaxResults<Void> generateFactorySeasonRecord(@ApiParam(value="年份") @RequestParam(name = "year", required = false)String year,@ApiParam(value="季度：一季度、二季度、三季度、四季度") @RequestParam(name = "season", required = false)String season) {

        isRaFactoryTransferRateAnalysisService.generateFactorySeasonRecord(year);
        return AjaxResults.success();
    }


    @ApiOperation(value = "触发数据传输率定时任务-企业年度任务", notes = "触发数据传输率定时任务-企业年度任务")
    @GetMapping("/generateFactoryYearRecord")
    public AjaxResults<Void> generateFactoryYearRecord(@ApiParam(value="年份") @RequestParam(name = "year", required = false)String year) {
        if(StringUtils.isEmpty(year)){
            Date lastYear = DateUtils.addDateYears(new Date(),-1);
            year = DateUtils.parseDateToStr("yyyy",lastYear);
        }
        isRaFactoryTransferRateAnalysisService.generateFactoryYearRecord(year);
        return AjaxResults.success();
    }

    @ApiOperation(value = "触发数据传输率定时任务-区域日任务", notes = "触发数据传输率定时任务-区域日任务")
    @GetMapping("/generateAreaDayRecord")
    public AjaxResults<Void> generateAreaDayRecord(@RequestParam(name = "statisticsDate", required = false) String statisticsDate) {
        LocalDate date;
        if(StrUtil.isNotBlank(statisticsDate)){
            date = LocalDate.parse(statisticsDate, DateTimeFormatter.ofPattern("yyyyMMdd"));
        }else{
            date = LocalDate.now().plusDays(-1L);;
        }
        isRaFactoryTransferRateAnalysisService.generateAreaDayRecord(date);
        return AjaxResults.success();
    }

    @ApiOperation(value = "触发数据传输率定时任务-区域月任务", notes = "触发数据传输率定时任务-区域月任务")
    @GetMapping("/generateAreaMonthRecord")
    public AjaxResults<Void> generateAreaMonthRecord(@ApiParam(value="生成日期格式为yyyyMM") @RequestParam(name = "statisticsDate", required = false) String statisticsDate) {
        if(StringUtils.isEmpty(statisticsDate)){
            statisticsDate = DateUtils.parseDateToStr("yyyy-MM",DateUtils.addDateMonths(new Date(),-1));
        }
        isRaFactoryTransferRateAnalysisService.generateAreaMonthRecord(statisticsDate);
        return AjaxResults.success();
    }

    @ApiOperation(value = "触发数据传输率定时任务-区域季度任务", notes = "触发数据传输率定时任务-区域季度任务")
    @GetMapping("/generateAreaSeasonRecord")
    public AjaxResults<Void> generateAreaSeasonRecord(@ApiParam(value="年份") @RequestParam(name = "year", required = false)String year,@ApiParam(value="季度：一季度、二季度、三季度、四季度") @RequestParam(name = "season", required = false)String season) {

        isRaFactoryTransferRateAnalysisService.generateAreaSeasonRecord(year);
        return AjaxResults.success();
    }

    @ApiOperation(value = "触发数据传输率定时任务-区域年度任务", notes = "触发数据传输率定时任务-区域年度任务")
    @GetMapping("/generateAreaYearRecord")
    public AjaxResults<Void> generateAreaYearRecord(@ApiParam(value="年份") @RequestParam(name = "year", required = false)String year) {
        if(StringUtils.isEmpty(year)){
            Date lastYear = DateUtils.addDateYears(new Date(),-1);
            year = DateUtils.parseDateToStr("yyyy",lastYear);
        }
        isRaFactoryTransferRateAnalysisService.generateAreaYearRecord(year);
        return AjaxResults.success();
    }
}
