package com.tofly.gpsboot.controller.statement;

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.es.PdaDeviceUpload;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.business.ExcelExportFileTypeEnum;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.calculate.ICalculateGpsService;
import com.tofly.gpsboot.service.statement.IStatementService;
import com.tofly.gpsboot.util.ExcelUtil;
import com.tofly.pojo.statement.dto.MaintenanceClassificationDto;
import com.tofly.pojo.statement.dto.StatementDto;
import com.tofly.pojo.statement.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表接口信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/statement")
@Api(tags = "报表接口信息")
@Slf4j
public class StatementController {
    @Autowired
    private IStatementService iStatementService;
    @Autowired
    private ICalculateGpsService iCalculateGpsService;

    @ApiOperation(value = "获取人员工时统计信息")
    @PostMapping("/getMaintenanceWork")
    @ToFlyAppLog(title = "获取人员工时统计信息")
    public ResultRespone<Object> getMaintenanceWork(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<MaintenanceWorkVo> maintenanceWork = iStatementService.getMaintenanceWork(dto);
            rest.setResult(maintenanceWork);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取管网设施维护情况")
    @PostMapping("/getMaintenanceStatistics")
    @ToFlyAppLog(title = "获取管网设施维护情况")
    public ResultRespone<Object> getMaintenanceStatistics(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<MaintenanceStatisticsVo> maintenanceStatistics = iStatementService.getMaintenanceStatistics(dto);
            rest.setResult(maintenanceStatistics);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取维修记录台账数据")
    @PostMapping("/getMaintenanceLogLedger")
    @ToFlyAppLog(title = "获取维修记录台账数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exportFlag", value = "不导出不传值,导出传1", dataType = "String"),
    })
    public ResultRespone<Page<MaintenanceLogLedgerVo>> getMaintenanceLogLedger(HttpServletResponse response, Page page, @ModelAttribute StatementDto dto, String exportFlag) {
        ResultRespone<Page<MaintenanceLogLedgerVo>> rest = new ResultRespone<>();
        try {
            Page<MaintenanceLogLedgerVo> maintenanceLogLedger = iStatementService.getMaintenanceLogLedger(page, dto);
            if (!StringUtils.isEmpty(exportFlag)) {
                //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
                // 从模型类型枚举根据templateServiceType获取策略对应模板数据
                Map<String, Object> dataMap = new HashMap<>(12);
                dataMap.put("list", maintenanceLogLedger.getRecords());
                ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("statementService", "0");
                ExcelUtil.export(dataMap, excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
                return null;
            } else {
                rest.setResult(maintenanceLogLedger);
            }

        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取维修保养记录台账数据")
    @PostMapping("/getMaintenanceMaintain")
    @ToFlyAppLog(title = "获取维修保养记录台账数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exportFlag", value = "不导出不传值,导出传1", dataType = "String"),
    })
    public ResultRespone<Page<MaintenanceMaintainVo>> getMaintenanceMaintain(HttpServletResponse response, Page page, @ModelAttribute StatementDto dto, String exportFlag) {
        ResultRespone<Page<MaintenanceMaintainVo>> rest = new ResultRespone<>();
        try {
            Page<MaintenanceMaintainVo> maintenanceLogLedger = iStatementService.getMaintenanceMaintain(page, dto);
            if (!StringUtils.isEmpty(exportFlag)) {
                //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
                // 从模型类型枚举根据templateServiceType获取策略对应模板数据
                Map<String, Object> dataMap = new HashMap<>(12);
                dataMap.put("list", maintenanceLogLedger.getRecords());
                ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("statementService", "1");
                ExcelUtil.export(dataMap, excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
                return null;
            } else {
                rest.setResult(maintenanceLogLedger);
            }
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取维修类型统计")
    @PostMapping("/getMaintenanceTypeStatistics")
    @ToFlyAppLog(title = "获取维修类型统计")
    public ResultRespone<Object> getMaintenanceTypeStatistics(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<MaintenanceTypeStatisticsVo> maintenanceTypeStatistics = iStatementService.getMaintenanceTypeStatistics(dto);
            rest.setResult(maintenanceTypeStatistics);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取耗材情况")
    @PostMapping("/getMaterialConditions")
    @ToFlyAppLog(title = "获取耗材情况")
    public ResultRespone<Object> getMaterialConditions(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<MaterialConditionsVo> materialConditions = iStatementService.getMaterialConditions(dto);
            rest.setResult(materialConditions);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "维护分类统计")
    @PostMapping("/getMaintenanceClassification")
    @ToFlyAppLog(title = "维护分类统计")
    public ResultRespone<Object> getMaintenanceClassification(@RequestBody MaintenanceClassificationDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<MaintenanceClassificationVo> maintenanceClassification = iStatementService.getMaintenanceClassification(dto.getDtoList());
            rest.setResult(maintenanceClassification);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "隐患类型分析")
    @PostMapping("/getTroubleTypeAnalysis")
    @ToFlyAppLog(title = "隐患类型分析")
    public ResultRespone<Object> getTroubleTypeAnalysis(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<TroubleTypeAnalysisVo> troubleTypeAnalysis = iStatementService.getTroubleTypeAnalysis(dto);
            rest.setResult(troubleTypeAnalysis);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "隐患原因分析")
    @PostMapping("/getTroubleCause")
    @ToFlyAppLog(title = "隐患原因分析")
    public ResultRespone<Object> getTroubleCause(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<TroubleCauseVo> troubleCause = iStatementService.getTroubleCause(dto);
            rest.setResult(troubleCause);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "隐患趋势分析")
    @PostMapping("/getTroubleTrendAnalysis")
    @ToFlyAppLog(title = "隐患趋势分析")
    public ResultRespone<Object> getTroubleTrendAnalysis(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<TroubleTrendAnalysisVo> troubleTrendAnalysis = iStatementService.getTroubleTrendAnalysis(dto);
            rest.setResult(troubleTrendAnalysis);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "隐患记录台账")
    @PostMapping("/getTroubleRecordLedger")
    @ToFlyAppLog(title = "隐患记录台账")
    public ResultRespone<Page> getTroubleRecordLedger(Page page, @ModelAttribute StatementDto dto) {
        ResultRespone<Page> rest = new ResultRespone<>();
        try {
            Page troubleRecordLedger = iStatementService.getTroubleRecordLedger(page, dto);
            rest.setResult(troubleRecordLedger);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取巡检日报")
    @PostMapping("/getInspectionDay")
    @ToFlyAppLog(title = "获取巡检日报")
    public ResultRespone<Object> getInspectionDay(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<InspectionDayVo> inspectionDay = iStatementService.getInspectionDay(dto);
            rest.setResult(inspectionDay);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @ApiOperation(value = "获取巡检月报")
    @PostMapping("/getInspectionMonth")
    @ToFlyAppLog(title = "获取巡检月报")
    public ResultRespone<Object> getInspectionMonth(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<InspectionMonthVo> inspectionMonth = iStatementService.getInspectionMonth(dto);
            rest.setResult(inspectionMonth);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取巡检统计")
    @PostMapping("/getInspectionStatistics")
    @ToFlyAppLog(title = "获取巡检统计")
    public ResultRespone<Object> getInspectionStatistics(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<InspectionStatisticsVo> inspectionStatistics = iStatementService.getInspectionStatistics(dto);
            rest.setResult(inspectionStatistics);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "考勤记录")
    @PostMapping("/getAttendanceRecords")
    @ToFlyAppLog(title = "考勤记录")
    public ResultRespone<Object> getAttendanceRecords(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<AttendanceRecordsVo> attendanceRecords = iStatementService.getAttendanceRecords(dto);
            rest.setResult(attendanceRecords);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "排班计划")
    @PostMapping("/getScheduling")
    @ToFlyAppLog(title = "排班计划")
    public ResultRespone<Object> getScheduling(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<SchedulingVo> scheduling = iStatementService.getScheduling(dto);
            rest.setResult(scheduling);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取终端记录台账")
    @PostMapping("/getTerminalRecord")
    @ToFlyAppLog(title = "获取终端记录台账")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exportFlag", value = "不导出不传值,导出传1", dataType = "String"),
    })
    public ResultRespone<Page<List<PdaDeviceUpload>>> getTerminalRecord(HttpServletResponse response, Page page, @ModelAttribute StatementDto dto, String exportFlag) {
        ResultRespone<Page<List<PdaDeviceUpload>>> rest = new ResultRespone<>();
        try {
            //导出时采用升序排序导出
            Page<List<PdaDeviceUpload>> terminalRecord = iStatementService.getTerminalRecord(page, dto, exportFlag);
            if (!StringUtils.isEmpty(exportFlag)) {
                //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
                // 从模型类型枚举根据templateServiceType获取策略对应模板数据
                Map<String, Object> dataMap = new HashMap<>(12);
                dataMap.put("list", terminalRecord.getRecords());
                ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("statementService", "2");
                ExcelUtil.export(dataMap, excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
                return null;
            } else {
                rest.setResult(terminalRecord);
            }
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "获取综合展示各模块任务数统计信息")
    @PostMapping("/getPlanDisplayStatistical")
    @ToFlyAppLog(title = "获取综合展示各模块任务数统计信息")
    public ResultRespone<Object> getPlanDisplayStatistical(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<PlanDisplayStatisticalVo> planDisplayStatistical = iStatementService.getPlanDisplayStatistical(dto);
            rest.setResult(planDisplayStatistical);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取综合展示各模块任务数统计信息异常:{}", e.getMessage());
        }
        return rest;
    }

    @GetMapping("/cal")
    @ApiOperation("测试计算接口")
    public void cal(String start,String end) {
        iCalculateGpsService.calculateGps(start, end);
    }

}
