package com.muyu.server.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.annotation.RequiresPermissions;
import com.muyu.domain.WarnRule;
import com.muyu.domain.resp.CarMessageResp;
import com.muyu.server.service.WarnRuleService;
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;

/**
 * 预警规则控制层
 * @Author：蓬叁
 * @Package：com.muyu.warn.controller
 * @Project：cloud-server-8
 * @name：WarnLogsController
 * @Date：2024/9/20 下午7:29
 */
@RestController
@RequestMapping("/rule")
@Tag(name = "预警规则控制层")
public class WarnRuleController extends BaseController
{
    @Resource
    private WarnRuleService warnRuleService;

    /**
     * 查询预警规则列表
     */
    @RequiresPermissions("platform:rule:list")
    @GetMapping("/list")
    public Result<TableDataInfo<WarnRule>> list(WarnRule warnRule)
    {
        startPage();
        List<WarnRule> list = warnRuleService.selectWarnRuleList(warnRule);
        return getDataTable(list);
    }

    /**
     * 导出预警规则列表
     */
    @RequiresPermissions("platform:rule:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarnRule warnRule)
    {
        List<WarnRule> list = warnRuleService.selectWarnRuleList(warnRule);
        ExcelUtil<WarnRule> util = new ExcelUtil<WarnRule>(WarnRule.class);
        util.exportExcel(response, list, "预警规则数据");
    }

    /**
     * 获取预警规则详细信息
     */
    @RequiresPermissions("platform:rule:query")
    @GetMapping(value = "/{id}")
    public Result<List<WarnRule>> getInfo(@PathVariable("id") Long id)
    {
        return success(warnRuleService.selectWarnRuleById(id));
    }

    /**
     * 新增预警规则
     */
    @RequiresPermissions("platform:rule:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody WarnRule warnRule)
    {
        if (warnRuleService.checkIdUnique(warnRule)) {
            return error("新增 预警规则 '" + warnRule + "'失败，预警规则已存在");
        }
        return toAjax(warnRuleService.save(warnRule));
    }

    /**
     * 修改预警规则
     */
    @RequiresPermissions("platform:rule:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody WarnRule warnRule)
    {
        if (!warnRuleService.checkIdUnique(warnRule)) {
            return error("修改 预警规则 '" + warnRule + "'失败，预警规则不存在");
        }
//        warnRule.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(warnRuleService.updateById(warnRule));
    }

    /**
     * 删除预警规则
     */
    @RequiresPermissions("platform:rule:remove")
	@DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        warnRuleService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }

    /**
     * 获取车辆类型报文
     */
    @RequiresPermissions("platform:rule:msg")
    @GetMapping("/msg/{id}")
    public Result<List<CarMessageResp>> findByMsg(@PathVariable("id") Long id)
    {
        return success(warnRuleService.findByMsg(id));
    }



}
