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.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.emergency.plan.domain.Plan;
import com.aegis.emergency.plan.service.IPlanService;
import java.io.Serializable;
import java.util.Date;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 预案管理 Controller
 */
@RestController
@RequestMapping("/api/plans")
public class PlanController extends BaseController {

    @Autowired
    private IPlanService planService;

    /**
     * 查询预案列表
     */
    @PreAuthorize("@ss.hasPermi('emergency:plan:list')")
    @GetMapping
    public TableDataInfo list(Plan condition,
                              @RequestParam(required = false) Date beginTime,
                              @RequestParam(required = false) Date endTime) {
        startPage();
        List<Plan> list = planService.listByCondition(condition, beginTime, endTime);
        return getDataTable(list);
    }

    /**
     * 获取预案详情
     */
    @PreAuthorize("@ss.hasPermi('emergency:plan:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        Plan plan = planService.getById(id);
        if (plan == null || "2".equals(plan.getDelFlag())) {
            return error("预案不存在或已删除");
        }
        return success(plan);
    }

    /**
     * 新增预案
     */
    @PreAuthorize("@ss.hasPermi('emergency:plan:add')")
    @Log(title = "预案管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Plan plan) {
        boolean saved = planService.save(plan);
        if (saved) {
            // 返回保存后的预案对象（包含ID）
            return success(plan);
        }
        return error("保存失败");
    }

    /**
     * 修改预案
     */
    @PreAuthorize("@ss.hasPermi('emergency:plan:edit')")
    @Log(title = "预案管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long id, @RequestBody Plan plan) {
        plan.setId(id);
        return toAjax(planService.updateById(plan));
    }

    /**
     * 删除预案（支持批量）
     */
    @PreAuthorize("@ss.hasPermi('emergency:plan:remove')")
    @Log(title = "预案管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        boolean result = true;
        for (Serializable id : ids) {
            result &= planService.removeById(id);
        }
        return toAjax(result);
    }

    /**
     * 发布预案
     */
    @PreAuthorize("@ss.hasPermi('emergency:plan:publish')")
    @Log(title = "预案管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/publish")
    public AjaxResult publish(@PathVariable Long id) {
        return toAjax(planService.publishPlan(id));
    }
}