SubjectServiceImpl.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. package cn.com.qmth.scancentral.service.impl;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.nio.charset.Charset;
  7. import java.util.*;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.LinkedBlockingQueue;
  10. import java.util.concurrent.ThreadPoolExecutor;
  11. import java.util.concurrent.TimeUnit;
  12. import org.apache.commons.collections4.CollectionUtils;
  13. import org.apache.commons.io.IOUtils;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  21. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  22. import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
  23. import com.linuxense.javadbf.DBFDataType;
  24. import com.linuxense.javadbf.DBFField;
  25. import com.linuxense.javadbf.DBFWriter;
  26. import com.qmth.boot.core.exception.ParameterException;
  27. import com.qmth.boot.core.exception.StatusException;
  28. import com.qmth.boot.core.fss.store.FileStore;
  29. import com.qmth.boot.tools.uuid.FastUUID;
  30. import cn.com.qmth.scancentral.bean.ImportSubjectDomain;
  31. import cn.com.qmth.scancentral.consumer.AnswerDataExportConsumer;
  32. import cn.com.qmth.scancentral.consumer.SliceDataExportConsumer;
  33. import cn.com.qmth.scancentral.dao.SubjectDao;
  34. import cn.com.qmth.scancentral.entity.ExamEntity;
  35. import cn.com.qmth.scancentral.entity.MarkSiteEntity;
  36. import cn.com.qmth.scancentral.entity.SubjectEntity;
  37. import cn.com.qmth.scancentral.enums.AsyncTaskStatus;
  38. import cn.com.qmth.scancentral.enums.ExamStatus;
  39. import cn.com.qmth.scancentral.enums.ScanStatus;
  40. import cn.com.qmth.scancentral.service.AsyncTaskService;
  41. import cn.com.qmth.scancentral.service.BatchPaperService;
  42. import cn.com.qmth.scancentral.service.ExamService;
  43. import cn.com.qmth.scancentral.service.FileService;
  44. import cn.com.qmth.scancentral.service.MarkSiteService;
  45. import cn.com.qmth.scancentral.service.OmrGroupService;
  46. import cn.com.qmth.scancentral.service.StudentService;
  47. import cn.com.qmth.scancentral.service.SubjectService;
  48. import cn.com.qmth.scancentral.support.SpringContextHolder;
  49. import cn.com.qmth.scancentral.util.Calculator;
  50. import cn.com.qmth.scancentral.util.DateUtil;
  51. import cn.com.qmth.scancentral.util.FileUtil;
  52. import cn.com.qmth.scancentral.vo.ExportCetMarkingQueryVo;
  53. import cn.com.qmth.scancentral.vo.ExportCetVo;
  54. import cn.com.qmth.scancentral.vo.OmrGroupVo;
  55. import cn.com.qmth.scancentral.vo.UpdateTimeVo;
  56. import cn.com.qmth.scancentral.vo.omr.OmrSubjectVo;
  57. import cn.com.qmth.scancentral.vo.subject.DataExportTaskVo;
  58. import cn.com.qmth.scancentral.vo.subject.ScanProgressVo;
  59. import cn.com.qmth.scancentral.vo.subject.SubjectBreachVo;
  60. import cn.com.qmth.scancentral.vo.subject.SubjectCustStatusVo;
  61. import cn.com.qmth.scancentral.vo.subject.SubjectScanProgressVo;
  62. import cn.com.qmth.scancentral.vo.subject.TaskIdVo;
  63. @Service
  64. public class SubjectServiceImpl extends MppServiceImpl<SubjectDao, SubjectEntity> implements SubjectService {
  65. private static final Logger log = LoggerFactory.getLogger(SubjectService.class);
  66. private static ExecutorService exec;
  67. @Autowired
  68. private ExamService examService;
  69. @Autowired
  70. private StudentService studentService;
  71. @Autowired
  72. private FileService fileService;
  73. @Autowired
  74. private FileStore fileStore;
  75. @Autowired
  76. private BatchPaperService batchPaperService;
  77. @Autowired
  78. private AsyncTaskService asyncTaskService;
  79. @Autowired
  80. private MarkSiteService markSiteService;
  81. @Autowired
  82. private OmrGroupService omrGroupService;
  83. static {
  84. int threadCount = 5;
  85. exec = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.SECONDS,
  86. new LinkedBlockingQueue<>(threadCount * 2), r -> {
  87. Thread t = new Thread(r);
  88. return t;
  89. }, (r, executor) -> {
  90. if (!executor.isShutdown()) {
  91. try {
  92. executor.getQueue().put(r);
  93. } catch (InterruptedException e) {
  94. throw new RuntimeException(e);
  95. }
  96. }
  97. });
  98. }
  99. @Override
  100. public List<SubjectEntity> listByExamId(Long examId) {
  101. QueryWrapper<SubjectEntity> queryWrapper = new QueryWrapper<>();
  102. queryWrapper.lambda().eq(SubjectEntity::getExamId, examId);
  103. queryWrapper.lambda().orderByAsc(SubjectEntity::getCode);
  104. return this.list(queryWrapper);
  105. }
  106. @Override
  107. public SubjectEntity findByExamIdAndCode(Long examId, String code) {
  108. QueryWrapper<SubjectEntity> queryWrapper = new QueryWrapper<>();
  109. queryWrapper.lambda().eq(SubjectEntity::getExamId, examId);
  110. queryWrapper.lambda().eq(SubjectEntity::getCode, code);
  111. return this.getOne(queryWrapper);
  112. }
  113. @Override
  114. public int countByExamId(Long examId) {
  115. QueryWrapper<SubjectEntity> wrapper = new QueryWrapper<>();
  116. LambdaQueryWrapper<SubjectEntity> lw = wrapper.lambda();
  117. lw.eq(SubjectEntity::getExamId, examId);
  118. return this.count(wrapper);
  119. }
  120. @Transactional
  121. @Override
  122. public int importSubject(List<ImportSubjectDomain> subjects) {
  123. Map<Long, Set<String>> map = new HashMap<Long, Set<String>>();
  124. List<SubjectEntity> list = this.list();
  125. for (SubjectEntity subjectEntity : list) {
  126. Set<String> set = map.get(subjectEntity.getExamId());
  127. if (set == null) {
  128. set = new HashSet<String>();
  129. }
  130. set.add(subjectEntity.getCode());
  131. map.put(subjectEntity.getExamId(), set);
  132. }
  133. List<SubjectEntity> saveList = new ArrayList<SubjectEntity>();
  134. for (ImportSubjectDomain importSubjectDomain : subjects) {
  135. Set<String> set = map.get(importSubjectDomain.getExamId());
  136. if (set != null) {
  137. if (!set.contains(importSubjectDomain.getSubjectCode())) {
  138. SubjectEntity subjectEntity = new SubjectEntity(importSubjectDomain.getExamId(),
  139. importSubjectDomain.getSubjectCode(), importSubjectDomain.getSubjectName());
  140. set.add(importSubjectDomain.getSubjectCode());
  141. saveList.add(subjectEntity);
  142. }
  143. } else {
  144. set = new HashSet<String>();
  145. SubjectEntity subjectEntity = new SubjectEntity(importSubjectDomain.getExamId(),
  146. importSubjectDomain.getSubjectCode(), importSubjectDomain.getSubjectName());
  147. set.add(importSubjectDomain.getSubjectCode());
  148. saveList.add(subjectEntity);
  149. }
  150. map.put(importSubjectDomain.getExamId(), set);
  151. }
  152. this.saveOrUpdateBatchByMultiId(saveList);
  153. return saveList.size();
  154. }
  155. @Override
  156. public ScanProgressVo scanProgress(Long examId, String subjectCode) {
  157. ScanProgressVo ret = new ScanProgressVo();
  158. // List<SubjectScanProgressVo> subjects =
  159. // studentService.scanProgress(examId, subjectCode);
  160. List<SubjectScanProgressVo> subjects = new ArrayList<>();
  161. if (StringUtils.isNotBlank(subjectCode)) {
  162. SubjectEntity subject = this.findByExamIdAndCode(examId, subjectCode);
  163. SubjectScanProgressVo vo = new SubjectScanProgressVo();
  164. vo.setSubjectCode(subject.getCode());
  165. vo.setSubjectName(subject.getName());
  166. vo.setUnexistCount(studentService.getUnscannedCountByExamAndSubject(examId, subject.getCode()));
  167. vo.setStudentCount(studentService.countByExamIdAndSubjectCode(examId, subject.getCode()));
  168. subjects.add(vo);
  169. } else {
  170. List<SubjectEntity> subjectList = this.listByExamId(examId);
  171. for (SubjectEntity subject : subjectList) {
  172. SubjectScanProgressVo vo = new SubjectScanProgressVo();
  173. vo.setSubjectCode(subject.getCode());
  174. vo.setSubjectName(subject.getName());
  175. vo.setUnexistCount(studentService.getUnscannedCountByExamAndSubject(examId, subject.getCode()));
  176. vo.setStudentCount(studentService.countByExamIdAndSubjectCode(examId, subject.getCode()));
  177. subjects.add(vo);
  178. }
  179. }
  180. ret.setSubjects(subjects);
  181. Calendar now = Calendar.getInstance();
  182. long endTime = now.getTimeInMillis();
  183. now.add(Calendar.HOUR_OF_DAY, -1);
  184. long startTime = now.getTimeInMillis();
  185. Integer inTimeScannedCount = batchPaperService.getScanStudentCount(examId, null, startTime, endTime);
  186. if (CollectionUtils.isNotEmpty(subjects)) {
  187. for (SubjectScanProgressVo vo : subjects) {
  188. vo.setScannedCount(vo.getStudentCount() - vo.getUnexistCount());
  189. vo.setProgress(Calculator.percentage(vo.getScannedCount(), vo.getStudentCount(), 2));
  190. if (inTimeScannedCount == null || inTimeScannedCount == 0) {
  191. vo.setEstimation("-");
  192. } else {
  193. double speed = Calculator.divide(endTime - startTime, inTimeScannedCount, 6);
  194. double es = Calculator.divide(Calculator.multiply(speed, vo.getUnexistCount()), 3600000, 2);
  195. vo.setEstimation(es + "h");
  196. }
  197. }
  198. }
  199. ret.setTotal(new SubjectScanProgressVo());
  200. ret.getTotal().setStudentCount(studentService.getCountByExam(examId));
  201. ret.getTotal().setScannedCount(studentService.getCountByExamAndScanStatus(examId, ScanStatus.SCANNED));
  202. ret.getTotal().setUnexistCount(ret.getTotal().getStudentCount() - ret.getTotal().getScannedCount());
  203. ret.getTotal().setProgress(
  204. Calculator.percentage(ret.getTotal().getScannedCount(), ret.getTotal().getStudentCount(), 2));
  205. if (inTimeScannedCount == null || inTimeScannedCount == 0) {
  206. ret.getTotal().setEstimation("-");
  207. } else {
  208. double speed = Calculator.divide(endTime - startTime, inTimeScannedCount, 6);
  209. double es = Calculator.divide(Calculator.multiply(speed, ret.getTotal().getUnexistCount()), 3600000, 2);
  210. ret.getTotal().setEstimation(es + "h");
  211. }
  212. return ret;
  213. }
  214. @Override
  215. public List<SubjectBreachVo> breachList(Long examId) {
  216. List<SubjectBreachVo> ret = new ArrayList<>();
  217. List<SubjectEntity> subjects = listByExamId(examId);
  218. if (CollectionUtils.isNotEmpty(subjects)) {
  219. for (SubjectEntity e : subjects) {
  220. SubjectBreachVo vo = new SubjectBreachVo();
  221. ret.add(vo);
  222. vo.setSubjectCode(e.getCode());
  223. vo.setSubjectName(e.getName());
  224. String url = fileService.getBreachInfoUri(examId, e.getCode());
  225. try {
  226. if (fileStore.exist(url)) {
  227. vo.setBreachCount(Integer.valueOf(FileUtil.readFileContent(fileStore.read(url)).trim()));
  228. } else {
  229. vo.setBreachCount(0);
  230. }
  231. } catch (Exception e1) {
  232. throw new RuntimeException("文件访问异常", e1);
  233. }
  234. }
  235. }
  236. return ret;
  237. }
  238. @Override
  239. public List<SubjectCustStatusVo> custStatusList(Long examId) {
  240. List<SubjectCustStatusVo> ret = new ArrayList<>();
  241. List<SubjectEntity> subjects = listByExamId(examId);
  242. if (CollectionUtils.isNotEmpty(subjects)) {
  243. for (SubjectEntity e : subjects) {
  244. SubjectCustStatusVo vo = new SubjectCustStatusVo();
  245. ret.add(vo);
  246. vo.setSubjectCode(e.getCode());
  247. vo.setSubjectName(e.getName());
  248. String url = fileService.getCustStatusInfoUri(examId, e.getCode());
  249. try {
  250. if (fileStore.exist(url)) {
  251. vo.setCustStatusCount(Integer.valueOf(FileUtil.readFileContent(fileStore.read(url)).trim()));
  252. } else {
  253. vo.setCustStatusCount(0);
  254. }
  255. } catch (Exception e1) {
  256. throw new RuntimeException("文件访问异常", e1);
  257. }
  258. }
  259. }
  260. return ret;
  261. }
  262. @Override
  263. public TaskIdVo answerDataExport(Long examId, String subjectCode) {
  264. ExportCetMarkingQueryVo req = new ExportCetMarkingQueryVo();
  265. req.setExamId(examId);
  266. req.setSubjectCode(subjectCode);
  267. int scount = studentService.countCetMarking(req);
  268. if (scount == 0) {
  269. throw new ParameterException("没有考生信息");
  270. }
  271. if (studentService.existUncheck(examId, subjectCode)) {
  272. throw new ParameterException("有缺考待确认考生");
  273. }
  274. if (studentService.existImageCheckUnFinish(examId, subjectCode)) {
  275. throw new ParameterException("有未完成的图片检查");
  276. }
  277. if (StringUtils.isBlank(examService.getById(examId).getScanSite())) {
  278. throw new ParameterException("扫描点代码未设置");
  279. }
  280. DataExportTaskVo vo = new DataExportTaskVo();
  281. vo.setTaskId(FastUUID.get());
  282. vo.setTotalCount(scount);
  283. vo.setExamId(examId);
  284. vo.setSubjectCode(subjectCode);
  285. vo.setStatus(AsyncTaskStatus.RUNNING);
  286. vo.setProgress(0.0);
  287. AnswerDataExportConsumer com = SpringContextHolder.getBean(AnswerDataExportConsumer.class);
  288. com.setVo(vo);
  289. exec.execute(com);
  290. asyncTaskService.addTask(vo);
  291. return TaskIdVo.create(vo.getTaskId());
  292. }
  293. @Override
  294. public TaskIdVo sliceDataExport(Long examId, String subjectCode) {
  295. ExportCetMarkingQueryVo req = new ExportCetMarkingQueryVo();
  296. req.setExamId(examId);
  297. req.setSubjectCode(subjectCode);
  298. int scount = studentService.countCetMarking(req);
  299. if (scount == 0) {
  300. throw new ParameterException("没有考生信息");
  301. }
  302. if (studentService.existUncheck(examId, subjectCode)) {
  303. throw new ParameterException("有缺考待确认考生");
  304. }
  305. if (studentService.existImageCheckUnFinish(examId, subjectCode)) {
  306. throw new ParameterException("有未完成的图片检查");
  307. }
  308. if (StringUtils.isBlank(examService.getById(examId).getScanSite())) {
  309. throw new ParameterException("扫描点代码未设置");
  310. }
  311. DataExportTaskVo vo = new DataExportTaskVo();
  312. vo.setTaskId(FastUUID.get());
  313. vo.setTotalCount(scount);
  314. vo.setExamId(examId);
  315. vo.setSubjectCode(subjectCode);
  316. vo.setStatus(AsyncTaskStatus.RUNNING);
  317. vo.setProgress(0.0);
  318. SliceDataExportConsumer com = SpringContextHolder.getBean(SliceDataExportConsumer.class);
  319. com.setVo(vo);
  320. exec.execute(com);
  321. asyncTaskService.addTask(vo);
  322. return TaskIdVo.create(vo.getTaskId());
  323. }
  324. @Override
  325. public void answerDataExportDispose(DataExportTaskVo vo) {
  326. try {
  327. File temDir = new File("temp/" + FastUUID.get() + "/");
  328. temDir.mkdirs();
  329. File dbf = new File(temDir.getAbsolutePath() + "/answer-data.dbf");
  330. dbf.createNewFile();
  331. ExportCetMarkingQueryVo req = new ExportCetMarkingQueryVo();
  332. req.setExamId(vo.getExamId());
  333. req.setSubjectCode(vo.getSubjectCode());
  334. req.setPageSize(10000);
  335. int pageNumber = 0;
  336. req.setPageNumber(pageNumber);
  337. Set<String> examNumbers = new HashSet<>();
  338. Map<String, MarkSiteEntity> cms = readCetMarking(vo.getExamId(), vo.getSubjectCode());
  339. Map<String, String> breachMap = breachMap(vo.getExamId(), vo.getSubjectCode());
  340. Map<String, String> custStatusMap = custStatusMap(vo.getExamId(), vo.getSubjectCode());
  341. List<ExportCetVo> list = new ArrayList<>();
  342. long startId = 0;
  343. while (true) {
  344. // req.setPageNumber(++pageNumber);
  345. req.setStartId(startId);
  346. List<ExportCetVo> temp = studentService.exportCetData(req);
  347. if (CollectionUtils.isEmpty(temp)) {
  348. break;
  349. }
  350. startId = temp.get(temp.size()-1).getId();
  351. list.addAll(temp);
  352. log.warn("【DBF导出】查询考生数据,当前第{}页共{}条! taskId:{}",req.getPageNumber(), list.size(), vo.getTaskId());
  353. }
  354. if (CollectionUtils.isEmpty(list)) {
  355. throw new StatusException("没有需要导出的数据");
  356. }
  357. exportAnswer(examNumbers, breachMap, custStatusMap, dbf, list, cms, vo);
  358. ExamEntity exam = examService.getById(vo.getExamId());
  359. SubjectEntity subejct = this.findByExamIdAndCode(vo.getExamId(), vo.getSubjectCode());
  360. vo.setFileName(subejct.getName() + "_" + exam.getScanSite() + "_OMR.dbf");
  361. vo.setFile(dbf);
  362. vo.setTempDir(temDir);
  363. vo.setStatus(AsyncTaskStatus.SUCCESS);
  364. } catch (StatusException e) {
  365. vo.setErrMsg(e.getMessage());
  366. vo.setStatus(AsyncTaskStatus.FAILED);
  367. } catch (ParameterException e) {
  368. vo.setErrMsg(e.getMessage());
  369. vo.setStatus(AsyncTaskStatus.FAILED);
  370. } catch (Exception e) {
  371. vo.setErrMsg("系统异常");
  372. vo.setStatus(AsyncTaskStatus.FAILED);
  373. log.error("导出扫描答案DBF异常", e);
  374. }
  375. }
  376. private void exportAnswer(Set<String> examNumbers, Map<String, String> breachMap, Map<String, String> custStatusMap,
  377. File dbf, List<ExportCetVo> list, Map<String, MarkSiteEntity> cms, DataExportTaskVo vo)
  378. throws FileNotFoundException {
  379. FileOutputStream fos = null;
  380. DBFWriter writer = null;
  381. try {
  382. DBFField fields[] = new DBFField[11];
  383. fields[0] = new DBFField();
  384. fields[0].setName("Zkzh");
  385. fields[0].setType(DBFDataType.CHARACTER);
  386. fields[0].setLength(15);
  387. fields[1] = new DBFField();
  388. fields[1].setName("Xm");
  389. fields[1].setType(DBFDataType.CHARACTER);
  390. fields[1].setLength(30);
  391. fields[2] = new DBFField();
  392. fields[2].setName("Qk");
  393. fields[2].setType(DBFDataType.NUMERIC);
  394. fields[2].setLength(4);
  395. fields[3] = new DBFField();
  396. fields[3].setName("Km_h");
  397. fields[3].setType(DBFDataType.NUMERIC);
  398. fields[3].setLength(4);
  399. fields[4] = new DBFField();
  400. fields[4].setName("Smda");
  401. fields[4].setType(DBFDataType.CHARACTER);
  402. fields[4].setLength(100);
  403. fields[5] = new DBFField();
  404. fields[5].setName("Yc1");
  405. fields[5].setType(DBFDataType.NUMERIC);
  406. fields[5].setLength(4);
  407. fields[6] = new DBFField();
  408. fields[6].setName("Yc2");
  409. fields[6].setType(DBFDataType.NUMERIC);
  410. fields[6].setLength(4);
  411. fields[7] = new DBFField();
  412. fields[7].setName("Wj");
  413. fields[7].setType(DBFDataType.NUMERIC);
  414. fields[7].setLength(2);
  415. fields[8] = new DBFField();
  416. fields[8].setName("Barcode");
  417. fields[8].setType(DBFDataType.CHARACTER);
  418. fields[8].setLength(6);
  419. fields[9] = new DBFField();
  420. fields[9].setName("Pjd");
  421. fields[9].setType(DBFDataType.CHARACTER);
  422. fields[9].setLength(2);
  423. fields[10] = new DBFField();
  424. fields[10].setName("Kszt");
  425. fields[10].setType(DBFDataType.CHARACTER);
  426. fields[10].setLength(2);
  427. fos = new FileOutputStream(dbf);
  428. writer = new DBFWriter(fos, Charset.forName("gbk"));
  429. writer.setFields(fields);
  430. log.warn("【DBF导出】导出考生数据,当前共{}条! taskId:{}", list.size(), vo.getTaskId());
  431. for (ExportCetVo data : list) {
  432. if (examNumbers.contains(data.getExamNumber())) {
  433. continue;
  434. }
  435. vo.setProgressCount(vo.getProgressCount() + 1);
  436. examNumbers.add(data.getExamNumber());
  437. Object[] rowData = new Object[11];
  438. rowData[0] = data.getExamNumber();
  439. rowData[1] = data.getName();
  440. if (ExamStatus.ABSENT.equals(data.getExamStatus())) {
  441. rowData[2] = 1;
  442. } else {
  443. rowData[2] = 0;
  444. }
  445. rowData[3] = getIntFromString(data.getSubjectCode());
  446. rowData[4] = data.getAnswer();
  447. rowData[5] = data.getCardFirst();
  448. rowData[6] = data.getCardSecond();
  449. String breach = breachMap.get(data.getExamNumber());
  450. rowData[7] = StringUtils.isBlank(breach) ? 0 : getIntFromString(breach);
  451. rowData[8] = data.getPaperType();
  452. int subjectCode = Integer.valueOf(data.getExamNumber().substring(9, 10));
  453. String markingCode;
  454. if (subjectCode >= 3 && subjectCode <= 9) {
  455. markingCode = "88";
  456. } else {
  457. if (ExamStatus.ABSENT.equals(data.getExamStatus()) || "000000".equals(data.getPaperType())
  458. || "999999".equals(data.getPaperType())) {
  459. markingCode = "00";
  460. } else {
  461. int site = Integer.valueOf(data.getExamNumber().substring(10, 13));
  462. MarkSiteEntity cm = cms.get(data.getPaperType());
  463. if (cm == null) {
  464. throw new StatusException("未找到评卷点数据,条码值:" + data.getPaperType());
  465. }
  466. if (StringUtils.isBlank(cm.getOddNumber())) {
  467. throw new StatusException("奇数考场评卷点代码不能为空,条码值:" + data.getPaperType());
  468. }
  469. if (StringUtils.isBlank(cm.getEvenNumber())) {
  470. throw new StatusException("偶数考场评卷点代码不能为空,条码值:" + data.getPaperType());
  471. }
  472. if (site % 2 == 0) {
  473. markingCode = cm.getEvenNumber();
  474. } else {
  475. markingCode = cm.getOddNumber();
  476. }
  477. }
  478. }
  479. rowData[9] = markingCode;
  480. String custStatus = custStatusMap.get(data.getExamNumber());
  481. rowData[10] = StringUtils.isBlank(custStatus) ? "0" : custStatus;
  482. writer.addRecord(rowData);
  483. }
  484. } finally {
  485. if (writer != null) {
  486. writer.close();
  487. }
  488. try {
  489. if (fos != null) {
  490. fos.close();
  491. }
  492. } catch (IOException e) {
  493. }
  494. }
  495. }
  496. private Map<String, String> breachMap(Long examId, String subjectCode) throws Exception {
  497. Map<String, String> ret = new HashMap<>();
  498. String path = fileService.getBreachUri(examId, subjectCode);
  499. if (fileStore.exist(path)) {
  500. List<String> list = IOUtils.readLines(fileStore.read(path), "utf-8");
  501. if (CollectionUtils.isNotEmpty(list)) {
  502. for (int i = 1; i < list.size(); i++) {
  503. String[] ss = list.get(i).split(",");
  504. ret.put(ss[0].trim(), ss[2].trim());
  505. }
  506. }
  507. }
  508. return ret;
  509. }
  510. private Map<String, String> custStatusMap(Long examId, String subjectCode) throws Exception {
  511. Map<String, String> ret = new HashMap<>();
  512. String path = fileService.getCustStatusUri(examId, subjectCode);
  513. if (fileStore.exist(path)) {
  514. List<String> list = IOUtils.readLines(fileStore.read(path), "utf-8");
  515. if (CollectionUtils.isNotEmpty(list)) {
  516. for (int i = 1; i < list.size(); i++) {
  517. String[] ss = list.get(i).split(",");
  518. ret.put(ss[0].trim(), ss[2].trim());
  519. }
  520. }
  521. }
  522. return ret;
  523. }
  524. private Integer getIntFromString(String v) {
  525. if (v == null) {
  526. return null;
  527. }
  528. return Integer.valueOf(v);
  529. }
  530. private Map<String, MarkSiteEntity> readCetMarking(Long examId, String subejctCode) {
  531. Map<String, MarkSiteEntity> ret = new HashMap<>();
  532. List<MarkSiteEntity> list = markSiteService.findByExamAndSubject(examId, subejctCode);
  533. if (CollectionUtils.isNotEmpty(list)) {
  534. for (MarkSiteEntity e : list) {
  535. ret.put(e.getPaperType(), e);
  536. }
  537. }
  538. return ret;
  539. }
  540. @Override
  541. public void sliceDataExportDispose(DataExportTaskVo vo) {
  542. try {
  543. File temDir = new File("temp/" + FastUUID.get() + "/");
  544. temDir.mkdirs();
  545. File dbf = new File(temDir.getAbsolutePath() + "/slice-data.dbf");
  546. dbf.createNewFile();
  547. ExportCetMarkingQueryVo req = new ExportCetMarkingQueryVo();
  548. req.setExamId(vo.getExamId());
  549. req.setSubjectCode(vo.getSubjectCode());
  550. req.setPageSize(10000);
  551. int pageNumber = 0;
  552. req.setPageNumber(pageNumber);
  553. Set<String> examNumbers = new HashSet<>();
  554. Map<String, MarkSiteEntity> cms = readCetMarking(vo.getExamId(), vo.getSubjectCode());
  555. List<ExportCetVo> list = new ArrayList<>();
  556. long startId = 0;
  557. while (true) {
  558. // req.setPageNumber(++pageNumber);
  559. req.setStartId(startId);
  560. List<ExportCetVo> temp = studentService.exportCetData(req);
  561. if (CollectionUtils.isEmpty(temp)) {
  562. break;
  563. }
  564. startId = temp.get(temp.size()-1).getId();
  565. list.addAll(temp);
  566. }
  567. if (CollectionUtils.isEmpty(list)) {
  568. throw new StatusException("没有需要导出的数据");
  569. }
  570. exportSlice(examNumbers, dbf, list, cms, vo);
  571. ExamEntity exam = examService.getById(vo.getExamId());
  572. vo.setFileName(exam.getScanSite() + "_" + vo.getTotalCount() + "_"
  573. + DateUtil.format(new Date(), DateUtil.DatePatterns.YYYYMMDDHH) + ".dbf");
  574. vo.setFile(dbf);
  575. vo.setTempDir(temDir);
  576. vo.setStatus(AsyncTaskStatus.SUCCESS);
  577. } catch (StatusException e) {
  578. vo.setErrMsg(e.getMessage());
  579. vo.setStatus(AsyncTaskStatus.FAILED);
  580. } catch (ParameterException e) {
  581. vo.setErrMsg(e.getMessage());
  582. vo.setStatus(AsyncTaskStatus.FAILED);
  583. } catch (Exception e) {
  584. vo.setErrMsg("系统异常");
  585. vo.setStatus(AsyncTaskStatus.FAILED);
  586. log.error("导出打包DBF异常", e);
  587. }
  588. }
  589. private void exportSlice(Set<String> examNumbers, File dbf, List<ExportCetVo> list, Map<String, MarkSiteEntity> cms,
  590. DataExportTaskVo vo) throws FileNotFoundException {
  591. FileOutputStream fos = null;
  592. DBFWriter writer = null;
  593. try {
  594. DBFField fields[] = new DBFField[3];
  595. fields[0] = new DBFField();
  596. fields[0].setName("Zkzh");
  597. fields[0].setType(DBFDataType.CHARACTER);
  598. fields[0].setLength(15);
  599. fields[1] = new DBFField();
  600. fields[1].setName("Yjd");
  601. fields[1].setType(DBFDataType.CHARACTER);
  602. fields[1].setLength(2);
  603. fields[2] = new DBFField();
  604. fields[2].setName("Wjdx");
  605. fields[2].setType(DBFDataType.CHARACTER);
  606. fields[2].setLength(254);
  607. fos = new FileOutputStream(dbf);
  608. writer = new DBFWriter(fos, Charset.forName("gbk"));
  609. writer.setFields(fields);
  610. for (ExportCetVo data : list) {
  611. if (examNumbers.contains(data.getExamNumber())) {
  612. continue;
  613. }
  614. vo.setProgressCount(vo.getProgressCount() + 1);
  615. examNumbers.add(data.getExamNumber());
  616. String markingCode;
  617. if ("888888".equals(data.getPaperType())) {
  618. markingCode = "88";
  619. } else if (ExamStatus.ABSENT.equals(data.getExamStatus()) || "000000".equals(data.getPaperType())
  620. || "999999".equals(data.getPaperType())) {
  621. markingCode = "00";
  622. } else {
  623. int site = Integer.valueOf(data.getExamNumber().substring(10, 13));
  624. MarkSiteEntity cm = cms.get(data.getPaperType());
  625. if (cm == null) {
  626. throw new StatusException("未找到评卷点数据,条码值:" + data.getPaperType());
  627. }
  628. if (StringUtils.isBlank(cm.getOddNumber())) {
  629. throw new StatusException("奇数考场评卷点代码不能为空,条码值:" + data.getPaperType());
  630. }
  631. if (StringUtils.isBlank(cm.getEvenNumber())) {
  632. throw new StatusException("偶数考场评卷点代码不能为空,条码值:" + data.getPaperType());
  633. }
  634. if (site % 2 == 0) {
  635. markingCode = cm.getEvenNumber();
  636. } else {
  637. markingCode = cm.getOddNumber();
  638. }
  639. }
  640. Object[] rowData = new Object[3];
  641. rowData[0] = data.getExamNumber();
  642. rowData[1] = markingCode;
  643. rowData[2] = StringUtils.join(data.getSliceImageInfo(), ";") + ";";
  644. writer.addRecord(rowData);
  645. }
  646. } finally {
  647. if (writer != null) {
  648. writer.close();
  649. }
  650. try {
  651. if (fos != null) {
  652. fos.close();
  653. }
  654. } catch (IOException e) {
  655. }
  656. }
  657. }
  658. @Transactional
  659. @Override
  660. public void initSubject(Long examId) {
  661. List<SubjectEntity> subs = new ArrayList<>();
  662. subs.add(new SubjectEntity(examId, "1", "CET4"));
  663. subs.add(new SubjectEntity(examId, "2", "CET6"));
  664. subs.add(new SubjectEntity(examId, "3", "CJT4"));
  665. subs.add(new SubjectEntity(examId, "4", "CJT6"));
  666. subs.add(new SubjectEntity(examId, "5", "CGT4"));
  667. subs.add(new SubjectEntity(examId, "6", "CGT6"));
  668. subs.add(new SubjectEntity(examId, "7", "CRT4"));
  669. subs.add(new SubjectEntity(examId, "8", "CRT6"));
  670. subs.add(new SubjectEntity(examId, "9", "CFT4"));
  671. this.saveBatch(subs);
  672. }
  673. @Override
  674. public List<OmrSubjectVo> listByExamIdForOmr(Long examId) {
  675. List<SubjectEntity> ss = listByExamId(examId);
  676. if (CollectionUtils.isEmpty(ss)) {
  677. return new ArrayList<>();
  678. }
  679. List<OmrSubjectVo> ret = new ArrayList<>();
  680. for (SubjectEntity s : ss) {
  681. OmrSubjectVo vo = new OmrSubjectVo();
  682. vo.setCode(s.getCode());
  683. vo.setName(s.getName());
  684. List<OmrGroupVo> os = omrGroupService.listByExamIdAndSubjectCode(examId, s.getCode());
  685. int todoCount = 0;
  686. if (CollectionUtils.isNotEmpty(os)) {
  687. for (OmrGroupVo o : os) {
  688. todoCount = todoCount + o.getTodoCount();
  689. }
  690. }
  691. vo.setTodoCount(todoCount);
  692. ret.add(vo);
  693. }
  694. return ret;
  695. }
  696. @Override
  697. public UpdateTimeVo edit(Long examId, String subjectCode, String subjectName) {
  698. QueryWrapper<SubjectEntity> queryWrapper = new QueryWrapper<>();
  699. queryWrapper.lambda().eq(SubjectEntity::getExamId, examId);
  700. queryWrapper.lambda().eq(SubjectEntity::getName, subjectName);
  701. SubjectEntity subject = this.getOne(queryWrapper);
  702. if (subject != null) {
  703. throw new ParameterException("科目代码在本考试中已存在");
  704. }
  705. subject = this.findByExamIdAndCode(examId, subjectCode);
  706. if (subject == null) {
  707. throw new ParameterException("subject不存在");
  708. }
  709. subject.setName(subjectName);
  710. saveOrUpdateByMultiId(subject);
  711. return UpdateTimeVo.create();
  712. }
  713. }