MyConsumer.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cn.com.qmth.im;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.stream.Collectors;
  7. import org.apache.commons.io.IOUtils;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.log4j.LogManager;
  10. import org.apache.log4j.Logger;
  11. import com.google.common.collect.Lists;
  12. import com.google.common.collect.Maps;
  13. import cn.com.qmth.multithread.Consumer;
  14. import cn.com.qmth.param.Param;
  15. import cn.com.qmth.run.ImportPaperByCourse;
  16. import okhttp3.Response;
  17. public class MyConsumer extends Consumer<ConsumerDto> {
  18. private static Logger logger = LogManager.getLogger(MyConsumer.class);
  19. @Override
  20. public void consume(ConsumerDto dto) {
  21. importCoursePaper(dto);
  22. ImportPaperByCourse.addDisposeCount();
  23. }
  24. private void importCoursePaper(ConsumerDto dto) {
  25. File file = new File(Param.dataDir + "/paper/" + dto.getIc().getSubjectCode() + "/");
  26. if (!file.exists()) {
  27. return;
  28. }
  29. File zfile = new File(Param.dataDir + "/paper/" + dto.getIc().getSubjectCode() + ".zip");
  30. try {
  31. List<File> files = new ArrayList<>();
  32. getPaperFile(file, files);
  33. if (files.size() == 0) {
  34. throw new NoPaperException("1000", "没有试卷信息:" + file.getName());
  35. }
  36. if (zfile.exists()) {
  37. zfile.delete();
  38. }
  39. FileUtil.doZip(file, zfile);
  40. submitZip(dto, zfile);
  41. } catch (NoPaperException e1) {
  42. logger.debug("忽略:" + e1.getDesc());
  43. } catch (Exception e) {
  44. throw new RuntimeException(e);
  45. } finally {
  46. if (zfile.exists()) {
  47. zfile.delete();
  48. }
  49. }
  50. }
  51. private void getPaperFile(File file, List<File> ret) {
  52. if (file.isFile()) {
  53. if (file.getName().endsWith(".json")) {
  54. ret.add(file);
  55. }
  56. return;
  57. } else {
  58. for (File paperDir : file.listFiles()) {
  59. getPaperFile(paperDir, ret);
  60. }
  61. }
  62. }
  63. private void submitZip(ConsumerDto dto, File zfile) {
  64. List<String> cs = dto.getIc().getCourses().stream().map(e -> e.getCode()).collect(Collectors.toList());
  65. Map<String, String> params = Maps.newHashMap();
  66. String courseCodes = StringUtils.join(cs, ",");
  67. params.put("rootOrgId", Param.rootOrgId);
  68. params.put("batch", dto.getBatch());
  69. params.put("courseCodes", courseCodes);
  70. Map<String, String> headers = Maps.newHashMap();
  71. headers.put("key", Param.key);
  72. headers.put("token", Param.token);
  73. List<FormFilePart> fileList = Lists.newArrayList();
  74. fileList.add(new FormFilePart("dataFile", zfile.getName(), zfile));
  75. Response resp = null;
  76. try {
  77. // OKHttpUtil.initOkHttpClient();
  78. resp = OKHttpUtil.call(HttpMethod.POST, Param.host + "/api/ecs_ques/importYunkaiPaper", headers, params,
  79. fileList);
  80. if (resp.code() != 200) {
  81. throw new Exception(
  82. zfile.getName() + "| courseCodes:" + courseCodes + "| body:" + resp.body().string());
  83. } else {
  84. logger.debug("成功处理:" + zfile.getName());
  85. }
  86. } catch (Exception e) {
  87. throw new RuntimeException(e);
  88. } finally {
  89. IOUtils.closeQuietly(resp);
  90. }
  91. }
  92. }