package cn.cecep.talroad.controller.analysis;

import cn.cecep.talroad.common.core.domain.R;
import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.analysis.SRaGasPermittedOnline;
import cn.cecep.talroad.domain.analysis.SRaWaterPermittedOnline;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaGasPermittedOnlineService;
import cn.cecep.talroad.vo.analysis.SRaGasPermittedOnlineVo;
import cn.cecep.talroad.vo.query.analysis.SRaGasPermittedOnlineQuery;
import cn.cecep.talroad.vo.query.analysis.SRaWaterPermittedOnlineQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

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.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.checkerframework.checker.guieffect.qual.UI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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;


/**
 * * 开发公司：中节能天融科技有限公司
 * 在线排放量废气明细Controller 控制层
 *
 * @author szhpt
 * @date 2023-06-02*/
@Api(tags = "在线排放量废气明细")
@RestController
@RequestMapping("/permitted/gas")
public class SRaGasPermittedOnlineController extends BasesController
{
    @Autowired
    private ISRaGasPermittedOnlineService sRaGasPermittedOnlineService;
    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询在线排放量废气明细列表
     */
    @ApiOperation(value = "查询在线、许可排放量废气明细")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaWaterPermittedOnline>> list(SRaWaterPermittedOnlineQuery sRaGasPermittedOnline)
    {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();

        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaGasPermittedOnline.setRegionCodes(regionCodes);
        IPage<SRaWaterPermittedOnline> list = sRaGasPermittedOnlineService.selectSRaGasPermittedOnlineListBy(sRaGasPermittedOnline);
        return AjaxResults.success(list);
    }
    

    /**
     * 导出在线排放量废气明细列表
     */
    @ApiOperation(value = "导出在线、许可排放量废气明细",produces = "application/octet-stream")
    @Log(title = "在线排放量废气明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaWaterPermittedOnlineQuery sRaGasPermittedOnline)
    {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();

        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaGasPermittedOnline.setRegionCodes(regionCodes);
        List<SRaWaterPermittedOnline> list = sRaGasPermittedOnlineService.selectSRaGasPermittedOnlineList(sRaGasPermittedOnline);
        ExcelUtil<SRaWaterPermittedOnline> util = new ExcelUtil<SRaWaterPermittedOnline>(SRaWaterPermittedOnline.class);
        if(sRaGasPermittedOnline.getType().equals("1")){
            if(StringUtils.isNotEmpty(sRaGasPermittedOnline.getHideIndex())){
                if(sRaGasPermittedOnline.getHideIndex().equals("nox")){
                    util.hideColumnNative(1,2,6,7,8,9,10,12,13,14);
                }
                if(sRaGasPermittedOnline.getHideIndex().equals("so2")){
                    util.hideColumnNative(1,2,6,7,8,9,10,11,13,14);
                }
                if(sRaGasPermittedOnline.getHideIndex().equals("pm")){
                    util.hideColumnNative(1,2,6,7,8,9,10,11,12,14);
                }
                if(sRaGasPermittedOnline.getHideIndex().equals("vocs")){
                    util.hideColumnNative(1,2,6,7,8,9,10,11,12,13);
                }
            }else {
                util.hideColumnNative(6,7,8,9,10);
            }
            util.exportExcel(response, list, "在线排放量废气明细数据");
        }else {
            if(StringUtils.isNotEmpty(sRaGasPermittedOnline.getHideIndex())){
                if(sRaGasPermittedOnline.getHideIndex().equals("nox")){
                    util.hideColumnNative(1,2,7,8,9,10,12,13,14);
                }
                if(sRaGasPermittedOnline.getHideIndex().equals("so2")){
                    util.hideColumnNative(1,2,7,8,9,10,11,13,14);
                }
                if(sRaGasPermittedOnline.getHideIndex().equals("pm")){
                    util.hideColumnNative(1,2,7,8,9,10,11,12,14);
                }
                if(sRaGasPermittedOnline.getHideIndex().equals("vocs")){
                    util.hideColumnNative(1,2,7,8,9,10,11,12,13);
                }
            }else {
                util.hideColumnNative(7,8,9,10);
            }
            util.exportExcel(response, list, "许可排放量废气明细数据");
        }
    }

    /**
     * 获取在线排放量废气明细详细信息
     */
    @ApiOperation(value = "获取在线排放量废气明细详细信息")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaGasPermittedOnline> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(sRaGasPermittedOnlineService.getById(id));
    }
    /**
     * 新增在线排放量废气明细
     */
    @ApiOperation(value = "新增在线排放量废气明细")
    @Log(title = "在线排放量废气明细", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaGasPermittedOnline sRaGasPermittedOnline) {
        return toAjax(sRaGasPermittedOnlineService.save(sRaGasPermittedOnline));
    }
    /**
     * 修改在线排放量废气明细
     */
    @ApiOperation(value = "修改在线排放量废气明细")
    @Log(title = "在线排放量废气明细", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaGasPermittedOnlineVo sRaGasPermittedOnlineVo) {
        if(ObjectUtil.isEmpty(sRaGasPermittedOnlineVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(sRaGasPermittedOnlineVo.getDataId())){
            throw new InvalidParameterException("参数id为空");
        }
        SRaGasPermittedOnline sRaGasPermittedOnline = sRaGasPermittedOnlineService.getById(sRaGasPermittedOnlineVo.getDataId());
        if(ObjectUtil.isEmpty(sRaGasPermittedOnline)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaGasPermittedOnlineVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaGasPermittedOnlineVo,sRaGasPermittedOnline);
        return toAjax(sRaGasPermittedOnlineService.saveOrUpdate(sRaGasPermittedOnline));
    }

    /**
     * 删除在线排放量废气明细
     */
    @ApiOperation(value = "删除在线排放量废气明细")
    @RequiresPermissions("talroad:online:remove")
    @Log(title = "在线排放量废气明细", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] dataIds) {
        return toAjax(sRaGasPermittedOnlineService.removeByIds(Arrays.asList(dataIds)));
    }
    /**
     * 监测点名称options
     */
    @ApiOperation(value = "监测点名称options")
    @GetMapping("/pullMonitorNameOptions")
    public AjaxResults<List<Map<String, Object>>> pullMonitorNameOptions(@RequestParam(value = "factoryName", required = false) String factoryName) {
        return AjaxResults.success(sRaGasPermittedOnlineService.pullMonitorNameOptions(factoryName));
    }
}
