package com.yihu.wlyy.web.statistic;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.wlyy.config.FastDFSConfig;
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by lyr-pc on 2016/12/13.
 */
@RestController
@RequestMapping(value = "/test/file/")
@Api(description = "文件测试")
public class FileTestController extends BaseController {

    @Autowired
    FastDFSConfig fastDFSConfig;

    @ApiOperation(value = "文件上传", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String resolve(
            @ApiParam("文件")
            @RequestPart() MultipartFile file) throws Throwable {
        try {
            FastDFSUtil fastDFSUtil = fastDFSConfig.fastDFSUtil();

            ObjectNode result = fastDFSUtil.upload(file.getInputStream(), "png", "测试");

            return write(200, "上传成功", "data", result.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "上传失败");
        }
    }

    @ApiOperation(value = "test")
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        try {
            String j = null;
            //JSONObject json = new JSONObject(j);
            JSONObject json2 = new JSONObject("");
            return "a";
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }
}
