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.DisPlanApprovalStep;
import com.zksy.dispatch.dto.DisPlanApprovalStepPageDTO;
import com.zksy.dispatch.service.DisPlanApprovalStepService;
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-step")
@Api(tags = "应急预案审批步骤")
public class DisPlanApprovalStepController {

    @Autowired
    private DisPlanApprovalStepService service;

    @GetMapping("/findByPage")
    @ApiOperation("审批步骤分页查询")
    @Anonymous
    public AjaxResult findByPage(DisPlanApprovalStepPageDTO dto) {
        Page<DisPlanApprovalStep> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询审批步骤")
    @Anonymous
    public AjaxResult getById(@PathVariable Long id) {
        DisPlanApprovalStep entity = service.getById(id);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("审批步骤不存在");
    }

    @GetMapping("/listByApprovalId")
    @ApiOperation("根据审批主表ID查询步骤列表")
    @Anonymous
    public AjaxResult listByApprovalId(@RequestParam Long approvalId) {
        return AjaxResult.success(service.listByApprovalId(approvalId));
    }

    @PostMapping("/save")
    @ApiOperation("新增审批步骤")
    @Log(title = "新增审批步骤", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody DisPlanApprovalStep 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 DisPlanApprovalStep 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("删除失败");
    }
}
