DzCardsServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package com.ruoyi.dz.service.impl;
  2. import java.util.*;
  3. import java.util.function.Function;
  4. import java.util.stream.Collectors;
  5. import cn.hutool.core.util.RandomUtil;
  6. import com.fasterxml.jackson.annotation.JsonFormat;
  7. import com.google.common.collect.Maps;
  8. import com.google.common.collect.Sets;
  9. import com.ruoyi.common.annotation.Excel;
  10. import com.ruoyi.common.core.domain.entity.SysDept;
  11. import com.ruoyi.common.core.domain.entity.SysUser;
  12. import com.ruoyi.common.enums.ExamType;
  13. import com.ruoyi.common.utils.DateUtils;
  14. import com.ruoyi.common.utils.NumberUtils;
  15. import com.ruoyi.common.utils.SecurityUtils;
  16. import com.ruoyi.criteria.CardCriteria;
  17. import com.ruoyi.dz.domain.*;
  18. import com.ruoyi.dz.mapper.*;
  19. import com.ruoyi.enums.*;
  20. import com.ruoyi.system.mapper.SysDeptMapper;
  21. import com.ruoyi.system.mapper.SysUserMapper;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import com.ruoyi.dz.service.IDzCardsService;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import javax.validation.ValidationException;
  28. /**
  29. * 学习卡Service业务层处理
  30. *
  31. * @author ruoyi
  32. * @date 2025-09-12
  33. */
  34. @Service
  35. public class DzCardsServiceImpl implements IDzCardsService
  36. {
  37. @Autowired
  38. private DzCardsMapper dzCardsMapper;
  39. @Autowired
  40. private DzCardsOpenMapper dzCardsOpenMapper;
  41. @Autowired
  42. private DzAgentMapper dzAgentMapper;
  43. @Autowired
  44. private SysDeptMapper sysDeptMapper;
  45. @Autowired
  46. private DzSchoolMapper dzSchoolMapper;
  47. @Autowired
  48. private DzClassesMapper dzClassesMapper;
  49. @Autowired
  50. private SysUserMapper userMapper;
  51. private final String format = "%d%08d";
  52. /**
  53. * 查询学习卡
  54. *
  55. * @param cardId 学习卡主键
  56. * @return 学习卡
  57. */
  58. @Override
  59. public DzCards selectDzCardsByCardId(Long cardId)
  60. {
  61. return dzCardsMapper.selectDzCardsByCardId(cardId);
  62. }
  63. @Override
  64. public DzCards selectDzCardsByCardNo(String cardNo)
  65. {
  66. DzCards cond = new DzCards();
  67. cond.setCardNo(cardNo);
  68. List<DzCards> list = dzCardsMapper.selectDzCardsList(cond);
  69. return list.size() != 1 ? null : list.get(0);
  70. }
  71. private List<DzCards> fillNames(List<DzCards> list) {
  72. Set<Long> classIdSet = Sets.newHashSet();
  73. Set<Long> schoolIdSet = Sets.newHashSet();
  74. Set<Long> deptIdSet = Sets.newHashSet();
  75. Set<Long> agentIdSet = Sets.newHashSet();
  76. Set<Long> cardIdSet = Sets.newHashSet();
  77. for(DzCards c : list) {
  78. if(null != c.getCardId()) {
  79. cardIdSet.add(c.getCardId());
  80. }
  81. if(null != c.getClassId()) {
  82. classIdSet.add(c.getClassId());
  83. }
  84. if(null != c.getCampusClassId()) {
  85. classIdSet.add(c.getCampusClassId());
  86. }
  87. if(null != c.getAssignSchoolId()) {
  88. schoolIdSet.add(c.getAssignSchoolId());
  89. }
  90. if(null != c.getSchoolId()) {
  91. schoolIdSet.add(c.getSchoolId());
  92. }
  93. if(null != c.getCampusId()) {
  94. schoolIdSet.add(c.getCampusId());
  95. }
  96. if(null != c.getDeptId()) {
  97. deptIdSet.add(c.getDeptId());
  98. }
  99. if(null != c.getLeafAgentId()) {
  100. agentIdSet.add(c.getLeafAgentId());
  101. }
  102. if(null != c.getAgentId()) {
  103. agentIdSet.add(c.getAgentId());
  104. }
  105. }
  106. Map<Long, String> deptMap = !deptIdSet.isEmpty() ? sysDeptMapper.selectDeptByIds(deptIdSet).stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName)) : Maps.newHashMap();
  107. Map<Long, String> schoolMap = !schoolIdSet.isEmpty() ? dzSchoolMapper.selectDzSchoolListByIds(schoolIdSet).stream().collect(Collectors.toMap(DzSchool::getId, DzSchool::getName)) : Maps.newHashMap();;
  108. Map<Long, String> agentMap = !agentIdSet.isEmpty() ? dzAgentMapper.selectDzAgentByAgentIds(agentIdSet).stream().collect(Collectors.toMap(DzAgent::getAgentId, DzAgent::getName)) : Maps.newHashMap();
  109. Map<Long, String> classesMap = !classIdSet.isEmpty() ? dzClassesMapper.selectClassesByIds(classIdSet).stream().collect(Collectors.toMap(DzClasses::getClassId, DzClasses::getName)) : Maps.newHashMap();
  110. Map<Long, SysUser> userMap = !cardIdSet.isEmpty() ? userMapper.selectUserByCardIds(cardIdSet).stream() .collect(Collectors.toMap( SysUser::getCardId, user -> user, (existing, replacement) -> existing)) : new HashMap<>();
  111. for(DzCards c : list) {
  112. c.setClassName(classesMap.get(c.getClassId()));
  113. c.setCampusClassName(classesMap.get(c.getCampusClassId()));
  114. c.setAssignSchoolName(schoolMap.get(c.getAssignSchoolId()));
  115. c.setSchoolName(schoolMap.get(c.getSchoolId()));
  116. c.setCampusName(schoolMap.get(c.getCampusId()));
  117. c.setDeptName(deptMap.get(c.getDeptId()));
  118. if(null != c.getAgentId()) {
  119. String name = agentMap.get(c.getAgentId());
  120. c.setAgentName(null != c.getLeafAgentId() && !c.getLeafAgentId().equals(c.getAgentId()) ? agentMap.get(c.getLeafAgentId()) + "(" + name + ")" : name);
  121. }
  122. if (userMap.containsKey(c.getCardId())) {
  123. SysUser user = userMap.get(c.getCardId());
  124. c.setPhonenumber(user.getPhonenumber());
  125. c.setNickName(user.getNickName());
  126. c.setExamType(user.getExamType().title());
  127. c.setLocation(user.getLocation());
  128. }
  129. }
  130. return list;
  131. }
  132. /**
  133. * 查询学习卡列表
  134. *
  135. * @param dzCards 学习卡
  136. * @return 学习卡
  137. */
  138. @Override
  139. public List<DzCards> selectDzCardsList(DzCards dzCards)
  140. {
  141. return fillNames(dzCardsMapper.selectDzCardsList(dzCards));
  142. }
  143. /**
  144. * 新增学习卡
  145. *
  146. * @param dzCards 学习卡
  147. * @return 结果
  148. */
  149. @Override
  150. public int insertDzCards(DzCards dzCards)
  151. {
  152. dzCards.setCreateTime(DateUtils.getNowDate());
  153. return dzCardsMapper.insertDzCards(dzCards);
  154. }
  155. /**
  156. * 修改学习卡
  157. *
  158. * @param dzCards 学习卡
  159. * @return 结果
  160. */
  161. @Override
  162. public int updateDzCards(DzCards dzCards)
  163. {
  164. dzCards.setUpdateTime(DateUtils.getNowDate());
  165. return dzCardsMapper.updateDzCards(dzCards);
  166. }
  167. /**
  168. * 批量删除学习卡
  169. *
  170. * @param cardIds 需要删除的学习卡主键
  171. * @return 结果
  172. */
  173. @Override
  174. public int deleteDzCardsByCardIds(Long[] cardIds)
  175. {
  176. return dzCardsMapper.deleteDzCardsByCardIds(cardIds);
  177. }
  178. /**
  179. * 删除学习卡信息
  180. *
  181. * @param cardId 学习卡主键
  182. * @return 结果
  183. */
  184. @Override
  185. public int deleteDzCardsByCardId(Long cardId)
  186. {
  187. return dzCardsMapper.deleteDzCardsByCardId(cardId);
  188. }
  189. @Override
  190. @Transactional(rollbackFor = Exception.class)
  191. public void issueCard(Long deptId, CardType type, Integer count) {
  192. Long maxNo = dzCardsMapper.selectMaxNo(type.getVal());
  193. if(null == maxNo) {
  194. maxNo = type.getVal() * 10000000L;
  195. }
  196. for(Integer i = 1; i <= count; i++) {
  197. DzCards c = new DzCards();
  198. c.setType(type.getVal());
  199. c.setCardNo(String.valueOf(maxNo + i));
  200. // String pass = RandomUtil.randomNumbers(6);
  201. // if (pass.charAt(0) == '0') {
  202. // pass = RandomUtil.randomString("123456789", 1) + pass.substring(1);
  203. // }
  204. String pass = NumberUtils.generatePwd(6);
  205. c.setPassword(pass);
  206. c.setDistributeStatus(CardDistributeStatus.Free.getVal());
  207. c.setStatus(CardStatus.Free.getVal());
  208. c.setTimeStatus(CardTimeStatus.Valid.getVal());
  209. c.setPayStatus(PayStatus.UnPay.getVal());
  210. c.setIsSettlement(0);
  211. c.setDeptId(deptId);
  212. dzCardsMapper.insertDzCards(c);
  213. }
  214. }
  215. @Override
  216. @Transactional(rollbackFor = Exception.class)
  217. public void assignCard(Long agentId, Long leafAgentId, CardType cardType, String beginNo, String endNo, String location, ExamType examType, Long schoolId, Integer days) {
  218. CardCriteria cond = new CardCriteria();
  219. cond.setType(cardType.getVal());
  220. cond.setStartNo(beginNo);
  221. cond.setEndNo(endNo);
  222. DzCards dzCards = new DzCards();
  223. dzCardsMapper.selectListByCond(cond).stream().forEach(c -> {
  224. dzCards.setCardId(c.getCardId());
  225. if(null != c.getAgentId() && !c.getAgentId().equals(agentId)) {
  226. throw new ValidationException("卡分配错误");
  227. }
  228. dzCards.setAgentId(agentId);
  229. dzCards.setLeafAgentId(leafAgentId);
  230. dzCards.setDistributeTime(DateUtils.getNowDate());
  231. dzCards.setAssignLocation(location);
  232. dzCards.setAssignExamType(null != examType ? examType.name() : null);
  233. dzCards.setAssignSchoolId(schoolId);
  234. dzCards.setDistributeStatus(CardDistributeStatus.Assign.getVal());
  235. dzCards.setPayStatus(PayStatus.UnPay.getVal()); // TODO 暂时分配即开卡
  236. dzCards.setStatus(CardStatus.Open.getVal());
  237. dzCards.setDays(days);
  238. dzCardsMapper.updateDzCards(dzCards);
  239. });
  240. }
  241. @Override
  242. @Transactional(rollbackFor = Exception.class)
  243. public Boolean openCard(Long agentId, String location, Long schoolId, String beginNo, String endNo, Integer days) {
  244. DzCardsOpen newOpen = new DzCardsOpen();
  245. newOpen.setAgentId(agentId);
  246. newOpen.setStartNo(beginNo);
  247. newOpen.setEndNo(endNo);
  248. newOpen.setEndDate(DateUtils.addDays(DateUtils.getNowDate(), 14)); // TODO MF 卡默认有效期
  249. newOpen.setSchoolId(schoolId);
  250. newOpen.setLocation(location);
  251. newOpen.setSender(SecurityUtils.getLoginUser().getUser().getNickName());
  252. newOpen.setIsReopen(0);
  253. newOpen.setStatus(RequestStatus.Accept.getVal());
  254. dzCardsOpenMapper.insertDzCardsOpen(newOpen);
  255. // TODO MF 检查已经使用的或无效的
  256. CardCriteria cond = new CardCriteria();
  257. cond.setStartNo(beginNo);
  258. cond.setEndNo(endNo);
  259. List<DzCards> cards = dzCardsMapper.selectListByCond(cond);
  260. if(cards.stream().filter(t -> !t.getPayStatus().equals(PayStatus.UnPay.getVal())).count() > 0) {
  261. throw new ValidationException("请求打开已开卡: " + beginNo + "-" + endNo);
  262. }
  263. DzAgent dzAgent = dzAgentMapper.selectDzAgentByAgentId(agentId);
  264. DzCards dzCards = new DzCards();
  265. cards.stream().forEach(c -> {
  266. dzCards.setCardId(c.getCardId());
  267. dzCards.setPayTime(DateUtils.getNowDate());
  268. dzCards.setPayStatus(PayStatus.Paid.getVal());
  269. dzCards.setStatus(CardStatus.Paid.getVal());
  270. dzCards.setDistributeStatus(CardDistributeStatus.Assign.getVal());
  271. dzCards.setAssignLocation(location);
  272. dzCards.setAssignSchoolId(schoolId);
  273. if(null != dzAgent.getParentId()) {
  274. dzCards.setAgentId(dzAgent.getParentId());
  275. dzCards.setLeafAgentId(dzAgent.getAgentId());
  276. } else {
  277. dzCards.setAgentId(dzAgent.getAgentId());
  278. dzCards.setLeafAgentId(dzAgent.getAgentId());
  279. }
  280. dzCards.setDays(days);
  281. dzCardsMapper.updateDzCards(dzCards);
  282. });
  283. // TODO MF 检查已经使用的或无效的
  284. return true;
  285. }
  286. @Override
  287. public Boolean requestOpenCard(DzCardsOpen dzCardsOpen) {
  288. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  289. if(UserTypeEnum.Agent.getVal().equals(sysUser.getUserType())) {
  290. dzCardsOpen.setAgentId(sysUser.getUserTypeId());
  291. }
  292. dzCardsOpen.setStatus(RequestStatus.Submit.getVal());
  293. dzCardsOpen.setEndDate(DateUtils.addDays(DateUtils.getNowDate(), 14));
  294. dzCardsOpen.setIsReopen(0);
  295. dzCardsOpenMapper.insertDzCardsOpen(dzCardsOpen);
  296. return true;
  297. }
  298. @Override
  299. @Transactional(rollbackFor = Exception.class)
  300. public Boolean confirmOpenCard(DzCardsOpen dzCardsOpen, SysUser sysUser) {
  301. DzCardsOpen exist = dzCardsOpenMapper.selectDzCardsOpenById(dzCardsOpen.getId());
  302. if(UserTypeEnum.Agent.getVal().equals(sysUser.getUserType()) && !exist.getAgentId().equals(dzCardsOpen.getAgentId())) {
  303. throw new ValidationException("不可修改他人申请");
  304. }
  305. if(RequestStatus.Accept.getVal().equals(exist.getStatus())) {
  306. throw new ValidationException("不可修改已经通过的申请");
  307. }
  308. if(!RequestStatus.Submit.getVal().equals(exist.getStatus())) {
  309. throw new ValidationException("状态无效");
  310. }
  311. DzCardsOpen upOpen = new DzCardsOpen();
  312. upOpen.setId(dzCardsOpen.getId());
  313. upOpen.setStatus(RequestStatus.Accept.getVal().equals(dzCardsOpen.getStatus()) ? RequestStatus.Accept.getVal() : RequestStatus.Reject.getVal());
  314. upOpen.setAuditDesc(dzCardsOpen.getAuditDesc());
  315. if(RequestStatus.Accept.getVal().equals(upOpen.getStatus())) {
  316. CardCriteria cond = new CardCriteria();
  317. cond.setStartNo(exist.getStartNo());
  318. cond.setEndNo(exist.getEndNo());
  319. List<DzCards> cards = dzCardsMapper.selectListByCond(cond);
  320. if(cards.stream().filter(t -> !t.getPayStatus().equals(PayStatus.UnPay.getVal())).count() > 0) {
  321. throw new ValidationException("重复打开已开卡: " + exist.getStartNo() + "-" + exist.getEndNo());
  322. }
  323. DzCards dzCards = new DzCards();
  324. cards.stream().forEach(c -> {
  325. dzCards.setCardId(c.getCardId());
  326. dzCards.setAssignLocation(exist.getLocation());
  327. dzCards.setAssignSchoolId(exist.getSchoolId());
  328. DzAgent dzAgent = dzAgentMapper.selectDzAgentByAgentId(exist.getAgentId());
  329. if(null != dzAgent.getParentId()) {
  330. dzCards.setAgentId(dzAgent.getParentId());
  331. dzCards.setLeafAgentId(dzAgent.getAgentId());
  332. } else {
  333. dzCards.setAgentId(dzAgent.getAgentId());
  334. dzCards.setLeafAgentId(dzAgent.getAgentId());
  335. }
  336. dzCards.setPayTime(DateUtils.getNowDate());
  337. dzCards.setPayStatus(PayStatus.Paid.getVal());
  338. dzCards.setStatus(CardStatus.Paid.getVal());
  339. dzCards.setDistributeStatus(CardDistributeStatus.Assign.getVal());
  340. dzCardsMapper.updateDzCards(dzCards);
  341. });
  342. }
  343. dzCardsOpenMapper.updateDzCardsOpen(upOpen);
  344. return true;
  345. }
  346. @Override
  347. @Transactional(rollbackFor = Exception.class)
  348. public void changeCard(CardAction action, Long[] cardIds) {
  349. List<DzCards> cards = dzCardsMapper.selectCardsByCardIds(cardIds);
  350. DzCards up = new DzCards();
  351. if(CardAction.Pay.equals(action)) {
  352. if(cards.stream().filter(t -> !t.getPayStatus().equals(PayStatus.UnPay.getVal())).count() > 0) {
  353. throw new ValidationException("重复支付已支付卡: " + StringUtils.join(cardIds, ","));
  354. }
  355. up.setPayStatus(PayStatus.Paid.getVal());
  356. up.setStatus(CardStatus.Paid.getVal());
  357. up.setPayTime(DateUtils.getNowDate());
  358. } else if(CardAction.Close.equals(action)) {
  359. if(cards.stream().filter(t -> t.getDistributeStatus().equals(CardDistributeStatus.Close.getVal())).count() > 0) {
  360. throw new ValidationException("重复关闭卡: " + StringUtils.join(cardIds, ","));
  361. }
  362. up.setDistributeStatus(CardDistributeStatus.Close.getVal());
  363. up.setCloseTime(DateUtils.getNowDate());
  364. } else if(CardAction.ReOpen.equals(action)) {
  365. if(cards.stream().filter(t -> !t.getDistributeStatus().equals(CardDistributeStatus.Close.getVal())).count() > 0) {
  366. throw new ValidationException("重开非关闭卡: " + StringUtils.join(cardIds, ","));
  367. }
  368. up.setDistributeStatus(CardDistributeStatus.Assign.getVal());
  369. } else if(CardAction.Refund.equals(action)) {
  370. if(cards.stream().filter(t -> t.getPayStatus().equals(PayStatus.Refund.getVal())).count() > 0) {
  371. throw new ValidationException("重复退款: " + StringUtils.join(cardIds, ","));
  372. }
  373. up.setDistributeStatus(CardDistributeStatus.Close.getVal());
  374. up.setPayStatus(PayStatus.Refund.getVal());
  375. up.setRefundTime(DateUtils.getNowDate());
  376. } else if(CardAction.Settlement.equals(action)) {
  377. if(cards.stream().filter(t -> null != t.getIsSettlement() && t.getIsSettlement().equals(1)).count() > 0) {
  378. throw new ValidationException("有卡已经结算: " + StringUtils.join(cardIds, ","));
  379. }
  380. up.setSettlementTime(DateUtils.getNowDate());
  381. up.setIsSettlement(1);
  382. } else if(CardAction.Renew.equals(action)) {
  383. } else {
  384. throw new ValidationException("无效操作");
  385. }
  386. for(DzCards card : cards) {
  387. up.setCardId(card.getCardId());
  388. if(CardAction.Renew.equals(action)) {
  389. card.setOutDate(DateUtils.addYears(card.getOutDate(), 3)); // TODO 暂时都续费三年?
  390. }
  391. dzCardsMapper.updateDzCards(up);
  392. }
  393. }
  394. public Boolean changeCampus(Long campusId, String beginNo, String endNo) {
  395. CardCriteria cond = new CardCriteria();
  396. cond.setStartNo(beginNo);
  397. cond.setEndNo(endNo);
  398. List<DzCards> cards = dzCardsMapper.selectListByCond(cond);
  399. DzCards dzCards = new DzCards();
  400. cards.stream().forEach(c -> {
  401. dzCards.setCardId(c.getCardId());
  402. dzCards.setCampusId(campusId);
  403. dzCardsMapper.updateDzCards(dzCards);
  404. });
  405. return true;
  406. }
  407. @Override
  408. public List<DzCards> selectCardsByCardIds(List<Long> cardIds){
  409. //cardIds转换为数组
  410. return dzCardsMapper.selectCardsByCardIds(cardIds.toArray(new Long[0]));
  411. }
  412. }