소스 검색

3.3.1 题库接口调用

xiaofei 1 년 전
부모
커밋
8a97c3e52c

+ 5 - 2
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ExamTaskServiceImpl.java

@@ -34,6 +34,7 @@ import com.qmth.teachcloud.common.bean.params.BasicStudentExtrasParam;
 import com.qmth.teachcloud.common.bean.result.BasicStudentResult;
 import com.qmth.teachcloud.common.bean.result.SysUserResult;
 import com.qmth.teachcloud.common.bean.result.TbTaskDetailResult;
+import com.qmth.teachcloud.common.bean.tiku.TikuCourseProperty;
 import com.qmth.teachcloud.common.bean.tiku.TikuPaperInfo;
 import com.qmth.teachcloud.common.bean.vo.PaperInfoVo;
 import com.qmth.teachcloud.common.contant.SystemConstant;
@@ -2140,13 +2141,15 @@ public class ExamTaskServiceImpl extends ServiceImpl<ExamTaskMapper, ExamTask> i
 
     @Override
     public IPage<TikuPaperInfo> pageTikuPaper(String courseCode, String paperName, String account, Integer pageNumber, Integer pageSize) {
-        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
+//        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
+        Long schoolId = 2l;
         return tikuUtils.pagePaperInfo(schoolId, courseCode, paperName, account, pageNumber, pageSize);
     }
 
     @Override
     public String getTikuPaperData(Long paperId) {
-        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
+//        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
+        Long schoolId = 2l;
         tikuUtils.getTikuPaperData(schoolId, paperId);
         return null;
     }

+ 1 - 1
distributed-print/src/main/java/com/qmth/distributed/print/api/ExamTaskApplyController.java

@@ -276,7 +276,7 @@ public class ExamTaskApplyController {
     }
 
     @Aac(auth = false)
-    @ApiOperation(value = "入库申请-题库试卷列表")
+    @ApiOperation(value = "入库申请-题库试卷")
     @RequestMapping(value = "/get_tiku_paper_data", method = RequestMethod.POST)
     @Transactional(rollbackFor = Exception.class)
     @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})

+ 64 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/bean/tiku/TikuCourseProperty.java

@@ -0,0 +1,64 @@
+package com.qmth.teachcloud.common.bean.tiku;
+
+import java.util.List;
+
+/**
+ * 课程知识点
+ */
+public class TikuCourseProperty {
+
+    private Long id;
+    private Long parentId;
+    private String name;
+    private String code;
+    private String number;
+    private List<TikuCourseProperty> propertyList;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Long getParentId() {
+        return parentId;
+    }
+
+    public void setParentId(Long parentId) {
+        this.parentId = parentId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getNumber() {
+        return number;
+    }
+
+    public void setNumber(String number) {
+        this.number = number;
+    }
+
+    public List<TikuCourseProperty> getPropertyList() {
+        return propertyList;
+    }
+
+    public void setPropertyList(List<TikuCourseProperty> propertyList) {
+        this.propertyList = propertyList;
+    }
+}

+ 60 - 26
teachcloud-common/src/main/java/com/qmth/teachcloud/common/kit/TikuUtils.java

@@ -6,12 +6,14 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.qmth.boot.tools.signature.SignatureType;
 import com.qmth.teachcloud.common.SignatureEntityTest;
+import com.qmth.teachcloud.common.bean.tiku.TikuCourseProperty;
 import com.qmth.teachcloud.common.bean.tiku.TikuPaperInfo;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.BasicSchool;
 import com.qmth.teachcloud.common.entity.SysConfig;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
 import com.qmth.teachcloud.common.service.CommonCacheService;
+import com.qmth.teachcloud.common.util.FileUtil;
 import com.qmth.teachcloud.common.util.HttpUtil;
 import com.qmth.teachcloud.common.util.JacksonUtil;
 import org.apache.commons.lang3.StringUtils;
@@ -20,11 +22,9 @@ import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.io.File;
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Optional;
+import java.util.*;
 
 /**
  * 题库工具类
@@ -52,11 +52,7 @@ public class TikuUtils {
      * @param pageSize   每页条数
      */
     public IPage<TikuPaperInfo> pagePaperInfo(Long schoolId, String courseCode, String paperName, String account, int pageNumber, int pageSize) {
-        SysConfig sysConfig = commonCacheService.addSysConfigCache(schoolId, SystemConstant.QUESTION_HOST_URL);
-        Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置题库地址"));
-        BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
-        String hostUrl = SystemConstant.getHost(sysConfig.getConfigValue(), basicSchool.getCode());
-
+        String hostUrl = getHostUrl(schoolId);
         String url = SystemConstant.TIKU_PAPER_LIST_API;
         validUrl(hostUrl, url);
         String postUrl = hostUrl.concat(url);
@@ -101,38 +97,76 @@ public class TikuUtils {
      * @param paperId  题库试卷ID
      */
     public boolean getTikuPaperData(Long schoolId, Long paperId) {
-        SysConfig sysConfig = commonCacheService.addSysConfigCache(schoolId, SystemConstant.TEACHCLOUD_REPORT_HOST_URL);
-        Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置教研分析地址"));
-        BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
-        String hostUrl = SystemConstant.getHost(sysConfig.getConfigValue(), basicSchool.getCode());
-
+        String hostUrl = getHostUrl(schoolId);
         String url = SystemConstant.TIKU_PAPER_DATA_API;
         validUrl(hostUrl, url);
         String postUrl = hostUrl.concat(url);
         long timestamp = System.currentTimeMillis();
+        File file = null;
         try {
             //参数
             Map<String, Object> map = new HashMap<>();
             map.put("paperId", validParam(paperId, null, true, "试卷ID"));
 
-            String result = HttpUtil.post(postUrl, map, createSign(schoolId, timestamp, url), timestamp);
-            JSONObject jsonObject = JSONObject.parseObject(result);
-//            if (jsonObject.containsKey("code")) {
-//                String code = jsonObject.getString("code");
-//                if ("200".equals(code)) {
-//                    return jsonObject.getLong("data");
-//                } else {
-//                    throw ExceptionResultEnum.ERROR.exception(jsonObject.getString("error"));
-//                }
-//            } else {
-//                throw ExceptionResultEnum.ERROR.exception("考试同步失败");
-//            }
+            File filePath = SystemConstant.getFileTempDirVar(SystemConstant.ZIP_PREFIX);
+            file = HttpUtil.postDownload(postUrl, map, createSign(schoolId, timestamp, url), timestamp, filePath.getPath());
+            if (file.exists()) {
+                System.out.println(filePath.getPath());
+                System.out.println(1);
+            } else {
+                throw ExceptionResultEnum.ERROR.exception("试卷结构包获取失败");
+            }
         } catch (Exception e) {
             throw ExceptionResultEnum.ERROR.exception(e.getMessage());
+        } finally {
+            if (file.exists()) {
+                FileUtil.deleteFile(file);
+            }
         }
         return true;
     }
 
+    /**
+     * 获取试卷知识点
+     *
+     * @param schoolId   学校ID
+     * @param courseCode 课程代码
+     */
+    public List<TikuCourseProperty> listCourseProperty(Long schoolId, String courseCode) {
+        String hostUrl = getHostUrl(schoolId);
+        String url = SystemConstant.TIKU_PAPER_PROPERTY_API;
+        validUrl(hostUrl, url);
+        String postUrl = hostUrl.concat(url);
+        long timestamp = System.currentTimeMillis();
+        //参数
+        try {
+            Map<String, Object> map = new HashMap<>();
+            map.put("courseCode", validParam(courseCode, null, true, "科目代码"));
+
+            String result = HttpUtil.post(postUrl, map, createSign(schoolId, timestamp, url), timestamp);
+            if (result.contains("\"code\":500000")) {
+                throw ExceptionResultEnum.ERROR.exception("课程知识点获取失败");
+            }
+            return JSON.parseArray(result, TikuCourseProperty.class);
+        } catch (IOException e) {
+            log.error(SystemConstant.LOG_ERROR, e);
+            throw ExceptionResultEnum.ERROR.exception(e.getMessage());
+        }
+    }
+
+    /**
+     * 查询题库服务器接口访问地址
+     *
+     * @param schoolId
+     * @return
+     */
+    private String getHostUrl(Long schoolId) {
+        SysConfig sysConfig = commonCacheService.addSysConfigCache(schoolId, SystemConstant.QUESTION_HOST_URL);
+        Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置题库地址"));
+        BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
+        return SystemConstant.getHost(sysConfig.getConfigValue(), basicSchool.getCode());
+    }
+
     /**
      * 签名
      *

+ 65 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/HttpUtil.java

@@ -224,4 +224,69 @@ public class HttpUtil {
         }
         return file;
     }
+
+    /**
+     * 下载文件
+     *
+     * @param url       url
+     * @param params    参数
+     * @param secret    鉴权
+     * @param timestamp 时间戳
+     * @param filePath  文件完整路径
+     */
+    public static File postDownload(String url, Map<String, Object> params, String secret, Long timestamp, String filePath) throws IOException {
+        // 构建post请求
+        HttpPost post = new HttpPost(url);
+        post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
+        post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
+        post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=utf-8");
+        // 构建请求参数
+        List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
+        if (params != null) {
+            for (String key : params.keySet()) {
+                pairs.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
+            }
+        }
+        HttpEntity entity = null;
+        try {
+            entity = new UrlEncodedFormEntity(pairs, SystemConstant.CHARSET_NAME);
+        } catch (UnsupportedEncodingException e) {
+            log.error(SystemConstant.LOG_ERROR, e);
+        }
+        post.setEntity(entity);
+        // 执行请求,获取响应
+        return getRespFile(post, filePath);
+    }
+
+    /**
+     * 获取响应信息
+     *
+     * @param request
+     * @return
+     */
+    public static File getRespFile(HttpUriRequest request, String filePath) throws IOException {
+        // 获取响应流
+        InputStream is = null;
+        ByteArrayOutputStream ou = null;
+        File file = null;
+        try {
+            is = getRespInputStream(request);
+            file = new File(filePath);
+            if (!file.exists()) {
+                file.getParentFile().mkdirs();
+                file.createNewFile();
+            }
+            FileUtils.copyInputStreamToFile(is, file);
+        } catch (Exception e) {
+            log.error(SystemConstant.LOG_ERROR, e);
+        } finally {
+            if (Objects.nonNull(is)) {
+                is.close();
+            }
+            if (Objects.nonNull(ou)) {
+                ou.close();
+            }
+        }
+        return file;
+    }
 }