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.SupermonitorDrainOutlet;
import cn.cecep.talroad.service.ISupermonitorDrainOutletService;
import cn.cecep.talroad.vo.SupermonitorDrainOutletVo;
import cn.cecep.talroad.vo.query.SupermonitorDrainOutletQuery;
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-01-10*/
@Api(tags = "基本档案--排污口")
@RestController
@RequestMapping("/supermonitorDrainOutlet")
public class SupermonitorDrainOutletController extends BasesController
{
    @Autowired
    private ISupermonitorDrainOutletService supermonitorDrainOutletService;

    /**
     * 查询排污口列表
     */
    @ApiOperation(value = "查询排污口")
    @GetMapping("/list")
    public AjaxResults<IPage<SupermonitorDrainOutlet>> list(SupermonitorDrainOutletQuery supermonitorDrainOutlet)
    {
        Page<SupermonitorDrainOutlet> page = new Page<>(supermonitorDrainOutlet.getPageNum(), supermonitorDrainOutlet.getPageSize());
        IPage<SupermonitorDrainOutlet> list = supermonitorDrainOutletService.page(page, SupermonitorDrainOutletQuery.createLambdaQueryWrapper(supermonitorDrainOutlet));
        return AjaxResults.success(list);
    }

    @ApiOperation(value = "排污许可-入河排口信息")
    @PostMapping("/rh/pwxk/list")
    public AjaxResults<IPage<SupermonitorDrainOutletVo.PwxkRh>> rhPwxkList(@RequestBody SupermonitorDrainOutletQuery.PwxkRh query) {
        Page<SupermonitorDrainOutletVo.PwxkRh> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<SupermonitorDrainOutletVo.PwxkRh> list = supermonitorDrainOutletService.selectRhPwxkList(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-入河排口信息导出")
    @PostMapping("/rh/export/pwxk/list")
    public void exportRhPwxkList(@RequestBody SupermonitorDrainOutletQuery.PwxkRh query) {
        Page<SupermonitorDrainOutletVo.PwxkRh> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<SupermonitorDrainOutletVo.PwxkRh> list = supermonitorDrainOutletService.selectRhPwxkList(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, SupermonitorDrainOutletVo.PwxkRh.class);
        ExcelUtil.export(exportMap, "入河排口信息");
    }

    @ApiOperation(value = "排污许可-雨水排口信息")
    @PostMapping("/ys/pwxk/list")
    public AjaxResults<IPage<SupermonitorDrainOutletVo.PwxkYs>> ysPwxkList(@RequestBody SupermonitorDrainOutletQuery.PwxkYs query) {
        Page<SupermonitorDrainOutletVo.PwxkYs> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<SupermonitorDrainOutletVo.PwxkYs> list = supermonitorDrainOutletService.selectYsPwxkList(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-雨水排口信息导出")
    @PostMapping("/ys/export/pwxk/list")
    public void exportRhPwxkList(@RequestBody SupermonitorDrainOutletQuery.PwxkYs query) {
        Page<SupermonitorDrainOutletVo.PwxkYs> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<SupermonitorDrainOutletVo.PwxkYs> list = supermonitorDrainOutletService.selectYsPwxkList(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, SupermonitorDrainOutletVo.PwxkYs.class);
        ExcelUtil.export(exportMap, "雨水排口信息");
    }

    @ApiOperation(value = "排水去向下拉选")
    @GetMapping("/drainageTo/options")
    public AjaxResults<List<String>> drainageToOptions() {
        List<SupermonitorDrainOutlet> list = supermonitorDrainOutletService.list(new QueryWrapper<SupermonitorDrainOutlet>()
                .select("drainage_to")
                .isNotNull("drainage_to")
                .groupBy("drainage_to")
                .orderByAsc("drainage_to")
        );
        List<String> collect = list.stream().map(SupermonitorDrainOutlet::getDrainageTo).collect(Collectors.toList());
        return AjaxResults.success(collect);
    }

    @ApiOperation(value = "排放规律下拉选")
    @GetMapping("/emissionsLaws/options")
    public AjaxResults<List<String>> emissionsLawsOptions() {
        List<SupermonitorDrainOutlet> list = supermonitorDrainOutletService.list(new QueryWrapper<SupermonitorDrainOutlet>()
                .select("emissions_laws")
                .isNotNull("emissions_laws")
                .groupBy("emissions_laws")
                .orderByAsc("emissions_laws")
        );
        List<String> collect = list.stream().map(SupermonitorDrainOutlet::getEmissionsLaws).collect(Collectors.toList());
        return AjaxResults.success(collect);
    }
    

    /**
     * 导出排污口列表
     */
    @ApiOperation(value = "导出排污口")
    @Log(title = "排污口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupermonitorDrainOutlet supermonitorDrainOutlet)
    {
        List<SupermonitorDrainOutlet> list = supermonitorDrainOutletService.selectSupermonitorDrainOutletList(supermonitorDrainOutlet);
        ExcelUtil<SupermonitorDrainOutlet> util = new ExcelUtil<SupermonitorDrainOutlet>(SupermonitorDrainOutlet.class);
        util.exportExcel(response, list, "排污口数据");
    }

    /**
     * 获取排污口详细信息
     */
    @ApiOperation(value = "获取排污口详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<SupermonitorDrainOutlet> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(supermonitorDrainOutletService.getById(id));
    }
    /**
     * 新增排污口
     */
    @ApiOperation(value = "新增排污口")
    @Log(title = "排污口", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SupermonitorDrainOutlet supermonitorDrainOutlet) {
        return toAjax(supermonitorDrainOutletService.save(supermonitorDrainOutlet));
    }
    /**
     * 修改排污口
     */
    @ApiOperation(value = "修改排污口")
    @Log(title = "排污口", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SupermonitorDrainOutletVo supermonitorDrainOutletVo) {
        if(ObjectUtil.isEmpty(supermonitorDrainOutletVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(supermonitorDrainOutletVo.getId())){
            throw new InvalidParameterException("参数id为空");
        }
        SupermonitorDrainOutlet supermonitorDrainOutlet = supermonitorDrainOutletService.getById(supermonitorDrainOutletVo.getId());
        if(ObjectUtil.isEmpty(supermonitorDrainOutlet)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", supermonitorDrainOutletVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(supermonitorDrainOutletVo,supermonitorDrainOutlet);
        return toAjax(supermonitorDrainOutletService.saveOrUpdate(supermonitorDrainOutlet));
    }

    /**
     * 删除排污口
     */
    @ApiOperation(value = "删除排污口")
    @RequiresPermissions("talroad:outlet:remove")
    @Log(title = "排污口", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(supermonitorDrainOutletService.removeByIds(Arrays.asList(ids)));
    }
}
