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.DisEventUrge;
import com.zksy.dispatch.domain.DisEventUrgeDetail;
import com.zksy.dispatch.dto.EventUrgePageInDTO;
import com.zksy.dispatch.service.DisEventUrgeService;
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/urge")
@Api(tags = "工作催办")
public class DisEventUrgeController {

    @Autowired
    private DisEventUrgeService service;

    @GetMapping("/findByPage")
    @ApiOperation("工作催办分页查询")
    @Anonymous
    public AjaxResult findByPage(EventUrgePageInDTO dto) {
        Page<DisEventUrge> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

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

    @PostMapping("/save")
    @ApiOperation("新增催办记录")
    @Log(title = "新增催办记录", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody DisEventUrge 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 DisEventUrge 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("/doUrge")
    @ApiOperation("执行催办")
    @Log(title = "执行催办", businessType = BusinessType.INSERT)
    public AjaxResult doUrge(@RequestBody DisEventUrgeDetail detail) {
        boolean urged = service.doUrge(detail);
        return urged ? AjaxResult.success("催办成功") : AjaxResult.error("催办失败");
    }

    @PutMapping("/closeUrge/{id}")
    @ApiOperation("办结催办")
    @Log(title = "办结催办", businessType = BusinessType.UPDATE)
    public AjaxResult closeUrge(@PathVariable Long id) {
        boolean closed = service.closeUrge(id);
        return closed ? AjaxResult.success("办结成功") : AjaxResult.error("办结失败");
    }

    @GetMapping("/getUrgeDetails/{urgeId}")
    @ApiOperation("查询催办明细")
    @Anonymous
    public AjaxResult getUrgeDetails(@PathVariable Long urgeId) {
        return AjaxResult.success(service.getUrgeDetails(urgeId));
    }
}
