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.PlanStep;
import com.aegis.emergency.plan.service.IPlanStepResourceService;
import com.aegis.emergency.plan.service.IPlanStepService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
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}/steps")
public class PlanStepController extends BaseController {

    @Autowired
    private IPlanStepService stepService;

    @Autowired
    private IPlanStepResourceService stepResourceService;

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

    @PreAuthorize("@ss.hasPermi('emergency:plan:step:add')")
    @Log(title = "预案行动项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@PathVariable Long planId, @RequestBody PlanStep step) {
        step.setPlanId(planId);
        step.setDelFlag("0");
        return toAjax(stepService.save(step));
    }

    @PreAuthorize("@ss.hasPermi('emergency:plan:step:edit')")
    @Log(title = "预案行动项", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long planId, @PathVariable Long id, @RequestBody PlanStep step) {
        step.setId(id);
        step.setPlanId(planId);
        return toAjax(stepService.updateById(step));
    }

    @PreAuthorize("@ss.hasPermi('emergency:plan:step:remove')")
    @Log(title = "预案行动项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long planId, @PathVariable Long[] ids) {
        boolean ok = true;
        for (Long id : ids) {
            stepResourceService.removeByStepId(id);
            ok &= stepService.removeById(id);
        }
        return toAjax(ok);
    }
}