package com.muyu.warning.controller;

import java.util.Arrays;
import java.util.List;

import jakarta.servlet.http.HttpServletResponse;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.warning.domain.WarnStrategy;
import com.muyu.warning.service.IWarnStrategyService;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 预警策略Controller
 *
 * @author muyu
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/strategy")
public class WarnStrategyController extends BaseController {
    @Resource
    private IWarnStrategyService warnStrategyService;

    /**
     * 查询预警策略列表
     */
    @RequiresPermissions("warning:strategy:list")
    @GetMapping("/list")
    public Result<TableDataInfo<WarnStrategy>> list(WarnStrategy warnStrategy) {
        startPage();
        List<WarnStrategy> list = warnStrategyService.selectWarnStrategyList(warnStrategy);
        return getDataTable(list);
    }

    /**
     * 导出预警策略列表
     */
    @RequiresPermissions("warning:strategy:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarnStrategy warnStrategy) {
        List<WarnStrategy> list = warnStrategyService.selectWarnStrategyList(warnStrategy);
        ExcelUtil<WarnStrategy> util = new ExcelUtil<WarnStrategy>(WarnStrategy.class);
        util.exportExcel(response, list, "预警策略数据");
    }

    /**
     * 获取预警策略详细信息
     */
    @RequiresPermissions("warning:strategy:query")
    @GetMapping(value = "/{id}")
    public Result<List<WarnStrategy>> getInfo(@PathVariable("id") Long id) {
        return success(warnStrategyService.selectWarnStrategyById(id));
    }

    /**
     * 新增预警策略
     */
    @RequiresPermissions("warning:strategy:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody WarnStrategy warnStrategy) {
        if (warnStrategyService.checkIdUnique(warnStrategy)) {
            return error("新增 预警策略 '" + warnStrategy + "'失败，预警策略已存在");
        }
        return toAjax(warnStrategyService.save(warnStrategy));
    }

    /**
     * 修改预警策略
     */
    @RequiresPermissions("warning:strategy:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody WarnStrategy warnStrategy) {
        if (!warnStrategyService.checkIdUnique(warnStrategy)) {
            return error("修改 预警策略 '" + warnStrategy + "'失败，预警策略不存在");
        }
        return toAjax(warnStrategyService.updateById(warnStrategy));
    }

    /**
     * 删除预警策略
     */
    @RequiresPermissions("warning:strategy:remove")
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids) {
        warnStrategyService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
