package com.njcn.influx.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.njcn.influx.service.*;
import com.njcn.oracle.bo.param.DataAsynParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Collections;


/**
 * Description:
 * Date: 2024/1/15  18:47【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/data")
@Api(tags = "OracleToInfluxDB数据同步")
@AllArgsConstructor
public class OracleToInfluxDBController {

    private final OracleToInfluxDBService oracleToInfluxDBService;
    private final PqsOnlineratePOService pqsOnlineratePOService;
    private final OracleEventDetailToMysqlService oracleEventDetailToMysqlService;
    private final OracleMonitorStatusToMysqlService oracleMonitorStatusToMysqlService;

    private final OracleToMysqlService oracleToMysqlService;

    @PostMapping("/dataSync")
    @ApiOperation("数据同步")
    @ApiImplicitParam(name = "dataAsynParam", value = "数据同步参数", required = true)
    @SneakyThrows
    public Boolean dataSync(@RequestBody DataAsynParam dataAsynParam){
        oracleToInfluxDBService.dataBacthSysc(dataAsynParam);
        return  true;// HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, "数据同步");
    }
    @GetMapping("/dataSyncHours")
    @ApiOperation("数据同步")
    public Boolean dataSyncHours(@RequestParam("startDateTime")   String startDateTime,@RequestParam("endDateTime")  String endDateTime) {
        DataAsynParam dataAsynParam = new DataAsynParam();
        dataAsynParam.setStartDateTime(LocalDateTimeUtil.parse(startDateTime, DatePattern.NORM_DATETIME_PATTERN));
        dataAsynParam.setEndDateTime(LocalDateTimeUtil.parse(endDateTime, DatePattern.NORM_DATETIME_PATTERN));
        oracleToInfluxDBService.AsyncData(dataAsynParam);
        return true;// HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, "数据同步");
    }

    @GetMapping("/dataSyncTable")
    @ApiOperation("数据同步")
    public Boolean dataSyncTable(@RequestParam("startDateTime")   String startDateTime,
                                 @RequestParam("endDateTime")  String endDateTime,
                                 @RequestParam("tableName")  String tableName
                                 ) {
        DataAsynParam dataAsynParam = new DataAsynParam();
        dataAsynParam.setStartDateTime(LocalDateTimeUtil.parse(startDateTime, DatePattern.NORM_DATETIME_PATTERN));
        dataAsynParam.setEndDateTime(LocalDateTimeUtil.parse(endDateTime, DatePattern.NORM_DATETIME_PATTERN));
        if(StrUtil.isNotBlank(tableName)){
            dataAsynParam.setTableNames(Collections.singletonList(tableName));
        }
        oracleToInfluxDBService.AsyncData(dataAsynParam);
        return true;// HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, "数据同步");
    }



    @PostMapping("/oneMonitorDataTransport")
    @ApiOperation("单个监测点数据同步")
    @ApiImplicitParam(name = "dataAsynParam", value = "数据同步参数", required = true)
    @SneakyThrows
    public Boolean oneMonitorDataTransport(@RequestBody DataAsynParam dataAsynParam){

        oracleToInfluxDBService.oneMonitorDataTransport(dataAsynParam);

        return  true;// HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, "数据同步");
    }


    @GetMapping("/onlinerateSync")
    @ApiOperation("onlinerate表数据同步")
    public Boolean onlinerateSync(@RequestParam("startDateTime")   String startDateTime,@RequestParam("endDateTime")  String endDateTime) {
        DataAsynParam dataAsynParam = new DataAsynParam();
        dataAsynParam.setStartDateTime(LocalDateTimeUtil.parse(startDateTime, DatePattern.NORM_DATETIME_PATTERN));
        dataAsynParam.setEndDateTime(LocalDateTimeUtil.parse(endDateTime, DatePattern.NORM_DATETIME_PATTERN));
        pqsOnlineratePOService.AsyncData(dataAsynParam);
        return true;// HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, "数据同步");
    }
    @GetMapping("/eventBatch")
    @ApiOperation("eventDetail表数据同步")
    public Boolean eventBatch(@RequestParam("startDateTime")   String startDateTime,@RequestParam("endDateTime")  String endDateTime) {
        try {
            LocalDateTime startDate = LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.parse(startDateTime, DatePattern.NORM_DATE_PATTERN));
            LocalDateTime endDate = LocalDateTimeUtil.endOfDay(LocalDateTimeUtil.parse(endDateTime, DatePattern.NORM_DATE_PATTERN));
            long betweenDay = LocalDateTimeUtil.between(startDate, endDate, ChronoUnit.DAYS);
            oracleEventDetailToMysqlService.eventBatch(startDate, LocalDateTime.of(LocalDate.from(startDate), LocalTime.MAX));
            for (int i = 0; i <=betweenDay; i++) {
                startDate = LocalDateTimeUtil.offset(startDate, 1, ChronoUnit.DAYS);
                oracleEventDetailToMysqlService.eventBatch(startDate, LocalDateTime.of(LocalDate.from(startDate), LocalTime.MAX));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return true;
    }

    @GetMapping("/eventRecall")
    @ApiOperation("eventDetail表数据同步补招接口")
    public Boolean eventRecall(@RequestParam("startDateTime")   String startDateTime,@RequestParam("endDateTime")  String endDateTime) {
        try {
            LocalDateTime startDate = LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.parse(startDateTime, DatePattern.NORM_DATETIME_PATTERN));
            LocalDateTime endDate = LocalDateTimeUtil.endOfDay(LocalDateTimeUtil.parse(endDateTime, DatePattern.NORM_DATETIME_PATTERN));
            oracleToMysqlService.OracleToMySqlJob(startDate, endDate );

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return true;
    }
    @GetMapping("/mSync")
    @ApiOperation("监测点信息同步")
    public Boolean monitorTimeSync() {
        oracleMonitorStatusToMysqlService.monitorTimeSync();
        return true;
    }
}
