package org.security.trans.common.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.security.trans.common.service.UploadService;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author zj 2022/4/29
 */
@AllArgsConstructor
@Api(value = "上传", tags = "上传")
@RestController
@RequestMapping("/upload")
public class UploadController {

	private final UploadService uploadService;

	@ApiOperationSupport(order = 0)
	@ApiOperation(value = "单个文件上传", notes = "单个文件上传")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "file", value = "FormData", dataType = "FormData"),
	})
	@PostMapping("/single")
	public R upload(@RequestParam MultipartFile file,
					@RequestParam(required = false) String groupType,
					@RequestParam(required = false) String groupName) throws IOException {
		if (file.isEmpty()) {
			return R.fail("文件不能为空");
		}
		return R.data(uploadService.uploadSingleFile(file, groupType, groupName));
	}

	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "批量文件上传", notes = "批量文件上传")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "files", value = "FormData", dataType = "FormData"),
	})
	@PostMapping("/list")
	public R uploadList(@RequestParam List<MultipartFile> files,
						@RequestParam(required = false) String groupType,
						@RequestParam(required = false) String groupName) throws IOException {
		if (files.isEmpty()) {
			return R.fail("文件列表不能为空");
		}
		return R.data(uploadService.uploadListFile(files, groupType, groupName));
	}
}
