package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.exception.ServiceException;
import cn.cecep.talroad.common.core.web.domain.AjaxResult;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.vo.AmProblemFiRatioImportVo;
import cn.cecep.talroad.vo.PcPollutantVo;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.cecep.talroad.vo.query.AmProblemFiRatioQuery;
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 java.io.IOException;
import java.security.InvalidParameterException;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.domain.AmProblemFiRatio;
import cn.cecep.talroad.vo.AmProblemFiRatioVo;
import cn.cecep.talroad.service.IAmProblemFiRatioService;
import org.springframework.web.multipart.MultipartFile;


/**
 * * 开发公司：中节能天融科技有限公司
 * 预警报警信息-预警比例定制Controller 控制层
 *
 * @author szhpt
 * @date 2023-06-27*/
@Api(tags = "预警报警信息-预警比例定制")
@RestController
@RequestMapping("/ratio")
public class AmProblemFiRatioController extends BasesController
{
    @Autowired
    private IAmProblemFiRatioService amProblemFiRatioService;

    /**
     * 查询预警报警信息-预警比例定制列表
     */
    @ApiOperation(value = "查询预警报警信息-预警比例定制")
    @GetMapping("/list")
    public AjaxResults<IPage<AmProblemFiRatioVo>> list(AmProblemFiRatioQuery amProblemFiRatio)
    {
        Page<AmProblemFiRatioVo> page = new Page<>(amProblemFiRatio.getPageNum(), amProblemFiRatio.getPageSize());
        amProblemFiRatioService.pageList(page, amProblemFiRatio);
        return AjaxResults.success(page);
    }

    /**
     * 查询预警报警信息-污染物下拉接口
     */
    @ApiOperation(value = "查询预警报警信息-污染物下拉接口")
    @GetMapping("/pcPollutantList")
    public AjaxResults<List<PcPollutantVo>> pcPollutantList()
    {
        List<PcPollutantVo> pcPollutantVos = amProblemFiRatioService.pcPollutantList();
        return AjaxResults.success(pcPollutantVos);
    }
    

    /**
     * 导出预警报警信息-预警比例定制列表
     */
    @ApiOperation(value = "导出预警报警信息-预警比例定制")
    @Log(title = "预警报警信息-预警比例定制", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AmProblemFiRatioQuery amProblemFiRatio)
    {
        List<AmProblemFiRatioVo> list = amProblemFiRatioService.selectAmProblemFiRatioList(amProblemFiRatio);
        ExcelUtil<AmProblemFiRatioVo> util = new ExcelUtil<>(AmProblemFiRatioVo.class);
        util.exportExcel(response, list, "预警报警信息-预警比例定制数据");
    }

    @ApiOperation(value = "预警比例定制导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<AmProblemFiRatioImportVo> util = new ExcelUtil<>(AmProblemFiRatioImportVo.class);
        util.importTemplateExcel(response, "预警报警信息-预警比例定制数据");
    }

    @ApiOperation(value = "预警比例定制导入数据")
    @Log(title = "预警比例定制", businessType = BusinessType.IMPORT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile"),
    })
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {

        String fileName = file.getOriginalFilename();
        String fileNameFormat = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (!"xlsx".equals(fileNameFormat) && !"xls".equals(fileNameFormat)) {
            throw new ServiceException("抱歉,导入的模板不是xlsx/xls格式,无法导入数据");
        }

        ExcelUtil<AmProblemFiRatioImportVo> util = null;
        List<AmProblemFiRatioImportVo> sDcFacilityFiVoList = null;
        try {
            util = new ExcelUtil<>(AmProblemFiRatioImportVo.class);
            sDcFacilityFiVoList = util.importExcel(file.getInputStream());
            //添加无法录入的数据
        } catch (Exception e) {
            throw new ServiceException("抱歉,导入的模板数据格式不正确,无法导入数据");
        }
        String message = amProblemFiRatioService.importAmProblemFiRatioList(sDcFacilityFiVoList);
        return AjaxResult.success(message);
    }

    /**
     * 获取预警报警信息-预警比例定制详细信息
     */
    @ApiOperation(value = "获取预警报警信息-预警比例定制详细信息")
    @GetMapping(value = "/getInfo")
    public AjaxResults<AmProblemFiRatioVo> getInfo(@RequestParam Long id) {
        return AjaxResults.success(amProblemFiRatioService.selectAmProblemFiRatio(id));
    }
    /**
     * 新增预警报警信息-预警比例定制
     */
    @ApiOperation(value = "新增预警报警信息-预警比例定制")
    @Log(title = "预警报警信息-预警比例定制", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody AmProblemFiRatio amProblemFiRatio) {
        return toAjax(amProblemFiRatioService.save(amProblemFiRatio));
    }
    /**
     * 修改预警报警信息-预警比例定制
     */
    @ApiOperation(value = "修改预警报警信息-预警比例定制")
    @Log(title = "预警报警信息-预警比例定制", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody AmProblemFiRatioVo amProblemFiRatioVo) {
        if(ObjectUtil.isEmpty(amProblemFiRatioVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(amProblemFiRatioVo.getId())){
            throw new InvalidParameterException("参数id为空");
        }
        AmProblemFiRatio amProblemFiRatio = amProblemFiRatioService.getById(amProblemFiRatioVo.getId());
        if(ObjectUtil.isEmpty(amProblemFiRatio)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", amProblemFiRatioVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(amProblemFiRatioVo,amProblemFiRatio);
        return toAjax(amProblemFiRatioService.saveOrUpdate(amProblemFiRatio));
    }

    /**
     * 删除预警报警信息-预警比例定制
     */
    @ApiOperation(value = "删除预警报警信息-预警比例定制")
    @RequiresPermissions("talroad:ratio:remove")
    @Log(title = "预警报警信息-预警比例定制", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable Long[] ids) {
        return toAjax(amProblemFiRatioService.removeByIds(Arrays.asList(ids)));
    }
}
