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.DisEventTrack;
import com.zksy.dispatch.domain.DisEventTrackLog;
import com.zksy.dispatch.dto.EventTrackPageInDTO;
import com.zksy.dispatch.service.DisEventTrackService;
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/track")
@Api(tags = "跟踪解除")
public class DisEventTrackController {

    @Autowired
    private DisEventTrackService service;

    @GetMapping("/findByPage")
    @ApiOperation("跟踪解除分页查询")
    @Anonymous
    public AjaxResult findByPage(EventTrackPageInDTO dto) {
        Page<DisEventTrack> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

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

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

    @PostMapping("/addTrackLog")
    @ApiOperation("添加跟踪日志")
    @Log(title = "添加跟踪日志", businessType = BusinessType.INSERT)
    public AjaxResult addTrackLog(@RequestBody DisEventTrackLog log) {
        boolean added = service.addTrackLog(log);
        return added ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
    }

    @PutMapping("/release/{id}")
    @ApiOperation("解除跟踪")
    @Log(title = "解除跟踪", businessType = BusinessType.UPDATE)
    public AjaxResult release(@PathVariable Long id) {
        boolean released = service.release(id);
        return released ? AjaxResult.success("解除成功") : AjaxResult.error("解除失败");
    }

    @PutMapping("/close/{id}")
    @ApiOperation("关闭跟踪")
    @Log(title = "关闭跟踪", businessType = BusinessType.UPDATE)
    public AjaxResult close(@PathVariable Long id) {
        boolean closed = service.close(id);
        return closed ? AjaxResult.success("关闭成功") : AjaxResult.error("关闭失败");
    }

    @GetMapping("/getTrackLogs/{trackId}")
    @ApiOperation("查询跟踪日志")
    @Anonymous
    public AjaxResult getTrackLogs(@PathVariable Long trackId) {
        return AjaxResult.success(service.getTrackLogs(trackId));
    }
}
