package com.yfd.platform.modules.patroltask.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.config.HttpServerConfig;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.patroltask.service.ITaskTodoService;
import com.yfd.platform.utils.HttpRESTfulUtils;
import com.yfd.platform.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 指标 前端控制器
 * </p>
 *
 *
 * @since 2023-05-03
 */
@RestController
@RequestMapping("/patroltask")
@Api(value = "AnalyseIndexController", tags = "可靠性指标")
public class AnalyseIndexController {

    @Resource
    private ITaskTodoService taskTodoService;
    @Resource
    private HttpRESTfulUtils httpRESTfulUtils;
    @Resource
    private HttpServerConfig httpServerConfig;


    @GetMapping("/getDeviceInspectionStat")
    @ApiOperation("巡视设备巡检统计")
    public ResponseResult getDeviceInspectionStat(Page<Map<String, Object>> page, String patrolDeviceCode,
                                                  String patrolDeviceName, String type) {

        Page<Map<String, Object>> mapPage = taskTodoService.getDeviceInspectionStat(page, patrolDeviceCode,
                patrolDeviceName, type);
        return ResponseResult.successData(mapPage);
    }

    @GetMapping("/getTaskDataStat")
    @ApiOperation("巡视任务数据统计")
    public ResponseResult getTaskDataStat(Page<Map<String, Object>> page, String stationCode, String type,
                                          String startDate,
                                          String endDate) {
        String startFormat = "";
        String endFormat = "";
        if ("3".equals(type)) {
            startDate = startDate + "-01";
            Date parseStart = DateUtil.parse(startDate);
            Date beginOfDay = DateUtil.beginOfMonth(parseStart);
            startFormat = DateUtil.format(beginOfDay, "yyyy-MM-dd HH:mm:ss");
            endDate = endDate + "-01";
            Date parseEnd = DateUtil.parse(endDate);
            //一月的结束
            Date endOfDay = DateUtil.endOfMonth(parseEnd);
            endFormat = DateUtil.format(endOfDay, "yyyy-MM-dd HH:mm:ss");
        } else {
            if (StrUtil.isNotBlank(startDate)) {
                Date parseStart = DateUtil.parse(startDate);
                //一天的开始
                Date beginOfDay = DateUtil.beginOfDay(parseStart);
                startFormat = DateUtil.format(beginOfDay, "yyyy-MM-dd HH:mm:ss");
            }

            if (StrUtil.isNotBlank(startDate)) {
                Date parseEnd = DateUtil.parse(endDate);
                //一天的结束
                Date endOfDay = DateUtil.endOfDay(parseEnd);
                endFormat = DateUtil.format(endOfDay, "yyyy-MM-dd HH:mm:ss");
            }
        }
        Page<Map<String, Object>> mapPage = taskTodoService.getTaskDataStat(page, stationCode, type, startFormat, endFormat);
        return ResponseResult.successData(mapPage);
    }

    @GetMapping("/exportTaskDataStat")
    @ApiOperation("导出巡视任务数据统计")
    public void exportTaskDataStat(Page<Map<String, Object>> page, String stationCode, String type, String startDate, String endDate, HttpServletResponse response) throws IOException {
        taskTodoService.exportTaskDataStat(page,stationCode,type,startDate,endDate,response);
    }
}
