package com.sy.coinage.upload;

import com.sy.coinage.config.CustomConfig;
import com.sy.coinage.core.vo.FileVO;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * -通用上传文件
 */
@RestController
public class UploadController {

    @Autowired
    private CustomConfig customConfig;

    private static final String YYMMDD = "yyyyMMdd";

    /**
     * -通用上传模块
     *
     * @return Object {@link FileVO}
     * @Param modelName 上传文件的模块名称
     */
    // @LoginNotRequired
    @PostMapping("/file/upload")
    public Object upload(HttpServletRequest request) throws Exception {
        List<FileVO> data = null;
        try {
            String modelName = request.getParameter("modelName");
            StandardMultipartHttpServletRequest multipartHttp = new StandardMultipartHttpServletRequest(request);
            Map<String, MultipartFile> map = multipartHttp.getFileMap();
            data = new ArrayList<>(map.size());
            String filePath = getSavePath(modelName);
            File path = new File(customConfig.getAttaPath() + filePath);
            if (!path.exists()) {
                path.mkdirs();
            }
            Iterator<Map.Entry<String, MultipartFile>> ent = map.entrySet().iterator();
            while (ent.hasNext()) {
                Map.Entry<String, MultipartFile> entry = ent.next();
                MultipartFile multipartFile = entry.getValue();
                String fileName = getSaveName(multipartFile.getOriginalFilename());
                File outFile = new File(path + "/" + fileName);
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), outFile);
                data.add(new FileVO(multipartFile.getOriginalFilename(), filePath + "/" + fileName));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return data;
    }

    /**
     * -获取存储的路径
     *
     * @param modelName 模块名称
     * @return
     */
    private String getSavePath(String modelName) {
        LocalDate date = LocalDate.now();
        StringBuffer filePath = new StringBuffer();
        filePath.append(customConfig.getAttaUrl());
        if (StringUtils.isNotBlank(modelName)) {
            filePath.append("/" + modelName);
        }
        filePath.append("/" + date.format(DateTimeFormatter.ofPattern(YYMMDD)));
        return filePath.toString();
    }

    /**
     * -获取存储的文件名
     *
     * @param name
     * @return
     */
    private String getSaveName(String name) {
        // 获取文件后缀
        String sufferFix = name.substring(name.lastIndexOf("."));
        return System.currentTimeMillis() + sufferFix;
    }

}
