package cn.cecep.talroad.controller;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.domain.BaseControlConcentStandards;
import cn.cecep.talroad.domain.MonDevFsHour;
import cn.cecep.talroad.service.BaseControlConcentStandardsService;

import cn.cecep.talroad.vo.BaseControlConcentStandardsVo;
import cn.cecep.talroad.vo.query.BaseControlConcentStandardsQuery;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.lang.Collections;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* (base_control_concent_standards)表控制层
*
* @author xxxxx
*/
@Api(tags ="管控浓度标准设置")
@RestController
@RequestMapping("/controlConcentSt")
public class BaseControlConcentStandardsController {
/**
* 服务对象
*/
@Resource
private BaseControlConcentStandardsService baseControlConcentStandardsService;

    /**
    * 查询列表
    *
    * @param query 查询条件
    * @return 单条数据
    */
    @ApiOperation("获取列表")
    @GetMapping("/selectList")
    public AjaxResults<IPage<BaseControlConcentStandardsVo>> selectList(BaseControlConcentStandardsQuery query) {
    return baseControlConcentStandardsService.selectList(query);
    }
    @ApiOperation("导出文件")
    @GetMapping("/export")
    public void export(HttpServletResponse response,  BaseControlConcentStandardsQuery query) {
//        query = new BaseControlConcentStandardsQuery();
        query.setPageNum(1);
        query.setPageSize(Integer.MAX_VALUE);
        AjaxResults<IPage<BaseControlConcentStandardsVo>> iPageAjaxResults = baseControlConcentStandardsService.selectList(query);
        if(iPageAjaxResults.getCode()==200){
            List<BaseControlConcentStandardsVo> records = iPageAjaxResults.getData().getRecords();
            ExcelUtil<BaseControlConcentStandardsVo> util = new ExcelUtil<>(BaseControlConcentStandardsVo.class);
            util.hideColumnNative(2,1,0);
            util.exportExcel(response, records, "管控浓度标准设置");
        }
    }
    @PostMapping("/import")
    @ResponseBody
    public AjaxResults<Boolean> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseControlConcentStandards> util = new ExcelUtil<>(BaseControlConcentStandards.class);
        List<BaseControlConcentStandards> voList = util.importExcel(file.getInputStream());
        List<BaseControlConcentStandards> data = voList.stream().filter(v -> StringUtils.isNotEmpty(v.getIncludeCalculations())).collect(Collectors.toList());
        if(Collections.isEmpty(data)){
            return AjaxResults.success(true);
        }
        baseControlConcentStandardsService.importData(data);
        return AjaxResults.success(true);
    }

    @ApiOperation("更新建议排放量小时均值")
    @PostMapping("/updateAdviceEmissionAvg")
    public AjaxResults updateAdviceEmissionAvg() {
        baseControlConcentStandardsService.updateAdviceEmissionAvg();
        return AjaxResults.success();
    }
    @GetMapping("/addPollutant")
    public AjaxResults addPollutant(@RequestParam String code, @RequestParam Integer type) {
       Integer integer= baseControlConcentStandardsService.addPollutant(code,type);
        return AjaxResults.success(integer);
    }

}
