package org.dromara.ems.base.controller;

import org.dromara.common.log.annotation.Log;
import org.dromara.ems.common.web.EmsBaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ems.base.domain.EmsAlarmActionStep;
import org.dromara.ems.base.service.IEmsAlarmActionStepService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 报警规则具体措施步骤Controller
 * 
 * @author zch
 * @date 2025-05-29
 */
@RestController
@RequiredArgsConstructor

@RequestMapping("/ems/base/emsAlarmActionStep")
public class EmsAlarmActionStepController extends EmsBaseController
{
    
    private final IEmsAlarmActionStepService emsAlarmActionStepService;


    /**
     * 查询报警规则具体措施步骤列表
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:list")
    @GetMapping("/list")
    public TableDataInfo list(EmsAlarmActionStep emsAlarmActionStep)
    {
        startPage();
        List<EmsAlarmActionStep> list = emsAlarmActionStepService.selectEmsAlarmActionStepList(emsAlarmActionStep);
        return getDataTable(list);
    }

    /**
     * 导出报警规则具体措施步骤列表
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:export")
    @Log(title = "报警规则具体措施步骤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsAlarmActionStep emsAlarmActionStep)
    {
        List<EmsAlarmActionStep> list = emsAlarmActionStepService.selectEmsAlarmActionStepList(emsAlarmActionStep);
        ExcelUtil.exportExcel(list, "报警规则具体措施步骤数据", EmsAlarmActionStep.class, response);
    }

    /**
     * 获取报警规则具体措施步骤详细信息
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:query")
    @GetMapping(value = "/{objId}")
    public R<?> getInfo(@PathVariable("objId") String objId)
    {
        return success(emsAlarmActionStepService.selectEmsAlarmActionStepByObjId(objId));
    }

    /**
     * 根据报警规则ID查询措施步骤列表（包含图片）
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:query")
    @GetMapping(value = "/rule/{ruleObjId}")
    public R<?> getStepsByRuleId(@PathVariable("ruleObjId") String ruleObjId)
    {
        List<EmsAlarmActionStep> steps = emsAlarmActionStepService.selectEmsAlarmActionStepByRuleObjId(ruleObjId);
        return success(steps);
    }

    /**
     * 新增报警规则具体措施步骤
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:add")
    @Log(title = "报警规则具体措施步骤", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody EmsAlarmActionStep emsAlarmActionStep)
    {
        return toAjax(emsAlarmActionStepService.insertEmsAlarmActionStep(emsAlarmActionStep));
    }

    /**
     * 修改报警规则具体措施步骤
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:edit")
    @Log(title = "报警规则具体措施步骤", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody EmsAlarmActionStep emsAlarmActionStep)
    {
        return toAjax(emsAlarmActionStepService.updateEmsAlarmActionStep(emsAlarmActionStep));
    }

    /**
     * 删除报警规则具体措施步骤
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:remove")
    @Log(title = "报警规则具体措施步骤", businessType = BusinessType.DELETE)
	@DeleteMapping("/{objIds}")
    public R<?> remove(@PathVariable String[] objIds)
    {
        return toAjax(emsAlarmActionStepService.deleteEmsAlarmActionStepByObjIds(objIds));
    }

    /**
     * 根据报警数据信息查询对应的措施步骤列表
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:query")
    @GetMapping(value = "/alarm/{monitorId}/{cause}")
    public R<?> getStepsByAlarmInfo(@PathVariable("monitorId") String monitorId, @PathVariable("cause") String cause)
    {
        List<EmsAlarmActionStep> steps = emsAlarmActionStepService.selectActionStepsByAlarmInfo(monitorId, cause);
        return success(steps);
    }

    /**
     * 根据报警数据信息查询对应的措施步骤列表（post方式，避免URL编码问题）
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:query")
    @PostMapping(value = "/getByAlarmInfo")
    public R<?> getStepsByAlarmInfopost(@RequestBody Map<String, String> params)
    {
        String monitorId = params.get("monitorId");
        String cause = params.get("cause");
        
        if (monitorId == null || cause == null) {
            return error("参数不能为空：monitorId=" + monitorId + ", cause=" + cause);
        }
        
        List<EmsAlarmActionStep> steps = emsAlarmActionStepService.selectActionStepsByAlarmInfo(monitorId, cause);
        return success(steps);
    }

    /**
     * 批量保存措施步骤（包含图片信息）
     */
    @SaCheckPermission("ems/base:emsAlarmActionStep:edit")
    @Log(title = "批量保存措施步骤", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/batchSave/{ruleObjId}")
    public R<?> batchSaveActionSteps(@PathVariable("ruleObjId") String ruleObjId, @RequestBody List<EmsAlarmActionStep> stepList)
    {
        return toAjax(emsAlarmActionStepService.batchSaveActionSteps(ruleObjId, stepList));
    }
}
