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.DisEventReport;
import com.zksy.dispatch.dto.EventReportPageInDTO;
import com.zksy.dispatch.service.DisEventReportService;
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/report")
@Api(tags = "接报管理")
public class DisEventReportController {

    @Autowired
    private DisEventReportService service;

    @GetMapping("/findByPage")
    @ApiOperation("接报管理分页查询")
    @Anonymous
    public AjaxResult findByPage(EventReportPageInDTO dto) {
        Page<DisEventReport> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

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

    @PostMapping("/save")
    @ApiOperation("新增接报记录")
    @Log(title = "新增接报记录", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody DisEventReport 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 DisEventReport 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("删除失败");
    }

    @PutMapping("/accept/{id}")
    @ApiOperation("受理接报")
    @Log(title = "受理接报", businessType = BusinessType.UPDATE)
    public AjaxResult accept(@PathVariable Long id) {
        boolean accepted = service.accept(id);
        return accepted ? AjaxResult.success("受理成功") : AjaxResult.error("受理失败");
    }

    @PutMapping("/reject/{id}")
    @ApiOperation("驳回接报")
    @Log(title = "驳回接报", businessType = BusinessType.UPDATE)
    public AjaxResult reject(@PathVariable Long id, @RequestParam String remark) {
        boolean rejected = service.reject(id, remark);
        return rejected ? AjaxResult.success("驳回成功") : AjaxResult.error("驳回失败");
    }
}
