package com.ads.business.api;

import com.ads.business.service.FileService;
import com.ads.business.service.data.GetFileOutData;
import com.ads.common.base.BaseController;
import com.ads.common.base.ExportExcel;
import com.ads.common.data.GetPictureConfigInData;
import com.ads.common.data.GetPicturePathOutData;
import com.ads.common.data.JsonResult;
import com.ads.common.util.UtilMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 作者 wuyifeng
 * @date 创建时间 2020年8月3日
 * @description 文件上传
 */
@Api(tags = "文件上传管理")
@RestController
@RequestMapping(value = "/api/file/")
public class FileController extends BaseController {

    @Autowired
    private GetPictureConfigInData configInData;
    @Autowired
    private FileService fileService;

    /**
     * 文件上传(本地)
     *
     * @param file 图片文件
     * @return
     */
    @ApiOperation(value = "文件上传(本地)", notes = "文件上传(本地)")
    @PostMapping(value = "local/upload")
    public JsonResult imageUpload(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {
        GetFileOutData outData = new GetFileOutData();
        String fileName = file.getOriginalFilename();
        String[] files = fileName.split("\\.");
        fileName = System.currentTimeMillis() + "." + files[1];
        String path = request.getScheme() + "://" + request.getServerName() + ":" + "19050" + request.getContextPath() + "/";
        GetPicturePathOutData picturePath = fileService.createUploadPath(fileName);
        outData.setPath(picturePath.getServerPath());
        picturePath.setServerPath(path + picturePath.getServerPath());
        try {
            fileService.savePicture(file.getBytes(), picturePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        outData.setUrl(picturePath.getServerPath());
        outData.setFileName(fileName);

        if (file.getSize() > 1024 * 1024) {
            double f = file.getSize();
            outData.setSize(String.format("%.2f", f / (1024 * 1024)) + "MB");
        } else {
            double f = file.getSize();
            outData.setSize(String.format("%.2f", f / 1024) + "KB");
        }
        return JsonResult.success(outData, UtilMessage.UPLOAD_MESSAGE_SUCCESS);
    }

    public static void main(String[] args) {
        double s = 1438808;
        System.out.println("f = " + String.format("%.2f", s / (1024 * 1024)) + "MB");
        double t = 722528;
        System.out.println("t = " + String.format("%.2f", t / 1024) + "KB");

    }

    /**
     * 导出excel模板
     *
     * @param response:
     * @param fileName: 文件名称
     * @param filePath: 文件路径
     * @return void
     * @author yuqiang
     * @date 2020/9/3 11:34 上午
     **/
    @ApiOperation(value = "下载excel文件信息", notes = "下载excel文件信息")
    @GetMapping(value = "exportExcel")
    public void exportExcel(HttpServletResponse response, HttpServletRequest request, @RequestParam("fileName") String fileName, @RequestParam("filePath") String filePath) {
        ExportExcel.exportFile(response, request, fileName, filePath);
    }

    /**
     * 下载文件
     *
     * @param response
     * @param fileName
     * @param filePath
     */
    @ApiOperation(value = "下载文件信息", notes = "下载文件信息")
    @GetMapping(value = "exportFile")
    public void exportFile(HttpServletResponse response, HttpServletRequest request, @RequestParam("fileName") String fileName, @RequestParam("filePath") String filePath) {
        ExportExcel.exportFile(response, request, fileName, filePath);
    }
}
