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.PatrolInspectionAbnormal;
import com.usky.fire.domain.PatrolInspectionRecord;
import com.usky.fire.domain.PatrolInspectionRoute;
import com.usky.fire.service.PatrolInspectionAbnormalService;
import com.usky.fire.service.vo.PatrolInspectionAbnormalExportVo;
import com.usky.fire.service.vo.PatrolInspectionAbnormalVo;
import com.usky.fire.service.vo.PatrolInspectionRecordVo;
import org.apache.ibatis.annotations.Param;
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;

/**
 * <p>
 * 巡检异常记录表 前端控制器
 * </p>
 *
 * @author fu
 * @since 2024-06-13
 */
@RestController
@RequestMapping("/patrolInspectionAbnormal")
public class PatrolInspectionAbnormalController {

    @Autowired
    private PatrolInspectionAbnormalService patrolInspectionAbnormalService;


    @PostMapping("/addAbnormal")
    public ApiResult<Void> add(@RequestBody PatrolInspectionAbnormal patrolInspectionAbnormal) {
        patrolInspectionAbnormalService.addAbnormal(patrolInspectionAbnormal);
        return ApiResult.success();
    }

    @GetMapping("/abnormalList")
    public ApiResult<CommonPage<PatrolInspectionAbnormal>> recordList(@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(patrolInspectionAbnormalService.recordList(areaName, siteName, name, planType, routeId, startDateTime, endDateTime, pageNum, pageSize));
    }

    @GetMapping("/addAbnormalRecordDetails")
    public ApiResult<List<PatrolInspectionAbnormalVo>> recordDetails(@RequestParam(value = "id") Integer id) {
        return ApiResult.success(patrolInspectionAbnormalService.recordDetails(id));
    }

    /**
     * 巡检异常记录导出
     *
     * @param response      HttpServletResponse
     * @param areaName      区域名称（可选）
     * @param siteName      地点名称（可选）
     * @param name          人员名称（可选）
     * @param planType      计划类型（可选）
     * @param routeId       路线ID（可选）
     * @param startDateTime 开始时间（可选）
     * @param endDateTime   结束时间（可选）
     * @throws IOException
     */
    @Log(title = "巡检异常记录", businessType = BusinessType.EXPORT)
    @PostMapping("/abnormalListExport")
    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 = "routeId", required = false) Integer routeId,
                       @RequestParam(value = "startDateTime", required = false) String startDateTime,
                       @RequestParam(value = "endDateTime", required = false) String endDateTime) throws IOException {
        List<PatrolInspectionAbnormalExportVo> list = patrolInspectionAbnormalService.abnormalListExport(areaName, siteName, name, planType, routeId, startDateTime, endDateTime);
        ExcelUtil<PatrolInspectionAbnormalExportVo> util = new ExcelUtil<>(PatrolInspectionAbnormalExportVo.class);
        util.exportExcel(response, list, "巡检异常记录", "巡检异常记录");
    }
}

