CmsGroupServiceImpl.java 14 KB

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