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.PlanVersion;
import com.aegis.emergency.plan.service.IPlanVersionService;
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}/versions")
public class PlanVersionController extends BaseController {

    @Autowired
    private IPlanVersionService planVersionService;

    @PreAuthorize("@ss.hasPermi('emergency:plan:version:list')")
    @GetMapping
    public AjaxResult list(@PathVariable Long planId) {
        List<PlanVersion> versions = planVersionService.listByPlanId(planId);
        return success(versions);
    }

    @PreAuthorize("@ss.hasPermi('emergency:plan:version:add')")
    @Log(title = "预案版本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult create(@PathVariable Long planId, @RequestBody PlanVersion version) {
        version.setPlanId(planId);
        PlanVersion saved = planVersionService.createVersion(version);
        return success(saved);
    }

    @PreAuthorize("@ss.hasPermi('emergency:plan:version:restore')")
    @Log(title = "预案版本", businessType = BusinessType.UPDATE)
    @PostMapping("/{versionId}/restore")
    public AjaxResult restore(@PathVariable Long planId, @PathVariable Long versionId) {
        return toAjax(planVersionService.restoreVersion(planId, versionId));
    }
}