package com.usky.fire.controller.web;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.fire.domain.PatrolInspectionRecord;
import com.usky.fire.service.PatrolInspectionRecordService;
import com.usky.fire.service.util.ExcelUtilImage;
import com.usky.fire.service.vo.PatrolInspectionRecordExportVo;
import com.usky.fire.service.vo.PatrolInspectionRecordVo;
import com.usky.fire.service.vo.RecordStatisticsVo;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.List;

/**
 * 巡查自检-巡检记录
 *
 * @author JCB
 * @since 2022-07-21
 */
@RestController
@RequestMapping("/patrolInspectionRecord")
public class PatrolInspectionRecordController {

    @Autowired
    private PatrolInspectionRecordService patrolInspectionRecordService;

    /**
     * 巡查自检-巡检记录-列表查询
     *
     * @param areaName      区域名称
     * @param siteName      地点名称
     * @param name          人员名称
     * @param planType      计划类型
     * @param routeId       路线id
     * @param startDateTime 开始时间
     * @param endDateTime   结束时间
     * @param pageNum       当前页
     * @param pageSize      每页条数
     * @return
     */
    @GetMapping("patrolInspectionRecordLsit")
    public ApiResult<CommonPage<PatrolInspectionRecord>> patrolInspectionRecordLsit(@RequestParam(value = "areaName", required = false) String areaName,
                                                                                    @RequestParam(value = "siteName", required = false) String siteName,
                                                                                    @RequestParam(value = "name", required = false) String name,
                                                                                    @RequestParam(value = "planType", required = false) Integer planType,
                                                                                    @RequestParam(value = "routeId", required = false) Integer routeId,
                                                                                    @RequestParam(value = "startDateTime", required = false) String startDateTime,
                                                                                    @RequestParam(value = "endDateTime", required = false) String endDateTime,
                                                                                    @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(patrolInspectionRecordService.patrolInspectionRecordLsit(areaName, siteName, name, planType, routeId, startDateTime, endDateTime, pageNum, pageSize));
    }

    /**
     * 巡查自检-巡检记录-详情查询
     *
     * @param id 记录ID
     * @return
     */
    @GetMapping("patrolInspectionRecordDetails")
    public ApiResult<List<PatrolInspectionRecordVo>> patrolInspectionRecordDetails(@RequestParam(value = "id") Integer id) {
        return ApiResult.success(patrolInspectionRecordService.patrolInspectionRecordDetails(id));
    }

    /**
     * 巡查自检-巡检记录-巡检情况统计
     *
     * @param startDate 开始日期 yyyy-MM-dd
     * @param endDate   结束日期 yyyy-MM-dd
     * @return
     */
    @GetMapping("recordStatistics")
    public ApiResult<RecordStatisticsVo> recordStatistics(@RequestParam(value = "startDate") String startDate,
                                                          @RequestParam(value = "endDate") String endDate) {
        return ApiResult.success(patrolInspectionRecordService.recordStatistics(startDate, endDate));
    }


    /**
     * 巡查自检-巡检记录-导出
     *
     * @param response
     * @param areaName      区域名称
     * @param siteName      地点名称
     * @param name          人员名称
     * @param planType      计划类型
     * @param startDateTime 开始时间
     * @param endDateTime   结束时间
     * @param idList        记录ID
     * @throws IOException
     */
    @Log(title = "巡检记录", businessType = BusinessType.EXPORT)
    @PostMapping("/recordLsitExport")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "areaName", required = false) String areaName,
                       @RequestParam(value = "siteName", required = false) String siteName,
                       @RequestParam(value = "name", required = false) String name,
                       @RequestParam(value = "planType", required = false) Integer planType,
                       @RequestParam(value = "startDateTime", required = false) String startDateTime,
                       @RequestParam(value = "endDateTime", required = false) String endDateTime,
                       @RequestParam(value = "idList", required = false) String idList) throws IOException {
        List<PatrolInspectionRecordExportVo> list = patrolInspectionRecordService.recordLsitExport(areaName, siteName, name, planType, startDateTime, endDateTime, idList);
        ExcelUtil<PatrolInspectionRecordExportVo> util = new ExcelUtil<PatrolInspectionRecordExportVo>(PatrolInspectionRecordExportVo.class);
        util.exportExcel(response, list, "巡检记录", "巡检记录");
    }


    /**
     * 巡查自检-巡检记录-导出(添加多媒体数据)
     *
     * @param response
     * @param areaName      区域名称
     * @param siteName      地点名称
     * @param name          人员名称
     * @param planType      计划类型
     * @param startDateTime 开始时间
     * @param endDateTime   结束时间
     * @param idList        记录ID
     * @throws IOException
     */
    @PostMapping("/recordLsitExportMultimedia")
    public void exportMultimedia(HttpServletResponse response,
                                 @RequestParam(value = "areaName", required = false) String areaName,
                                 @RequestParam(value = "siteName", required = false) String siteName,
                                 @RequestParam(value = "name", required = false) String name,
                                 @RequestParam(value = "planType", required = false) Integer planType,
                                 @RequestParam(value = "startDateTime", required = false) String startDateTime,
                                 @RequestParam(value = "endDateTime", required = false) String endDateTime,
                                 @RequestParam(value = "idList", required = false) String idList) throws IOException {
        List<PatrolInspectionRecordExportVo> list = patrolInspectionRecordService.exportImage(areaName, siteName, name, planType, startDateTime, endDateTime, idList);
        ExcelUtilImage<PatrolInspectionRecordExportVo> util = new ExcelUtilImage<PatrolInspectionRecordExportVo>(PatrolInspectionRecordExportVo.class);
        util.exportExcel(response, list, "巡检记录", "巡检记录");
//        patrolInspectionRecordService.exportImage(response,areaName, siteName, name, planType, startDateTime, endDateTime, idList);
    }
}

