package cn.estsh.i3plus.core.apiservice.controller;

import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import cn.estsh.impp.framework.boot.fastdfs.FastDFSClient;
import cn.estsh.impp.framework.boot.fastdfs.FastDFSFile;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @Description :
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-09-28 11:20
 * @Modify:
 **/
@RestController
@Api(description="复杂对象服务demo")
@RequestMapping(PlatformConstWords.BASE_URL + "/demo-upload")
public class DemoUploadController extends CoreBaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoUploadController.class);

    /********************** 文件上传 ***********************/

    @Autowired
    private FastDFSClient dfsClient;

    @GetMapping("/")
    public String index() {
        return "/index";
    }

    @GetMapping("/index")
    public String indexAgain() {
        return "/index";
    }

    @GetMapping("/upfile")
    public String indexUpload() {
        return "/upload";
    }

    @GetMapping("/upload-status")
    public String uploadStatus() {
        return "/upload-status";
    }

    @PostMapping("/upload")
    @ApiOperation(value = "文件上传",notes = "文件上传")
    public ResultBean singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
        try {
            FastDFSFile dfsFile = dfsClient.saveFile(file);
            return ResultBean.success("上传成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(dfsFile);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
