SubjectServiceImpl.java 33 KB

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