package com.zksy.web.controller.dispatch;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import com.zksy.dispatch.domain.DisEventRecord;
import com.zksy.dispatch.dto.DisEventRecordPageDTO;
import com.zksy.dispatch.dto.EventStatsVO;
import com.zksy.dispatch.service.DisEventRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/dispatch/event-record")
@Api(tags = "应急事件数据查询与统计")
public class DisEventRecordController {

    @Autowired
    private DisEventRecordService service;

    @GetMapping("/findByPage")
    @ApiOperation("多维条件分页查询")
    @Anonymous
    public AjaxResult findByPage(DisEventRecordPageDTO dto) {
        Page<DisEventRecord> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/stats")
    @ApiOperation("多维条件统计（事件总数/损失合计/平均损失/重大及以上事件数）")
    @Anonymous
    public AjaxResult stats(DisEventRecordPageDTO dto) {
        EventStatsVO stats = service.stats(dto);
        return AjaxResult.success(stats);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询事件记录")
    @Anonymous
    public AjaxResult getById(@PathVariable Long id) {
        DisEventRecord entity = service.getById(id);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("事件记录不存在");
    }

    @PostMapping("/save")
    @ApiOperation("新增事件记录")
    @Log(title = "新增事件记录", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody DisEventRecord entity) {
        boolean saved = service.save(entity);
        return saved ? AjaxResult.success("新增成功", saved) : AjaxResult.error("新增失败");
    }

    @PutMapping("/updateById")
    @ApiOperation("修改事件记录")
    @Log(title = "修改事件记录", businessType = BusinessType.UPDATE)
    public AjaxResult updateById(@RequestBody DisEventRecord entity) {
        boolean updated = service.updateById(entity);
        return updated ? AjaxResult.success("修改成功", updated) : AjaxResult.error("修改失败");
    }

    @DeleteMapping("/deleteById")
    @ApiOperation("删除事件记录")
    @Log(title = "删除事件记录", businessType = BusinessType.DELETE)
    public AjaxResult deleteById(@RequestParam Long id) {
        boolean deleted = service.removeById(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}
