package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.domain.PcControlRequirements;
import cn.cecep.talroad.service.IPcControlRequirementsService;
import cn.cecep.talroad.vo.PcControlRequirementsVo;
import cn.cecep.talroad.vo.query.PcControlRequirementsQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * * 开发公司：中节能天融科技有限公司
 * 管控要求Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-10*/
@Api(tags = "基本档案--管控要求")
@RestController
@RequestMapping("/pcControlRequirements")
public class PcControlRequirementsController extends BasesController
{
    @Autowired
    private IPcControlRequirementsService pcControlRequirementsService;

    /**
     * 查询管控要求列表
     */
    @ApiOperation(value = "查询管控要求")
    @GetMapping("/list")
    public AjaxResults<IPage<PcControlRequirements>> list(PcControlRequirementsQuery pcControlRequirements)
    {
        Page<PcControlRequirements> page = new Page<>(pcControlRequirements.getPageNum(), pcControlRequirements.getPageSize());
        IPage<PcControlRequirements> list = pcControlRequirementsService.page(page, PcControlRequirementsQuery.createLambdaQueryWrapper(pcControlRequirements));
        return AjaxResults.success(list);
    }
    

    /**
     * 导出管控要求列表
     */
    @ApiOperation(value = "导出管控要求")
    @Log(title = "管控要求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcControlRequirements pcControlRequirements)
    {
        List<PcControlRequirements> list = pcControlRequirementsService.selectPcControlRequirementsList(pcControlRequirements);
        ExcelUtil<PcControlRequirements> util = new ExcelUtil<PcControlRequirements>(PcControlRequirements.class);
        util.exportExcel(response, list, "管控要求数据");
    }

    /**
     * 获取管控要求详细信息
     */
    @ApiOperation(value = "获取管控要求详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<PcControlRequirements> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(pcControlRequirementsService.getById(id));
    }

    @ApiOperation(value = "排污许可-废气有组织排放要求")
    @PostMapping("/pwxk/gas/org/list")
    public AjaxResults<IPage<PcControlRequirementsVo.PwxkGasOrg>> pwxkGasOrg(@RequestBody PcControlRequirementsQuery.PwxkGasOrg query) {
        Page<PcControlRequirementsVo.PwxkGasOrg> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcControlRequirementsVo.PwxkGasOrg> list = pcControlRequirementsService.selectPwxkGasOrg(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-废气有组织排放要求导出")
    @PostMapping("/export/pwxk/gas/org/list")
    public void exportPwxkGasOrg(@RequestBody PcControlRequirementsQuery.PwxkGasOrg query) {
        Page<PcControlRequirementsVo.PwxkGasOrg> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcControlRequirementsVo.PwxkGasOrg> list = pcControlRequirementsService.selectPwxkGasOrg(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, PcControlRequirementsVo.PwxkGasOrg.class);
        ExcelUtil.export(exportMap, "废气有组织排放要求");
    }

    @ApiOperation(value = "排污许可-废气无组织排放要求")
    @PostMapping("/pwxk/gas/noOrg/list")
    public AjaxResults<IPage<PcControlRequirementsVo.PwxkGasNoOrg>> pwxkGasNoOrg(@RequestBody PcControlRequirementsQuery.PwxkGasNoOrg query) {
        Page<PcControlRequirementsVo.PwxkGasNoOrg> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcControlRequirementsVo.PwxkGasNoOrg> list = pcControlRequirementsService.selectPwxkGasNoOrg(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-废气无组织排放要求导出")
    @PostMapping("/export/pwxk/gas/noOrg/list")
    public void exportPwxkGasNoOrg(@RequestBody PcControlRequirementsQuery.PwxkGasNoOrg query) {
        Page<PcControlRequirementsVo.PwxkGasNoOrg> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcControlRequirementsVo.PwxkGasNoOrg> list = pcControlRequirementsService.selectPwxkGasNoOrg(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, PcControlRequirementsVo.PwxkGasNoOrg.class);
        ExcelUtil.export(exportMap, "废气无组织排放要求");
    }

    @ApiOperation(value = "排污许可-废水直接排口排放要求")
    @PostMapping("/pwxk/water/direct/list")
    public AjaxResults<IPage<PcControlRequirementsVo.PwxkWaterDirect>> pwxkWaterDirect(@RequestBody PcControlRequirementsQuery.PwxkWaterDirect query) {
        Page<PcControlRequirementsVo.PwxkWaterDirect> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcControlRequirementsVo.PwxkWaterDirect> list = pcControlRequirementsService.selectPwxkWaterDirect(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-废水直接排口排放要求导出")
    @PostMapping("/export/pwxk/water/direct/list")
    public void exportPwxkWaterDirect(@RequestBody PcControlRequirementsQuery.PwxkWaterDirect query) {
        Page<PcControlRequirementsVo.PwxkWaterDirect> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcControlRequirementsVo.PwxkWaterDirect> list = pcControlRequirementsService.selectPwxkWaterDirect(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, PcControlRequirementsVo.PwxkWaterDirect.class);
        ExcelUtil.export(exportMap, "废水直接排口排放要求");
    }

    @ApiOperation(value = "排污许可-废水间接排口排放要求")
    @PostMapping("/pwxk/water/indirect/list")
    public AjaxResults<IPage<PcControlRequirementsVo.PwxkWaterIndirect>> pwxkWaterIndirect(@RequestBody PcControlRequirementsQuery.PwxkWaterIndirect query) {
        Page<PcControlRequirementsVo.PwxkWaterIndirect> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcControlRequirementsVo.PwxkWaterIndirect> list = pcControlRequirementsService.selectPwxkWaterIndirect(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-废水间接排口排放要求导出")
    @PostMapping("/export/pwxk/water/indirect/list")
    public void exportPwxkWaterIndirect(@RequestBody PcControlRequirementsQuery.PwxkWaterIndirect query) {
        Page<PcControlRequirementsVo.PwxkWaterIndirect> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcControlRequirementsVo.PwxkWaterIndirect> list = pcControlRequirementsService.selectPwxkWaterIndirect(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, PcControlRequirementsVo.PwxkWaterIndirect.class);
        ExcelUtil.export(exportMap, "废水间接排口排放要求");
    }

    @ApiOperation(value = "污染物种类下拉选")
    @GetMapping("/pollutant/options")
    public AjaxResults<List<String>> pollutantOptions() {
        List<PcControlRequirements> list = pcControlRequirementsService.list(new QueryWrapper<PcControlRequirements>()
                .select("pollutant_name")
                .isNotNull("pollutant_name")
                .groupBy("pollutant_name")
                .orderByAsc("pollutant_name")
        );
        List<String> collect = list.stream().map(PcControlRequirements::getPollutantName).collect(Collectors.toList());
        return AjaxResults.success(collect);
    }

}
