|
@@ -7,13 +7,18 @@ import com.qmth.teachcloud.common.config.DictionaryConfig;
|
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
import com.qmth.teachcloud.common.entity.BasicSchool;
|
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
+import com.qmth.teachcloud.common.enums.SyncFileTypeEnum;
|
|
|
import com.qmth.teachcloud.common.service.CommonCacheService;
|
|
|
+import com.qmth.teachcloud.common.util.HttpKit;
|
|
|
import com.qmth.teachcloud.common.util.ServletUtil;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
+import java.io.*;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* 同步云阅卷接口工具类
|
|
@@ -42,24 +47,24 @@ public class StmmsUtils {
|
|
|
* @return
|
|
|
*/
|
|
|
public Long syncExam(String code, String name, String examTime) {
|
|
|
-// String hostUrl = dictionaryConfig.syncDataDomain().getHostUrl();
|
|
|
-// String examSaveUrl = dictionaryConfig.syncDataDomain().getExamSaveUrl();
|
|
|
-// String postUrl = hostUrl.concat(examSaveUrl);
|
|
|
-// //参数
|
|
|
-// Map<String, String> map = new HashMap<>();
|
|
|
-// map.put("code", code);
|
|
|
-// map.put("name", name);
|
|
|
-// map.put("examTime", examTime);
|
|
|
-// map.put("type", SAVE_EXAM_TYPE);
|
|
|
-//
|
|
|
-// String result = HttpKit.sendPost(postUrl, getHeaders(examSaveUrl), map, null, null, null);
|
|
|
-// JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
-// if (jsonObject.containsKey("id")) {
|
|
|
-// return Long.valueOf(jsonObject.get("id").toString());
|
|
|
-// } else {
|
|
|
-// throw ExceptionResultEnum.ERROR.exception("考试同步失败");
|
|
|
-// }
|
|
|
- return null;
|
|
|
+ String hostUrl = dictionaryConfig.syncDataDomain().getHostUrl();
|
|
|
+ String examSaveUrl = dictionaryConfig.syncDataDomain().getExamSaveUrl();
|
|
|
+ validatUrl(hostUrl, examSaveUrl);
|
|
|
+ String postUrl = hostUrl.concat(examSaveUrl);
|
|
|
+ //参数
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("code", validParam(code, null, true, "唯一标识"));
|
|
|
+ map.put("name", validParam(name, null, true, "考试名称"));
|
|
|
+ map.put("examTime", validParam(examTime, null, true, "考试时间"));
|
|
|
+ map.put("type", SAVE_EXAM_TYPE);
|
|
|
+
|
|
|
+ String result = HttpKit.sendPost(postUrl, getHeaders(examSaveUrl), map, null, null, null);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ if (jsonObject.containsKey("id")) {
|
|
|
+ return Long.valueOf(jsonObject.get("id").toString());
|
|
|
+ } else {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("考试同步失败");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -80,36 +85,119 @@ public class StmmsUtils {
|
|
|
* @param examRoom 考场
|
|
|
* @return
|
|
|
*/
|
|
|
- public Long syncStudent(String examId, String examNumber, String studentCode, String name, String college,
|
|
|
- String className, String teacher, String subjectCode, String subjectName,
|
|
|
- String packageCode, String paperType, String examSite, String examRoom) {
|
|
|
+ public Boolean syncStudent(String examId, String examNumber, String studentCode, String name, String college,
|
|
|
+ String className, String teacher, String subjectCode, String subjectName,
|
|
|
+ String packageCode, String paperType, String examSite, String examRoom) {
|
|
|
String hostUrl = dictionaryConfig.syncDataDomain().getHostUrl();
|
|
|
String studentSaveUrl = dictionaryConfig.syncDataDomain().getStudentSaveUrl();
|
|
|
+ validatUrl(hostUrl, studentSaveUrl);
|
|
|
String postUrl = hostUrl.concat(studentSaveUrl);
|
|
|
-// try {
|
|
|
-// //参数
|
|
|
-// Map<String, String> map = new HashMap<>();
|
|
|
-// map.put("examId", examId);
|
|
|
-// map.put("examNumber", examNumber);
|
|
|
-// map.put("studentCode", studentCode);
|
|
|
-// map.put("name", name);
|
|
|
-// map.put("college", StringUtils.isBlank(college) ? "无" : college);
|
|
|
-// map.put("className", StringUtils);
|
|
|
-// map.put("teacher", "无");
|
|
|
-// map.put("subjectCode", examStudent.getPaperNumber()); // 取试卷编号
|
|
|
-// map.put("subjectName", examStudent.getCourseName());
|
|
|
-//
|
|
|
-// String result = HttpKit.sendPost(postUrl, getHeaders(examPrintPlan.getSchoolId(), studentSaveUrl), map, null, null, null);
|
|
|
-// JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
-// if (jsonObject.containsKey("updateTime")) {
|
|
|
-// UpdateWrapper<ExamStudent> updateWrapper = new UpdateWrapper<>();
|
|
|
-// updateWrapper.lambda().set(ExamStudent::getSyncStatus, true).eq(ExamStudent::getId, examStudent.getId());
|
|
|
-// examStudentService.update(updateWrapper);
|
|
|
-// }
|
|
|
-// } catch (Exception e) {
|
|
|
-// throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
-// }
|
|
|
- return null;
|
|
|
+ try {
|
|
|
+ //参数
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("examId", validParam(examId, null, true, "考试ID"));
|
|
|
+ map.put("examNumber", validParam(examNumber, null, true, "准考证号"));
|
|
|
+ map.put("studentCode", validParam(studentCode, null, true, "学号"));
|
|
|
+ map.put("name", validParam(name, null, true, "姓名"));
|
|
|
+ map.put("college", validParam(college, "无", true, "学院"));
|
|
|
+ map.put("className", validParam(className, null, true, "班级名称"));
|
|
|
+ map.put("teacher", validParam(teacher, "无", true, "教师姓名"));
|
|
|
+ map.put("subjectCode", validParam(subjectCode, null, true, "科目代码")); // 取试卷编号
|
|
|
+ map.put("subjectName", validParam(subjectName, null, true, "科目名称"));
|
|
|
+ map.put("packageCode", validParam(packageCode, null, false, "签到表编号"));
|
|
|
+ map.put("paperType", validParam(paperType, null, false, "试卷类型"));
|
|
|
+ map.put("examSite", validParam(examSite, null, false, "考点"));
|
|
|
+ map.put("examRoom", validParam(examRoom, null, false, "考场"));
|
|
|
+
|
|
|
+ String result = HttpKit.sendPost(postUrl, getHeaders(studentSaveUrl), map, null, null, null);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ if (jsonObject.containsKey("updateTime")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传题卡
|
|
|
+ *
|
|
|
+ * @param examId 考试ID
|
|
|
+ * @param subjectCode 科目代码
|
|
|
+ * @param content 题卡json内容
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean syncCardFile(String examId, String subjectCode, String content) {
|
|
|
+ //生成json文件
|
|
|
+ File file = null;
|
|
|
+ try {
|
|
|
+ String filePath = SystemConstant.TEMP_FILES_DIR + File.separator + "card-upload" + File.separator + System.currentTimeMillis();
|
|
|
+ file = createJsonFile(filePath, content);
|
|
|
+ return sendFile(examId, subjectCode, SyncFileTypeEnum.CARD, file);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (file != null && file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公共上传接口
|
|
|
+ *
|
|
|
+ * @param examId 考试ID
|
|
|
+ * @param subjectCode 科目代码
|
|
|
+ * @param type 文件类型 SyncFileTypeEnum枚举
|
|
|
+ * @param file 文件
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Boolean sendFile(String examId, String subjectCode, SyncFileTypeEnum type, File file) {
|
|
|
+ String hostUrl = dictionaryConfig.syncDataDomain().getHostUrl();
|
|
|
+ String fileUploadUrl = dictionaryConfig.syncDataDomain().getFileUploadUrl();
|
|
|
+ validatUrl(hostUrl, fileUploadUrl);
|
|
|
+ fileUploadUrl = fileUploadUrl.replace("{type}", type.name().toLowerCase());
|
|
|
+ String postUrl = hostUrl.concat(fileUploadUrl);
|
|
|
+ try {
|
|
|
+ Map<String, String> files = new HashMap<>();
|
|
|
+ if (file.exists()) {
|
|
|
+ files.put(file.getName(), file.getPath());
|
|
|
+ }
|
|
|
+ //表单数据
|
|
|
+ Map<String, String> formText = new HashMap<>();
|
|
|
+ formText.put("examId", validParam(examId, null, true, "考试ID"));
|
|
|
+ formText.put("subjectCode", validParam(subjectCode, null, true, "科目代码"));
|
|
|
+ String fileName = file.getName();
|
|
|
+ String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ if (type.equals(SyncFileTypeEnum.CARD)) {
|
|
|
+ if (!"zip".equals(suffix) && !"json".equals(suffix)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件类型有误");
|
|
|
+ }
|
|
|
+ } else if (type.equals(SyncFileTypeEnum.PAPER) || type.equals(SyncFileTypeEnum.ANSWER)) {
|
|
|
+ if (!"pdf".equals(suffix) && !"json".equals(suffix)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件类型有误");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("请传入文件上传类型");
|
|
|
+ }
|
|
|
+ formText.put("format", suffix);
|
|
|
+ formText.put("md5", DigestUtils.md5Hex(new FileInputStream(file)));
|
|
|
+
|
|
|
+ String result = HttpKit.sendPost(postUrl, getHeaders(fileUploadUrl), formText, files, null, null);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ if (jsonObject.containsKey("success")) {
|
|
|
+ String success = jsonObject.get("success").toString();
|
|
|
+ return Boolean.valueOf(success);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (file != null && file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -147,11 +235,57 @@ public class StmmsUtils {
|
|
|
return signature;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 校验参数值并返回
|
|
|
+ *
|
|
|
+ * @param value 参数值
|
|
|
+ * @param defaulValue 默认值
|
|
|
+ * @param require 是否必填(true:是,false:否)
|
|
|
+ * @param name 参数名称
|
|
|
+ * @return
|
|
|
+ */
|
|
|
private String validParam(String value, String defaulValue, boolean require, String name) {
|
|
|
if (require && StringUtils.isAnyBlank(name, defaulValue)) {
|
|
|
throw ExceptionResultEnum.ERROR.exception((StringUtils.isBlank(name) ? "" : name) + "值必填");
|
|
|
}
|
|
|
-
|
|
|
return StringUtils.isBlank(value) ? defaulValue : value;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验url是否配置
|
|
|
+ *
|
|
|
+ * @param urls
|
|
|
+ */
|
|
|
+ private void validatUrl(String... urls) {
|
|
|
+ if (StringUtils.isAnyBlank(urls)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("云阅卷同步接口未正确配置");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成文件
|
|
|
+ *
|
|
|
+ * @param url 文件路径+文件名称
|
|
|
+ * @param conent 要生成的文件内容
|
|
|
+ */
|
|
|
+ public static File createJsonFile(String url, String conent) {
|
|
|
+ File file = new File(url);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ BufferedWriter out = null;
|
|
|
+ file = new File(file, UUID.randomUUID().toString() + ".json");
|
|
|
+ try {
|
|
|
+ file.createNewFile();
|
|
|
+ out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
|
|
|
+ out.write(conent);
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return file;
|
|
|
+ }
|
|
|
}
|