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.DisEventTransfer;
import com.zksy.dispatch.dto.EventReportDTO;
import com.zksy.dispatch.dto.EventTransferPageInDTO;
import com.zksy.dispatch.service.DisEventTransferService;
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/transfer")
@Api(tags = "转接报送")
public class DisEventTransferController {

    @Autowired
    private DisEventTransferService service;

    @GetMapping("/findByPage")
    @ApiOperation("转接报送分页查询")
    @Anonymous
    public AjaxResult findByPage(EventTransferPageInDTO dto) {
        Page<DisEventTransfer> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

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

    @PostMapping("/save")
    @ApiOperation("新增转接记录")
    @Log(title = "新增转接记录", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody DisEventTransfer 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 DisEventTransfer 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("/doTransfer")
    @ApiOperation("执行转接")
    @Log(title = "执行转接", businessType = BusinessType.UPDATE)
    public AjaxResult doTransfer(@RequestBody DisEventTransfer entity) {
        boolean transferred = service.doTransfer(entity);
        return transferred ? AjaxResult.success("转接成功") : AjaxResult.error("转接失败");
    }

    @PutMapping("/doReport")
    @ApiOperation("执行报送")
    @Log(title = "执行报送", businessType = BusinessType.UPDATE)
    public AjaxResult doReport(@RequestBody EventReportDTO dto) {
        boolean reported = service.doReport(dto);
        return reported ? AjaxResult.success("报送成功") : AjaxResult.error("报送失败");
    }

    @GetMapping("/getTransferLogs/{transferId}")
    @ApiOperation("查询转接日志")
    @Anonymous
    public AjaxResult getTransferLogs(@PathVariable Long transferId) {
        return AjaxResult.success(service.getTransferLogs(transferId));
    }
}
