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.PcFactoryPollutantEmissionsRank;
import cn.cecep.talroad.service.IPcFactoryPollutantEmissionsRankService;
import cn.cecep.talroad.vo.PcFactoryPollutantEmissionsRankVo;
import cn.cecep.talroad.vo.query.PcFactoryPollutantEmissionsRankQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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;


/**
 * * 开发公司：中节能天融科技有限公司
 * 污染排放跟踪(实际排放/许可排放)Controller 控制层
 *
 * @author szhpt
 * @date 2023-02-10*/
@Api(tags = "污染排放跟踪-实际排放-许可排放")
@RestController
@RequestMapping("/pcFactoryPollutantEmissionsRank")
public class PcFactoryPollutantEmissionsRankController extends BasesController
{
    @Autowired
    private IPcFactoryPollutantEmissionsRankService pcFactoryPollutantEmissionsRankService;

    /**
     * 查询污染排放跟踪(实际排放/许可排放)列表
     */
    @ApiOperation(value = "查询污染排放跟踪(实际排放/许可排放)")
    @GetMapping("/list")
    public AjaxResults<List<PcFactoryPollutantEmissionsRank>> list(PcFactoryPollutantEmissionsRankQuery pcFactoryPollutantEmissionsRank)
    {
        // b_main_emission_pollution_air b_main_emission_pollution_water 非当前实例表，接口做适配
        List<PcFactoryPollutantEmissionsRank> list = pcFactoryPollutantEmissionsRankService.selectRankList(pcFactoryPollutantEmissionsRank);
        return AjaxResults.success(list);
    }


    /**
     * 导出污染排放跟踪(实际排放/许可排放)列表
     */
    @ApiOperation(value = "导出污染排放跟踪(实际排放/许可排放)")
    @Log(title = "污染排放跟踪(实际排放/许可排放)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcFactoryPollutantEmissionsRank pcFactoryPollutantEmissionsRank)
    {
        List<PcFactoryPollutantEmissionsRank> list = pcFactoryPollutantEmissionsRankService.selectPcFactoryPollutantEmissionsRankList(pcFactoryPollutantEmissionsRank);
        ExcelUtil<PcFactoryPollutantEmissionsRank> util = new ExcelUtil<PcFactoryPollutantEmissionsRank>(PcFactoryPollutantEmissionsRank.class);
        util.exportExcel(response, list, "污染排放跟踪(实际排放/许可排放)数据");
    }

    /**
     * 获取污染排放跟踪(实际排放/许可排放)详细信息
     */
    @ApiOperation(value = "获取污染排放跟踪(实际排放/许可排放)详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<PcFactoryPollutantEmissionsRank> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(pcFactoryPollutantEmissionsRankService.getById(id));
    }
    /**
     * 新增污染排放跟踪(实际排放/许可排放)
     */
    @ApiOperation(value = "新增污染排放跟踪(实际排放/许可排放)")
    @Log(title = "污染排放跟踪(实际排放/许可排放)", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody PcFactoryPollutantEmissionsRank pcFactoryPollutantEmissionsRank) {
        return toAjax(pcFactoryPollutantEmissionsRankService.save(pcFactoryPollutantEmissionsRank));
    }
    /**
     * 修改污染排放跟踪(实际排放/许可排放)
     */
    @ApiOperation(value = "修改污染排放跟踪(实际排放/许可排放)")
    @Log(title = "污染排放跟踪(实际排放/许可排放)", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody PcFactoryPollutantEmissionsRankVo pcFactoryPollutantEmissionsRankVo) {
        if(ObjectUtil.isEmpty(pcFactoryPollutantEmissionsRankVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(pcFactoryPollutantEmissionsRankVo.getId())){
            throw new InvalidParameterException("参数id为空");
        }
        PcFactoryPollutantEmissionsRank pcFactoryPollutantEmissionsRank = pcFactoryPollutantEmissionsRankService.getById(pcFactoryPollutantEmissionsRankVo.getId());
        if(ObjectUtil.isEmpty(pcFactoryPollutantEmissionsRank)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", pcFactoryPollutantEmissionsRankVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(pcFactoryPollutantEmissionsRankVo,pcFactoryPollutantEmissionsRank);
        return toAjax(pcFactoryPollutantEmissionsRankService.saveOrUpdate(pcFactoryPollutantEmissionsRank));
    }

    /**
     * 删除污染排放跟踪(实际排放/许可排放)
     */
    @ApiOperation(value = "删除污染排放跟踪(实际排放/许可排放)")
    @RequiresPermissions("talroad:pcFactoryPollutantEmissionsRank:remove")
    @Log(title = "污染排放跟踪(实际排放/许可排放)", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(pcFactoryPollutantEmissionsRankService.removeByIds(Arrays.asList(ids)));
    }
}
