package com.prolog.rdc.tms.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.web.multipart.MultipartFile;

import com.prolog.framework.core.exception.ParameterException;
import com.prolog.rdc.tms.constant.BussinessConstant;

/**
 * 
 *
 * 文件处理公共方法
 *
 * @className :FileUtils
 * @author :fanzw
 * @creationTime :2022/04/27
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/04/27
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
public class FileUtils {

	/**
	 * 文件判断
	 * 
	 * @param multipartFile
	 */
	public static void multipartFileJudge(MultipartFile multipartFile) {
		if (multipartFile.getSize() == 0 || multipartFile == null) {
			throw new ParameterException("文件为空，请上传正确的文件");
		}

		if (String.valueOf(multipartFile.getSize()).length() > BussinessConstant.FILE_SIZE) {
			throw new ParameterException("上传文件大小超过15M,请拆分导入");
		}

		if (!multipartFile.getOriginalFilename().endsWith("xls") && !multipartFile.getOriginalFilename().endsWith("xlsx")) {
			throw new ParameterException("文件不是excel文件");
		}
	}

	/**
	 * 判断字符串是数字还是中文
	 * 
	 * @param str
	 * @return
	 */
	public static boolean StringType(String str) {
		Pattern pattern = Pattern.compile("[0-9]+");
		Matcher matcher = pattern.matcher((CharSequence) str);
		boolean result = matcher.matches();
		if (result) {
			return true;
		} else {
			return false;
		}
	}

}
