/*
 * 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.esstorageenterprise.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.basic.info.modular.orgInfo.param.OrgInfoPageParam;
import com.unis.emergencySupport.modular.esstorageenterprise.param.*;
import com.unis.emergencySupport.modular.esstorageenterprisegrain.param.BizEsStorageEnterpriseGrainAddParam;
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.esstorageenterprise.entity.BizEsStorageEnterprise;
import com.unis.emergencySupport.modular.esstorageenterprise.service.BizEsStorageEnterpriseService;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 应急保障-应急仓储企业管理表控制器
 *
 * @author shine
 * @date  2024/05/25 14:37
 */
@Api(tags = "应急保障-应急仓储企业管理表控制器")
@ApiSupport(author = "UNIS_TEAM", order = 1)
@RestController
@Validated
public class BizEsStorageEnterpriseController {

    @Resource
    private BizEsStorageEnterpriseService bizEsStorageEnterpriseService;

    /**
     * 获取应急保障-应急仓储企业管理表分页
     *
     * @author shine
     * @date  2024/05/25 14:37
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("*获取应急保障-应急仓储企业管理表分页")
    @GetMapping("/emergencySupport/esstorageenterprise/page")
    public CommonResult<Page<BizEsStorageEnterprise>> page(BizEsStorageEnterprisePageParam bizEsStorageEnterprisePageParam) {
        return CommonResult.data(bizEsStorageEnterpriseService.page(bizEsStorageEnterprisePageParam));
    }


    /**
     * 添加应急保障-应急仓储企业管理表
     *
     * @author shine
     * @date  2024/05/25 14:37
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("*添加应急保障-应急仓储企业管理表")
    @CommonLog("*添加应急保障-应急仓储企业管理表")
    @PostMapping("/emergencySupport/esstorageenterprise/add")
    public CommonResult<String> add(@RequestBody @Valid BizEsStorageEnterpriseAddParam bizEsStorageEnterpriseAddParam ) {

        bizEsStorageEnterpriseService.saveAll(bizEsStorageEnterpriseAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑应急保障-应急仓储企业管理表
     *
     * @author shine
     * @date  2024/05/25 14:37
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("*编辑应急保障-应急仓储企业管理表")
    @CommonLog("*编辑应急保障-应急仓储企业管理表")
    @PostMapping("/emergencySupport/esstorageenterprise/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizEsStorageEnterpriseEditParam bizEsStorageEnterpriseEditParam) {
        bizEsStorageEnterpriseService.edit(bizEsStorageEnterpriseEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除应急保障-应急仓储企业管理表
     *
     * @author shine
     * @date  2024/05/25 14:37
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("*删除应急保障-应急仓储企业管理表")
    @CommonLog("*删除应急保障-应急仓储企业管理表")
    @PostMapping("/emergencySupport/esstorageenterprise/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<BizEsStorageEnterpriseIdParam> bizEsStorageEnterpriseIdParamList) {
        bizEsStorageEnterpriseService.delete(bizEsStorageEnterpriseIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取应急保障-应急仓储企业管理表详情
     *
     * @author shine
     * @date  2024/05/25 14:37
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("*获取应急保障-应急仓储企业管理表详情")
    @GetMapping("/emergencySupport/esstorageenterprise/detail")
    public CommonResult<BizEsStorageEnterprise> detail(@Valid BizEsStorageEnterpriseIdParam bizEsStorageEnterpriseIdParam) {
        return CommonResult.data(bizEsStorageEnterpriseService.detail(bizEsStorageEnterpriseIdParam));
    }

    /**
     * 导入
     */
    @ApiOperation(value = "*应急仓储企业导入数据", notes = "应急仓储企业导入数据---")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象集合")
    })
    @PostMapping(value= "/importByEntity", headers= "content-type= multipart/form-data", consumes= "multipart/*")
    public CommonResult<Map<String,Object>> importByEntity(@RequestParam(value= "file", required = true) MultipartFile file) throws Exception {

        Map<String,Object> map = new HashMap<String,Object>();
        if(file== null){
            map.put("massage","无文件数据");
            map.put("status","500");
            return CommonResult.data(map);
        }
        //获得文件名
        String fileName = file.getOriginalFilename();
        //判断文件是否是excel文件
        if (!fileName.endsWith("xls") && !fileName.endsWith("xlsx")) {
            map.put("massage","请使用正确的导入模板");
            map.put("status","500");
            return CommonResult.data(map);
        }

        return CommonResult.data(bizEsStorageEnterpriseService.importByEntityResult(file,map));

    }

    /**
     *应急保障中心
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("*应急保障中心")
    @GetMapping("/emergencySupport/supportCenter/getAllList")
    public CommonResult<List<Map<String,Object>>> getAllList(@RequestParam(value = "type") @NotEmpty(message = "数字,不可为空") String type) {
        return CommonResult.data(bizEsStorageEnterpriseService.getAllList(type));
    }

    /**
     *
     *应急仓储企业统计
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("*应急仓储企业统计")
    @GetMapping("/emergencySupport/supportCenter/statistics")
    public CommonResult<List<Map<String,Object>>> statistics(@RequestParam(value = "city",required = false) String city,@RequestParam(value = "county",required = false) String county) {
        return CommonResult.data(bizEsStorageEnterpriseService.statistics(city,county));
    }

}
