package com.muyu.enterpise.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.domain.WarnStrategy;
import com.muyu.enterpise.service.IWarnStrategyService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 预警策略Controller
 * @author muyu
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/strategy")
@Tag(name = "查询预警策略模块",description = "查询预警策略模块")
public class WarnStrategyController extends BaseController
{
    @Resource
    private IWarnStrategyService warnStrategyService;

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

    /**
     * 导出预警策略列表
     */
    @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, "预警策略数据");
    }

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

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

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

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