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.PlanStepResource;
import com.aegis.emergency.plan.service.IPlanStepResourceService;
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/{stepId}/resources")
public class PlanStepResourceController extends BaseController {

    @Autowired
    private IPlanStepResourceService stepResourceService;

    @PreAuthorize("@ss.hasPermi('emergency:plan:resource:list')")
    @GetMapping
    public AjaxResult list(@PathVariable Long stepId) {
        List<PlanStepResource> list = stepResourceService.listByStepId(stepId);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('emergency:plan:resource:add')")
    @Log(title = "预案资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@PathVariable Long planId, @PathVariable Long stepId, @RequestBody PlanStepResource resource) {
        resource.setPlanId(planId);
        resource.setStepId(stepId);
        resource.setDelFlag("0");
        return toAjax(stepResourceService.save(resource));
    }

    @PreAuthorize("@ss.hasPermi('emergency:plan:resource:edit')")
    @Log(title = "预案资源", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long planId, @PathVariable Long stepId, @PathVariable Long id,
                           @RequestBody PlanStepResource resource) {
        resource.setId(id);
        resource.setPlanId(planId);
        resource.setStepId(stepId);
        return toAjax(stepResourceService.updateById(resource));
    }

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