package com.yonge.cooleshow.teacher.controller;

import com.ksyun.ks3.dto.PostObjectFormFields;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.thirdparty.entity.UploadSign;
import io.swagger.annotations.*;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yonge.cooleshow.biz.dal.service.UploadFileService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.UploadReturnBean;
import com.yonge.toolset.utils.upload.UploadUtil;

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

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

    @Autowired
    private UploadFileService uploadFileService;

    @PostMapping("/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()));
                bean.setName(file.getOriginalFilename());
                if (bean.isStatus()) {
                    return succeed(bean);
                }
                return failed(bean.getMessage());
            }
        } catch (Exception e) {
            LOGGER.error("上传失败", e);
        }
        return failed("上传失败");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", dataType = "String", value = "要上传的文件名称,不包含路径信息"),
            @ApiImplicitParam(name = "postData", dataType = "Map", value = "1.如果使用js sdk上传的时候设置了ACL请设置,例\"acl\":\"public-read\"值要与SDK中一致,没有则删除该项</br>" +
                    "2.提供js sdk中的key值,例\"key\":\"20150115/中文/${filename}\""),
            @ApiImplicitParam(name = "unknowValueField", dataType = "List", value = "对于用户无法确定表单值的放在unknownValueField中(比如有的上传控件会添加一些表单项,但表单项的值可能是随机的)"),
    })
    @ApiOperation(value = "获取上传文件签名", notes = "{\n" +
            "    \"fileName\":\"aa\",\n" +
            "    \"postData\":{\n" +
            "        \"acl\":\"public-read\",\n" +
            "        \"key\":\"20150115/中文/${filename}\"\n" +
            "    },\n" +
            "    \"unknowValueField\":[\"test\"]\n" +
            "}")
    @PostMapping("/getUploadSign")
    public HttpResponseResult<PostObjectFormFields> getUploadSign(@RequestBody UploadSign uploadSign) {
        return succeed(uploadFileService.getUploadSign(uploadSign));
    }
}
