Explorar o código

下载考生模板

ting.yin %!s(int64=3) %!d(string=hai) anos
pai
achega
2279115735

+ 130 - 0
src/main/java/cn/com/qmth/print/manage/controller/BaseController.java

@@ -0,0 +1,130 @@
+package cn.com.qmth.print.manage.controller;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.net.URLEncoder;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import com.qmth.boot.core.exception.StatusException;
+
+import cn.com.qmth.print.manage.utils.ObjectUtil;
+
+public class BaseController {
+
+    protected HttpServletRequest currentRequest() {
+        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+    }
+
+    /**
+     * 参数trim
+     *
+     * @param bean
+     * @param nullIfBlank
+     * @author
+     */
+    protected void trim(Object bean, boolean nullIfBlank) {
+        if (null == bean) {
+            return;
+        }
+        Class<? extends Object> clazz = bean.getClass();
+        if (clazz.equals(Map.class)) {
+            return;
+        } else if (clazz.equals(List.class)) {
+            return;
+        } else if (clazz.equals(Set.class)) {
+            return;
+        } else if (clazz.isEnum()) {
+            return;
+        } else if (ObjectUtil.isBaseDataType(clazz)) {
+            return;
+        }
+
+        Field[] fields = clazz.getDeclaredFields();
+        try {
+            for (int i = 0; i < fields.length; i++) {
+                Field f = fields[i];
+                f.setAccessible(true);
+                Object value = f.get(bean);
+                if (null == value) {
+                    continue;
+                }
+                if (f.getType().equals(String.class)) {
+                    if (null != value) {
+                        String s = (String) value;
+                        s = s.trim();
+                        if (nullIfBlank && StringUtils.isBlank(s)) {
+                            s = null;
+                        }
+                        f.set(bean, s);
+                    }
+                } else if (f.getType().equals(Map.class)) {
+                } else if (f.getType().equals(List.class)) {
+                } else if (f.getType().equals(Set.class)) {
+                } else if (f.getType().isEnum()) {
+                } else if (ObjectUtil.isBaseDataType(f.getType())) {
+                } else {
+                    trim(value);
+                }
+
+            }
+        } catch (Exception e) {
+            throw new StatusException("系统错误", e);
+        }
+    }
+
+    /**
+     * 参数trim.<br>
+     * set null if blank.
+     *
+     * @param bean
+     * @author
+     */
+    protected void trim(Object bean) {
+        trim(bean, false);
+    }
+
+    @SuppressWarnings("deprecation")
+    protected void exportFile(String fileName, File file) {
+        OutputStream out = null;
+        InputStream in = null;
+        try {
+            in = new FileInputStream(file);
+            fileName = URLEncoder.encode(fileName, "UTF-8");
+            HttpServletResponse response = getResponse();
+            response.reset();
+            response.setHeader("Content-Disposition", "inline; filename=" + fileName);
+            response.addHeader("Content-Length", "" + file.length());
+            response.setContentType("application/octet-stream;charset=UTF-8");
+            out = new BufferedOutputStream(response.getOutputStream());
+            IOUtils.copy(in, out);
+            out.flush();
+        } catch (Exception e) {
+            throw new StatusException("系统错误", e);
+        } finally {
+            IOUtils.closeQuietly(out);
+            IOUtils.closeQuietly(in);
+        }
+    }
+
+    protected HttpServletResponse getResponse() {
+        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
+                .getRequestAttributes();
+        HttpServletResponse response = requestAttributes.getResponse();
+        return response;
+    }
+
+}

+ 16 - 8
src/main/java/cn/com/qmth/print/manage/controller/ExamStudentController.java

@@ -1,21 +1,24 @@
 package cn.com.qmth.print.manage.controller;
 
-import cn.com.qmth.print.manage.service.ExamStudentService;
-import cn.com.qmth.print.manage.service.query.ExamStudentQuery;
-
-import com.qmth.boot.api.annotation.Aac;
-import com.qmth.boot.api.annotation.BOOL;
-import com.qmth.boot.api.constant.ApiConstant;
+import java.io.File;
+import java.io.IOException;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.IOException;
+import cn.com.qmth.print.manage.service.ExamStudentService;
+import cn.com.qmth.print.manage.service.query.ExamStudentQuery;
+import cn.com.qmth.print.manage.utils.PathUtil;
+
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.annotation.BOOL;
+import com.qmth.boot.api.constant.ApiConstant;
 
 /**
  * @Date: 2021/11/16.
@@ -23,7 +26,7 @@ import java.io.IOException;
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/student")
 @Aac(strict = BOOL.FALSE, auth = BOOL.TRUE)
-public class ExamStudentController {
+public class ExamStudentController extends BaseController {
 
     @Autowired
     private ExamStudentService examStudentService;
@@ -48,4 +51,9 @@ public class ExamStudentController {
         return examStudentService.pageStudent(query);
     }
 
+    @GetMapping("/template")
+    public void getImportTemplate() {
+        String resoucePath = PathUtil.getResoucePath("importtemplates/studentImportTemplate.xlsx");
+        exportFile("考生导入模板.xlsx", new File(resoucePath));
+    }
 }

+ 16 - 16
src/main/java/cn/com/qmth/print/manage/service/impl/ExamStudentServiceImpl.java

@@ -1,5 +1,18 @@
 package cn.com.qmth.print.manage.service.impl;
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringJoiner;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
+import org.springframework.web.multipart.MultipartFile;
+
 import cn.com.qmth.print.manage.dao.ExamStudentDao;
 import cn.com.qmth.print.manage.dto.StudentDTO;
 import cn.com.qmth.print.manage.entity.ExamStudentEntity;
@@ -8,26 +21,13 @@ import cn.com.qmth.print.manage.service.ExamStudentService;
 import cn.com.qmth.print.manage.service.query.ExamStudentQuery;
 import cn.com.qmth.print.manage.utils.excel.ExcelError;
 import cn.com.qmth.print.manage.utils.excel.ExcelReader;
-import cn.com.qmth.print.manage.utils.excel.ExcelReaderHandle;
+
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.boot.core.exception.StatusException;
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.lang.StringUtils;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-import org.springframework.transaction.interceptor.TransactionAspectSupport;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.StringJoiner;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
 
 @Service
 public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStudentEntity> implements
@@ -122,7 +122,7 @@ public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStud
                 studentList.add(examStudentEntity);
                 return null;
             } catch (RuntimeException e) {
-                //手动回滚
+                // 手动回滚
                 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                 ExcelError excelError = new ExcelError();
                 excelError.setExcelErrorType(e.getMessage());
@@ -143,7 +143,7 @@ public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStud
                 }
                 data.add(stu);
             }
-            //将剩下的数据也导入
+            // 将剩下的数据也导入
             if (!data.isEmpty()) {
                 this.saveBatch(data);
             }

+ 69 - 0
src/main/java/cn/com/qmth/print/manage/utils/ObjectUtil.java

@@ -0,0 +1,69 @@
+package cn.com.qmth.print.manage.utils;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Map;
+
+/**
+ * 对象工具
+ *
+ * @author 
+ */
+public final class ObjectUtil {
+
+	/**
+	 * 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Map为empty
+	 *
+	 * @author 
+	 * @param obj
+	 * @return
+	 */
+	public static boolean isNullOrEmpty(Object obj) {
+		if (obj == null)
+			return true;
+
+		if (obj instanceof CharSequence)
+			return ((CharSequence) obj).length() == 0;
+
+		if (obj instanceof Collection)
+			return ((Collection<?>) obj).isEmpty();
+
+		if (obj instanceof Map)
+			return ((Map<?, ?>) obj).isEmpty();
+
+		if (obj instanceof Object[]) {
+			Object[] object = (Object[]) obj;
+			if (object.length == 0) {
+				return true;
+			}
+			boolean empty = true;
+			for (int i = 0; i < object.length; i++) {
+				if (!isNullOrEmpty(object[i])) {
+					empty = false;
+					break;
+				}
+			}
+			return empty;
+		}
+		return false;
+	}
+
+	/**
+	 * 判断一个类是否为基本数据类型
+	 *
+	 * @author 
+	 * @param clazz
+	 * @return
+	 */
+	public static boolean isBaseDataType(Class<?> clazz) {
+		Boolean isBaseType = (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class)
+				|| clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class)
+				|| clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class)
+				|| clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class)
+				|| clazz.isPrimitive());
+		return isBaseType;
+	}
+
+}

+ 180 - 0
src/main/java/cn/com/qmth/print/manage/utils/PathUtil.java

@@ -0,0 +1,180 @@
+package cn.com.qmth.print.manage.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLDecoder;
+
+/**
+ * 路径工具
+ *
+ * @author 
+ */
+public class PathUtil {
+
+	/**
+	 * 获取标准路径
+	 *
+	 * @author 
+	 * @param path
+	 * @return
+	 */
+	public static String getCanonicalPath(String path) {
+		path = path.replaceAll("\\\\+", "/");
+		path = path.replaceAll("/+", "/");
+		return path;
+	}
+
+	/**
+	 * 以"/"开头
+	 *
+	 * @author 
+	 * @param path
+	 * @return
+	 */
+	public static String startsWithSeparator(String path) {
+		path = getCanonicalPath(path);
+		if (path.startsWith("/")) {
+			return path;
+		}
+		return "/" + path;
+	}
+
+	/**
+	 * 不以"/"开头
+	 *
+	 * @author 
+	 * @param path
+	 * @return
+	 */
+	public static String startsWithoutSeparator(String path) {
+		path = getCanonicalPath(path);
+		if (path.startsWith("/")) {
+			return path.substring(1);
+		}
+		return path;
+	}
+
+	/**
+	 * 以"/"结尾
+	 *
+	 * @author 
+	 * @param path
+	 * @return
+	 */
+	public static String endsWithSeparator(String path) {
+		path = getCanonicalPath(path);
+		if (path.endsWith("/")) {
+			return path;
+		}
+		return path + "/";
+	}
+
+	/**
+	 * 不以"/"结尾
+	 *
+	 * @author 
+	 * @param path
+	 * @return
+	 */
+	public static String endsWithoutSeparator(String path) {
+		path = getCanonicalPath(path);
+		if (path.endsWith("/")) {
+			return path.substring(0, path.length() - 1);
+		}
+		return path;
+	}
+
+	/**
+	 * 获取路径
+	 *
+	 * @author 
+	 * @param file
+	 * @return
+	 */
+	public static String getCanonicalPath(File file) {
+		try {
+			return file.getCanonicalPath();
+		} catch (IOException e) {
+			throw new RuntimeException("Fail to get canonical path.", e);
+		}
+
+	}
+
+	/**
+	 * 获取当前路径
+	 *
+	 * @author 
+	 * @return
+	 * @throws IOException
+	 */
+	public static String currentPath() {
+		File directory = new File(". ");
+		return getCanonicalPath(directory);
+	}
+
+	/**
+	 * 获取资源路径
+	 *
+	 * @author 
+	 * @param resourceName
+	 * @return
+	 */
+	public static String getResoucePath(String resourceName) {
+		try {
+			ClassLoader classLoader = PathUtil.class.getClassLoader();
+
+			URL url = classLoader.getResource(resourceName);
+			if (null != url) {
+				String path = URLDecoder.decode(url.getPath(), "UTF-8");
+				return path;
+			} else {
+				return null;
+			}
+		} catch (Exception e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	/**
+	 * 获取windows盘符
+	 *
+	 * @author 
+	 * @param path
+	 * @return
+	 */
+	public static String getDrive(String path) {
+		if (path.matches("[a-zA-Z]:[\\\\/].*")) {
+			return path.substring(0, 2);
+		} else {
+			throw new RuntimeException("Path is not a windows path.");
+		}
+	}
+
+	/**
+	 * 拼接路径
+	 *
+	 * @author 
+	 * @param fragments
+	 * @return
+	 */
+	public static String joinUrl(String... fragments) {
+		StringBuilder sb = new StringBuilder();
+		for (int i = 0; i < fragments.length; i++) {
+			String cur = fragments[i].trim();
+			if (cur.endsWith("/")) {
+				cur = cur.substring(0, cur.length() - 1);
+			}
+			if (0 == i) {
+				sb.append(cur);
+			} else if (cur.startsWith("/")) {
+				sb.append(cur);
+			} else {
+				sb.append("/").append(cur);
+			}
+		}
+
+		return sb.toString();
+	}
+
+}

BIN=BIN
src/main/resources/importtemplates/studentImportTemplate.xlsx