xiatian 2 년 전
부모
커밋
473883fcd9

+ 10 - 84
src/main/java/cn/com/qmth/export/Answer.java

@@ -1,18 +1,9 @@
 package cn.com.qmth.export;
 
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.commons.lang3.StringUtils;
-import org.jsoup.Jsoup;
-import org.jsoup.nodes.Document;
-import org.jsoup.nodes.Element;
-import org.jsoup.nodes.Node;
-
 public class Answer {
-	private Long id;
+	private Long questionId;
 	private String body;
-	private Double score;
+	private Integer right;
 
 	public String getBody() {
 		return body;
@@ -22,85 +13,20 @@ public class Answer {
 		this.body = body;
 	}
 
-	public Double getScore() {
-		return score;
-	}
-
-	public void setScore(Double score) {
-		this.score = score;
+	public Integer getRight() {
+		return right;
 	}
 
-	public Long getId() {
-		return id;
+	public void setRight(Integer right) {
+		this.right = right;
 	}
 
-	public void setId(Long id) {
-		this.id = id;
+	public Long getQuestionId() {
+		return questionId;
 	}
 
-	public static void main(String[] args) {
-		String s="<p><span style=\"color: #4e4e4e; font-family: -apple-system, system-ui, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 15px; white-space: nowrap; background-color: #e4e4e4;\">为知笔记</span></p>";
-		System.out.println(disBody(s));
-	}
-
-	private static String disBody(String html) {
-		Document doc = Jsoup.parse(html);
-		Element b = doc.body();
-		if (b.childrenSize()== 1&&"p".equals(b.child(0).nodeName())) {
-			b.child(0).tagName("span");
-			b.child(0).removeAttr("style");
-		}
-		if (b.childrenSize()> 0) {
-			for (Element n : b.children()) {
-				if("p".equals(n.nodeName())) {
-					boolean[] find=new boolean[] {false};
-					findImg(n, find);
-					if(!find[0]&&StringUtils.isBlank(n.text())) {
-						n.tagName("span");
-					}
-				}
-				disNode(n);
-			}
-		}
-		return b.html();
-	}
-	
-	private static void findImg(Element e,boolean[] find) {
-		if(find[0]) {
-			return;
-		}else {
-			if("img".equals(e.nodeName())) {
-				find[0]=true;
-				return;
-			}else {
-				if(e.childrenSize()>0) {
-					for(Element ce:e.children()) {
-						findImg(ce, find);
-					}
-				}else {
-					return;
-				}
-			}
-		}
-	}
-	private static void disNode(Node n) {
-		if (n instanceof Element) {
-			if (!"img".equals(n.nodeName())) {
-				n.removeAttr("style");
-			}
-			if ("h1".equals(n.nodeName()) || "h2".equals(n.nodeName()) || "h3".equals(n.nodeName())
-					|| "h4".equals(n.nodeName()) || "h5".equals(n.nodeName())) {
-				((Element) n).tagName("h6");
-			}
-			if ("strong".equals(n.nodeName())||"a".equals(n.nodeName())||"u".equals(n.nodeName())||"em".equals(n.nodeName())) {
-				((Element) n).tagName("span");
-			}
-			if (n.childNodeSize() > 0) {
-				for (Node cn : n.childNodes()) {
-					disNode(cn);
-				}
-			}
-		}
+	public void setQuestionId(Long questionId) {
+		this.questionId = questionId;
 	}
 
 }

+ 0 - 22
src/main/java/cn/com/qmth/export/AnswerOb.java

@@ -1,22 +0,0 @@
-package cn.com.qmth.export;
-
-import java.util.List;
-
-public class AnswerOb {
-	private List<String> subArea;
-	private List<String> answer;
-	public List<String> getSubArea() {
-		return subArea;
-	}
-	public void setSubArea(List<String> subArea) {
-		this.subArea = subArea;
-	}
-	public List<String> getAnswer() {
-		return answer;
-	}
-	public void setAnswer(List<String> answer) {
-		this.answer = answer;
-	}
-	
-	
-}

+ 4 - 4
src/main/java/cn/com/qmth/export/Basket.java

@@ -48,9 +48,9 @@ public class Basket {
 			LOG.error("**********************offer isExcuteError threadId:"+Thread.currentThread().getId());
 			throw new StatusException("1000001","线程异常");
 		}else {
-			boolean ret=queue.offer(value, 1, TimeUnit.MINUTES);
+			boolean ret=queue.offer(value, 10, TimeUnit.SECONDS);
 			if(!ret) {
-				LOG.info("**********************offer time out threadId:"+Thread.currentThread().getId()+value);
+//				LOG.info("**********************offer time out threadId:"+Thread.currentThread().getId()+value);
 				this.offer(value);
 			}
 		}
@@ -65,9 +65,9 @@ public class Basket {
 			LOG.error("**********************poll isExcuteError  threadId:"+Thread.currentThread().getId());
 			return new EndObject();
 		}else {
-			Object ob=queue.poll(1, TimeUnit.MINUTES);
+			Object ob=queue.poll(10, TimeUnit.SECONDS);
 			if(ob==null) {
-				LOG.info("**********************poll time out  threadId:"+Thread.currentThread().getId());
+//				LOG.info("**********************poll time out  threadId:"+Thread.currentThread().getId());
 				return this.consume();
 			}else {
 				return ob;

+ 53 - 0
src/main/java/cn/com/qmth/export/BatchGetDataUtil.java

@@ -0,0 +1,53 @@
+package cn.com.qmth.export;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.collections4.CollectionUtils;
+
+/**
+ *	多次批量获取数据
+ * @author xiatian
+ * @param <R> 结果类
+ * @param <P> 参数类
+ */
+public abstract  class BatchGetDataUtil<R,P> {
+	/**
+	 * @param resultList 全部结果集合
+	 * @param paramList 全部参数集合
+	 * @param batchSize 每批参数数量
+	 */
+	public final List<R> getDataForBatch(List<P> paramList,int batchSize) {
+		if(CollectionUtils.isEmpty(paramList)) {
+			return null;
+		}
+		List<R> resultList=new ArrayList<>();
+		if(paramList.size()<=batchSize) {
+			List<R> temlist = getData(paramList);
+			if(temlist!=null&&temlist.size()>0) {
+				resultList.addAll(temlist);
+			}
+		}else {
+			int size = paramList.size();
+			int len=batchSize;
+			int count = (size + len - 1) / len;
+
+			for (int i = 0; i < count; i++) {
+				List<P> subList = paramList.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
+				List<R> temlist = getData(subList);
+				if(temlist!=null&&temlist.size()>0) {
+					resultList.addAll(temlist);
+				}
+			}
+		}
+		return resultList;
+	}
+	/**
+	 * 	每批获取数据方法
+	 * @param <R>
+	 * @param <P>
+	 * @param paramList 获取每批数据时参数
+	 * @return
+	 */
+	protected abstract  List<R> getData(List<P> paramList);
+}

+ 1 - 1
src/main/java/cn/com/qmth/export/Consumer.java

@@ -35,7 +35,7 @@ public abstract class Consumer<T>  extends Thread{
 				}
 				@SuppressWarnings("unchecked")
 				T t=(T)o;
-				LOG.info("*******************Consumer:"+Thread.currentThread().getId()+" consume");
+//				LOG.info("*******************Consumer:"+Thread.currentThread().getId()+" consume");
 				//消费数据实现
 				consume(t);
 			}

+ 10 - 30
src/main/java/cn/com/qmth/export/Course.java

@@ -1,20 +1,16 @@
 package cn.com.qmth.export;
 
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
 public class Course {
-	private String name;
-	private String code;
-	private String idnumber;
 	private Long id;
+	private String code;
+	private String name;
 
-	public String getName() {
-		return name;
+	public Long getId() {
+		return id;
 	}
 
-	public void setName(String name) {
-		this.name = name;
+	public void setId(Long id) {
+		this.id = id;
 	}
 
 	public String getCode() {
@@ -25,28 +21,12 @@ public class Course {
 		this.code = code;
 	}
 
-	public Long getId() {
-		return id;
-	}
-
-	public void setId(Long id) {
-		this.id = id;
-	}
-
-	public String getIdnumber() {
-		return idnumber;
-	}
-
-	public void setIdnumber(String idnumber) {
-		this.idnumber = idnumber;
+	public String getName() {
+		return name;
 	}
 
-	public static void main(String[] args) {
-		String s = "<span><span>已知<span lang=\"EN-US\">A=<img src=\"@@PLUGINFILE@@/4.png?time=1594114072778\" alt=\"\" width=\"75\" height=\"54\"></span></span><span>,<span lang=\"EN-US\">B=<img src=\"@@PLUGINFILE@@/5.png?time=1594114090754\" alt=\"\" width=\"93\" height=\"48\"></span></span><span>,下列运算可行的是(&nbsp; &nbsp; &nbsp; )。&nbsp;</span></span>";
-		String k="@@PLUGINFILE@@/4.png?time=1594114072778";
-		k=k.replaceAll("\\?", "\\\\?");
-		System.out.println(k);
-		System.out.println(s.replaceAll(k, "AAAAAA"));
+	public void setName(String name) {
+		this.name = name;
 	}
 
 }

+ 0 - 174
src/main/java/cn/com/qmth/export/CreatePaperByExcel.java

@@ -1,174 +0,0 @@
-package cn.com.qmth.export;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.poi.xssf.usermodel.XSSFRow;
-import org.apache.poi.xssf.usermodel.XSSFSheet;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-
-import com.alibaba.fastjson.JSONObject;
-
-public class CreatePaperByExcel {
-	private static String paperSuff = "(211)";
-	private static int maxqc = 100;
-	private static String excelDir = "D:\\kd_excel_export\\";
-	public static void main(String[] args) {
-		try {
-			File excelFolder = new File(excelDir);
-			if (excelFolder.exists()) {
-				FileUtil.clearDirectory(excelDir);
-			} else {
-				excelFolder.mkdir();
-			}
-			Course c=new Course();
-			c.setName("汽车维修基本技能");
-			c.setCode("10613");
-			List<KdQuestion> qs=getQus(read());
-			System.out.println("小题数量:"+qs.size());
-			List<KdQuestion> single = new ArrayList<>();
-			List<KdQuestion> muti = new ArrayList<>();
-			List<KdQuestion> boo = new ArrayList<>();
-			for (KdQuestion q : qs) {
-				if (q.getStructType() == 1) {
-					single.add(q);
-				} else if (q.getStructType() == 2) {
-					muti.add(q);
-				} else if (q.getStructType() == 3) {
-					boo.add(q);
-				}
-			}
-			createPapers(single, c, 1);
-			createPapers(muti, c, 2);
-			createPapers(boo, c, 3);
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-	}
-	private static void createPapers(List<KdQuestion> qs, Course c, int structType) throws IOException {
-		if (qs == null || qs.size() == 0) {
-			return;
-		}
-		if (qs.size() <= maxqc) {
-			createPaper(qs, c, structType, 1);
-		} else {
-			int size = qs.size();
-			int len = maxqc;
-			int count = (size + len - 1) / len;
-
-			for (int i = 0; i < count; i++) {
-				List<KdQuestion> subList = qs.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
-				createPaper(subList, c, structType, i + 1);
-			}
-		}
-
-	}
-
-	private static void createPaper(List<KdQuestion> qs, Course c, int structType, int indx) throws IOException {
-		if (qs.size() == 0) {
-			return;
-		}
-		String detailName="";
-		if (structType == 1) {
-			detailName="单选题";
-		} else if (structType == 2) {
-			detailName="多选题";
-		} else if (structType == 3) {
-			detailName="判断题";
-		}
-		KdPaper paper = new KdPaper();
-		paper.setTotalScore((double) qs.size());
-		paper.setName(c.getName()+paperSuff+detailName+"_"+indx);
-		paper.setCourseCode(c.getCode());
-		List<KdDetail> des = new ArrayList<>();
-		KdDetail d = new KdDetail();
-		d.setName(detailName);
-		d.setNumber(1);
-		d.setQuestionCount(qs.size());
-		d.setTotalScore((double) qs.size());
-		d.setQuestions(qs);
-		des.add(d);
-		paper.setDetails(des);
-		paper.setDetailCount(1);
-		File paperdir = new File(excelDir + c.getCode() + "\\paper"+structType+"_"+indx+"\\");
-		paperdir.mkdirs();
-		FileUtil.writeFile(paperdir.getAbsolutePath(), "\\paper.json", JSONObject.toJSONString(paper));
-	}
-	public static List<KdQuestion> getQus(List<Question> qs) {
-		List<KdQuestion> kqs = new ArrayList<>();
-		for (Question q : qs) {
-			KdQuestion kq = new KdQuestion();
-			kqs.add(kq);
-			kq.setBody(q.getBody());
-			AnswerOb ao = JSONObject.parseObject(q.getAnswer(), AnswerOb.class);
-			if ("判断题".equals(q.getType().trim())) {
-				kq.setStructType(3);
-				if("1".equals(ao.getAnswer().get(0).trim())) {
-					kq.setAnswer("true");
-				}else {
-					kq.setAnswer("false");
-				}
-
-			} else if ("单项选择题".equals(q.getType().trim()) || "多项选择题".equals(q.getType().trim())) {
-				int index = 0;
-				List<KdQuesOption> ops = new ArrayList<>();
-				StringBuilder sb = new StringBuilder();
-				for (String a : ao.getSubArea()) {
-					index++;
-					KdQuesOption op = new KdQuesOption();
-					op.setNumber(index);
-					op.setBody(a);
-					if (ao.getAnswer().contains((index-1)+"")) {
-						op.setSelect(true);
-						sb.append(index).append(",");
-					} else {
-						op.setSelect(false);
-					}
-					ops.add(op);
-				}
-				kq.setOptions(ops);
-				if (sb.length() > 0) {
-					sb.deleteCharAt(sb.length() - 1);
-				}
-				if (sb.indexOf(",") > 0) {
-					kq.setStructType(2);
-				} else {
-					kq.setStructType(1);
-				}
-				kq.setAnswer(sb.toString());
-			} else {
-				throw new RuntimeException("错误");
-			}
-		}
-		return kqs;
-	}
-
-	public static List<Question> read() throws IOException {
-		List<Question> list = new ArrayList<>();
-		XSSFWorkbook wb = null;
-		try {
-			wb = new XSSFWorkbook("d:\\dz.xlsx");
-			XSSFSheet sheet = wb.getSheetAt(0);
-			int rows = sheet.getLastRowNum();
-			for (int i = 1; i <= rows; i++) {
-				Question q = new Question();
-				XSSFRow row = sheet.getRow(i);
-				String b = row.getCell(1).getStringCellValue().trim();
-				String a = row.getCell(2).getStringCellValue().trim();
-				String t = row.getCell(4).getStringCellValue().trim();
-				q.setBody(b);
-				q.setAnswer(a);
-				q.setType(t);
-				list.add(q);
-			}
-		} finally {
-			if (wb != null) {
-				wb.close();
-			}
-		}
-		return list;
-	}
-}

+ 7 - 97
src/main/java/cn/com/qmth/export/ExportByCourseCode.java

@@ -1,121 +1,31 @@
 package cn.com.qmth.export;
 
-import java.io.File;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.Date;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import org.apache.commons.io.FileUtils;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 
-import com.google.common.base.Joiner;
-
 public class ExportByCourseCode {
 	private static Logger logger = LogManager.getLogger(ExportByCourseCode.class);
-	private static String notFoundDir = "D:\\not_find\\";
-	private static AtomicInteger count=new AtomicInteger(0);	
-	private static AtomicInteger fdcount=new AtomicInteger(0);
-	private static AtomicInteger notfdcount=new AtomicInteger(0);
-	
-	private  static List<String> qdb = new ArrayList<>();
-	private  static List<String> qzip = new ArrayList<>();
-	private  static List<String> adb = new ArrayList<>();
-	private  static List<String> azip = new ArrayList<>();
-	private  static List<String> http = new ArrayList<>();
-	private  static List<String> filepath = new ArrayList<>();
-	private  static Set<String> noques = new HashSet<>();
+	private static AtomicInteger count=new AtomicInteger(0);
 	
-	
-	private  static Set<Long> invalidAnswerCode = new HashSet<>();
-	@SuppressWarnings({ "rawtypes", "unchecked" })
 	public static void main(String[] args) {
 		logger.debug("导出开始");
+		Date start=new Date();
 		try {
-			File excelFolder = new File(notFoundDir);
-			if (excelFolder.exists()) {
-				FileUtil.clearDirectory(notFoundDir);
-			} else {
-				excelFolder.mkdir();
-			}
 			MyProducer pro=new MyProducer();
-			pro.startDispose(MyConsumer.class, 8, null);
-			for(Consumer co:pro.getConsumers()) {
-				qdb.addAll((List<String>)co.getResult().get("qdb"));
-				qzip.addAll((List<String>)co.getResult().get("qzip"));
-				adb.addAll((List<String>)co.getResult().get("adb"));
-				azip.addAll((List<String>)co.getResult().get("azip"));
-				http.addAll((List<String>)co.getResult().get("http"));
-				filepath.addAll((List<String>)co.getResult().get("file"));
-				noques.addAll((Set<String>)co.getResult().get("noques"));
-				invalidAnswerCode.addAll((Set<Long>)co.getResult().get("invalidAnswerCode"));
-			}
-			logger.debug("===============================================小题数据库没有文件:" + qdb.size());
-			if (qdb.size() > 0) {
-				File file=new File(notFoundDir+"\\题干在数据库中找不到文件.txt");
-				file.createNewFile();
-				FileUtils.writeStringToFile(file, "questionid idnumber source", "utf-8", true);
-				FileUtils.writeLines(file, qdb,true);
-			}
-			logger.debug("===============================================小题zip没有文件:" + qzip.size());
-			if (qzip.size() > 0) {
-				File file=new File(notFoundDir+"\\题干在zip中找不到文件.txt");
-				file.createNewFile();
-				FileUtils.writeStringToFile(file, "questionid idnumber source", "utf-8", true);
-				FileUtils.writeLines(file, qzip,true);
-			}
-			logger.debug("===============================================答案数据库没有文件" + adb.size());
-			if (adb.size() > 0) {
-				File file=new File(notFoundDir+"\\答案在数据库中找不到文件.txt");
-				file.createNewFile();
-				FileUtils.writeStringToFile(file, "answerid idnumber source", "utf-8", true);
-				FileUtils.writeLines(file, adb,true);
-			}
-			logger.debug("===============================================答案zip没有文件:" + azip.size());
-			if (azip.size() > 0) {
-				File file=new File(notFoundDir+"\\答案在zip中找不到文件.txt");
-				file.createNewFile();
-				FileUtils.writeStringToFile(file, "answerid idnumber source", "utf-8", true);
-				FileUtils.writeLines(file, azip,true);
-			}
-			logger.debug("===============================================http连接数:" + http.size());
-			if (http.size() > 0) {
-				File file=new File(notFoundDir+"\\http连接.txt");
-				file.createNewFile();
-				FileUtils.writeLines(file, http,true);
-			}
-			logger.debug("===============================================file连接数:" + filepath.size());
-			if (filepath.size() > 0) {
-				File file=new File(notFoundDir+"\\file连接.txt");
-				file.createNewFile();
-				FileUtils.writeLines(file, filepath,true);
-			}
-			logger.debug("===============================================内容空白校验:" + invalidAnswerCode.size());
-			if (invalidAnswerCode.size() > 0) {
-				logger.debug(Joiner.on(",").join(invalidAnswerCode));
-			}
-			logger.debug("===============================================没有小题:" + noques.size());
-			if (noques.size() > 0) {
-				logger.debug(Joiner.on(",").join(noques));
-			}
-			logger.debug("===============================================找到:" + fdcount+" 未找到:"+notfdcount);
+			pro.startDispose(MyConsumer.class, 10, null);
 		} catch (Exception e) {
 			logger.error(e.getCause(), e);
 		}
-		logger.debug("导出结束");
+		Date end=new Date();
+		logger.debug("导出结束,耗时:"+((end.getTime()-start.getTime())/1000));
 	}
 
+
 	public static void  addDisposeCount() {
 		count.addAndGet(1);
 		logger.debug("处理了"+count);
 	}
-	
-	public static void addFd() {
-		fdcount.addAndGet(1);
-	}
-	public static void addNotFd() {
-		notfdcount.addAndGet(1);
-	}
 }

+ 0 - 8
src/main/java/cn/com/qmth/export/KdQuesOption.java

@@ -1,7 +1,6 @@
 package cn.com.qmth.export;
 
 public class KdQuesOption {
-	private Long answerId;
 	private Boolean select;
 	private Integer number;
 	private String body;
@@ -23,12 +22,5 @@ public class KdQuesOption {
 	public void setSelect(Boolean select) {
 		this.select = select;
 	}
-	public Long getAnswerId() {
-		return answerId;
-	}
-	public void setAnswerId(Long answerId) {
-		this.answerId = answerId;
-	}
-	
 	
 }

+ 50 - 2
src/main/java/cn/com/qmth/export/KdQuestion.java

@@ -5,74 +5,122 @@ import java.util.List;
 public class KdQuestion {
 	private Long id;
 	private Integer number;
+	private YunkaiQuesStructType qst;
 	private Integer structType;
 	private Boolean objective;
+	private YunkaiDifficulty difficulty;
 	private String body;
 	private String answer;
 	private String qtype;
 	private Boolean haveAudio;
 	private List<KdQuesOption> options;
+	private List<Long> propIds;
 	private Boolean valid;
+
 	public Integer getNumber() {
 		return number;
 	}
+
 	public void setNumber(Integer number) {
 		this.number = number;
 	}
+
 	public Integer getStructType() {
 		return structType;
 	}
+
 	public void setStructType(Integer structType) {
 		this.structType = structType;
 	}
+
 	public Boolean getObjective() {
 		return objective;
 	}
+
 	public void setObjective(Boolean objective) {
 		this.objective = objective;
 	}
+
 	public String getBody() {
 		return body;
 	}
+
 	public void setBody(String body) {
 		this.body = body;
 	}
+
 	public String getAnswer() {
 		return answer;
 	}
+
 	public void setAnswer(String answer) {
 		this.answer = answer;
 	}
+
 	public List<KdQuesOption> getOptions() {
 		return options;
 	}
+
 	public void setOptions(List<KdQuesOption> options) {
 		this.options = options;
 	}
+
 	public Long getId() {
 		return id;
 	}
+
 	public void setId(Long id) {
 		this.id = id;
 	}
+
 	public String getQtype() {
 		return qtype;
 	}
+
 	public void setQtype(String qtype) {
 		this.qtype = qtype;
 	}
+
 	public Boolean getHaveAudio() {
 		return haveAudio;
 	}
+
 	public void setHaveAudio(Boolean haveAudio) {
 		this.haveAudio = haveAudio;
 	}
+
 	public Boolean getValid() {
 		return valid;
 	}
+
 	public void setValid(Boolean valid) {
 		this.valid = valid;
 	}
-	
-	
+
+
+	public List<Long> getPropIds() {
+		return propIds;
+	}
+
+	public void setPropIds(List<Long> propIds) {
+		this.propIds = propIds;
+	}
+
+	public YunkaiQuesStructType getQst() {
+		return qst;
+	}
+
+	public void setQst(YunkaiQuesStructType qst) {
+		this.qst = qst;
+	}
+
+	public YunkaiDifficulty getDifficulty() {
+		return difficulty;
+	}
+
+	public void setDifficulty(YunkaiDifficulty difficulty) {
+		this.difficulty = difficulty;
+	}
+
+
 }

+ 251 - 678
src/main/java/cn/com/qmth/export/MyConsumer.java

@@ -2,8 +2,6 @@ package cn.com.qmth.export;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
@@ -11,60 +9,41 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
+import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.jsoup.Jsoup;
-import org.jsoup.nodes.Document;
-import org.jsoup.nodes.Element;
-import org.jsoup.nodes.Node;
 
 import com.alibaba.fastjson.JSONObject;
 
-public class MyConsumer extends Consumer<String> {
-	private String paperSuff = "(211)";
-	private int maxqc = 100;
+public class MyConsumer extends Consumer<Course> {
+	private static String paperSuff = "(230517)";
+	private int maxqc = 200;
 
-	private String excelDir = "D:\\kd_export\\";
-	private String picFiles = "D:\\files\\";
+	private static String paperDir = "d:/yunkai/paper/";
 
-	private Pattern mp3Pat = Pattern.compile("<a[^<]+href=\"([^<\"]+\\.mp3)\"[^<]*>[^<]*</a>");
 	private Pattern imgPat = Pattern.compile("<img[^<]+src=\"([^<\"]+)\"[^<]*>");
 
-	private Pattern notmp3Pat = Pattern.compile("<a[^<]+href=\"[^<\"]+(?!\\.mp3)\"[^<]*>([^<]*)</a>");
-
-	private List<String> qdb = new ArrayList<>();
-	private List<String> qzip = new ArrayList<>();
-	private List<String> adb = new ArrayList<>();
-	private List<String> azip = new ArrayList<>();
-	private List<String> http = new ArrayList<>();
-	private List<String> file = new ArrayList<>();
-
-	private Set<String> noques = new HashSet<>();
-
-	private Set<Long> invalidAnswerCode = new HashSet<>();
-
 	@Override
-	public void consume(String code) {
+	public void consume(Course course) {
 		Connection connect = null;
-
+		File sub = new File(paperDir + course.getId() + "/");
+		sub.mkdir();
 		try {
 			Class.forName("com.mysql.cj.jdbc.Driver");
 
-			String url = "jdbc:mysql://210.38.32.28:3306/moodle_question?serverTimezone=GMT%2B8";
+			String url = "jdbc:mysql://localhost:3306/yunkai_question?serverTimezone=GMT%2B8";
 
-			String user = "questionreader";
+			String user = "root";
 
-			String password = "xhhG88tdsk4Rvbgd";
+			String password = "123456";
 			connect = DriverManager.getConnection(url, user, password);
-			exportPaper(connect, code);
+			exportPaper(connect, course);
 			ExportByCourseCode.addDisposeCount();
 		} catch (Exception e) {
 			throw new RuntimeException(e);
@@ -78,27 +57,32 @@ public class MyConsumer extends Consumer<String> {
 		}
 	}
 
-	private Course getCourse(Connection connect, String code) throws SQLException, IOException {
-		Course c = new Course();
+	private List<KdQuestion> getQuestion(Connection connect, Course course) throws SQLException, IOException {
+		List<KdQuestion> qs = new ArrayList<>();
 		PreparedStatement preState = null;
 		ResultSet resultSet = null;
 		try {
-			String sql = "select * from mdl_course where idnumber like '" + code + "_%';";
+			String sql = " select q.id,q.question_type,q.topic,q.difficulty,q.answer "
+					+ " from wq_question_bank_subject t " + " left join wq_question_bank b on t.question_bank_id=b.id "
+					+ " left join wq_question_question_bank f on t.question_bank_id=f.question_bank_id "
+					+ " left join wq_question q on f.question_id=q.id " + " where t.subject_id=" + course.getId()
+					+ " and b.rent_id=811 and b.is_deleted=0 " + " and q.is_deleted=0 and q.rent_id=811 ";
+
 			preState = connect.prepareStatement(sql);
 
 			resultSet = preState.executeQuery();
-			int count = 0;
+
 			while (resultSet.next()) {
-				count++;
-				if (count > 1) {
-					return null;
-				}
-				c.setId(resultSet.getLong("id"));
-				c.setName(resultSet.getString("shortname"));
-				c.setIdnumber(resultSet.getString("idnumber"));
-				c.setCode(code);
+				KdQuestion q = new KdQuestion();
+				q.setId(resultSet.getLong("id"));
+				q.setBody(disposeImg(resultSet.getString("topic"), course.getId()));
+				q.setQst(YunkaiQuesStructType.getByYunKaiType(resultSet.getInt("question_type")));
+				q.setDifficulty(YunkaiDifficulty.getByYunKaiType(resultSet.getInt("difficulty")));
+				q.setAnswer(disposeImg(resultSet.getString("answer"), course.getId()));
+				q.setObjective(q.getQst().isObjective());
+				qs.add(q);
 			}
-			return c;
+			return qs;
 		} finally {
 			if (resultSet != null) {
 				resultSet.close();
@@ -109,231 +93,282 @@ public class MyConsumer extends Consumer<String> {
 		}
 	}
 
-	private List<Long> getContextIds(Connection connect, Long courseId) throws SQLException, IOException {
-		List<Long> ids = new ArrayList<>();
-		PreparedStatement preState = null;
-		ResultSet resultSet = null;
-		try {
-			String sql = "select * from mdl_context where instanceid =" + courseId
-					+ " and contextlevel=50 order by id ";
-			preState = connect.prepareStatement(sql);
+	private String disposeImg(String str, Long subid) {
+		if (StringUtils.isBlank(str)) {
+			return str;
+		}
+		StringBuffer sb = new StringBuffer("");
+		Matcher matcher = imgPat.matcher(str);
 
-			resultSet = preState.executeQuery();
+		while (matcher.find()) {
+			String imgSrc = matcher.group(1).trim();
+			String suff = imgSrc.substring(imgSrc.lastIndexOf(".") + 1).toLowerCase();
+			File img = new File(paperDir + subid + "/" + UUID.randomUUID() + "." + suff);
+			FileUtil.saveUrlAs(imgSrc, img.getAbsolutePath());
+			String base64 = FileUtil.fileToBase64Src(img);
+			matcher.appendReplacement(sb, base64);
+		}
 
-			while (resultSet.next()) {
-				ids.add(resultSet.getLong("id"));
-			}
-			return ids;
-		} finally {
-			if (resultSet != null) {
-				resultSet.close();
-			}
-			if (preState != null) {
-				preState.close();
-			}
+		if (StringUtils.isEmpty(sb.toString())) {
+			return str;
+		} else {
+			matcher.appendTail(sb);
+			return sb.toString();
 		}
 	}
 
-	private List<Long> getQuestionCategorie(Connection connect, List<Long> cids) throws SQLException, IOException {
-		List<Long> ids = new ArrayList<>();
+	private List<QuestionProp> getQuestionProp(Connection connect, List<KdQuestion> qs) {
+		List<QuestionProp> as = new ArrayList<>();
+		List<Long> ids = qs.stream().map(e -> e.getId()).collect(Collectors.toList());
+		String idsstr = StringUtils.join(ids, ",");
 		PreparedStatement preState = null;
 		ResultSet resultSet = null;
 		try {
-			String sql = "select * from mdl_question_categories where contextid in (" + getInStr(cids)
-					+ ") order by id ";
+			String sql = " select t.knowledge_system_id,t.question_id " + " from wq_question_knowledge_system t "
+					+ " where t.question_id in (" + idsstr + ") ";
 			preState = connect.prepareStatement(sql);
 
 			resultSet = preState.executeQuery();
 
 			while (resultSet.next()) {
-				ids.add(resultSet.getLong("id"));
+				QuestionProp a = new QuestionProp();
+				a.setKnowledgeId(resultSet.getLong("knowledge_system_id"));
+				a.setQuestionId(resultSet.getLong("question_id"));
+				as.add(a);
 			}
-			return ids;
+			return as;
+		} catch (Exception e1) {
+			throw new RuntimeException(e1);
 		} finally {
 			if (resultSet != null) {
-				resultSet.close();
+				try {
+					resultSet.close();
+				} catch (SQLException e1) {
+				}
 			}
 			if (preState != null) {
-				preState.close();
+				try {
+					preState.close();
+				} catch (SQLException e1) {
+				}
 			}
 		}
 	}
 
-	private List<KdQuestion> getQuestion(Connection connect, List<Long> qcids) throws SQLException, IOException {
-		List<KdQuestion> qs = new ArrayList<>();
-		PreparedStatement preState = null;
-		ResultSet resultSet = null;
-		try {
-			String sql = "SELECT f.* FROM mdl_question f WHERE category IN (" + getInStr(qcids)
-					+ ") AND f.qtype IN ('multichoice','truefalse','multichoiceset') ";
-//			+ ") AND f.qtype = 'multichoiceset' ";
-			
-			preState = connect.prepareStatement(sql);
+	private void fillProp(Connection connect, List<KdQuestion> qs) {
+		List<QuestionProp> qps = new BatchGetDataUtil<QuestionProp, KdQuestion>() {
 
-			resultSet = preState.executeQuery();
+			@Override
+			protected List<QuestionProp> getData(List<KdQuestion> paramList) {
+				return getQuestionProp(connect, paramList);
+			}
+		}.getDataForBatch(qs, 200);
+		if (CollectionUtils.isEmpty(qps)) {
+			return;
+		}
+		Map<Long, KdQuestion> map = new HashMap<>();
+		for (KdQuestion q : qs) {
+			map.put(q.getId(), q);
+		}
+		for (QuestionProp qp : qps) {
+			KdQuestion q = map.get(qp.getQuestionId());
+			List<Long> qpids = q.getPropIds();
+			if (qpids == null) {
+				qpids = new ArrayList<>();
+				q.setPropIds(qpids);
+			}
+			qpids.add(qp.getKnowledgeId());
+		}
+	}
 
-			while (resultSet.next()) {
-				KdQuestion q = new KdQuestion();
-				q.setId(resultSet.getLong("id"));
-				q.setBody(disBody(resultSet.getString("questiontext")));
-				q.setQtype(resultSet.getString("qtype"));
-				q.setObjective(true);
-				qs.add(q);
+	private void fillAnswer(Connection connect, List<KdQuestion> qs, Course course) {
+		List<Answer> qps = new BatchGetDataUtil<Answer, KdQuestion>() {
+
+			@Override
+			protected List<Answer> getData(List<KdQuestion> paramList) {
+				return getAnswerBatch(connect, paramList, course);
 			}
-			return qs;
-		} finally {
-			if (resultSet != null) {
-				resultSet.close();
+		}.getDataForBatch(qs, 200);
+		if (CollectionUtils.isEmpty(qps)) {
+			return;
+		}
+
+		Map<Long, List<Answer>> map = new HashMap<>();
+		for (Answer qp : qps) {
+			List<Answer> list = map.get(qp.getQuestionId());
+			if (list == null) {
+				list = new ArrayList<>();
+				map.put(qp.getQuestionId(), list);
 			}
-			if (preState != null) {
-				preState.close();
+			list.add(qp);
+		}
+
+		for (KdQuestion q : qs) {
+			if (YunkaiQuesStructType.DANXUAN.equals(q.getQst())) {
+				List<KdQuesOption> ops = new ArrayList<>();
+				q.setOptions(ops);
+				int num = 0;
+				for (Answer a : map.get(q.getId())) {
+					num++;
+					KdQuesOption op = new KdQuesOption();
+					ops.add(op);
+					op.setNumber(num);
+					op.setBody(a.getBody());
+					op.setSelect(a.getRight() == 1);
+					if (op.getSelect()) {
+						q.setAnswer(getOptionNum(num));
+					}
+				}
+			} else if (YunkaiQuesStructType.DUOXUAN.equals(q.getQst())
+					|| YunkaiQuesStructType.BUDINGXIANG.equals(q.getQst())) {
+				List<String> as = new ArrayList<>();
+				List<KdQuesOption> ops = new ArrayList<>();
+				q.setOptions(ops);
+				int num = 0;
+				for (Answer a : map.get(q.getId())) {
+					num++;
+					KdQuesOption op = new KdQuesOption();
+					ops.add(op);
+					op.setNumber(num);
+					op.setBody(a.getBody());
+					op.setSelect(a.getRight() == 1);
+					if (op.getSelect()) {
+						as.add(getOptionNum(num));
+					}
+				}
+				q.setAnswer(StringUtils.join(as, ","));
+			} else if (YunkaiQuesStructType.PANDUAN.equals(q.getQst())) {
+				for (Answer a : map.get(q.getId())) {
+					if (a.getRight() == 1) {
+					}
+					q.setAnswer(getBool(a.getBody()));
+				}
+
+			} else if (YunkaiQuesStructType.TIANKONGTI.equals(q.getQst())) {
+				q.setBody(relaceQuestionIdx(q.getBody()));
+				if (map.get(q.getId()) != null) {
+					List<String> as = new ArrayList<>();
+					try {
+						for (Answer a : map.get(q.getId())) {
+							if (a.getRight() == 1) {
+								as.add(a.getBody());
+							}
+						}
+					} catch (Exception e) {
+						throw e;
+					}
+					q.setAnswer(StringUtils.join(as, "##"));
+				}
+			} else {
+				// nothing
 			}
 		}
 	}
 
-	private List<Answer> getAnswer(Connection connect, KdQuestion q) throws SQLException, IOException {
+	private String relaceQuestionIdx(String str) {
+		StringBuffer sb = new StringBuffer("");
+		Pattern pattern = Pattern.compile("__(\\d+)__");
+
+		Matcher matcher = pattern.matcher(str);
+
+		while (matcher.find()) {
+			matcher.appendReplacement(sb, "###");
+		}
+
+		if (StringUtils.isEmpty(sb.toString())) {
+			return str;
+		} else {
+			matcher.appendTail(sb);
+			return sb.toString();
+		}
+	}
+
+	/**
+	 * 将数字1,2,3,4转化成A,B,C,D
+	 *
+	 * @param number
+	 * @return
+	 */
+	private String getOptionNum(int number) {
+		char optionNum = (char) (65 + number);
+		return String.valueOf(optionNum);
+	}
+
+	private String getBool(String bu) {
+		if (bu.contains("对")) {
+			return "正确";
+		} else {
+			return "错误";
+		}
+	}
+
+	private List<Answer> getAnswerBatch(Connection connect, List<KdQuestion> qs, Course course) {
 		List<Answer> as = new ArrayList<>();
+		List<Long> ids = qs.stream().map(e -> e.getId()).collect(Collectors.toList());
+		String idsstr = StringUtils.join(ids, ",");
 		PreparedStatement preState = null;
 		ResultSet resultSet = null;
 		try {
-			String sql = "select * from mdl_question_answers where question =" + q.getId();
+			String sql = " select t.question_id,t.answer_text,t.is_right " + " from wq_question_answer_item t "
+					+ " where t.question_id in (" + idsstr + ")  " + " order by t.question_id,t.sequence ";
 			preState = connect.prepareStatement(sql);
 
 			resultSet = preState.executeQuery();
 
 			while (resultSet.next()) {
 				Answer a = new Answer();
-				a.setId(resultSet.getLong("id"));
-				a.setBody(resultSet.getString("answer"));
-				a.setScore(resultSet.getDouble("fraction"));
+				a.setBody(disposeImg(resultSet.getString("answer_text"), course.getId()));
+				a.setRight(resultSet.getInt("is_right"));
+				a.setQuestionId(resultSet.getLong("question_id"));
 				as.add(a);
 			}
 			return as;
+		} catch (Exception e1) {
+			throw new RuntimeException(e1);
 		} finally {
 			if (resultSet != null) {
-				resultSet.close();
+				try {
+					resultSet.close();
+				} catch (SQLException e1) {
+				}
 			}
 			if (preState != null) {
-				preState.close();
+				try {
+					preState.close();
+				} catch (SQLException e1) {
+				}
 			}
 		}
 	}
 
-	private void exportPaper(Connection connect, String code) throws Exception {
-		Course c = getCourse(connect, code);
-		if (c == null) {
-			noques.add(code);
-			return;
-		}
-		List<Long> ctids = getContextIds(connect, c.getId());
-		if (ctids.size() == 0) {
-			noques.add(code);
-			return;
-		}
-		List<Long> qcids = getQuestionCategorie(connect, ctids);
-		if (qcids.size() == 0) {
-			noques.add(code);
-			return;
-		}
-		List<KdQuestion> qs = getQuestion(connect, qcids);
+	private void exportPaper(Connection connect, Course course) throws Exception {
+		List<KdQuestion> qs = getQuestion(connect, course);
 		if (qs.size() == 0) {
-			noques.add(code);
 			return;
 		}
-		for (KdQuestion q : qs) {
-			List<Answer> as = getAnswer(connect, q);
-			disposeAnswer(q, as);
-		}
+		fillProp(connect, qs);
+		fillAnswer(connect, qs, course);
 
-		qs = removeInvalidQuestion(qs, code);
-		if (qs.size() == 0) {
-			noques.add(code);
-			return;
-		}
-		File courseCode = new File(excelDir + c.getCode() + "\\");
-		courseCode.mkdirs();
-		File att = new File(excelDir + c.getCode() + "\\att\\");
-		att.mkdirs();
+		Map<YunkaiQuesStructType, List<KdQuestion>> qmap = new HashMap<>();
 		for (KdQuestion q : qs) {
-			try {
-				disposeMedia(connect, c.getIdnumber(), q, att);
-				q.setValid(true);
-			} catch (MediaNotFoundException e) {
-				q.setValid(false);
+			List<KdQuestion> list = qmap.get(q.getQst());
+			if (list == null) {
+				list = new ArrayList<>();
+				qmap.put(q.getQst(), list);
 			}
+			list.add(q);
 		}
-		List<KdQuestion> tem = new ArrayList<>();
-		for (KdQuestion q : qs) {
-			if (q.getValid()) {
-				tem.add(q);
-			}
-		}
-		qs = tem;
-		if (qs.size() == 0) {
-			noques.add(code);
-			return;
-		}
-
-		List<KdQuestion> single = new ArrayList<>();
-		List<KdQuestion> muti = new ArrayList<>();
-		List<KdQuestion> boo = new ArrayList<>();
-		for (KdQuestion q : qs) {
-			if (q.getStructType() == 1) {
-				single.add(q);
-			} else if (q.getStructType() == 2) {
-				muti.add(q);
-			} else if (q.getStructType() == 3) {
-				boo.add(q);
-			}
+		for (YunkaiQuesStructType qt : qmap.keySet()) {
+			createPapers(qmap.get(qt), qt, course);
 		}
-		createPapers(single, c, 1);
-		createPapers(muti, c, 2);
-		createPapers(boo, c, 3);
-
-//		int detailIndx = 0;
-//		List<KdDetail> des = new ArrayList<>();
-//		if (single.size() > 0) {
-//			detailIndx++;
-//			KdDetail d = new KdDetail();
-//			d.setName("单选题");
-//			d.setNumber(detailIndx);
-//			d.setQuestionCount(single.size());
-//			d.setTotalScore((double) single.size());
-//			d.setQuestions(single);
-//			des.add(d);
-//		}
-//		if (muti.size() > 0) {
-//			detailIndx++;
-//			KdDetail d = new KdDetail();
-//			d.setName("多选题");
-//			d.setNumber(detailIndx);
-//			d.setQuestionCount(muti.size());
-//			d.setTotalScore((double) muti.size());
-//			d.setQuestions(muti);
-//			des.add(d);
-//		}
-//		if (boo.size() > 0) {
-//			detailIndx++;
-//			KdDetail d = new KdDetail();
-//			d.setName("判断题");
-//			d.setNumber(detailIndx);
-//			d.setQuestionCount(boo.size());
-//			d.setTotalScore((double) boo.size());
-//			d.setQuestions(boo);
-//			des.add(d);
-//		}
-//		paper.setDetails(des);
-//		paper.setDetailCount(detailIndx);
-//
-//		FileUtil.writeFile(courseCode.getAbsolutePath(), "\\paper.json", JSONObject.toJSONString(paper));
 	}
 
-	private void createPapers(List<KdQuestion> qs, Course c, int structType) throws IOException {
+	private void createPapers(List<KdQuestion> qs, YunkaiQuesStructType qt, Course course) {
 		if (qs == null || qs.size() == 0) {
 			return;
 		}
 		if (qs.size() <= maxqc) {
-			createPaper(qs, c, structType, 1);
+			createPaper(qs, qt, course, 1);
 		} else {
 			int size = qs.size();
 			int len = maxqc;
@@ -341,28 +376,21 @@ public class MyConsumer extends Consumer<String> {
 
 			for (int i = 0; i < count; i++) {
 				List<KdQuestion> subList = qs.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
-				createPaper(subList, c, structType, i + 1);
+				createPaper(subList, qt, course, i + 1);
 			}
 		}
 
 	}
 
-	private void createPaper(List<KdQuestion> qs, Course c, int structType, int indx) throws IOException {
+	private void createPaper(List<KdQuestion> qs, YunkaiQuesStructType qt, Course course, int indx) {
 		if (qs.size() == 0) {
 			return;
 		}
-		String detailName="";
-		if (structType == 1) {
-			detailName="单选题";
-		} else if (structType == 2) {
-			detailName="多选题";
-		} else if (structType == 3) {
-			detailName="判断题";
-		}
+		String detailName = qt.getYunKaiDesc();
 		KdPaper paper = new KdPaper();
 		paper.setTotalScore((double) qs.size());
-		paper.setName(c.getName()+paperSuff+detailName+"_"+indx);
-		paper.setCourseCode(c.getCode());
+		paper.setName(course.getName() + paperSuff + detailName + "_" + indx);
+		paper.setCourseCode(course.getCode());
 		List<KdDetail> des = new ArrayList<>();
 		KdDetail d = new KdDetail();
 		d.setName(detailName);
@@ -373,472 +401,17 @@ public class MyConsumer extends Consumer<String> {
 		des.add(d);
 		paper.setDetails(des);
 		paper.setDetailCount(1);
-		File paperdir = new File(excelDir + c.getCode() + "\\paper"+structType+"_"+indx+"\\");
+		File paperdir = new File(paperDir + course.getId() + "/" + qt.getYunKaiType() + "/");
 		paperdir.mkdirs();
-		FileUtil.writeFile(paperdir.getAbsolutePath(), "\\paper.json", JSONObject.toJSONString(paper));
-	}
-
-	private List<KdQuestion> removeInvalidQuestion(List<KdQuestion> qs, String courseCode) {
-		List<KdQuestion> ret = new ArrayList<>();
-		for (KdQuestion q : qs) {
-			if (StringUtils.isBlank(q.getAnswer())) {
-				invalidAnswerCode.add(q.getId());
-				continue;
-			}
-			if (isEmpty(q.getBody())) {
-				invalidAnswerCode.add(q.getId());
-				continue;
-			}
-			if (q.getStructType() == 1 || q.getStructType() == 2) {
-				if (!checkQuestionAndRemoveInvalidOption(q)) {
-					invalidAnswerCode.add(q.getId());
-					continue;
-				}
-			}
-			ret.add(q);
-		}
-		return ret;
-	}
-
-	private boolean checkQuestionAndRemoveInvalidOption(KdQuestion q) {
-		List<KdQuesOption> ret = new ArrayList<>();
-		for (KdQuesOption op : q.getOptions()) {
-			if (isEmpty(op.getBody())) {
-				if (op.getSelect()) {
-					return false;
-				}
-			} else {
-				ret.add(op);
-			}
-		}
-		if (ret.size() < 2) {
-			return false;
-		} else {
-			q.setOptions(ret);
-		}
-		return true;
-	}
-
-	private void disposeAnswer(KdQuestion q, List<Answer> as) {
-		if (CollectionUtils.isEmpty(as)) {
-			return;
-		}
-		if ("truefalse".equals(q.getQtype())) {
-			q.setStructType(3);
-			for (Answer a : as) {
-				if (a.getScore() > 0) {
-					q.setAnswer("对".equals(a.getBody().trim()) ? "true" : "false");
-					return;
-				}
-			}
-		} else {
-			int index = 0;
-			List<KdQuesOption> ops = new ArrayList<>();
-			StringBuilder sb = new StringBuilder();
-			for (Answer a : as) {
-				index++;
-				KdQuesOption op = new KdQuesOption();
-				op.setAnswerId(a.getId());
-				op.setNumber(index);
-				op.setBody(disBody(a.getBody()));
-				if (a.getScore() > 0) {
-					op.setSelect(true);
-					sb.append(index).append(",");
-				} else {
-					op.setSelect(false);
-				}
-				ops.add(op);
-			}
-			q.setOptions(ops);
-			if (sb.length() > 0) {
-				sb.deleteCharAt(sb.length() - 1);
-			}
-			if (sb.indexOf(",") > 0) {
-				q.setStructType(2);
-			} else {
-				q.setStructType(1);
-			}
-			q.setAnswer(sb.toString());
-		}
-	}
-
-	private String disBody(String html) {
-		Document doc = Jsoup.parse(html);
-		Element b = doc.body();
-		if (b.childrenSize() == 1 && "p".equals(b.child(0).nodeName())) {
-			b.child(0).tagName("span");
-		}
-		if (b.childrenSize() > 0) {
-			for (Element n : b.children()) {
-				if ("p".equals(n.nodeName())) {
-					boolean[] find = new boolean[] { false };
-					findImg(n, find);
-					if (!find[0] && StringUtils.isBlank(n.text())) {
-						n.tagName("span");
-					}
-				}
-				disNode(n);
-			}
-		}
-		return b.html();
-	}
-
-	private void findImg(Element e, boolean[] find) {
-		if (find[0]) {
-			return;
-		} else {
-			if ("img".equals(e.nodeName())) {
-				find[0] = true;
-				return;
-			} else {
-				if (e.childrenSize() > 0) {
-					for (Element ce : e.children()) {
-						findImg(ce, find);
-					}
-				} else {
-					return;
-				}
-			}
-		}
-	}
-
-	private static void disNode(Node n) {
-		if (n instanceof Element) {
-			if (!"img".equals(n.nodeName())) {
-				n.removeAttr("style");
-			}
-			if ("h1".equals(n.nodeName()) || "h2".equals(n.nodeName()) || "h3".equals(n.nodeName())
-					|| "h4".equals(n.nodeName()) || "h5".equals(n.nodeName())) {
-				((Element) n).tagName("h6");
-			}
-			if ("strong".equals(n.nodeName()) || "a".equals(n.nodeName()) || "u".equals(n.nodeName())
-					|| "em".equals(n.nodeName())) {
-				((Element) n).tagName("span");
-			}
-			if (n.childNodeSize() > 0) {
-				for (Node cn : n.childNodes()) {
-					disNode(cn);
-				}
-			}
-		}
-	}
-
-	private String getInStr(List<Long> cids) {
-		StringBuilder sb = new StringBuilder();
-		for (Long id : cids) {
-			sb.append(id).append(",");
-		}
-		sb.deleteCharAt(sb.length() - 1);
-		return sb.toString();
-	}
-
-//	private Map<String, String> findAtag(String body) {
-//		Map<String, String> set = new HashMap<>();
-//		if (StringUtils.isBlank(body)) {
-//			return set;
-//		}
-//		Matcher m = notmp3Pat.matcher(body);
-//		while (m.find()) {
-//			String f = m.group(1);
-//			set.put(m.group(), f);
-//		}
-//		return set;
-//	}
-//
-//	private void removeATag(KdQuestion q) throws Exception {
-//		String body = q.getBody();
-//
-//		Map<String, String> as = findAtag(body);
-//		if (as.size() > 0) {
-//			for (String k : as.keySet()) {
-//				String val = as.get(k);
-//				body = body.replaceAll(k, val);
-//			}
-//			q.setBody(body);
-//		}
-//
-//		if (CollectionUtils.isNotEmpty(q.getOptions())) {
-//			for (KdQuesOption o : q.getOptions()) {
-//				String obody = o.getBody();
-//
-//				Map<String, String> oas = findAtag(obody);
-//				if (oas.size() > 0) {
-//					for (String k : oas.keySet()) {
-//						String val = oas.get(k);
-//						obody = obody.replaceAll(k, val);
-//					}
-//					o.setBody(obody);
-//				}
-//			}
-//		}
-//	}
-
-	private void disposeMedia(Connection connect, String courseidnumber, KdQuestion q, File att) throws Exception {
-		String body = q.getBody();
-
-		Map<String, String> imgs = findAllImg(body, q.getId(), null, courseidnumber);
-		if (imgs.size() > 0) {
-			for (String k : imgs.keySet()) {
-				String img = imgs.get(k);
-				String fileName = getFileName(img);
-				File file = getQuestionFile(connect, q.getId(), courseidnumber, fileName);
-				if (file == null) {
-					ExportByCourseCode.addNotFd();
-//					body = body.replaceAll(k, "[未找到图片文件]");
-					throw new MediaNotFoundException();
-				} else {
-					ExportByCourseCode.addFd();
-					body = body.replaceAll(img.replaceAll("\\?", "\\\\?"), FileUtil.fileToBase64Src(file));
-				}
-			}
-			q.setBody(body);
-		}
-		Map<String, String> audios = findAllAudio(body, q.getId(), null, courseidnumber);
-		if (audios.size() > 0) {
-			q.setHaveAudio(true);
-			for (String k : audios.keySet()) {
-				String val = audios.get(k);
-				String fileName = getFileName(val);
-				File file = getQuestionFile(connect, q.getId(), courseidnumber, fileName);
-				if (file == null) {
-					ExportByCourseCode.addNotFd();
-//					body = body.replaceAll(k, "[未找到音频文件]");
-					throw new MediaNotFoundException();
-				} else {
-					ExportByCourseCode.addFd();
-					File newAudio = new File(att.getAbsoluteFile() + "\\" + file.getName());
-					newAudio.createNewFile();
-					FileUtils.copyFile(file, newAudio);
-					body = body.replaceAll(k.replaceAll("\\?", "\\\\?"), "<a id=\"" + file.getName() + "\" name=\"" + file.getName() + "\"></a>");
-				}
-			}
-			q.setBody(body);
-		}
-
-		if (CollectionUtils.isNotEmpty(q.getOptions())) {
-			for (KdQuesOption o : q.getOptions()) {
-				String obody = o.getBody();
-
-				Map<String, String> oimgs = findAllImg(obody, null, o.getAnswerId(), courseidnumber);
-				if (oimgs.size() > 0) {
-					for (String k : oimgs.keySet()) {
-						String img = oimgs.get(k);
-						String fileName = getFileName(img);
-						File file = getAnswerFile(connect, o.getAnswerId(), courseidnumber, fileName);
-						if (file == null) {
-							ExportByCourseCode.addNotFd();
-//							obody = obody.replaceAll(k, "[未找到图片文件]");
-							throw new MediaNotFoundException();
-						} else {
-							ExportByCourseCode.addFd();
-							obody = obody.replaceAll(img.replaceAll("\\?", "\\\\?"), FileUtil.fileToBase64Src(file));
-						}
-					}
-					o.setBody(obody);
-				}
-
-				Map<String, String> oaudios = findAllAudio(obody, null, o.getAnswerId(), courseidnumber);
-				if (oaudios.size() > 0) {
-					q.setHaveAudio(true);
-					for (String k : oaudios.keySet()) {
-						String val = oaudios.get(k);
-						String fileName = getFileName(val);
-						File file = getAnswerFile(connect, o.getAnswerId(), courseidnumber, fileName);
-						if (file == null) {
-							ExportByCourseCode.addNotFd();
-//							obody = obody.replaceAll(k, "[未找到音频文件]");
-							throw new MediaNotFoundException();
-						} else {
-							ExportByCourseCode.addFd();
-							File newAudio = new File(att.getAbsoluteFile() + "\\" + file.getName());
-							newAudio.createNewFile();
-							FileUtils.copyFile(file, newAudio);
-							obody = obody.replaceAll(k.replaceAll("\\?", "\\\\?"),
-									"<a id=\"" + file.getName() + "\" name=\"" + file.getName() + "\"></a>");
-						}
-					}
-					o.setBody(obody);
-				}
-			}
-		}
-	}
-
-	private Map<String, String> findAllImg(String body, Long qid, Long aid, String courseidnumber) {
-		Map<String, String> set = new HashMap<>();
-		Matcher m = imgPat.matcher(body);
-		while (m.find()) {
-			String f = m.group(1);
-			if (f.startsWith("file:") || f.startsWith("FILE:")) {
-				if (qid != null)
-					file.add("questionid:" + qid + " " + courseidnumber + " " + f);
-				if (aid != null)
-					file.add("answerId:" + aid + " " + courseidnumber + " " + f);
-				continue;
-			}
-			if (f.startsWith("http:") || f.startsWith("HTTP:")) {
-				if (qid != null)
-					http.add("questionid:" + qid + " " + courseidnumber + " " + f);
-				if (aid != null)
-					http.add("answerId:" + aid + " " + courseidnumber + " " + f);
-				continue;
-			}
-			if (!f.startsWith("data:image")) {
-				set.put(m.group(), f);
-			}
-		}
-		return set;
-	}
-
-	private Map<String, String> findAllAudio(String body, Long qid, Long aid, String courseidnumber) {
-		Map<String, String> set = new HashMap<>();
-		Matcher m = mp3Pat.matcher(body);
-		while (m.find()) {
-			String f = m.group(1);
-			if (f.startsWith("file:") || f.startsWith("FILE:")) {
-				if (qid != null)
-					file.add("questionid:" + qid + " " + courseidnumber + " " + f);
-				if (aid != null)
-					file.add("answerId:" + aid + " " + courseidnumber + " " + f);
-				continue;
-			}
-			if (f.startsWith("http:") || f.startsWith("HTTP:")) {
-				if (qid != null)
-					http.add("questionid:" + qid + " " + courseidnumber + " " + f);
-				if (aid != null)
-					http.add("answerId:" + aid + " " + courseidnumber + " " + f);
-				continue;
-			}
-			set.put(m.group(), f);
-		}
-		return set;
-	}
-
-	private boolean hasImg(String body) {
-		Matcher m = imgPat.matcher(body);
-		while (m.find()) {
-			return true;
-		}
-		return false;
-	}
-
-	private boolean hasAudio(String body) {
-		Matcher m = mp3Pat.matcher(body);
-		while (m.find()) {
-			return true;
-		}
-		return false;
-	}
-
-	private boolean isEmpty(String html) {
-		if (StringUtils.isBlank(html)) {
-			return true;
-		}
-		if (hasImg(html) || hasAudio(html)) {
-			return false;
-		}
-		Document doc = Jsoup.parse(html);
-		Element b = doc.body();
-		if (StringUtils.isBlank(b.wholeText())) {
-			return true;
-		} else {
-			return false;
-		}
-	}
-
-	private String getFileName(String s) throws UnsupportedEncodingException {
-		int end = s.length();
-		int f = s.indexOf("?");
-		if (f != -1) {
-			end = f;
-		}
-		return URLDecoder.decode(s.substring(s.lastIndexOf("/") + 1, end), "utf-8");
-	}
-
-	private File getQuestionFile(Connection connect, Long quesId, String courseidnumber, String fileName)
-			throws Exception {
-		PreparedStatement preState = null;
-		ResultSet resultSet = null;
 		try {
-			String hash = null;
-			String sql = " select * from mdl_files where itemid = " + quesId + " and source='" + fileName
-					+ "' and component = 'question' and filearea = 'questiontext' ";
-			preState = connect.prepareStatement(sql);
-
-			resultSet = preState.executeQuery();
-
-			while (resultSet.next()) {
-				hash = resultSet.getString("contenthash");
-				break;
-			}
-			if (hash == null) {
-				qdb.add(quesId + " " + courseidnumber + " " + fileName);
-				return null;
-			}
-			String suff = fileName.substring(fileName.lastIndexOf("."));
-			File file = new File(picFiles + courseidnumber + "\\" + hash + suff);
-			if (!file.exists()) {
-				qzip.add(quesId + " " + courseidnumber + " " + fileName);
-				return null;
-			}
-			return file;
-		} finally {
-			if (resultSet != null) {
-				resultSet.close();
-			}
-			if (preState != null) {
-				preState.close();
-			}
-		}
-	}
-
-	private File getAnswerFile(Connection connect, Long answerId, String courseidnumber, String fileName)
-			throws Exception {
-		PreparedStatement preState = null;
-		ResultSet resultSet = null;
-		try {
-			String hash = null;
-			String sql = " select * from mdl_files where itemid = " + answerId + " and source='" + fileName
-					+ "' and component = 'question' and filearea = 'answer' ";
-			preState = connect.prepareStatement(sql);
-
-			resultSet = preState.executeQuery();
-
-			while (resultSet.next()) {
-				hash = resultSet.getString("contenthash");
-				break;
-			}
-			if (hash == null) {
-				adb.add(answerId + " " + courseidnumber + " " + fileName);
-				return null;
-			}
-			String suff = fileName.substring(fileName.lastIndexOf("."));
-			File file = new File(picFiles + courseidnumber + "\\" + hash + suff);
-			if (!file.exists()) {
-				azip.add(answerId + " " + courseidnumber + " " + fileName);
-				return null;
-			}
-			return file;
-		} finally {
-			if (resultSet != null) {
-				resultSet.close();
-			}
-			if (preState != null) {
-				preState.close();
-			}
+			FileUtil.writeFile(paperdir.getAbsolutePath(), "/paper_" + indx + ".json", JSONObject.toJSONString(paper));
+		} catch (IOException e) {
+			throw new RuntimeException(e);
 		}
 	}
 
 	@Override
 	public void initResult() {
-		setResult(new HashMap<>());
-		getResult().put("qdb", qdb);
-		getResult().put("qzip", qzip);
-		getResult().put("adb", adb);
-		getResult().put("azip", azip);
-		getResult().put("invalidAnswerCode", invalidAnswerCode);
-		getResult().put("file", file);
-		getResult().put("http", http);
-		getResult().put("noques", noques);
 
 	}
 }

+ 31 - 20
src/main/java/cn/com/qmth/export/MyProducer.java

@@ -2,13 +2,13 @@ package cn.com.qmth.export;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.HashSet;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
+import org.apache.commons.collections4.CollectionUtils;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -16,47 +16,58 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 public class MyProducer extends Producer {
 	private static Logger logger = LogManager.getLogger(MyProducer.class);
-	private static String excelDir = "D:\\kd_export\\";
+	private static String dir = "d:/yunkai/";
+	private static String paperDir = "d:/yunkai/paper/";
 
 	@Override
 	protected void produce(Map<String, Object> param) throws Exception {
 		logger.info("***************************任务生产开始");
-		File excelFolder = new File(excelDir);
+		File excelFolder = new File(paperDir);
 		if (excelFolder.exists()) {
-			FileUtil.clearDirectory(excelDir);
+			FileUtil.clearDirectory(paperDir);
 		} else {
-			excelFolder.mkdir();
+			excelFolder.mkdirs();
 		}
-		Set<String> codes = readCourseCode();
-		if (codes == null || codes.size() == 0) {
+		List<Course> cs = readSubject();
+		if (CollectionUtils.isEmpty(cs)) {
 			logger.debug("无数据导出");
 			return;
 		} else {
-			logger.debug(codes.size() + "个课程");
+			logger.debug(cs.size() + "个课程");
 		}
-		for (String code : codes) {
-			offer(code);
+		for (Course c : cs) {
+			offer(c);
 		}
 		logger.info("***************************任务生产结束");
 	}
 	
-	private  Set<String> readCourseCode() throws InvalidFormatException, IOException {
-		File directory = new File("");
-		Set<String> list = new HashSet<String>();
+	
+	private static List<Course> readSubject() {
+		List<Course> list = new ArrayList<>();
 		XSSFWorkbook wb = null;
 		try {
-			wb = new XSSFWorkbook(directory.getAbsolutePath() + "\\course.xlsx");
+			wb = new XSSFWorkbook(dir + "subject.xlsx");
 			XSSFSheet sheet = wb.getSheetAt(0);
 			int rows = sheet.getLastRowNum();
 			for (int i = 1; i <= rows; i++) {
+				Course c = new Course();
 				XSSFRow row = sheet.getRow(i);
-				String tem = row.getCell(0).getStringCellValue().trim();
-				tem = tem.substring(0, tem.length() - 2);
-				list.add(tem);
+				String id = row.getCell(0).getStringCellValue().trim();
+				String name = row.getCell(1).getStringCellValue().trim();
+				String code = row.getCell(2).getStringCellValue().trim();
+				c.setId(Long.valueOf(id));
+				c.setCode(code);
+				c.setName(name);
+				list.add(c);
 			}
+		} catch (IOException e) {
+			throw new RuntimeException(e);
 		} finally {
 			if (wb != null) {
-				wb.close();
+				try {
+					wb.close();
+				} catch (IOException e) {
+				}
 			}
 		}
 		return list;

+ 23 - 0
src/main/java/cn/com/qmth/export/QuestionProp.java

@@ -0,0 +1,23 @@
+package cn.com.qmth.export;
+
+public class QuestionProp {
+	private Long questionId;
+	private Long knowledgeId;
+
+	public Long getQuestionId() {
+		return questionId;
+	}
+
+	public void setQuestionId(Long questionId) {
+		this.questionId = questionId;
+	}
+
+	public Long getKnowledgeId() {
+		return knowledgeId;
+	}
+
+	public void setKnowledgeId(Long knowledgeId) {
+		this.knowledgeId = knowledgeId;
+	}
+
+}

+ 89 - 0
src/main/java/cn/com/qmth/export/YunkaiDifficulty.java

@@ -0,0 +1,89 @@
+package cn.com.qmth.export;
+
+public enum YunkaiDifficulty {
+	
+	RONGYI(1,"容易",1.0,"易"),
+	JIAORONGYI(2,"较容易",0.7,"易"),
+	ZHONGDENG(3,"中等",0.5,"中"),
+	JIAONAN(4,"较难",0.3,"难"),
+	KUNNAN(5,"困难",0.1,"难"),
+	;
+	
+	private Integer yunKaiType;
+	private String yunKaiDesc;
+	private Double type;
+	private String desc;
+	
+	private YunkaiDifficulty(Integer yunKaiType, String yunKaiDesc, Double type, String desc) {
+		this.yunKaiType = yunKaiType;
+		this.yunKaiDesc = yunKaiDesc;
+		this.type = type;
+		this.desc = desc;
+	}
+	
+	
+		
+	public Integer getYunKaiType() {
+		return yunKaiType;
+	}
+
+
+
+	public void setYunKaiType(Integer yunKaiType) {
+		this.yunKaiType = yunKaiType;
+	}
+
+
+
+	public String getYunKaiDesc() {
+		return yunKaiDesc;
+	}
+
+
+
+	public void setYunKaiDesc(String yunKaiDesc) {
+		this.yunKaiDesc = yunKaiDesc;
+	}
+
+
+
+	public Double getType() {
+		return type;
+	}
+
+
+
+	public void setType(Double type) {
+		this.type = type;
+	}
+
+
+
+	public String getDesc() {
+		return desc;
+	}
+
+
+
+	public void setDesc(String desc) {
+		this.desc = desc;
+	}
+
+
+
+
+
+	public static YunkaiDifficulty getByYunKaiType(Integer yunKaiType) {
+		if(yunKaiType==null) {
+			return null;
+		}
+		for(YunkaiDifficulty t:YunkaiDifficulty.values()) {
+			if(t.getYunKaiType().equals(yunKaiType)) {
+				return t;
+			}
+		}
+		return null;
+	}
+	
+}
+

+ 109 - 0
src/main/java/cn/com/qmth/export/YunkaiQuesStructType.java

@@ -0,0 +1,109 @@
+package cn.com.qmth.export;
+
+public enum YunkaiQuesStructType {
+	
+	DANXUAN(1,"单选题",1,"单选题",true),
+	DUOXUAN(2,"多选题",2,"多选题",true),
+	BUDINGXIANG(3,"不定项选择题",2,"多选题",true),
+	PANDUAN(4,"判断题",3,"判断题",true),
+	JIANDA(5,"简答题",5,"问答题",false),
+	MIGNCIJIESHI(6,"名词解释",5,"问答题",false),
+	LIJIETI(7,"理解题",5,"问答题",false),
+	ANLITI(8,"案例题",5,"问答题",false),
+	LUNSHUTI(9,"论述题",5,"问答题",false),
+	FENXITI(10,"分析题",5,"问答题",false),
+	ZONGHETI(11,"综合题",5,"问答题",false),
+	JISUANTI(12,"计算题",5,"问答题",false),
+	TIANKONGTI(13,"填空题",4,"填空题",false),
+	;
+	
+	private Integer yunKaiType;
+	private String yunKaiDesc;
+	private Integer type;
+	private String desc;
+	private boolean objective;//是否是客观题
+	
+	private YunkaiQuesStructType(Integer yunKaiType, String yunKaiDesc, Integer type, String desc, boolean objective) {
+		this.yunKaiType = yunKaiType;
+		this.yunKaiDesc = yunKaiDesc;
+		this.type = type;
+		this.desc = desc;
+		this.objective = objective;
+	}
+	
+	
+		
+	public Integer getYunKaiType() {
+		return yunKaiType;
+	}
+
+
+
+	public void setYunKaiType(Integer yunKaiType) {
+		this.yunKaiType = yunKaiType;
+	}
+
+
+
+	public String getYunKaiDesc() {
+		return yunKaiDesc;
+	}
+
+
+
+	public void setYunKaiDesc(String yunKaiDesc) {
+		this.yunKaiDesc = yunKaiDesc;
+	}
+
+
+
+	public Integer getType() {
+		return type;
+	}
+
+
+
+	public void setType(Integer type) {
+		this.type = type;
+	}
+
+
+
+	public String getDesc() {
+		return desc;
+	}
+
+
+
+	public void setDesc(String desc) {
+		this.desc = desc;
+	}
+
+
+
+	public boolean isObjective() {
+		return objective;
+	}
+
+
+
+	public void setObjective(boolean objective) {
+		this.objective = objective;
+	}
+
+
+
+	public static YunkaiQuesStructType getByYunKaiType(Integer yunKaiType) {
+		if(yunKaiType==null) {
+			throw new RuntimeException("题型异常");
+		}
+		for(YunkaiQuesStructType t:YunkaiQuesStructType.values()) {
+			if(t.getYunKaiType().equals(yunKaiType)) {
+				return t;
+			}
+		}
+		throw new RuntimeException("题型异常");
+	}
+	
+}
+