package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ym.mec.biz.service.UploadFileService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.UploadReturnBean;
import com.ym.mec.util.upload.UploadUtil;

/** 
 * 上传控制层
 */
@RestController
@Api(tags = "文件上传服务")
public class UploadFileController extends BaseController {

	private final static Logger LOGGER = LoggerFactory.getLogger(UploadFileController.class);

	@Autowired
	private UploadFileService uploadFileService;

	@PostMapping(value = "uploadFile")
	public Object uploadFile(@ApiParam(value = "上传的文件", required = true) @RequestParam("file") MultipartFile file) {
		try {
			if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
				UploadReturnBean bean = uploadFileService.uploadFile(file.getInputStream(), UploadUtil.getExtension(file.getOriginalFilename()));
				if (bean.isStatus()) {
					return succeed(bean);
				}
				return failed(bean.getMessage());
			}
		} catch (Exception e) {
			LOGGER.error("上传失败", e);
		}
		return failed("上传失败");
	}
}
