package com.energy.manage.service.controller;

import com.alibaba.fastjson.JSONObject;
import com.energy.manage.common.reponse.RespMsg;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.config.annotations.PassToken;
import com.energy.manage.service.controller.base.BaseServiceController;
import com.energy.manage.service.util.MinioUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 上传模块
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/check")
@CrossOrigin
@Api(value = "UploadController", tags = "上传接口")
public class UploadController extends BaseServiceController {

  @Autowired
  private MinioUtils minioUtils;

  @PassToken
  @RequestMapping(value = "/uploadMaterial", method = RequestMethod.POST)
  @ApiOperation(value = "文件上传")
  public ResultResp uploadMaterial(@RequestPart MultipartFile[] files) {
    if (null == files || files.length == 0) {
      return ResultResp.FAIL("请选择要上传的内容");
    }
    try {
      List<String> list = minioUtils.upload(files);
      if (CollectionUtils.isEmpty(list)) {
        return ResultResp.FAIL("文件上传失败，请重试或联系管理员");
      }
      return ResultResp.SUCCESS(list);
    } catch (Exception ex) {
      log.error(ex.getMessage(), ex);
    }
    return ResultResp.FAIL("文件上传失败，请重试或联系管理员");
  }

  /**
   * 单文件上传
   *
   * @param file
   * @return
   */
  @PassToken
  @RequestMapping(value = "/upload", method = RequestMethod.POST)
  @ApiOperation(value = "单文件上传")
  public JSONObject upload(@RequestPart MultipartFile file) {

    if (null == file) {
      return RespMsg.FAIL("请选择要上传的内容");
    }
    log.info("文件名称：===>" + file.getName());
    log.info("文件大小：===>" + file.getSize());
    String path = minioUtils.upload(file);
    if (StringUtils.isEmpty(path)) {
      return RespMsg.FAIL("文件上传失败，请重试或联系管理员");
    }
    return RespMsg.SUCCESS(path);
  }

  /**
   * 内部方法，有序Map转list
   *
   * @param map
   * @return
   */
  private List<String> sortMap2List(Map<Integer, String> map) {
    if (CollectionUtils.isEmpty(map)) {
      return Collections.EMPTY_LIST;
    }
    List<String> list = new ArrayList<>(map.size());
    for (Integer item : map.keySet()) {
      list.add(map.get(item));
    }
    return list;
  }


  /**
   * 判断文件大小
   *
   * @param len  文件长度
   * @param size 限制大小
   *             //   * @param unit
   *             限制单位（B,K,M,G）
   * @return
   */
  private boolean checkFileSize(Long len, int size) {
    double fileSize = (double) len / 1048576;
    if (fileSize >= size) {
      return false;
    }
    return true;
  }


}
