CmsGroupServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package com.cyksj.service.mange.group;
  2. import cn.hutool.core.date.DateField;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.cyksj.common.EnvCommonService;
  9. import com.cyksj.common.constant.Constant;
  10. import com.cyksj.common.exception.BusinessRuntimeException;
  11. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  12. import com.cyksj.mapper.*;
  13. import com.cyksj.mapper.manage.GroupsTripsAccountReplaceRecordMapper;
  14. import com.cyksj.mapper.manage.cms.CmsUserMapper;
  15. import com.cyksj.model.entity.*;
  16. import com.cyksj.model.request.ReplaceTripsAccountReq;
  17. import com.cyksj.model.views.GroupsAccountView;
  18. import com.cyksj.service.mange.CmsGroupService;
  19. import com.cyksj.service.sms.SmsService;
  20. import com.ejlchina.searcher.BeanSearcher;
  21. import com.ejlchina.searcher.util.MapUtils;
  22. import lombok.RequiredArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.util.Arrays;
  27. import java.util.Date;
  28. import java.util.List;
  29. import java.util.Optional;
  30. import java.util.stream.Collectors;
  31. /**
  32. * @author chan
  33. * @date 2022/9/27 6:27 PM
  34. */
  35. @Slf4j
  36. @Service
  37. @RequiredArgsConstructor
  38. public class CmsGroupServiceImpl extends ServiceImpl<GroupsMapper,GroupsTrips> implements CmsGroupService {
  39. private final GoodsDonSkuMapper skuMapper;
  40. private final GroupsRelationMapper relationMapper;
  41. private final OrderDonMapper orderDonMapper;
  42. private final AccountMapper accountMapper;
  43. private final BeanSearcher beanSearcher;
  44. private final GroupsTripsAccountReplaceRecordMapper replaceRecordMapper;
  45. private final CmsUserMapper cmsUserMapper;
  46. private final GoodsDonMapper goodsDonMapper;
  47. private final SmsService smsService;
  48. private final EnvCommonService envCommonService;
  49. private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
  50. @Override
  51. public GroupsTrips issuance(GroupsTrips groups, String verifyCodes) {
  52. GoodsDonSku sku = skuMapper.selectById(groups.getSkuId());
  53. if (sku == null) {
  54. throw new BusinessRuntimeException("该商品规格不存在!");
  55. }
  56. List<String> verifyCodeList = null;
  57. if (StrUtil.isNotBlank(verifyCodes)) {
  58. verifyCodes = verifyCodes.replaceAll("\n", "");
  59. String[] verifyCodeArray = verifyCodes.split("\\*");
  60. verifyCodeList = Arrays.stream(verifyCodeArray).filter(str -> StrUtil.isNotBlank(str)).map(str -> str.trim()).collect(Collectors.toList());
  61. }
  62. groups.setNum(sku.getNum());
  63. groups.setAvailableNum(sku.getNum());
  64. if (groups.getAccountId() == null || accountMapper.selectById(groups.getAccountId()) == null) {
  65. throw new BusinessRuntimeException("该账号不存在");
  66. }
  67. groups.setStatus(GroupsTrips.Status.validity);
  68. this.save(groups);
  69. for (Integer i = 1; i <= groups.getNum(); i++) {
  70. GroupsRelation relation = new GroupsRelation();
  71. relation.setGroupsId(groups.getId());
  72. relation.setStatus(GroupsRelation.Status.none);
  73. relation.setNum(i);
  74. if (verifyCodeList != null && verifyCodeList.size() > 0 && verifyCodeList.size() >= i) {
  75. relation.setVerifyCode(verifyCodeList.get(i - 1));
  76. }
  77. relationMapper.insert(relation);
  78. }
  79. return groups;
  80. }
  81. @Override
  82. public void close(Long groupId) {
  83. GroupsTrips groupsTrips = this.getById(groupId);
  84. if(groupsTrips == null){
  85. throw new BusinessRuntimeException("非法参数.车队不存在");
  86. }
  87. Integer count = relationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  88. .eq(GroupsRelation::getGroupsId, groupId)
  89. .in(GroupsRelation::getStatus, List.of(GroupsRelation.Status.locking, GroupsRelation.Status.validity)));
  90. if (count > 0) {
  91. throw new BusinessRuntimeException("该车队已有用户购买或锁定.无法删除");
  92. }else {
  93. relationMapper.delete(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId,groupId));
  94. this.removeById(groupId);
  95. }
  96. }
  97. @Override
  98. @Transactional(rollbackFor = Throwable.class)
  99. public void setAccount(GroupsTrips groupsTrips) {
  100. int count = count(Wrappers.lambdaQuery(GroupsTrips.class).eq(GroupsTrips::getAccountId, groupsTrips.getAccountId()));
  101. if (count > 0) {
  102. throw new BusinessRuntimeException("该账号已发车.请选择未发车账户");
  103. }
  104. List<GroupsRelation> groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()).eq(GroupsRelation::getStatus, GroupsRelation.Status.validity));
  105. GroupsTrips trips = beanSearcher.searchFirst(GroupsTrips.class, MapUtils.builder().field(GroupsTrips::getId, groupsTrips.getId()).onlySelect(GroupsTrips::getTitle).build());
  106. String title = "车票";
  107. if (trips != null && StrUtil.isNotBlank(trips.getTitle())) {
  108. title = trips.getTitle();
  109. }
  110. String msg = String.format(Constant.CHANGE_GROUPS_TRIPS_ACCOUNT, title);
  111. //配置midJourney安全码
  112. String verifyCodes = groupsTrips.getVerifyCodes();
  113. List<String> verifyCodeList = null;
  114. if (StrUtil.isNotBlank(verifyCodes)) {
  115. verifyCodes = verifyCodes.replaceAll("\n", "");
  116. String[] verifyCodesArray = verifyCodes.split("\\*");
  117. verifyCodeList = Arrays.stream(verifyCodesArray).filter(str -> StrUtil.isNotBlank(str)).map(str -> str.trim()).collect(Collectors.toList());
  118. }
  119. Boolean isPrdEnv = envCommonService.isPrdEnv();
  120. for (int i = 0; i < groupsRelations.size(); i++) {
  121. GroupsRelation relation = groupsRelations.get(i);
  122. if (relation.getExpiryTime() == null) {
  123. List<OrderDon> orderDonList = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getUserId, relation.getUserId()).eq(OrderDon::getRelationId, relation.getId()).eq(OrderDon::getStatus, OrderDon.Status.hasPayment));
  124. orderDonList.forEach((orderDon) -> {
  125. //如果续费增加时间,如果首次则设置当前时间为初始时间
  126. Date expiryTime = Optional.ofNullable(relation.getExpiryTime()).orElse(new Date());
  127. log.info("设置账号,该用户 {} 初始时间:{} ", orderDon.getUserId(), DateUtil.format(expiryTime, "yyyy-MM-dd HH:mm:ss"));
  128. GoodsDonSku donSku = skuMapper.selectById(orderDon.getSkuId());
  129. Date newDate = DateUtil.offset(expiryTime, DateField.MONTH, donSku.getMonths() * orderDon.getNum());
  130. relation.setExpiryTime(newDate);
  131. log.info("设置账号,该用户 {} 过期时间:{} ", orderDon.getUserId(), DateUtil.format(newDate, "yyyy-MM-dd HH:mm:ss"));
  132. relation.setStartTime(relation.getStartTime() == null ? new Date() : null);
  133. relationMapper.updateById(relation);
  134. orderDon.setStatus(OrderDon.Status.complete);
  135. orderDonMapper.updateById(orderDon);
  136. });
  137. }
  138. if (relation.getUserId() != 0 && isPrdEnv) {
  139. //发送短信
  140. smsService.sendSmsToRelationUser(relation.getUserId(), msg);
  141. }
  142. if (verifyCodeList != null && verifyCodeList.size() > 0 && i < verifyCodeList.size()) {
  143. relation.setVerifyCode(verifyCodeList.get(i));
  144. relationMapper.updateById(relation);
  145. }
  146. }
  147. if (groupsRelations.isEmpty() && verifyCodeList != null && verifyCodeList.size() > 0) {
  148. groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()));
  149. for (int i = 0; i < groupsRelations.size(); i++) {
  150. GroupsRelation relation = groupsRelations.get(i);
  151. if (i < verifyCodeList.size()) {
  152. relation.setVerifyCode(verifyCodeList.get(i));
  153. relationMapper.updateById(relation);
  154. }
  155. }
  156. }
  157. groupsTrips.setStatus(GroupsTrips.Status.validity);
  158. updateById(groupsTrips);
  159. }
  160. @Override
  161. @Transactional(rollbackFor = Throwable.class)
  162. public void replaceTripsAccount(ReplaceTripsAccountReq req) {
  163. Long groupsId = req.getGroupsId();
  164. String account = req.getAccount().trim();
  165. String password = req.getPassword().trim();
  166. String remark = req.getRemark();
  167. Date afterStartTime = req.getStartTime();
  168. Date afterExpiryTime = req.getExpiryTime();
  169. GroupsAccountView groupsAccountView = beanSearcher.searchFirst(GroupsAccountView.class, MapUtils.builder()
  170. .field(GroupsAccountView::getGroupsId, groupsId)
  171. .build());
  172. if (groupsId == null) {
  173. throw BusinessRuntimeException.getInstance("车队已不存在");
  174. }
  175. Long accountId = groupsAccountView.getAccountId();
  176. if (accountId == null) {
  177. throw BusinessRuntimeException.getInstance("该车队暂未配置账号");
  178. }
  179. Long goodsId = groupsAccountView.getGoodsId();
  180. if (!account.contains("客服")) {
  181. int count = accountMapper.selectCount(Wrappers.lambdaQuery(Account.class)
  182. .eq(Account::getAccount, account)
  183. .eq(Account::getGoodsId, goodsId));
  184. if (count > 0) {
  185. throw BusinessRuntimeException.getInstance("该平台下已生成该账号");
  186. }
  187. }
  188. //替换账号
  189. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  190. .set(Account::getAccount, account)
  191. .set(Account::getPassword, password)
  192. .set(Account::getRemark, remark)
  193. .set(Account::getStartTime, afterStartTime)
  194. .set(Account::getExpiryTime, afterExpiryTime)
  195. .eq(Account::getId, accountId));
  196. //保存替换记录
  197. GroupsTripsAccountReplaceRecord replaceRecord = new GroupsTripsAccountReplaceRecord();
  198. replaceRecord.setGroupsId(groupsId);
  199. replaceRecord.setOldAccountId(accountId);
  200. replaceRecord.setOldAccount(groupsAccountView.getAccount());
  201. replaceRecord.setOldPassword(groupsAccountView.getPassword());
  202. replaceRecord.setOldStartTime(groupsAccountView.getStartTime());
  203. replaceRecord.setOldExpiryTime(groupsAccountView.getExpiryTime());
  204. Long adminId = req.getAdminId();
  205. Optional.ofNullable(cmsUserMapper.selectById(adminId)).ifPresent(admin -> replaceRecord.setOperator(admin.getNickname()));
  206. replaceRecord.setNewAccount(account);
  207. replaceRecord.setNewPassword(password);
  208. replaceRecord.setNewStartTime(afterStartTime);
  209. replaceRecord.setNewExpiryTime(afterExpiryTime);
  210. replaceRecord.setRemark(remark);
  211. replaceRecordMapper.insert(replaceRecord);
  212. TASK_EXECUTOR.execute(() -> sendChangeAccountMsg(groupsId, goodsId));;
  213. }
  214. private void sendChangeAccountMsg(Long groupsId, Long goodsId) {
  215. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  216. String title = goodsDon != null ? goodsDon.getTitle() : "车票";
  217. DateTime now = DateTime.now();
  218. Boolean isPrd = envCommonService.isPrdEnv();
  219. List<GroupsRelation> groupsRelations = beanSearcher.searchAll(GroupsRelation.class, MapUtils.builder().field(GroupsRelation::getGroupsId, groupsId).build());
  220. groupsRelations.forEach(relation -> {
  221. if (relation.getExpiryTime() == null) {
  222. return;
  223. }
  224. //账号有效期小于5天的人,不发账号替换/改密码短信提醒
  225. if (now.after(DateUtil.offsetDay(relation.getExpiryTime(), -5))) {
  226. return;
  227. }
  228. if (Constant.CHANGE_ACCOUNT_PWD_FILTER_GIDS.contains(goodsId)) {
  229. return;
  230. }
  231. if (relation.getUserId() != 0 && isPrd) {
  232. String msg = String.format(Constant.CHANGE_GROUPS_TRIPS_ACCOUNT, title);
  233. smsService.sendSmsToRelationUser(relation.getUserId(), msg);
  234. }
  235. });
  236. }
  237. }