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.AlarmFire;
import com.usky.fire.service.vo.DemInspectExportVO;
import com.usky.fire.service.vo.InspectDetailVO;
import com.usky.fire.service.vo.InspectHandleVO;
import com.usky.fire.service.vo.InspectManagerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.usky.fire.service.DemInspectListService;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.List;

/**
 * <p>
 * 督察单 前端控制器
 * </p>
 *
 * @author ZYJ
 * @since 2022-12-29
 */
@RestController
@RequestMapping("/demInspect")
public class DemInspectListController {
    @Autowired
    private DemInspectListService demInspectListService;

    /**
     * 事件管理-数字平台电子督察单-列表查询
     *
     * @param inspectCode    督察单编号
     * @param companyName    单位名称
     * @param deviceCode     设备编号
     * @param handlingStatus 处置状态（1、已处置 2、待处置 3、督查超时）
     * @param pageNum        当前页
     * @param pageSize       每页条数
     * @return
     */
    @GetMapping("demInspectList")
    public ApiResult<CommonPage<InspectDetailVO>> demInspectList(@RequestParam(value = "inspectCode", required = false) String inspectCode,
                                                             @RequestParam(value = "companyName", required = false) String companyName,
                                                             @RequestParam(value = "deviceCode", required = false) String deviceCode,
                                                             @RequestParam(value = "handlingStatus", required = false) Integer handlingStatus,
                                                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(demInspectListService.demInspectList(inspectCode, companyName, deviceCode, handlingStatus, pageNum, pageSize));
    }

    /**
     * 事件管理-数字平台电子督察单-列表查询-导出
     *
     * @param inspectCode    督查单编号
     * @param companyName    单位名称
     * @param deviceCode     设备编号
     * @param handlingStatus 处置状态（1、已处置 2、待处置 3、督查超时）
     * @param exportTitle    文件名
     * @throws IOException
     */
    @PostMapping("demInspectListExport")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "inspectCode", required = false) String inspectCode,
                       @RequestParam(value = "companyName", required = false) String companyName,
                       @RequestParam(value = "deviceCode", required = false) String deviceCode,
                       @RequestParam(value = "handlingStatus", required = false) Integer handlingStatus,
                       @RequestParam(value = "exportTitle") String exportTitle)throws IOException {
        List<DemInspectExportVO> list = demInspectListService.demInspectListExport(inspectCode, companyName, deviceCode, handlingStatus);
        ExcelUtil<DemInspectExportVO> util = new ExcelUtil<DemInspectExportVO>(DemInspectExportVO.class);
        util.exportExcel(response, list, exportTitle, exportTitle);
    }

    /**
     * 事件管理-企业电子督察单-列表管理
     *
     * @param inspectCode    督察单编号
     * @param deviceCode     设备编号
     * @param handlingStatus 处置状态（1、已处置 2、待处置 3、超时未处置）
     * @param pageNum        当前页
     * @param pageSize       每页条数
     * @return
     */
    @GetMapping("inspectManageList")
    public ApiResult<CommonPage<InspectManagerVO>> inspectManageList(@RequestParam(value = "inspectCode", required = false) String inspectCode,
                                                                 @RequestParam(value = "deviceCode", required = false) String deviceCode,
                                                                 @RequestParam(value = "handlingStatus", required = false) Integer handlingStatus,
                                                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(demInspectListService.inspectManageList(inspectCode, deviceCode, handlingStatus, pageNum, pageSize));
    }


    /**
     * 事件管理-数字平台电子督察单-数据统计
     *
     */
    @GetMapping("inspectStatistics")
    public ApiResult<Map<String, Object>> inspectStatistics(@RequestParam(value = "startDate", required = false) String startDate,
                                                         @RequestParam(value = "endDate", required = false) String endDate) {
        return ApiResult.success(demInspectListService.inspectStatistics(startDate, endDate));
    }

    /**
     * 事件管理-数字平台/企业版-查看电子督察单
     *
     * @param inspectCode
     * @return
     */
    @GetMapping("inspectInfo")
    public ApiResult<List<InspectHandleVO>> inspectInfo(@RequestParam(value = "inspectCode") String inspectCode) {
        return ApiResult.success(demInspectListService.inspectInfo(inspectCode));
    }

    /**
     * 事件管理-企业版-填报电子督察单处理结果
     * @param inspectCode  督察单编号
     * @param handler    处理人
     * @param handlerPhone  处理人联系电话
     * @param deviceStatus  设备状态（1 已恢复 2 设备损坏 3 设备维修 4 设备停用）
     * @param handlingContent   处理内容
     * @param sitePhoto1    现场照片1
     * @param sitePhoto2    现场照片2
     * @param sitePhoto3    现场照片3
     * @return
     */
    @Log(title = "企业版-填报电子督察单处理结果", businessType = BusinessType.UPDATE)
    @PutMapping("updateInspectManage")
    public ApiResult<Void> updateInspectManage(@RequestParam(value = "inspectCode") String inspectCode,
                                               @RequestParam(value = "handler") String handler,
                                               @RequestParam(value = "handlerPhone") String handlerPhone,
                                               @RequestParam(value = "deviceStatus") Integer deviceStatus,
                                               @RequestParam(value = "handlingContent") String handlingContent,
                                               @RequestParam(value = "sitePhoto1") String sitePhoto1,
                                               @RequestParam(value = "sitePhoto2", required = false) String sitePhoto2,
                                               @RequestParam(value = "sitePhoto3", required = false) String sitePhoto3) {
        demInspectListService.updateInspectManage(inspectCode, handler, handlerPhone, deviceStatus, handlingContent, sitePhoto1, sitePhoto2, sitePhoto3);
        return ApiResult.success();
    }
}

