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.BMainPollDisFile;
import cn.cecep.talroad.service.IBMainPollDisFileService;
import cn.cecep.talroad.vo.BMainPollDisFileVo;
import cn.cecep.talroad.vo.query.BMainPollDisFileQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
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.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.*;
import java.util.stream.Collectors;


/**
 * * 开发公司：中节能天融科技有限公司
 * 排污许可附件信息Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-18*/
@Api(tags = "排污许可--排污许可附件信息")
@RestController
@RequestMapping("/BMainPollDisFile")
public class BMainPollDisFileController extends BasesController
{
    @Autowired
    private IBMainPollDisFileService bMainPollDisFileService;

    /**
     * 查询排污许可附件信息列表
     */
    @ApiOperation(value = "查询排污许可附件信息")
    @GetMapping("/list")
    public AjaxResults<ArrayList<HashMap>> list(BMainPollDisFileQuery bMainPollDisFile)
    {
        Page<BMainPollDisFile> page = new Page<>(bMainPollDisFile.getPageNum(), 99);
        IPage<BMainPollDisFile> list = bMainPollDisFileService.page(page, BMainPollDisFileQuery.createLambdaQueryWrapper(bMainPollDisFile));
        List<BMainPollDisFile> dbFiles = list.getRecords();
        Map<String, List<BMainPollDisFile>> dbFileMaps = CollectionUtils.isEmpty(dbFiles) ? Maps.newHashMap() : dbFiles.stream().collect(Collectors.groupingBy(BMainPollDisFile::getFileType));
        BMainPollDisFile.PwxkzFile[] values = BMainPollDisFile.PwxkzFile.values();
        ArrayList<HashMap> res = Lists.newArrayList();
        for (BMainPollDisFile.PwxkzFile file : values) {
            HashMap<String, Object> item = Maps.newHashMap();
            item.put("type", file.desc);
            item.put("files", dbFileMaps.containsKey(file.desc) ? dbFileMaps.get(file.desc) : Lists.newArrayList());
            res.add(item);
        }
        return AjaxResults.success(res);
    }
    

    /**
     * 导出排污许可附件信息列表
     */
    @ApiOperation(value = "导出排污许可附件信息")
    @Log(title = "排污许可附件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BMainPollDisFile bMainPollDisFile)
    {
        List<BMainPollDisFile> list = bMainPollDisFileService.selectBMainPollDisFileList(bMainPollDisFile);
        ExcelUtil<BMainPollDisFile> util = new ExcelUtil<BMainPollDisFile>(BMainPollDisFile.class);
        util.exportExcel(response, list, "排污许可附件信息数据");
    }

    /**
     * 获取排污许可附件信息详细信息
     */
    @ApiOperation(value = "获取排污许可附件信息详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<BMainPollDisFile> getInfo(@PathVariable("id") Long id) {
        return AjaxResults.success(bMainPollDisFileService.getById(id));
    }

    /**
     * 获取指定企业指定排污许可证附件信息
     */
    @ApiOperation(value = "获取指定企业指定排污许可证附件信息")
    @PostMapping(value = "/{factoryId}/{pollDisId}")
    public AjaxResults<List<BMainPollDisFile>> getInfo(@PathVariable("factoryId") String factoryId,
                                                 @PathVariable("pollDisId") String pollDisId) {
        return AjaxResults.success(bMainPollDisFileService.list(BMainPollDisFile.createLambdaQueryWrapper(factoryId, pollDisId)));
    }

    /**
     * 新增排污许可附件信息
     */
    @ApiOperation(value = "新增排污许可附件信息")
    @Log(title = "排污许可附件信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BMainPollDisFile bMainPollDisFile) {
        return toAjax(bMainPollDisFileService.save(bMainPollDisFile));
    }
    /**
     * 修改排污许可附件信息
     */
    @ApiOperation(value = "修改排污许可附件信息")
    @Log(title = "排污许可附件信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BMainPollDisFileVo bMainPollDisFileVo) {
        if(ObjectUtil.isEmpty(bMainPollDisFileVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(bMainPollDisFileVo.getFileId())){
            throw new InvalidParameterException("参数id为空");
        }
        BMainPollDisFile bMainPollDisFile = bMainPollDisFileService.getById(bMainPollDisFileVo.getFileId());
        if(ObjectUtil.isEmpty(bMainPollDisFile)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", bMainPollDisFileVo.getFileId()));
        }
        //赋值
        BeanUtil.copyProperties(bMainPollDisFileVo,bMainPollDisFile);
        return toAjax(bMainPollDisFileService.saveOrUpdate(bMainPollDisFile));
    }

    /**
     * 删除排污许可附件信息
     */
    @ApiOperation(value = "删除排污许可附件信息")
    @RequiresPermissions("talroad:BMainPollDisFile:remove")
    @Log(title = "排污许可附件信息", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable Long[] ids) {
        return toAjax(bMainPollDisFileService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 单条删除-排污许可附件信息
     */
    @ApiOperation(value = "删除排污许可附件信息-单条")
    @RequiresPermissions("talroad:BMainPollDisFile:remove")
    @Log(title = "排污许可附件信息", businessType = BusinessType.DELETE)
    @PostMapping("/removeOne/{id}")
    public AjaxResults<Boolean> removeOne(@PathVariable Long id) {
        return toAjax(bMainPollDisFileService.removeById(id));
    }
}
