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.DemWaterSource;
import com.usky.fire.service.DemWaterSourceService;
import com.usky.fire.service.po.FireWaterPO;
import com.usky.fire.service.vo.DemWaterSourceExportVO;
import com.usky.fire.service.vo.FireWaterStatisticsVO;
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;

/**
 * <p>
 * 水源信息表 前端控制器
 * </p>
 *
 * @author JCB
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/demWaterSource")
public class DemWaterSourceController {

    @Autowired
    private DemWaterSourceService demWaterSourceService;

    /**
     * 消防水源管理-列表
     *
     * @param waterName 水源名称
     * @param id        主键ID
     * @param pageNum   当前页
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("waterSourceList")
    public ApiResult<CommonPage<Map<String, Object>>> waterSourceList(@RequestParam(value = "waterName", required = false) String waterName,
                                                                      @RequestParam(value = "id", required = false, defaultValue = "0") Integer id,
                                                                      @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                      @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(demWaterSourceService.waterSourceList(waterName, id, pageNum, pageSize));
    }

    /**
     * 消防水源管理-列表-导出
     *
     * @param waterName 水源名称
     * @param exportTitle    文件名
     * @throws IOException
     */
    @PostMapping("waterSourceListExport")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "waterName", required = false) String waterName,
                       @RequestParam(value = "exportTitle") String exportTitle)throws IOException{
        List<DemWaterSourceExportVO> list = demWaterSourceService.waterSourceListExport(waterName);
        ExcelUtil<DemWaterSourceExportVO> util = new ExcelUtil<DemWaterSourceExportVO>(DemWaterSourceExportVO.class);
        util.exportExcel(response, list, exportTitle, exportTitle);
    }

    /**
     * 消防水源管理-修改
     *
     * @param demWaterSource
     */
    @Log(title = "消防水源管理-修改", businessType = BusinessType.UPDATE)
    @PutMapping("updateWaterSource")
    public ApiResult<Void> updateWaterSource(@RequestBody DemWaterSource demWaterSource) {
        demWaterSourceService.updateWaterSource(demWaterSource);
        return ApiResult.success();
    }

    /**
     * 消防水源管理-删除
     *
     * @param id 主键ID
     */
    @Log(title = "消防水源管理-删除", businessType = BusinessType.DELETE)
    @DeleteMapping("delWaterSource")
    public ApiResult<Void> delWaterSource(@RequestParam(value = "id") Integer id) {
        demWaterSourceService.delWaterSource(id);
        return ApiResult.success();
    }


    /**
     * 消防水源情况-警情撒点
     *
     * @param scattererType 撒点类型
     * @param streetTown    街镇
     * @return
     */
    @GetMapping("waterSourceScatterer")
    public ApiResult<List<Object>> waterSourceScatterer(@RequestParam(value = "scattererType") String scattererType,
                                                        @RequestParam(value = "streetTown", required = false) String streetTown) {
        return ApiResult.success(demWaterSourceService.waterSourceScatterer(scattererType, streetTown));
    }


    /**
     * 消防水源情况-消防水源数量
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/fireWaterStatistics")
    public ApiResult<FireWaterStatisticsVO> getFireWaterStatistics(@RequestParam(value = "startTime", required = false) String startTime,
                                                                   @RequestParam(value = "endTime", required = false) String endTime) {
        return ApiResult.success(demWaterSourceService.getFireWaterStatistics(startTime, endTime));
    }

    /**
     * 消防水源情况-水源情况
     *
     * @param current   当前页
     * @param size      每页条数
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/page")
    public ApiResult<CommonPage<FireWaterPO>> page(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                   @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
                                                   @RequestParam(value = "startTime", required = false) String startTime,
                                                   @RequestParam(value = "endTime", required = false) String endTime) {
        return ApiResult.success(demWaterSourceService.page(current, size, startTime, endTime));
    }
}

