package com.aegis.web.controller.emergency.plan;

import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.enums.BusinessType;
import com.aegis.emergency.plan.domain.PlanApproval;
import com.aegis.emergency.plan.service.IPlanApprovalService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 预案审批 Controller
 */
@RestController
@RequestMapping("/api/plans/{planId}/approvals")
public class PlanApprovalController extends BaseController {

    @Autowired
    private IPlanApprovalService approvalService;

    @PreAuthorize("@ss.hasPermi('emergency:plan:approval:list')")
    @GetMapping
    public AjaxResult list(@PathVariable Long planId) {
        List<PlanApproval> approvals = approvalService.lambdaQuery()
            .eq(PlanApproval::getPlanId, planId)
            .orderByAsc(PlanApproval::getApprovalOrder)
            .list();
        return success(approvals);
    }

    @PreAuthorize("@ss.hasPermi('emergency:plan:approval:add')")
    @Log(title = "预案审批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult record(@PathVariable Long planId, @RequestBody PlanApproval approval) {
        approval.setPlanId(planId);
        PlanApproval saved = approvalService.recordApproval(approval);
        return success(saved);
    }
}