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.DisPlanApproval;
import com.zksy.dispatch.dto.DisPlanApprovalPageDTO;
import com.zksy.dispatch.service.DisPlanApprovalService;
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/plan-approval")
@Api(tags = "应急预案审批")
public class DisPlanApprovalController {

    @Autowired
    private DisPlanApprovalService service;

    @GetMapping("/findByPage")
    @ApiOperation("应急预案审批分页查询")
    @Anonymous
    public AjaxResult findByPage(DisPlanApprovalPageDTO dto) {
        Page<DisPlanApproval> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询应急预案审批")
    @Anonymous
    public AjaxResult getById(@PathVariable Long id) {
        DisPlanApproval entity = service.getById(id);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("审批记录不存在");
    }

    @PostMapping("/save")
    @ApiOperation("新增应急预案审批")
    @Log(title = "新增应急预案审批", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody DisPlanApproval 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 DisPlanApproval 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("/submitForApproval/{id}")
    @ApiOperation("提交审批")
    @Log(title = "提交审批", businessType = BusinessType.UPDATE)
    public AjaxResult submitForApproval(@PathVariable Long id) {
        boolean result = service.submitForApproval(id);
        return result ? AjaxResult.success("提交审批成功") : AjaxResult.error("提交审批失败，请检查状态");
    }

    @PutMapping("/approve/{id}")
    @ApiOperation("初审通过")
    @Log(title = "初审通过", businessType = BusinessType.UPDATE)
    public AjaxResult approve(@PathVariable Long id, @RequestParam(required = false) String opinion) {
        boolean result = service.approve(id, opinion);
        return result ? AjaxResult.success("初审通过") : AjaxResult.error("审批失败，请检查状态");
    }

    @PutMapping("/reject/{id}")
    @ApiOperation("驳回")
    @Log(title = "驳回审批", businessType = BusinessType.UPDATE)
    public AjaxResult reject(@PathVariable Long id, @RequestParam(required = false) String opinion) {
        boolean result = service.reject(id, opinion);
        return result ? AjaxResult.success("已驳回") : AjaxResult.error("驳回失败，请检查状态");
    }

    @PutMapping("/publish/{id}")
    @ApiOperation("终审通过并发布")
    @Log(title = "终审通过并发布", businessType = BusinessType.UPDATE)
    public AjaxResult publish(@PathVariable Long id, @RequestParam(required = false) String opinion) {
        boolean result = service.publish(id, opinion);
        return result ? AjaxResult.success("已发布") : AjaxResult.error("发布失败，请检查状态");
    }

    @PostMapping("/submit/{planId}")
    @ApiOperation("根据预案ID创建审批并提交")
    @Log(title = "提交审批", businessType = BusinessType.INSERT)
    public AjaxResult submitFromPlan(@PathVariable Long planId) {
        boolean result = service.createFromPlan(planId);
        return result ? AjaxResult.success("提交审批成功") : AjaxResult.error("提交审批失败");
    }
}
