package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
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.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.domain.BMainProductionOrganizGas;
import cn.cecep.talroad.service.IBMainProductionOrganizGasService;
import cn.cecep.talroad.service.IProductionManageService;
import cn.cecep.talroad.vo.BMainProductionOrganizGasVo;
import cn.cecep.talroad.vo.query.BMainProductionOrganizGasQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.security.InvalidParameterException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * * 开发公司：中节能天融科技有限公司
 * 废气有组织排放产治排信息Controller 控制层
 *
 * @author szhpt
 * @date 2023-02-08*/
@Api(tags = "废气有组织排放产治排信息")
@RestController
@RequestMapping("/bmainProductionOrganizGas")
public class BMainProductionOrganizGasController extends BasesController
{
    @Autowired
    private IBMainProductionOrganizGasService bMainProductionOrganizGasService;

    /**
     * 查询废气有组织排放产治排信息列表
     */
    @ApiOperation(value = "查询废气有组织排放产治排信息")
    @GetMapping("/list")
    public AjaxResults<IPage<BMainProductionOrganizGas>> list(BMainProductionOrganizGasQuery bMainProductionOrganizGas)
    {
        Page<BMainProductionOrganizGas> page = new Page<>(bMainProductionOrganizGas.getPageNum(), bMainProductionOrganizGas.getPageSize());
        IPage<BMainProductionOrganizGas> list = bMainProductionOrganizGasService.page(page, BMainProductionOrganizGasQuery.createLambdaQueryWrapper(bMainProductionOrganizGas));
        return AjaxResults.success(list);
    }
    

    /**
     * 导出废气有组织排放产治排信息列表
     */
    @ApiOperation(value = "导出废气有组织排放产治排信息")
    @Log(title = "废气有组织排放产治排信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody BMainProductionOrganizGas bMainProductionOrganizGas)
    {
        List<BMainProductionOrganizGas> list = bMainProductionOrganizGasService.selectBMainProductionOrganizGasList(bMainProductionOrganizGas);
        ExcelUtil<BMainProductionOrganizGas> util = new ExcelUtil<>(BMainProductionOrganizGas.class);
        util.exportExcel(response, list, "废气有组织排放产治排信息数据");
    }

    /**
     * 获取废气有组织排放产治排信息详细信息
     */
    @ApiOperation(value = "获取废气有组织排放产治排信息详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<BMainProductionOrganizGas> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(bMainProductionOrganizGasService.getById(id));
    }
    /**
     * 新增废气有组织排放产治排信息
     */
    @ApiOperation(value = "新增废气有组织排放产治排信息")
    @Log(title = "废气有组织排放产治排信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BMainProductionOrganizGas bMainProductionOrganizGas) {
        return toAjax(bMainProductionOrganizGasService.save(bMainProductionOrganizGas));
    }
    /**
     * 修改废气有组织排放产治排信息
     */
    @ApiOperation(value = "修改废气有组织排放产治排信息")
    @Log(title = "废气有组织排放产治排信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BMainProductionOrganizGasVo bMainProductionOrganizGasVo) {
        if(ObjectUtil.isEmpty(bMainProductionOrganizGasVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(bMainProductionOrganizGasVo.getId())){
            throw new InvalidParameterException("参数id为空");
        }
        BMainProductionOrganizGas bMainProductionOrganizGas = bMainProductionOrganizGasService.getById(bMainProductionOrganizGasVo.getId());
        if(ObjectUtil.isEmpty(bMainProductionOrganizGas)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", bMainProductionOrganizGasVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(bMainProductionOrganizGasVo,bMainProductionOrganizGas);
        return toAjax(bMainProductionOrganizGasService.saveOrUpdate(bMainProductionOrganizGas));
    }

    @ApiOperation(value = "排污许可-废气排污节点及治理设施信息")
    @PostMapping("/pwxk/list")
    public AjaxResults<IPage<BMainProductionOrganizGasVo.PwxkCzp>> pwxkList(@RequestBody BMainProductionOrganizGasQuery.PwxkCzp query) {
        Page<BMainProductionOrganizGasVo.PwxkCzp> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<BMainProductionOrganizGasVo.PwxkCzp> list = bMainProductionOrganizGasService.selectPwxkList(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-废气排污节点及治理设施信息导出")
    @PostMapping("/export/pwxk/list")
    public void exportPwxkList(@RequestBody BMainProductionOrganizGasQuery.PwxkCzp query) {
        Page<BMainProductionOrganizGasVo.PwxkCzp> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<BMainProductionOrganizGasVo.PwxkCzp> list = bMainProductionOrganizGasService.selectPwxkList(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, BMainProductionOrganizGasVo.PwxkCzp.class);
        ExcelUtil.export(exportMap, "废气排污节点及治理设施信息");
    }

    @ApiOperation(value = "污染物种类下拉选")
    @GetMapping("/pollutionType/options")
    public AjaxResults<List<String>> pollutionTypeOptions() {
        List<BMainProductionOrganizGas> list = bMainProductionOrganizGasService.list(new QueryWrapper<BMainProductionOrganizGas>()
                .select("pollution_type")
                .isNotNull("pollution_type")
                .groupBy("pollution_type")
                .orderByAsc("pollution_type")
        );
        List<String> collect = list.stream().map(BMainProductionOrganizGas::getPollutionType).collect(Collectors.toList());
        return AjaxResults.success(collect);
    }

    @ApiOperation(value = "治理设施工艺下拉选")
    @GetMapping("/cepFacilityCraft/options")
    public AjaxResults<List<String>> cepFacilityCraftOptions() {
        List<BMainProductionOrganizGas> list = bMainProductionOrganizGasService.list(new QueryWrapper<BMainProductionOrganizGas>()
                .select("cep_facility_craft")
                .isNotNull("cep_facility_craft")
                .groupBy("cep_facility_craft")
                .orderByAsc("cep_facility_craft")
        );
        List<String> collect = list.stream().map(BMainProductionOrganizGas::getCepFacilityCraft).collect(Collectors.toList());
        return AjaxResults.success(collect);
    }

    /**
     * 删除废气有组织排放产治排信息
     */
    @ApiOperation(value = "删除废气有组织排放产治排信息")
    @RequiresPermissions("talroad:bmainProductionOrganizGas:remove")
    @Log(title = "废气有组织排放产治排信息", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable Long[] ids) {
        return toAjax(bMainProductionOrganizGasService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation(value = "获取弹框数据（数据趋势、视频监控）")
    @GetMapping("/frameData")
    public AjaxResults<IProductionManageService.FrameDataVo> frameData(
            @RequestParam("id") String id,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("pageNum") Long pageNum,
            @RequestParam("pageSize") Long pageSize
    ) {
        IProductionManageService.FrameDataVo data = bMainProductionOrganizGasService.getFrameData(id, startTime, endTime, pageNum, pageSize);
        return AjaxResults.success(data);
    }

}
