package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.cecep.talroad.vo.query.BasThresholdConfigQuery;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.security.InvalidParameterException;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;

import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.domain.BasThresholdConfig;
import cn.cecep.talroad.vo.BasThresholdConfigVo;
import cn.cecep.talroad.service.IBasThresholdConfigService;


/**
 * * 开发公司：中节能天融科技有限公司
 * 关联分析-阈值设置Controller 控制层
 *
 * @author szhpt
 * @date 2023-05-18*/
@Api(tags = "关联分析-阈值设置")
@RestController
@RequestMapping("/thresholdConfig")
public class BasThresholdConfigController extends BasesController
{
    @Autowired
    private IBasThresholdConfigService basThresholdConfigService;

    /**
     * 查询关联分析-阈值设置列表
     */
    @ApiOperation(value = "查询关联分析-阈值设置")
    @GetMapping("/list")
    public AjaxResults<IPage<BasThresholdConfig>> list(BasThresholdConfigQuery basThresholdConfig)
    {
        Page<BasThresholdConfig> page = new Page<>(basThresholdConfig.getPageNum(), basThresholdConfig.getPageSize());
        IPage<BasThresholdConfig> list = basThresholdConfigService.page(page, BasThresholdConfigQuery.createLambdaQueryWrapper(basThresholdConfig));
        return AjaxResults.success(list);
    }


    /**
     * 导出关联分析-阈值设置列表
     */
    @ApiOperation(value = "导出关联分析-阈值设置")
    @Log(title = "关联分析-阈值设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasThresholdConfig basThresholdConfig)
    {
        List<BasThresholdConfig> list = basThresholdConfigService.selectBasThresholdConfigList(basThresholdConfig);
        ExcelUtil<BasThresholdConfig> util = new ExcelUtil<BasThresholdConfig>(BasThresholdConfig.class);
        util.exportExcel(response, list, "关联分析-阈值设置数据");
    }

    /**
     * 获取关联分析-阈值设置详细信息
     */
    @ApiOperation(value = "获取关联分析-阈值设置详细信息")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<BasThresholdConfig> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(basThresholdConfigService.getById(id));
    }
    /**
     * 新增关联分析-阈值设置
     */
    @ApiOperation(value = "新增关联分析-阈值设置")
    @Log(title = "关联分析-阈值设置", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BasThresholdConfig basThresholdConfig) {
        return toAjax(basThresholdConfigService.save(basThresholdConfig));
    }
    /**
     * 修改关联分析-阈值设置
     */
    @ApiOperation(value = "修改关联分析-阈值设置")
    @Log(title = "关联分析-阈值设置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BasThresholdConfigVo basThresholdConfigVo) {
        if(ObjectUtil.isEmpty(basThresholdConfigVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(basThresholdConfigVo.getDataId())){
            throw new InvalidParameterException("参数id为空");
        }
        BasThresholdConfig basThresholdConfig = basThresholdConfigService.getById(basThresholdConfigVo.getDataId());
        if(ObjectUtil.isEmpty(basThresholdConfig)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", basThresholdConfigVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(basThresholdConfigVo,basThresholdConfig);
        return toAjax(basThresholdConfigService.saveOrUpdate(basThresholdConfig));
    }

    /**
     * 删除关联分析-阈值设置
     */
    @ApiOperation(value = "删除关联分析-阈值设置")
    @RequiresPermissions("talroad:thresholdConfig:remove")
    @Log(title = "关联分析-阈值设置", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable Long[] dataIds) {
        return toAjax(basThresholdConfigService.removeByIds(Arrays.asList(dataIds)));
    }
}
