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.PlanParticipant;
import com.aegis.emergency.plan.service.IPlanParticipantService;
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}/participants")
public class PlanParticipantController extends BaseController {

    @Autowired
    private IPlanParticipantService participantService;

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

    @PreAuthorize("@ss.hasPermi('emergency:plan:participant:add')")
    @Log(title = "预案执行对象", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@PathVariable Long planId, @RequestBody PlanParticipant participant) {
        participant.setPlanId(planId);
        participant.setDelFlag("0");
        return toAjax(participantService.save(participant));
    }

    @PreAuthorize("@ss.hasPermi('emergency:plan:participant:edit')")
    @Log(title = "预案执行对象", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long planId, @PathVariable Long id, @RequestBody PlanParticipant participant) {
        participant.setId(id);
        participant.setPlanId(planId);
        return toAjax(participantService.updateById(participant));
    }

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