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.PcPollutant;
import cn.cecep.talroad.service.IPcPollutantService;
import cn.cecep.talroad.vo.PcPollutantVo;
import cn.cecep.talroad.vo.query.PcPollutantQuery;
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.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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;


/**
 * * 开发公司：中节能天融科技有限公司
 * 污染物Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-10*/
@Api(tags = "污染物")
@RestController
@RequestMapping("/pcPollutant")
public class PcPollutantController extends BasesController
{
    @Autowired
    private IPcPollutantService pcPollutantService;

    /**
     * 查询污染物列表
     */
    @ApiOperation(value = "查询污染物")
    @GetMapping("/list")
    public AjaxResults<IPage<PcPollutant>> list(PcPollutantQuery pcPollutant)
    {
        Page<PcPollutant> page = new Page<>(pcPollutant.getPageNum(), pcPollutant.getPageSize());
        IPage<PcPollutant> list = pcPollutantService.page(page, PcPollutantQuery.createLambdaQueryWrapper(pcPollutant));
        return AjaxResults.success(list);
    }
    

    /**
     * 导出污染物列表
     */
    @ApiOperation(value = "导出污染物")
    @Log(title = "污染物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcPollutant pcPollutant)
    {
        List<PcPollutant> list = pcPollutantService.selectPcPollutantList(pcPollutant);
        ExcelUtil<PcPollutant> util = new ExcelUtil<PcPollutant>(PcPollutant.class);
        util.exportExcel(response, list, "污染物数据");
    }

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

    /**
     * 删除污染物
     */
    @ApiOperation(value = "删除污染物")
    @RequiresPermissions("talroad:pollutant:remove")
    @Log(title = "污染物", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(pcPollutantService.removeByIds(Arrays.asList(ids)));
    }
    /**
     * 手工监测污染物下拉框
     */
    @ApiOperation(value = "手工监测污染物下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气传：气污染物｜废水传：水污染物）", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping("/manualMonitoringPollutants")
    public AjaxResults<List<PcPollutant>> manualMonitoringPollutants(@RequestParam(value = "pollutantType") String pollutantType)
    {
        List<PcPollutant> list = pcPollutantService.manualMonitoringPollutants(pollutantType);
        return AjaxResults.success(list);
    }
}
