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.AlarmFireService;
import com.usky.fire.service.vo.AlarmFireVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 事件管理-火警告警处置
 *
 * @author JCB
 * @since 2022-07-29
 */
@RestController
@RequestMapping("/alarmFire")
public class AlarmFireController {

    @Autowired
    private AlarmFireService alarmFireService;

    /**
     * 事件管理-火警告警处置/隐患报警处理-火灾报警处置/故障告警处置-列表查询
     *
     * @param companyName    单位名称
     * @param handlingStatus 处理状态（0、未处理 1、已处理）
     * @param startDate      开始时间 格式：yyyy-MM-dd HH:mm:ss
     * @param endDate        结束时间 格式：yyyy-MM-dd HH:mm:ss
     * @param pageNum        当前页
     * @param pageSize       每页条数
     * @param alarmType      告警类型（2 火警、4 故障、16 监管、100 火警隐患等）
     * @return
     */
    @GetMapping("alarmFireLowerList")
    public ApiResult<CommonPage<AlarmFireVo>> alarmFireLowerList(@RequestParam(value = "companyName", required = false) String companyName,
                                                                 @RequestParam(value = "handlingStatus", required = false) Integer handlingStatus,
                                                                 @RequestParam(value = "alarmType", required = false) String alarmType,
                                                                 @RequestParam(value = "startDate", required = false) String startDate,
                                                                 @RequestParam(value = "endDate", required = false) String endDate,
                                                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(alarmFireService.alarmFireLowerList(companyName, handlingStatus, startDate, endDate, pageNum, pageSize, alarmType));
    }


    /**
     * 事件管理-火警告警处置/隐患报警处理-火灾报警处置/故障告警处置-左侧数据统计
     *
     * @param alarmType 告警类型（2 火警、4 故障、16 监管等）
     * @return
     */
    @GetMapping("fireStatistics")
    public ApiResult<Map<String, Object>> fireStatistics(@RequestParam(value = "companyName", required = false) String companyName,
                                                         @RequestParam(value = "alarmType", required = false) String alarmType,
                                                         @RequestParam(value = "startDate", required = false) String startDate,
                                                         @RequestParam(value = "endDate", required = false) String endDate) {
        return ApiResult.success(alarmFireService.fireStatistics(companyName, alarmType, startDate, endDate));
    }


    /**
     * 事件管理-火警告警处置/隐患报警处理-火灾报警处置/故障告警处置-数据统计图
     *
     * @param alarmType 告警类型（2 火警、4 故障、16 监管、100火警隐患等）
     * @param startDate 开始时间  格式：yyyy-MM-dd HH:mm:ss
     * @param endDate   结束时间  格式：yyyy-MM-dd HH:mm:ss
     * @return
     */
    @GetMapping("fireStatisticalChart")
    public ApiResult<Map<String, Object>> fireStatisticalChart(@RequestParam(value = "alarmType", required = false) String alarmType,
                                                               @RequestParam(value = "startDate", required = false) String startDate,
                                                               @RequestParam(value = "endDate", required = false) String endDate) {
        return ApiResult.success(alarmFireService.fireStatisticalChart(alarmType, startDate, endDate));
    }


    /**
     * 事件管理-火警告警处置/隐患报警处理-火灾报警处置/故障告警处置-告警处理
     *
     * @param alarmFire
     * @return
     */
    @Log(title = "告警处理", businessType = BusinessType.UPDATE)
    @PutMapping("updateAlarmFire")
    public ApiResult<Void> updateAlarmFire(@RequestBody AlarmFire alarmFire) {
        alarmFireService.updateAlarmFire(alarmFire);
        return ApiResult.success();
    }


    /**
     * 事件管理-/火警告警处置/隐患报警处理-火灾报警处置/故障告警处置-导出
     *
     * @param companyName    单位名称
     * @param handlingStatus 处理状态（0、未处理 1、已处理）
     * @param alarmType      告警类型（2 火警、4 故障、16 监管等）
     * @param startDate      开始时间 格式：yyyy-MM-dd HH:mm:ss
     * @param endDate        结束时间 格式：yyyy-MM-dd HH:mm:ss
     * @param exportTitle    文件名
     * @throws IOException
     */
//    @GetMapping("alarmFireLowerListExport")
//    public ApiResult<String> export(@RequestParam(value = "companyName", required = false) String companyName,
//                                    @RequestParam(value = "handlingStatus", required = false) Integer handlingStatus,
//                                    @RequestParam(value = "alarmType", required = false) String alarmType,
//                                    @RequestParam(value = "startDate", required = false) String startDate,
//                                    @RequestParam(value = "endDate", required = false) String endDate,
//                                    @RequestParam(value = "exportTitle", required = false) String exportTitle) {
//        return ApiResult.success(alarmFireService.alarmFireExport(companyName, handlingStatus, startDate, endDate, alarmType, exportTitle));
//    }
    @PostMapping("alarmFireLowerListExport")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "companyName", required = false) String companyName,
                       @RequestParam(value = "handlingStatus", required = false) Integer handlingStatus,
                       @RequestParam(value = "alarmType", required = false) String alarmType,
                       @RequestParam(value = "startDate", required = false) String startDate,
                       @RequestParam(value = "endDate", required = false) String endDate,
                       @RequestParam(value = "exportTitle") String exportTitle) throws IOException {
        List<AlarmFireVo> list = alarmFireService.alarmFireLowerListExport(companyName, handlingStatus, startDate, endDate, alarmType);
        ExcelUtil<AlarmFireVo> util = new ExcelUtil<AlarmFireVo>(AlarmFireVo.class);
        util.exportExcel(response, list, exportTitle, exportTitle);
    }
}

