package org.dromara.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.domain.bo.SenEventsObjectBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.domain.vo.SenEventsVo;
import org.dromara.domain.bo.SenEventsBo;
import org.dromara.service.ISenEventsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 事件信息
 *
 * @author luogang
 * @date 2024-09-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/events")
public class SenEventsController extends BaseController {

    private final ISenEventsService senEventsService;

    /**
     * 查询事件信息列表
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/list")
    public TableDataInfo<SenEventsVo> list(SenEventsBo bo, PageQuery pageQuery) {
        return senEventsService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询最新的事件数据
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/eventsCount")
    public R<HashMap<String,Object>> eventsCount() {
        return R.ok(senEventsService.eventsCount());
    }

    /**
     * 导出事件信息列表
     */
    @SaCheckPermission("manage:events:export")
    @Log(title = "事件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SenEventsBo bo, HttpServletResponse response) {
        List<SenEventsVo> list = senEventsService.queryList(bo);
        ExcelUtil.exportExcel(list, "事件信息", SenEventsVo.class, response);
    }

    /**
     * 获取事件信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("manage:events:query")
    @GetMapping("/{id}")
    public R<SenEventsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(senEventsService.queryById(id));
    }

    /**
     * 新增事件信息
     */
    @SaCheckPermission("manage:events:add")
    @Log(title = "事件信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SenEventsBo bo) {
        return toAjax(senEventsService.insertByBo(bo));
    }

    /**
     * 修改事件信息
     */
    @SaCheckPermission("manage:events:edit")
    @Log(title = "事件信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SenEventsBo bo) {
        return toAjax(senEventsService.updateByBo(bo));
    }

    /**
     * 删除事件信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("manage:events:remove")
    @Log(title = "事件信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(senEventsService.deleteWithValidByIds(List.of(ids), true));
    }
    /**
     * 重点事件统计
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/importantEventsStat")
    public R<List<HashMap<String,Object>>> importantEventsStat(@RequestParam String dateType) {
        return R.ok(senEventsService.importantEventsStat(dateType));
    }
    /**
     * 机占非TOP10
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/motorInLanesTop10")
    public R<HashMap<String,Object>> motorInLanesTop10(@RequestParam String dateType) {
        return R.ok(senEventsService.motorInLanesTop10(dateType));
    }
    /**
     * 事件占比TOP10（%）
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/eventPercentTop10")
    public R<List<Map<String,Object>>> eventPercentTop10() {
        return R.ok(senEventsService.eventPercentTop10());
    }
    /**
     * 近30日事件统计
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/eventMonthCount")
    public R<List<Map<String,Object>>> eventMonthCount() {
        return R.ok(senEventsService.eventMonthCount());
    }
    /**
     * 每日事件统计
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/eventDayCount")
    public R<List<Map<String, Object>>> eventDayCount() {
        return R.ok(senEventsService.eventDayCount());
    }
    /**
     * 监控点位违反统计
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/pointEventsStat")
    public R<List<HashMap<String,Object>>> pointEventsStat(@RequestParam String dateType) {
        return R.ok(senEventsService.pointEventsStat(dateType));
    }
    /**
     * 区域违法分布
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/regionEventsStat")
    public R<List<HashMap<String,Object>>> regionEventsStat(@RequestParam String dateType) {
        return R.ok(senEventsService.regionEventsStat(dateType));
    }
    /**
     * 每分钟事件统计
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/minuteEventsStat")
    public R<List<HashMap<String,Object>>> minuteEventsStat(@RequestParam String eventType) {
        return R.ok(senEventsService.minuteEventsStat(eventType));
    }
    /**
     * 交通违法数据来源分析
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/eventsDatasourceStat")
    public R<List<HashMap<String,Object>>> eventsDatasourceStat(@RequestParam String dateType) {
        return R.ok(senEventsService.eventsDatasourceStat(dateType));
    }
    /**
     * 交通违法分布式时间
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/eventsHourStat")
    public R<List<HashMap<String,Object>>> eventsHourStat(@RequestParam String dateType) {
        return R.ok(senEventsService.eventsHourStat(dateType));
    }
    /**
     * 交通违法处置分析
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/eventsDealRateStat")
    public R<List<HashMap<String,Object>>> eventsDealRateStat(@RequestParam String dateType) {
        return R.ok(senEventsService.eventsDealRateStat(dateType));
    }
    /**
     * 骑行违法分析
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/cycleEventStat")
    public TableDataInfo<Map<String,Object>> cycleEventStat(SenEventsBo senEventsBo, PageQuery pageQuery) {
        return senEventsService.cycleEventStat(senEventsBo,pageQuery);
    }
    /**
     * 重点车辆分析
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/keyVehicleEventStat")
    public TableDataInfo<Map<String,Object>> keyVehicleEventStat(SenEventsObjectBo bo, PageQuery pageQuery) {
        return senEventsService.keyVehicleEventStat(bo,pageQuery);
    }
    /**
     * 查询事件的照片列表
     */
    @SaCheckPermission("manage:events:list")
    @GetMapping("/eventsMediaList")
    public TableDataInfo<SenEventsVo> eventsMediaList(SenEventsBo bo, PageQuery pageQuery) {
        return senEventsService.eventsMediaList(bo, pageQuery);
    }
}
