/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.emergencySupport.modular.es.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.unis.emergencySupport.modular.es.entity.vo.EsNetworkEntryCountVO;
import com.unis.emergencySupport.modular.es.entity.vo.EsProcessEnterpriseCountVo;
import com.unis.emergencySupport.modular.es.entity.vo.EsProcessEnterprisePrCaCountVo;
import com.unis.emergencySupport.modular.es.param.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.unis.common.annotation.CommonLog;
import com.unis.common.pojo.CommonResult;
import com.unis.common.pojo.CommonValidList;
import com.unis.emergencySupport.modular.es.entity.EsProcessEnterprise;
import com.unis.emergencySupport.modular.es.service.EsProcessEnterpriseService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应急保障-应急加工企业管理表控制器
 *
 * @author ZeroLiYi
 * @date  2024/05/30 16:39
 */
@Api(tags = "应急保障-应急加工企业管理表控制器")
@ApiSupport(author = "UNIS_TEAM", order = 1)
@RestController
@Validated
public class EsProcessEnterpriseController {

    @Resource
    private EsProcessEnterpriseService esProcessEnterpriseService;

    /**
     * 获取应急保障-应急加工企业管理表分页
     *
     * @author ZeroLiYi
     * @date  2024/05/30 16:39
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取应急保障-应急加工企业管理表分页")
    @GetMapping("/emergencySupport/esProcessEnterprise/page")
    public CommonResult<Page<EsProcessEnterprise>> page(EsProcessEnterprisePageParam esProcessEnterprisePageParam) {
        return CommonResult.data(esProcessEnterpriseService.page(esProcessEnterprisePageParam));
    }

    /**
     * 添加和修改应急保障-应急加工企业管理表
     *
     * @author ZeroLiYi
     * @date  2024/05/30 16:39
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加和修改应急保障-应急加工企业管理表")
    @CommonLog("添加和修改应急保障-应急加工企业管理表")
    @PostMapping("/emergencySupport/esProcessEnterprise/saveOrupdate")
    public CommonResult<String> saveOrupdate(@RequestBody @Valid EsProcessEnterpriseAddParam esProcessEnterpriseAddParam) {
        esProcessEnterpriseService.add(esProcessEnterpriseAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑应急保障-应急加工企业管理表
     *
     * @author ZeroLiYi
     * @date  2024/05/30 16:39
     */
//    @ApiOperationSupport(order = 3)
//    @ApiOperation("编辑应急保障-应急加工企业管理表")
//    @CommonLog("编辑应急保障-应急加工企业管理表")
//    @SaCheckPermission("/emergencySupport/esProcessEnterprise/edit")
//    @PostMapping("/emergencySupport/esProcessEnterprise/edit")
//    public CommonResult<String> edit(@RequestBody @Valid EsProcessEnterpriseEditParam esProcessEnterpriseEditParam) {
//        esProcessEnterpriseService.edit(esProcessEnterpriseEditParam);
//        return CommonResult.ok();
//    }

    /**
     * 删除应急保障-应急加工企业管理表
     *
     * @author ZeroLiYi
     * @date  2024/05/30 16:39
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("删除应急保障-应急加工企业管理表")
    @CommonLog("删除应急保障-应急加工企业管理表")
    @PostMapping("/emergencySupport/esProcessEnterprise/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<EsProcessEnterpriseIdParam> esProcessEnterpriseIdParamList) {
        esProcessEnterpriseService.delete(esProcessEnterpriseIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取应急保障-应急加工企业管理表详情
     *
     * @author ZeroLiYi
     * @date  2024/05/30 16:39
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("获取应急保障-应急加工企业管理表详情")
    @GetMapping("/emergencySupport/esProcessEnterprise/detail")
    public CommonResult<EsProcessEnterprise> detail(@Valid EsProcessEnterpriseIdParam esProcessEnterpriseIdParam) {
        return CommonResult.data(esProcessEnterpriseService.detail(esProcessEnterpriseIdParam));
    }


    /**
     * 获取应急保障-应急加工企业统计
     *
     * @author ZeroLiYi
     * @date  2024/05/28 09:21
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取应急保障-应急加工企业统计")
    @GetMapping("/emergencySupport/esProcessEnterprise/count")
    public CommonResult<List<EsProcessEnterpriseCountVo>> selectCount(EsNetworkEnterpriseCountParam esNetworkEnterpriseCountParam) {
        List<EsProcessEnterpriseCountVo> esNetworkEntryList=esProcessEnterpriseService.selectCount(esNetworkEnterpriseCountParam);
        return CommonResult.data(esNetworkEntryList);
    }

    /**
     * 获取应急保障-应急加工企业加工能力统计
     *
     * @author ZeroLiYi
     * @date  2024/05/28 09:21
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取应急保障-应急加工企业加工能力统计")
    @GetMapping("/emergencySupport/esProcessEnterprise/prCacount")
    public CommonResult<List<EsProcessEnterprisePrCaCountVo>> prCacount(EsProcessEnterpriseCountParam esProcessEnterpriseCountParam) {
        List<EsProcessEnterprisePrCaCountVo> esNetworkEntryList=esProcessEnterpriseService.prCacount(esProcessEnterpriseCountParam);
        return CommonResult.data(esNetworkEntryList);
    }


    /**
     * 导入
     */
    @ApiOperation(value = "粮食应急保障企业信息（加工企业）导入数据", notes = "粮食应急保障企业信息（加工企业）导入数据---")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象集合")
    })
    @PostMapping(value= "/emergencySupport/esProcessEnterprise/importByEntity", headers= "content-type= multipart/form-data", consumes= "multipart/*")
    public Map<String,Object> importByEntity(@RequestParam(value= "file", required = true) MultipartFile file) throws IOException {
        Map<String, Object> map = new HashMap<>();
        try {
            if (file == null || file.isEmpty()) {
                map.put("message", "无文件数据");
                map.put("status", "500");
                return map;
            }
            // 获得文件名
            String fileName = file.getOriginalFilename();
            // 判断文件是否是excel文件
            if (!fileName.endsWith("xls") && !fileName.endsWith("xlsx")) {
                map.put("message", "请使用正确的导入模板");
                map.put("status", "500");
                return map;
            }

            return esProcessEnterpriseService.importByEntityResult(file, map);
        } catch (IOException e) {
            map.put("message", "文件处理异常: " + e.getMessage());
            map.put("status", "500");
            return map;
        } catch (Exception e) {
            map.put("message", "发生异常: " + e.getMessage());
            map.put("status", "500");
            return map;
        }

    }


}
