package com.tofly.scada.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.scada.common.BaseController;
import com.tofly.scada.entity.Scada;
import com.tofly.scada.entity.dto.DeviceQuery;
import com.tofly.scada.entity.dto.ScadaMoreQuery;
import com.tofly.scada.entity.dto.ScadaQuery;
import com.tofly.scada.service.ScadaService;
import com.tofly.scada.service.StatisticsScadaService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author HaiQiu
 * @date 2022/4/25
 */
@Api(tags = "SCADA数据操作")
@RequestMapping("/scada")
@RestController
public class ScadaController extends BaseController {

    @Autowired
    private ScadaService scadaService;

    @Autowired
    private StatisticsScadaService statisticsScadaService;

    @ToFlyAppLog(title = "查询最新SCADA所有数据")
    @ApiOperation(value = "查询最新SCADA所有数据")
    @PostMapping("/all")
    public ResultRespone getData() {
        return decide(scadaService.getListData());
    }

    @ToFlyAppLog(title = "根据设备指标查询最新SCADA所有数据")
    @ApiOperation(value = "根据设备指标查询最新SCADA所有数据")
    @PostMapping("/app/data")
    public ResultRespone getAppData(@RequestBody(required = false) DeviceQuery deviceQuery) {
        return decide(scadaService.getAppData(deviceQuery));
    }


    @ToFlyAppLog(title = "分页查询统计SCADA数据")
    @ApiOperation(value = "分页查询统计SCADA数据")
    @GetMapping("/page")
    public ResultRespone getMonthsData(Page page, ScadaQuery scadaQuery,
                                       @ApiParam("true 分页，false不分页（默认分页）")
                                       @RequestParam(required = false) Boolean isPage) {
        return decide(statisticsScadaService.getPageData(page, scadaQuery,isPage));
    }

//    @ToFlyAppLog(title = "查询统计SCADA数据列表")
//    @ApiOperation(value = "查询统计SCADA数据列表")
//    @GetMapping("/list")
//    public ResultRespone getDataList(ScadaQuery scadaQuery){
//        return decide(statisticsScadaService.getDataList(scadaQuery));
//    }

    @ToFlyAppLog(title = "趋势分析统计SCADA数据")
    @ApiOperation(value = "趋势分析统计SCADA数据")
    @GetMapping("/trend/page")
    public ResultRespone geTrendData(Page page, ScadaQuery scadaQuery,
                                     @ApiParam("true 分页，false不分页（默认分页）")
                                     @RequestParam(required = false) Boolean isPage) {
        if (isPage == null) {
            isPage = true;
        }
        return decide(statisticsScadaService.geTrendData(page, scadaQuery, isPage));
    }


    @ToFlyAppLog(title = "手动更新范围数据")
    @ApiOperation(value = "手动更新范围数据")
    @PutMapping("/data/fetch")
    public ResultRespone fetchData(@ApiParam("code编码，非必传,多个逗号隔开") @RequestParam(required = false) String code,
                                   @ApiParam("开始时间:2022-01-01 00:00:00") @RequestParam String start,
                                   @ApiParam("结束时间:2022-10-01 14:00:00") @RequestParam String end,
                                   @ApiParam("统计类型0小时，1按天，2月份，3年份") @RequestParam Integer type) {
        try {
            scadaService.fetchData(code, start, end, type);
        }catch (Exception e){
            e.printStackTrace();
            return decide(e.getMessage());

        }

        return decide("更新正在进行中...请稍后再查看结果");
    }

//    @ToFlyAppLog(title = "手动更新小时数据")
//    @ApiOperation(value = "手动更新小时数据")
//    @PutMapping("/hours/fetch")
//    public ResultRespone fetch24HoursData(@ApiParam("需要更新的时间数据:2022-05-05 11:00:00,不传默认更新当前时间往前的一个小时，" +
//            "时间不允许更新当前及其未来时间数据") @RequestParam(required = false) String syncTime) {
//        return decide(scadaService.fetch24HoursData(syncTime));
//    }
//
//    @ToFlyAppLog(title = "手动更新月份数据数据")
//    @ApiOperation(value = "手动更新月份数据数据")
//    @PutMapping("/months/fetch")
//    public ResultRespone fetchMonthsData(@ApiParam("需要更新的时间数据:2022-05-05,不传默认更新当前时间往后的一个月，" +
//            "时间不允许更新未来时间数据，默认更新上个月数据") @RequestParam(required = false) String syncTime) {
//        scadaService.fetchMonthsData(syncTime);
//        return decide("更新成功");
//    }


//    @ToFlyAppLog(title = "手动更新天数数据数据")
//    @ApiOperation(value = "手动更新天数数据数据")
//    @PutMapping("/days/fetch")
//    public ResultRespone fetchDaysData(@ApiParam("需要更新的时间数据:2022-05-05,不传默认更新当前时间往后的一天，" +
//            "时间不允许更新未来时间数据，默认更新昨天数据") @RequestParam(required = false) String syncTime) {
//        scadaService.fetchDaysData(syncTime);
//        return decide("更新成功");
//    }

    @ToFlyAppLog(title = "手动更新虚拟表数据")
    @ApiOperation(value = "手动更新虚拟表数据")
    @PutMapping("/xn/fetch")
    public ResultRespone xnFetchData(String sTime,String eTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date startTime = format.parse(sTime);
            Date endTime = format.parse(eTime);
            scadaService.manualSendNetCumulativeFlow(startTime,endTime);
        } catch (Exception e) {
            e.printStackTrace();
            return decide(e.getMessage());
        }


        return decide("更新成功");
    }

    /**
     * 批量新增scada历史数据
     * @param scadaMoreQuery scada历史表
     * @return ResultData
     */
    @ToFlyAppLog(title="批量新增scada历史数据" )
    @ApiOperation(value = "批量新增scada历史数据")
    @PostMapping("/insertBacthHistoryTemp")
    public ResultRespone insertBacthHistoryTemp(@RequestBody ScadaMoreQuery scadaMoreQuery) {
        try {
            statisticsScadaService.insertBacthHistoryTemp(scadaMoreQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return decide(e.getMessage());
        }
        return decide("批量新增成功");
    }

    @ToFlyAppLog(title = "手动更新异常点位虚拟数据")
    @ApiOperation(value = "手动更新异常点位虚拟数据")
    @PutMapping("/syncAbnormalPointData")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间（yyyy-MM-dd HH:mm:ss）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束时间（yyyy-MM-dd HH:mm:ss）", required = true, dataType = "String"),
    })
    public ResultRespone syncAbnormalPointData(String startDate,String endDate) {
        try {
            statisticsScadaService.syncAbnormalPointData(startDate, endDate);
        } catch (Exception e) {
            e.printStackTrace();
            return decide(e.getMessage());
        }
        return decide("更新成功");
    }
}
