package cn.cecep.talroad.controller.analysis;

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

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.analysis.SRaEmissionsMonitoringVariance;
import cn.cecep.talroad.vo.analysis.SRaEmissionsMonitoringVarianceVo;
import cn.cecep.talroad.service.analysis.ISRaEmissionsMonitoringVarianceService;


/**
 * * 开发公司：中节能天融科技有限公司
 * 企业异常行为分析-企业排放分析-监测差异分析Controller 控制层
 *
 * @author szhpt
 * @date 2023-06-27*/
@Api(tags = "企业异常行为分析-企业排放分析-监测差异分析")
@RestController
@RequestMapping("/variance")
public class SRaEmissionsMonitoringVarianceController extends BasesController
{
    @Autowired
    private ISRaEmissionsMonitoringVarianceService sRaEmissionsMonitoringVarianceService;

    /**
     * 查询企业异常行为分析-企业排放分析-监测差异分析列表
     */
    @ApiOperation(value = "查询企业异常行为分析-企业排放分析-监测差异分析")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaEmissionsMonitoringVarianceVo>> list(SRaEmissionsMonitoringVarianceQuery query)
    {
        Page<SRaEmissionsMonitoringVarianceVo> page = new Page<>(query.getPageNum(), query.getPageSize());
        sRaEmissionsMonitoringVarianceService.selectList(page, query);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "污染物下拉选")
    @GetMapping("/poll/list")
    public AjaxResults<List<PcPollutant>> pollList() {
        List<PcPollutant> list = sRaEmissionsMonitoringVarianceService.pollList();
        return AjaxResults.success(list);
    }


    /**
     * 导出企业异常行为分析-企业排放分析-监测差异分析列表
     */
    @ApiOperation(value = "导出企业异常行为分析-企业排放分析-监测差异分析")
    @Log(title = "企业异常行为分析-企业排放分析-监测差异分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody SRaEmissionsMonitoringVarianceQuery query)
    {
        Page<SRaEmissionsMonitoringVarianceVo> page = new Page<>(query.getPageNum(), query.getPageSize());
        List<SRaEmissionsMonitoringVarianceVo> list = sRaEmissionsMonitoringVarianceService.selectList(page, query);
        ExcelUtil<SRaEmissionsMonitoringVarianceVo> util = new ExcelUtil<>(SRaEmissionsMonitoringVarianceVo.class);
        util.exportExcel(response, list, "企业异常行为分析-企业排放分析-监测差异分析数据");
    }

    /**
     * 获取企业异常行为分析-企业排放分析-监测差异分析详细信息
     */
    @ApiOperation(value = "获取企业异常行为分析-企业排放分析-监测差异分析详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<SRaEmissionsMonitoringVariance> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(sRaEmissionsMonitoringVarianceService.getById(id));
    }
    /**
     * 新增企业异常行为分析-企业排放分析-监测差异分析
     */
    @ApiOperation(value = "新增企业异常行为分析-企业排放分析-监测差异分析")
    @Log(title = "企业异常行为分析-企业排放分析-监测差异分析", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaEmissionsMonitoringVariance sRaEmissionsMonitoringVariance) {
        return toAjax(sRaEmissionsMonitoringVarianceService.save(sRaEmissionsMonitoringVariance));
    }
    /**
     * 修改企业异常行为分析-企业排放分析-监测差异分析
     */
    @ApiOperation(value = "修改企业异常行为分析-企业排放分析-监测差异分析")
    @Log(title = "企业异常行为分析-企业排放分析-监测差异分析", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaEmissionsMonitoringVarianceVo sRaEmissionsMonitoringVarianceVo) {
        if(ObjectUtil.isEmpty(sRaEmissionsMonitoringVarianceVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(sRaEmissionsMonitoringVarianceVo.getId())){
            throw new InvalidParameterException("参数id为空");
        }
        SRaEmissionsMonitoringVariance sRaEmissionsMonitoringVariance = sRaEmissionsMonitoringVarianceService.getById(sRaEmissionsMonitoringVarianceVo.getId());
        if(ObjectUtil.isEmpty(sRaEmissionsMonitoringVariance)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaEmissionsMonitoringVarianceVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaEmissionsMonitoringVarianceVo,sRaEmissionsMonitoringVariance);
        return toAjax(sRaEmissionsMonitoringVarianceService.saveOrUpdate(sRaEmissionsMonitoringVariance));
    }

    /**
     * 删除企业异常行为分析-企业排放分析-监测差异分析
     */
    @ApiOperation(value = "删除企业异常行为分析-企业排放分析-监测差异分析")
    @RequiresPermissions("talroad:variance:remove")
    @Log(title = "企业异常行为分析-企业排放分析-监测差异分析", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable Long[] ids) {
        return toAjax(sRaEmissionsMonitoringVarianceService.removeByIds(Arrays.asList(ids)));
    }
}
