package com.ruoyi.web.controller.gas;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 阈值管理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/threshold")
@Api(tags = "阈值管理")
public class ThresholdController extends BaseController {
    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表(type:so2、black、ais)
     */
//    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    @ApiOperation("获取参数配置列表(type:so2、black、ais)")
    public TableDataInfo list(@RequestParam String type) {
        List<SysConfig> list = configService.selectRightList(type);
        return getDataTable(list);
    }

    /**
     * 修改参数配置
     */
//    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "阈值更新", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation("阈值更新")
    public AjaxResult edit(@RequestBody JSONObject jsonObject) {
        if (StrUtil.isBlank(jsonObject.getString("configId"))) {
            return AjaxResult.error("id不能为空");
        }
        SysConfig sysConfig = configService.selectConfigById(jsonObject.getLongValue("configId"));
        if (sysConfig == null) {
            return AjaxResult.error();
        }
        sysConfig.setConfigValue(jsonObject.getString("configValue"));
        sysConfig.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(configService.updateThresholdConfig(sysConfig));
    }

}
