StudentApplyServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package com.qmth.exam.reserve.service.impl;
  2. import java.io.InputStream;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.stream.Collectors;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import org.springframework.transaction.interceptor.TransactionAspectSupport;
  14. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  15. import com.baomidou.mybatisplus.core.metadata.IPage;
  16. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  17. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  18. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  19. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  20. import com.qmth.boot.core.collection.PageResult;
  21. import com.qmth.boot.core.exception.StatusException;
  22. import com.qmth.boot.tools.excel.ExcelReader;
  23. import com.qmth.boot.tools.excel.enums.ExcelType;
  24. import com.qmth.boot.tools.excel.model.DataMap;
  25. import com.qmth.exam.reserve.bean.login.LoginUser;
  26. import com.qmth.exam.reserve.bean.stdapply.AgentAndTimeVO;
  27. import com.qmth.exam.reserve.bean.stdapply.StudentApplyReq;
  28. import com.qmth.exam.reserve.bean.stdapply.StudentApplyVO;
  29. import com.qmth.exam.reserve.bean.stdapply.StudentImportVO;
  30. import com.qmth.exam.reserve.dao.ApplyTaskDao;
  31. import com.qmth.exam.reserve.dao.OperateLogDao;
  32. import com.qmth.exam.reserve.dao.StudentApplyDao;
  33. import com.qmth.exam.reserve.dao.TimePeriodDao;
  34. import com.qmth.exam.reserve.entity.ApplyTaskEntity;
  35. import com.qmth.exam.reserve.entity.CategoryEntity;
  36. import com.qmth.exam.reserve.entity.ExamSiteEntity;
  37. import com.qmth.exam.reserve.entity.OperateLogEntity;
  38. import com.qmth.exam.reserve.entity.StudentApplyEntity;
  39. import com.qmth.exam.reserve.entity.StudentEntity;
  40. import com.qmth.exam.reserve.entity.TimePeriodEntity;
  41. import com.qmth.exam.reserve.enums.CategoryLevel;
  42. import com.qmth.exam.reserve.enums.EventType;
  43. import com.qmth.exam.reserve.service.CategoryService;
  44. import com.qmth.exam.reserve.service.ExamSiteService;
  45. import com.qmth.exam.reserve.service.StudentApplyService;
  46. import com.qmth.exam.reserve.service.StudentService;
  47. import com.qmth.exam.reserve.util.DateUtil;
  48. import com.qmth.exam.reserve.util.JsonHelper;
  49. import com.qmth.exam.reserve.util.PageUtil;
  50. @Service
  51. public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, StudentApplyEntity>
  52. implements StudentApplyService {
  53. private static final String[] EXCEL_HEADER = new String[] { "学号", "姓名", "证件号", "所属教学点", "预约考点1", "预约时段1", "预约考点2",
  54. "预约时段2", "预约考点3", "预约时段3", "预约考点4", "预约时段4" };
  55. @Autowired
  56. private TimePeriodDao timePeriodDao;
  57. @Autowired
  58. private ApplyTaskDao applyTaskDao;
  59. @Autowired
  60. private OperateLogDao operateLogDao;
  61. @Autowired
  62. private CategoryService categoryService;
  63. @Autowired
  64. private StudentService studentService;
  65. @Autowired
  66. private ExamSiteService examSiteService;
  67. @Override
  68. public PageResult<StudentApplyVO> page(StudentApplyReq req) {
  69. IPage<StudentApplyVO> iPage = this.baseMapper
  70. .page(new Page<StudentApplyVO>(req.getPageNumber(), req.getPageSize()), req);
  71. return PageUtil.of(iPage);
  72. }
  73. @Transactional
  74. @Override
  75. public void cancel(LoginUser user, Long id) {
  76. // 时间判断
  77. StudentApplyEntity studentApply = this.baseMapper.selectById(id);
  78. if (studentApply == null || studentApply.getTimePeriodId() == null)
  79. throw new StatusException("考生没有预约,无法取消!");
  80. TimePeriodEntity timePeroid = this.timePeriodDao.selectById(studentApply.getTimePeriodId());
  81. if (timePeroid == null)
  82. throw new StatusException("考试时段不存在,请检查考试时段数据!");
  83. LambdaQueryWrapper<ApplyTaskEntity> wrapper = new LambdaQueryWrapper<ApplyTaskEntity>()
  84. .eq(ApplyTaskEntity::getEnable, Boolean.TRUE);
  85. ApplyTaskEntity task = applyTaskDao.selectOne(wrapper);
  86. Date applyDate = DateUtil.parse(DateUtil.getShortDateByLongTime(timePeroid.getStartTime()), "yyyy-MM-dd");
  87. Date canCancelDay = DateUtil.addValues(applyDate, 5, -task.getAllowApplyCancelDays());
  88. if (new Date().after(canCancelDay))
  89. throw new StatusException("可取消时间已过,无法取消!");
  90. studentApply.setCancel(Boolean.TRUE);
  91. this.baseMapper.updateById(studentApply);
  92. // TODO redis更新:该时段redis已预约的数量减1
  93. // 写入日志
  94. OperateLogEntity log = new OperateLogEntity();
  95. log.setCreateTime(System.currentTimeMillis());
  96. log.setUpdateTime(System.currentTimeMillis());
  97. log.setOperateId(user.getId());
  98. log.setEventType(EventType.CANCEL.toString());
  99. log.setContent(JsonHelper.toJson(studentApply));
  100. operateLogDao.insert(log);
  101. }
  102. @Transactional
  103. @Override
  104. public List<Map<String, Object>> importPreExam(LoginUser user, Long teachingId, InputStream inputStream) {
  105. checkInTime();
  106. List<DataMap> lineList = null;
  107. ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
  108. try {
  109. lineList = reader.getDataMapList();
  110. } catch (Exception e) {
  111. throw new StatusException("Excel 解析失败");
  112. }
  113. if (!Arrays.equals(EXCEL_HEADER, reader.getColumnNames())) {
  114. throw new StatusException("Excel表头错误");
  115. }
  116. if (CollectionUtils.isEmpty(lineList)) {
  117. throw new StatusException("Excel无内容");
  118. }
  119. List<Map<String, Object>> failRecords = new ArrayList<Map<String, Object>>();
  120. Map<String, Long> teachingCache = getTeachingCache();
  121. Map<String, Long> agentCache = getAgentCache(teachingId);
  122. Map<String, Long> timeCache = getTimePeriodCache();
  123. List<StudentImportVO> applyList = new ArrayList<>();
  124. AgentAndTimeVO agentTime = new AgentAndTimeVO();
  125. for (int i = 0; i < lineList.size(); i++) {
  126. List<AgentAndTimeVO> agentTimeList = new ArrayList<>();
  127. DataMap line = lineList.get(i);
  128. StudentImportVO apply = new StudentImportVO();
  129. StringBuilder msg = new StringBuilder();
  130. String studentCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[0]));
  131. if (StringUtils.isBlank(studentCode)) {
  132. msg.append(" 学号不能为空");
  133. }
  134. String name = trimAndNullIfBlank(line.get(EXCEL_HEADER[1]));
  135. if (StringUtils.isBlank(name)) {
  136. msg.append(" 姓名不能为空");
  137. }
  138. String identityNumber = trimAndNullIfBlank(line.get(EXCEL_HEADER[2]));
  139. if (StringUtils.isBlank(identityNumber)) {
  140. msg.append(" 证件号不能为空");
  141. }
  142. StudentEntity student = null;
  143. try {
  144. student = checkStd(studentCode, name, identityNumber);
  145. apply.setStudentId(student.getId());
  146. } catch (StatusException e) {
  147. msg.append(" " + e.getMessage());
  148. failRecords.add(newError(i + 1, msg.toString()));
  149. continue;
  150. }
  151. String teachingName = trimAndNullIfBlank(line.get(EXCEL_HEADER[3]));
  152. if (StringUtils.isBlank(teachingName)) {
  153. msg.append(" 所属教学点不能为空");
  154. }
  155. Long categoryId = teachingCache.get(teachingName);
  156. if (categoryId == null) {
  157. msg.append(" 所属教学点不存在");
  158. }
  159. if (!student.getCategoryId().equals(categoryId)) {
  160. msg.append(" 考生所属教学点和库中的考生教学点不匹配");
  161. }
  162. String agentName1 = trimAndNullIfBlank(line.get(EXCEL_HEADER[4]));
  163. if (StringUtils.isBlank(agentName1)) {
  164. msg.append(" 预约考点1不能为空");
  165. }
  166. agentTime = new AgentAndTimeVO();
  167. Long agentId = agentCache.get(agentName1);
  168. if (agentId == null) {
  169. msg.append(" 预约考点1不存在");
  170. }
  171. String timePeriod1 = trimAndNullIfBlank(line.get(EXCEL_HEADER[5]));
  172. if (StringUtils.isBlank(timePeriod1)) {
  173. msg.append(" 预约时段1不能为空");
  174. }
  175. Long timePeriodId = null;
  176. try {
  177. timePeriodId = checkTimePeriod(timePeriod1, timeCache);
  178. agentTime.setAgentId(agentId);
  179. agentTime.setTimePeriodId(timePeriodId);
  180. agentTimeList.add(agentTime);
  181. } catch (StatusException e) {
  182. msg.append(" " + e.getMessage());
  183. }
  184. String agentName2 = trimAndNullIfBlank(line.get(EXCEL_HEADER[6]));
  185. String timePeriod2 = trimAndNullIfBlank(line.get(EXCEL_HEADER[7]));
  186. if (StringUtils.isBlank(agentName2) && StringUtils.isBlank(timePeriod2)) {
  187. apply.setAgentTimeList(agentTimeList);
  188. applyList.add(apply);
  189. if (msg.length() > 0)
  190. failRecords.add(newError(i + 1, msg.toString()));
  191. continue;
  192. } else {
  193. agentId = agentCache.get(agentName2);
  194. if (agentId == null)
  195. msg.append(" 预约考点2不存在");
  196. try {
  197. timePeriodId = checkTimePeriod(timePeriod2, timeCache);
  198. agentTime = new AgentAndTimeVO();
  199. agentTime.setAgentId(agentId);
  200. agentTime.setTimePeriodId(timePeriodId);
  201. agentTimeList.add(agentTime);
  202. } catch (StatusException e) {
  203. msg.append(" " + e.getMessage());
  204. }
  205. }
  206. String agentName3 = trimAndNullIfBlank(line.get(EXCEL_HEADER[8]));
  207. String timePeriod3 = trimAndNullIfBlank(line.get(EXCEL_HEADER[9]));
  208. if (StringUtils.isBlank(agentName3) && StringUtils.isBlank(timePeriod3)) {
  209. apply.setAgentTimeList(agentTimeList);
  210. applyList.add(apply);
  211. if (msg.length() > 0)
  212. failRecords.add(newError(i + 1, msg.toString()));
  213. continue;
  214. } else {
  215. agentId = agentCache.get(agentName3);
  216. if (agentId == null)
  217. msg.append(" 预约考点3不存在");
  218. try {
  219. timePeriodId = checkTimePeriod(timePeriod3, timeCache);
  220. agentTime = new AgentAndTimeVO();
  221. agentTime.setAgentId(agentId);
  222. agentTime.setTimePeriodId(timePeriodId);
  223. agentTimeList.add(agentTime);
  224. } catch (StatusException e) {
  225. msg.append(" " + e.getMessage());
  226. }
  227. }
  228. String agentName4 = trimAndNullIfBlank(line.get(EXCEL_HEADER[10]));
  229. String timePeriod4 = trimAndNullIfBlank(line.get(EXCEL_HEADER[11]));
  230. if (StringUtils.isBlank(agentName4) && StringUtils.isBlank(timePeriod4)) {
  231. apply.setAgentTimeList(agentTimeList);
  232. applyList.add(apply);
  233. if (msg.length() > 0)
  234. failRecords.add(newError(i + 1, msg.toString()));
  235. continue;
  236. } else {
  237. agentId = agentCache.get(agentName4);
  238. if (agentId == null)
  239. msg.append(" 预约考点4不存在");
  240. try {
  241. timePeriodId = checkTimePeriod(timePeriod4, timeCache);
  242. agentTime = new AgentAndTimeVO();
  243. agentTime.setAgentId(agentId);
  244. agentTime.setTimePeriodId(timePeriodId);
  245. agentTimeList.add(agentTime);
  246. apply.setAgentTimeList(agentTimeList);
  247. applyList.add(apply);
  248. } catch (StatusException e) {
  249. msg.append(" " + e.getMessage());
  250. }
  251. }
  252. }
  253. if (CollectionUtils.isNotEmpty(failRecords)) {
  254. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  255. return failRecords;
  256. }
  257. for (int i = 0; i < applyList.size(); i++) {
  258. StudentImportVO vo = applyList.get(i);
  259. try {
  260. saveStdApply(vo);
  261. } catch (StatusException e) {
  262. failRecords.add(newError(i + 1, e.getMessage()));
  263. } catch (Exception e) {
  264. failRecords.add(newError(i + 1, " 系统异常"));
  265. log.error("导入异常", e);
  266. }
  267. }
  268. if (CollectionUtils.isNotEmpty(failRecords)) {
  269. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  270. }
  271. // TODO 更新redis
  272. return failRecords;
  273. }
  274. private void checkInTime() {
  275. LambdaQueryWrapper<ApplyTaskEntity> wrapper = new LambdaQueryWrapper<ApplyTaskEntity>()
  276. .eq(ApplyTaskEntity::getEnable, Boolean.TRUE);
  277. ApplyTaskEntity task = applyTaskDao.selectOne(wrapper);
  278. Date start = DateUtil.parse(DateUtil.getLongDateByLongTime(task.getOpenApplyStartTime()), null);
  279. // DateUtil.isBetwwen(start, end)
  280. }
  281. private void saveStdApply(StudentImportVO vo) {
  282. List<AgentAndTimeVO> agentTimeList = vo.getAgentTimeList();
  283. LambdaQueryWrapper<StudentApplyEntity> lm = new LambdaQueryWrapper<>();
  284. lm.eq(StudentApplyEntity::getStudentId, vo.getStudentId());
  285. this.baseMapper.delete(lm);
  286. for (AgentAndTimeVO agentTime : agentTimeList) {
  287. StudentApplyEntity entity = new StudentApplyEntity();
  288. entity.setStudentId(vo.getStudentId());
  289. entity.setCreateTime(System.currentTimeMillis());
  290. entity.setUpdateTime(System.currentTimeMillis());
  291. entity.setExamSiteId(agentTime.getAgentId());
  292. entity.setTimePeriodId(agentTime.getTimePeriodId());
  293. entity.setCancel(Boolean.FALSE);
  294. this.baseMapper.insert(entity);
  295. }
  296. }
  297. private Long checkTimePeriod(String timePeriod, Map<String, Long> timeCache) {
  298. if (timePeriod.split(" ").length != 2) {
  299. throw new StatusException(" 预约时段格式不正确");
  300. }
  301. String[] arr = timePeriod.split("-");
  302. String startTime = arr[0] + ":00";
  303. String endTime = startTime.substring(0, startTime.indexOf("日") + 1) + " " + arr[1] + ":00";
  304. Long startTimeLong = DateUtil.getLongTimeByZHDate(startTime);
  305. Long endTimeLong = DateUtil.getLongTimeByZHDate(endTime);
  306. if (timeCache.get(startTimeLong + "-" + endTimeLong) == null) {
  307. throw new StatusException(" 预约时段不存在");
  308. }
  309. return timeCache.get(startTimeLong + "-" + endTimeLong);
  310. }
  311. private Map<String, Long> getTimePeriodCache() {
  312. Map<String, Long> map = new HashMap<>();
  313. LambdaQueryWrapper<TimePeriodEntity> lm = new LambdaQueryWrapper<>();
  314. List<TimePeriodEntity> timeList = timePeriodDao.selectList(lm);
  315. for (TimePeriodEntity time : timeList) {
  316. map.put(time.getStartTime() + "-" + time.getEndTime(), time.getId());
  317. }
  318. return map;
  319. }
  320. private Map<String, Long> getTeachingCache() {
  321. LambdaQueryWrapper<CategoryEntity> lm = new LambdaQueryWrapper<>();
  322. lm.eq(CategoryEntity::getEnable, Boolean.TRUE);
  323. lm.eq(CategoryEntity::getLevel, CategoryLevel.TEACHING.getValue());
  324. List<CategoryEntity> categoryList = categoryService.list(lm);
  325. return categoryList.stream().collect(Collectors.toMap(CategoryEntity::getName, CategoryEntity::getId));
  326. }
  327. private Map<String, Long> getAgentCache(Long categoryId) {
  328. LambdaQueryWrapper<ExamSiteEntity> lm = new LambdaQueryWrapper<>();
  329. lm.eq(ExamSiteEntity::getEnable, Boolean.TRUE);
  330. lm.eq(ExamSiteEntity::getCategoryId, categoryId);
  331. List<ExamSiteEntity> categoryList = examSiteService.list(lm);
  332. return categoryList.stream().collect(Collectors.toMap(ExamSiteEntity::getName, ExamSiteEntity::getId));
  333. }
  334. private StudentEntity checkStd(String studentCode, String name, String identityNumber) {
  335. LambdaQueryWrapper<StudentEntity> lm = new LambdaQueryWrapper<>();
  336. lm.eq(StudentEntity::getStudentCode, studentCode);
  337. lm.eq(StudentEntity::getName, name);
  338. lm.eq(StudentEntity::getIdentityNumber, identityNumber);
  339. StudentEntity student = studentService.getOne(lm);
  340. if (student == null) {
  341. throw new StatusException(" 考生信息填写错误");
  342. }
  343. return student;
  344. }
  345. private Map<String, Object> newError(int lineNum, String msg) {
  346. Map<String, Object> map = new HashMap<>();
  347. map.put("lineNum", lineNum);
  348. map.put("msg", msg);
  349. return map;
  350. }
  351. private String trimAndNullIfBlank(String s) {
  352. if (StringUtils.isBlank(s)) {
  353. return null;
  354. }
  355. return s.trim();
  356. }
  357. }