|
@@ -3,22 +3,21 @@ package com.ssj.service.kmt.kmt.service.impl;
|
|
|
import com.alibaba.dubbo.config.annotation.Service;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.ssj.bean.kmt.kmt.domain.KmtExerciseBook;
|
|
|
import com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion;
|
|
|
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
|
|
|
import com.ssj.dao.weixin.library.dao.IBookManagerDao;
|
|
|
import com.ssj.es.service.ExerciseQuestionSearchService;
|
|
|
-import com.ssj.framework.core.util.PropertiesUtil;
|
|
|
-import com.ssj.framework.core.util.ResponseConstant;
|
|
|
-import com.ssj.framework.core.util.ResponseEntity;
|
|
|
-import com.ssj.framework.core.util.StringUtil;
|
|
|
+import com.ssj.framework.core.util.*;
|
|
|
import com.ssj.framework.idworker.IdWorker;
|
|
|
import com.ssj.framework.weixin.util.DateUtil;
|
|
|
import com.ssj.kmt.utils.JSONFileUtil;
|
|
|
import com.ssj.service.kmt.constant.KnowledgeVersion;
|
|
|
import com.ssj.service.kmt.dto.exercise.ExerciseBookEditDto;
|
|
|
import com.ssj.service.kmt.dto.exercise.ExerciseQuestionEditDto;
|
|
|
+import com.ssj.service.kmt.request.questionmark.QuestionNumMarkRemoveVo;
|
|
|
import com.ssj.service.kmt.request.rollbackai.AddExerciseQuestionRequest;
|
|
|
import com.ssj.service.kmt.request.rollbackai.ExerciseBooksEditRequest;
|
|
|
import com.ssj.service.kmt.request.rollbackai.ExerciseQuestionDealRequest;
|
|
@@ -34,6 +33,7 @@ import com.ssj.taskthread.ThreadPool;
|
|
|
import com.ssj.utils.ExerciseUtil;
|
|
|
import com.ssj.utils.SortByChineseUtil;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
@@ -41,6 +41,14 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
import java.util.*;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
@@ -198,6 +206,8 @@ public class KmtExerciseQuestionDealServiceImpl implements KmtExerciseQuestionDe
|
|
|
}
|
|
|
if (list.size() > 0){
|
|
|
result = list.get(0).replace(PropertiesUtil.getValue("STATIC_FILE_URL"), "");
|
|
|
+ //测试环境图片可能用的是正式的图片,再做一次replace
|
|
|
+ result = result.replace("https://img.sharingschool.com", "");
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
@@ -528,4 +538,154 @@ public class KmtExerciseQuestionDealServiceImpl implements KmtExerciseQuestionDe
|
|
|
data.put("list", list);
|
|
|
return new ResponseEntity().success(data, "成功");
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseEntity removeQuestionNumMark(String userId, QuestionNumMarkRemoveVo request) {
|
|
|
+ ResponseEntity responseEntity = new ResponseEntity();
|
|
|
+ if (request.getStartX() < 0 || request.getStartY() < 0 || request.getEndX() < 0 || request.getEndY() < 0){
|
|
|
+ return responseEntity.failure("请把坐标定位在图片内");
|
|
|
+ }
|
|
|
+
|
|
|
+ KmtExerciseQuestion kmtExerciseQuestion = kmtExerciseQuestionService.getById(request.getQuestionId());
|
|
|
+ kmtExerciseMapper.updateQuestionStartPointAndEndPoint(request.getStartX(), request.getStartY(),
|
|
|
+ request.getEndX(), request.getEndY(), request.getQuestionId());
|
|
|
+ TbLibManager manager = bookManagerDao.findByUserIdAndType(userId, 18);
|
|
|
+ if (manager != null && kmtExerciseQuestion != null && kmtExerciseQuestion.getDealStatus() > 3 && kmtExerciseQuestion.getDealStatus() != 7) {
|
|
|
+ String path = this.removeQuestionNumMark(request.getStartX(), request.getStartY(), request.getEndX(), request.getEndY(), kmtExerciseQuestion.getImage());
|
|
|
+ String content = JSONFileUtil.readJSONFile(PropertiesUtil.getValue("EXERCISE_JSON_FILE_PATH") + kmtExerciseQuestion.getQuestionPath());
|
|
|
+ if (StringUtil.isNotBlank(content) && StringUtil.isNotBlank(path)) {
|
|
|
+ Map<String, Object> map = JSON.parseObject(content, Map.class);
|
|
|
+ map.put("question_json", Collections.singletonList(ExerciseUtil.getExerciseQuestionAnswerValue(path)));
|
|
|
+ map.put("question", "<img src='" + PropertiesUtil.getValue("STATIC_FILE_URL") + path + "'/>");
|
|
|
+ JSONFileUtil.saveJSONFile(PropertiesUtil.getValue("EXERCISE_JSON_FILE_PATH") + kmtExerciseQuestion.getQuestionPath(), JSON.toJSONString(map));
|
|
|
+ ThreadPool.getInstance().addTask(new SyncSaveExerciseQuestionsTask(kmtExerciseMapper, exerciseQuestionSearchService, Collections.singletonList(kmtExerciseQuestion.getId())));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return responseEntity.success("成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String removeQuestionNumMark(int startX, int startY, int endX, int endY, String filePath) {
|
|
|
+ int temp = 0;
|
|
|
+ if (startX > endX){
|
|
|
+ temp = startX;
|
|
|
+ startX = endX;
|
|
|
+ endX = temp;
|
|
|
+ }
|
|
|
+ if (startY > endY){
|
|
|
+ temp = startY;
|
|
|
+ startY = endY;
|
|
|
+ endY = temp;
|
|
|
+ }
|
|
|
+ String result = null;
|
|
|
+ if (StringUtil.isNotBlank(filePath) && endX > 0 && endY > 0){
|
|
|
+ //1、查询是否有备份图片,没有的话新增一个备份图片
|
|
|
+ //this.checkBakImage(filePath);
|
|
|
+ //2、生成一个临时图片, 图片区域填充, 临时图片要随机, 把图片上传到oss
|
|
|
+ result = this.createNewFile(startX, startY, endX, endY, filePath);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查是否有备份图片
|
|
|
+ * @param relatePath
|
|
|
+ */
|
|
|
+ private void checkBakImage (String relatePath){
|
|
|
+ String bakParentPath = "/www/files/data/upload/ai/picture_bak/"; //备份图片父级目录
|
|
|
+ if (relatePath.indexOf("/ai/results") != -1){
|
|
|
+ String filePath = relatePath.replace("/ai/results/", bakParentPath); //备份图片绝对路径
|
|
|
+ //如果目录不存在,新建目录,如果存在,判断文件是否存在
|
|
|
+ String dir = filePath.substring(0, filePath.lastIndexOf("/") + 1);
|
|
|
+ File dirFile = new File(dir);
|
|
|
+ if (!dirFile.exists()){
|
|
|
+ dirFile.mkdirs();
|
|
|
+ }
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()){
|
|
|
+ OSSUtils.downloadFile(filePath, relatePath.substring(1));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("图片路径不存在 = {}", relatePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String createNewFile (int startX, int startY, int endX, int endY, String relatePath) {
|
|
|
+ String prefix = PropertiesUtil.getValue("file_save_path");
|
|
|
+ String dir = relatePath.substring(0, relatePath.lastIndexOf("/") + 1);
|
|
|
+ String fileName = relatePath.substring(relatePath.lastIndexOf("/") + 1, relatePath.lastIndexOf("."));
|
|
|
+ String resultFilePath = prefix + dir + "result_" + fileName + relatePath.substring(relatePath.lastIndexOf("."));
|
|
|
+// OSSUtils.downloadFile(resultFilePath, relatePath.substring(1));
|
|
|
+ boolean result = drawImagePartColorByPosition(startX, startY, endX, endY, resultFilePath, relatePath);
|
|
|
+ if (result){
|
|
|
+ OSSUtils.uploadFile(resultFilePath, resultFilePath.replace(prefix + "/", ""));
|
|
|
+ return resultFilePath.replace(prefix, "");
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 区域颜色填充,以左上角颜色填充
|
|
|
+ * @param minx
|
|
|
+ * @param miny
|
|
|
+ * @param maxx
|
|
|
+ * @param maxy
|
|
|
+ * @param filePath
|
|
|
+ */
|
|
|
+ public boolean drawImagePartColorByPosition (int minx, int miny, int maxx, int maxy, String filePath, String relatePath) {
|
|
|
+ BufferedImage bi = null;
|
|
|
+ boolean result = true;
|
|
|
+ try {
|
|
|
+ //有宽高的才处理
|
|
|
+ if (maxx > 0 && maxy > 0){
|
|
|
+ String prefix = PropertiesUtil.getValue("OSS_INTERNAL_PATH");
|
|
|
+// String prefix = "https://xt-sharingschool.oss-cn-shenzhen.aliyuncs.com";
|
|
|
+ bi = ImageIO.read(getImageStream(prefix + relatePath));
|
|
|
+ int pixel = bi.getRGB(minx, miny); //左上角像素值
|
|
|
+// int pixel = -256;
|
|
|
+ for (int i = minx; i < maxx; i++) {
|
|
|
+ for (int j = miny; j < maxy; j++) {
|
|
|
+ bi.setRGB(i, j, pixel);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ File file = new File(filePath);
|
|
|
+ String dirPath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
|
|
|
+ File dir = new File(dirPath);
|
|
|
+ if (!dir.exists()){
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream ops = new FileOutputStream(file);
|
|
|
+ ImageIO.write(bi, filePath.substring(filePath.lastIndexOf(".") + 1), ops);
|
|
|
+ ops.flush();
|
|
|
+ ops.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("异常", e);
|
|
|
+ result = false;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取网络图片流
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private InputStream getImageStream(String url) {
|
|
|
+ try {
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
+ connection.setReadTimeout(5000);
|
|
|
+ connection.setConnectTimeout(5000);
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ InputStream inputStream = connection.getInputStream();
|
|
|
+ return inputStream;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("获取网络图片出现异常,图片路径为:" + url);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|