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.fire.domain.DemPoliceInfo;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.fire.domain.DemPoliceRecord;
import com.usky.fire.service.DemMicroStationService;
import com.usky.fire.service.DemPoliceInfoService;
import com.usky.fire.service.DemVehicleFightService;
import com.usky.fire.service.DemWaterSourceService;
import com.usky.fire.service.vo.AlertStatisticsVO;
import com.usky.fire.service.vo.DemPoliceRecordExportVO;
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("/demPoliceInfo")
public class DemPoliceInfoController {

    @Autowired
    private DemMicroStationService demMicroStationService;

    @Autowired
    private DemPoliceInfoService demPoliceInfoService;

    @Autowired
    private DemPoliceInfoService demPoliceRecordService;

    @Autowired
    private DemWaterSourceService demWaterSourceService;

    @Autowired
    private DemVehicleFightService demVehicleFightService;

    /**
     * 接处警数据-列表
     *
     * @param caseCode 案件编号
     * @param branch   主管支队
     * @param id       主键ID
     * @param pageNum  当前页面
     * @param pageSize 每页条数
     * @return
     */
    @GetMapping("policeRecordList")
    public ApiResult<CommonPage<Object>> policeRecordList(@RequestParam(value = "caseCode", required = false) String caseCode,
                                                          @RequestParam(value = "branch", required = false) String branch,
                                                          @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(demPoliceInfoService.policeRecordList(caseCode, branch, id, pageNum, pageSize));
    }

    /**
     * 接处警数据-列表-导出
     *
     * @param caseCode 案件编号
     * @param branch   主管支队
     * @param exportTitle   文件名
     * @throws IOException
     */
    @PostMapping("policeRecordListExport")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "caseCode", required = false) String caseCode,
                       @RequestParam(value = "branch", required = false) String branch,
                       @RequestParam(value = "exportTitle") String exportTitle)throws IOException{
        List<DemPoliceRecordExportVO> list = demPoliceInfoService.policeRecordListExport(caseCode, branch);
        ExcelUtil<DemPoliceRecordExportVO> util = new ExcelUtil<DemPoliceRecordExportVO>(DemPoliceRecordExportVO.class);
        util.exportExcel(response, list, exportTitle, exportTitle);
    }

    /**
     * 接处警数据-修改
     *
     * @param demPoliceInfo
     */
    @Log(title = "接处警数据-修改", businessType = BusinessType.UPDATE)
    @PutMapping("updatePoliceRecord")
    public ApiResult<Void> updatePoliceRecord(@RequestBody DemPoliceInfo demPoliceInfo) {
        demPoliceRecordService.updatePoliceRecord(demPoliceInfo);
        return ApiResult.success();
    }

    /**
     * 综合警情-撒点
     *
     * @param streetTown    街镇
     * @param scattererType 撒点类型
     * @return
     */
    @GetMapping("synthesizePoliceSituationScatterer")
    public ApiResult<List<Object>> synthesizePoliceSituationScatterer(@RequestParam(value = "streetTown", required = false) String streetTown,
                                                                      @RequestParam(value = "scattererType", required = false) String scattererType) {
        return ApiResult.success(demMicroStationService.synthesizePoliceSituationScatterer(streetTown, scattererType));
    }

    /**
     * 综合警情-查询每年12个月警情
     *
     * @return
     */
    @GetMapping("/alertStatisticsByMonth")
    public ApiResult<Map<String, List<AlertStatisticsVO>>> getAlertStatisticsByMonth(@RequestParam String startTime,
                                                                                     @RequestParam String endTime,
                                                                                     @RequestParam(value = "streetTown", required = false) String streetTown) {
        return ApiResult.success(demPoliceInfoService.getAlertStatisticsByMonth(startTime, endTime, streetTown));
    }

    /**
     * 查询不同警情(社会救助，警情，火灾)的占比
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/alertStatistics")
    public ApiResult<List<AlertStatisticsVO>> getAlertStatistics(@RequestParam(value = "startTime") String startTime,
                                                                 @RequestParam(value = "endTime") String endTime,
                                                                 @RequestParam(value = "streetTown", required = false) String streetTown) {
        return ApiResult.success(demPoliceInfoService.getAlertStatistics(startTime, endTime, streetTown));
    }

    /**
     * 警情详细信息
     *
     * @param current   页数
     * @param size      条数
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/page")
    public ApiResult<CommonPage<DemPoliceInfo>> page(@RequestParam(value = "startTime", required = false) String startTime,
                                                     @RequestParam(value = "endTime", required = false) String endTime,
                                                     @RequestParam(value = "streetTown", required = false) String streetTown,
                                                     @RequestParam(value = "caseCode", required = false) String caseCode,
                                                     @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                     @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return ApiResult.success(demPoliceInfoService.page(current, size, startTime, endTime, streetTown, caseCode));
    }


    /**
     * 综合警情-警情时段分布
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/alertStatisticsByHouse")
    public ApiResult<Map<String, List<AlertStatisticsVO>>> getAlertStatisticsByHouse(@RequestParam(value = "startTime") String startTime,
                                                                                     @RequestParam(value = "endTime") String endTime,
                                                                                     @RequestParam(value = "streetTown", required = false) String streetTown) {
        return ApiResult.success(demPoliceInfoService.getAlertStatisticsByHouse(startTime, endTime, streetTown));
    }


    /**
     * 综合警情-警情撒点
     *
     * @param streetTown 街镇
     * @return
     */
    @GetMapping("/warningInstanceScatterer")
    public ApiResult<List<Map<String, Object>>> warningInstanceScatterer(@RequestParam(value = "streetTown", required = false) String streetTown,
                                                                         @RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
        return ApiResult.success(demPoliceInfoService.warningInstanceScatterer(streetTown, id));
    }

    /**
     * 综合警情-周边资源
     *
     * @param longitude 实时警情经度
     * @param latitude  实时警情纬度
     * @param distance  距离
     * @return
     */
    @GetMapping("/waterSourceList")
    public ApiResult<List<Map<String, Object>>> waterSourceList(@RequestParam(value = "longitude") double longitude,
                                                                @RequestParam(value = "latitude") double latitude,
                                                                @RequestParam(value = "distance", required = false, defaultValue = "150") Integer distance) {
        return ApiResult.success(demWaterSourceService.surroundingResources(longitude, latitude, distance));
    }

    /**
     * 综合警情-警力实况
     *
     * @param policeId 案件信息ID
     * @return
     */
    @GetMapping("/vehicleFightList")
    public ApiResult<List<Map<String, Object>>> vehicleFightList(@RequestParam(value = "policeId") String policeId) {
        return ApiResult.success(demVehicleFightService.vehicleFightList(policeId));
    }


}

