CmsGroupServiceImpl.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package com.cyksj.service.mange.group;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateField;
  4. import cn.hutool.core.date.DateTime;
  5. import cn.hutool.core.date.DateUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  9. import com.cyksj.common.EnvCommonService;
  10. import com.cyksj.common.constant.Constant;
  11. import com.cyksj.common.exception.BusinessRuntimeException;
  12. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  13. import com.cyksj.dto.RedisKey;
  14. import com.cyksj.mapper.*;
  15. import com.cyksj.mapper.manage.GroupsTripsAccountReplaceRecordMapper;
  16. import com.cyksj.mapper.manage.cms.CmsUserMapper;
  17. import com.cyksj.model.entity.*;
  18. import com.cyksj.model.manage.views.AccountView;
  19. import com.cyksj.model.manage.views.GroupsRelationView;
  20. import com.cyksj.model.request.ReplaceTripsAccountReq;
  21. import com.cyksj.model.views.GroupsAccountView;
  22. import com.cyksj.redis.RedisService;
  23. import com.cyksj.service.mange.AccountCommonService;
  24. import com.cyksj.service.mange.CmsGroupService;
  25. import com.cyksj.service.relation.GroupsRelationNetflixService;
  26. import com.cyksj.service.sms.SmsService;
  27. import com.cyksj.service.user.UserSysMsgNotifyService;
  28. import com.ejlchina.searcher.BeanSearcher;
  29. import com.ejlchina.searcher.param.Operator;
  30. import com.ejlchina.searcher.util.MapBuilder;
  31. import com.ejlchina.searcher.util.MapUtils;
  32. import lombok.RequiredArgsConstructor;
  33. import lombok.extern.slf4j.Slf4j;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import java.util.Arrays;
  37. import java.util.Date;
  38. import java.util.List;
  39. import java.util.Optional;
  40. import java.util.stream.Collectors;
  41. /**
  42. * @author chan
  43. * @date 2022/9/27 6:27 PM
  44. */
  45. @Slf4j
  46. @Service
  47. @RequiredArgsConstructor
  48. public class CmsGroupServiceImpl extends ServiceImpl<GroupsMapper,GroupsTrips> implements CmsGroupService {
  49. private final GoodsDonSkuMapper skuMapper;
  50. private final GroupsRelationMapper relationMapper;
  51. private final OrderDonMapper orderDonMapper;
  52. private final AccountMapper accountMapper;
  53. private final BeanSearcher beanSearcher;
  54. private final GroupsTripsAccountReplaceRecordMapper replaceRecordMapper;
  55. private final CmsUserMapper cmsUserMapper;
  56. private final GoodsDonMapper goodsDonMapper;
  57. private final SmsService smsService;
  58. private final EnvCommonService envCommonService;
  59. private final RedisService redisService;
  60. private final GroupsMapper groupsMapper;
  61. private final AccountCommonService accountCommonService;
  62. private final GroupsRelationAlertMapper alertMapper;
  63. private final UserSysMsgNotifyService userSysMsgNotifyService;
  64. private final GroupsRelationNetflixService groupsRelationNetflixService;
  65. private final UserMapper userMapper;
  66. private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
  67. @Override
  68. public GroupsTrips issuance(GroupsTrips groups, String verifyCodes) {
  69. GoodsDonSku sku = skuMapper.selectById(groups.getSkuId());
  70. if (sku == null) {
  71. throw new BusinessRuntimeException("该商品规格不存在!");
  72. }
  73. List<String> verifyCodeList = null;
  74. if (StrUtil.isNotBlank(verifyCodes)) {
  75. verifyCodes = verifyCodes.replaceAll("\n", "");
  76. String[] verifyCodeArray = verifyCodes.split("\\*");
  77. verifyCodeList = Arrays.stream(verifyCodeArray).filter(str -> StrUtil.isNotBlank(str)).map(str -> str.trim()).collect(Collectors.toList());
  78. }
  79. groups.setNum(sku.getNum());
  80. groups.setAvailableNum(sku.getNum());
  81. if (groups.getAccountId() == null || accountMapper.selectById(groups.getAccountId()) == null) {
  82. throw new BusinessRuntimeException("该账号不存在");
  83. }
  84. groups.setStatus(GroupsTrips.Status.validity);
  85. this.save(groups);
  86. for (Integer i = 1; i <= groups.getNum(); i++) {
  87. GroupsRelation relation = new GroupsRelation();
  88. relation.setGroupsId(groups.getId());
  89. relation.setStatus(GroupsRelation.Status.none);
  90. relation.setNum(i);
  91. if (verifyCodeList != null && verifyCodeList.size() > 0 && verifyCodeList.size() >= i) {
  92. relation.setVerifyCode(verifyCodeList.get(i - 1));
  93. }
  94. relationMapper.insert(relation);
  95. }
  96. Integer alertCount = alertMapper.selectCount(Wrappers.lambdaQuery(GroupsRelationAlert.class)
  97. .eq(GroupsRelationAlert::getSkuId, sku.getId())
  98. .last("limit 1"));
  99. if (alertCount > 0) {
  100. alertMapper.update(null, Wrappers.lambdaUpdate(GroupsRelationAlert.class)
  101. .set(GroupsRelationAlert::getIsReset, true)
  102. .eq(GroupsRelationAlert::getSkuId, sku.getId())
  103. .eq(GroupsRelationAlert::getIsReset, false));
  104. }
  105. return groups;
  106. }
  107. @Override
  108. public void close(Long groupId) {
  109. GroupsTrips groupsTrips = this.getById(groupId);
  110. if(groupsTrips == null){
  111. throw new BusinessRuntimeException("非法参数.车队不存在");
  112. }
  113. Integer count = relationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  114. .eq(GroupsRelation::getGroupsId, groupId)
  115. .ne(GroupsRelation::getUserId, 0)
  116. .ne(GroupsRelation::getStatus, GroupsRelation.Status.none));
  117. if (count > 0) {
  118. throw BusinessRuntimeException.getInstance("请移除该车队下的用户再进行删掉操作");
  119. }
  120. //可强制删除
  121. //后台记录
  122. // if (count > 0) {
  123. // List<GroupsRelation> groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class)
  124. // .eq(GroupsRelation::getGroupsId, groupId)
  125. // .ne(GroupsRelation::getStatus, GroupsRelation.Status.none));
  126. // if (groupsRelations.size() > 0) {
  127. // Account account = accountMapper.selectById(groupsTrips.getAccountId());
  128. // TASK_EXECUTOR.execute(() -> {
  129. // if (redisService.setNx(String.format("%s-%s", groupId, groupsTrips.getAccount()), groupId, 10l)) {
  130. // groupsRelations.forEach(data -> {
  131. // DelGroupsTripsRecord delGroupsTripsRecord = new DelGroupsTripsRecord();
  132. // delGroupsTripsRecord.setGroupsId(groupId);
  133. // delGroupsTripsRecord.setGroupsStatus(groupsTrips.getStatus().name());
  134. // if (account != null) {
  135. // delGroupsTripsRecord.setAccount(account.getAccount());
  136. // delGroupsTripsRecord.setPassword(account.getPassword());
  137. // }
  138. // delGroupsTripsRecord.setRelationId(data.getId());
  139. // delGroupsTripsRecord.setSkuId(groupsTrips.getSkuId());
  140. // delGroupsTripsRecord.setUserId(data.getUserId());
  141. // delGroupsTripsRecord.setStartTime(data.getStartTime());
  142. // delGroupsTripsRecord.setExpiryTime(data.getExpiryTime());
  143. // delGroupsTripsRecord.setRelationStatus(data.getStatus().name());
  144. // delGroupsTripsRecord.setType("delete");
  145. // delGroupsTripsRecordMapper.insert(delGroupsTripsRecord);
  146. // });
  147. // }
  148. // });
  149. // }
  150. // }
  151. relationMapper.delete(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId,groupId));
  152. this.removeById(groupId);
  153. }
  154. @Override
  155. @Transactional(rollbackFor = Throwable.class)
  156. public void setAccount(GroupsTrips groupsTrips) throws Exception {
  157. int count = count(Wrappers.lambdaQuery(GroupsTrips.class).eq(GroupsTrips::getAccountId, groupsTrips.getAccountId()));
  158. if (count > 0) {
  159. throw new BusinessRuntimeException("该账号已发车.请选择未发车账户");
  160. }
  161. Account account = accountMapper.selectById(groupsTrips.getAccountId());
  162. //将预备账号 类型替换掉
  163. if (account.getType() == 2) {
  164. int update = accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  165. .set(Account::getType, 1)
  166. .eq(Account::getId, account.getId())
  167. .eq(Account::getType, 2));
  168. if (update == 0) {
  169. throw BusinessRuntimeException.getInstance("该账号已配置车队");
  170. }
  171. }
  172. List<GroupsRelation> groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()).eq(GroupsRelation::getStatus, GroupsRelation.Status.validity));
  173. GroupsTrips trips = beanSearcher.searchFirst(GroupsTrips.class, MapUtils.builder().field(GroupsTrips::getId, groupsTrips.getId()).onlySelect(GroupsTrips::getTitle).build());
  174. String title = "车票";
  175. if (trips != null && StrUtil.isNotBlank(trips.getTitle())) {
  176. title = trips.getTitle();
  177. }
  178. //配置midJourney安全码
  179. String verifyCodes = groupsTrips.getVerifyCodes();
  180. List<String> verifyCodeList = null;
  181. if (StrUtil.isNotBlank(verifyCodes)) {
  182. verifyCodes = verifyCodes.replaceAll("\n", "");
  183. String[] verifyCodesArray = verifyCodes.split("\\*");
  184. verifyCodeList = Arrays.stream(verifyCodesArray).filter(str -> StrUtil.isNotBlank(str)).map(str -> str.trim()).collect(Collectors.toList());
  185. }
  186. Boolean isPrdEnv = envCommonService.isPrdEnv();
  187. for (int i = 0; i < groupsRelations.size(); i++) {
  188. GroupsRelation relation = groupsRelations.get(i);
  189. if (relation.getExpiryTime() == null) {
  190. 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));
  191. //加购车票
  192. if (orderDonList.isEmpty()) {
  193. GoodsDonSku donSku = skuMapper.selectById(groupsTrips.getSkuId());
  194. Date expiryTime = Optional.ofNullable(relation.getExpiryTime()).orElse(new Date());
  195. Date newDate;
  196. if (donSku.getDays() != null && donSku.getDays() > 0) {
  197. newDate = DateUtil.offsetDay(expiryTime, donSku.getDays() * 1);
  198. } else {
  199. //奈飞月付 按30天计算
  200. if (donSku.getId() == 4) {
  201. newDate = DateUtil.offsetDay(expiryTime, 30);
  202. } else {
  203. newDate = DateUtil.offset(expiryTime, DateField.MONTH, donSku.getMonths() * 1);
  204. }
  205. }
  206. relation.setExpiryTime(newDate);
  207. relation.setStartTime(relation.getStartTime() == null ? new Date() : null);
  208. relationMapper.updateById(relation);
  209. return;
  210. }
  211. orderDonList.forEach((orderDon) -> {
  212. //如果续费增加时间,如果首次则设置当前时间为初始时间
  213. Date expiryTime = Optional.ofNullable(relation.getExpiryTime()).orElse(new Date());
  214. log.info("设置账号,该用户 {} 初始时间:{} ", orderDon.getUserId(), DateUtil.format(expiryTime, "yyyy-MM-dd HH:mm:ss"));
  215. GoodsDonSku donSku = skuMapper.selectById(orderDon.getSkuId());
  216. Date newDate;
  217. if (donSku.getDays() != null && donSku.getDays() > 0) {
  218. newDate = DateUtil.offsetDay(expiryTime, donSku.getDays() * orderDon.getNum());
  219. } else {
  220. newDate = DateUtil.offset(expiryTime, DateField.MONTH, donSku.getMonths() * orderDon.getNum());
  221. }
  222. relation.setExpiryTime(newDate);
  223. log.info("设置账号,该用户 {} 过期时间:{} ", orderDon.getUserId(), DateUtil.format(newDate, "yyyy-MM-dd HH:mm:ss"));
  224. relation.setStartTime(relation.getStartTime() == null ? new Date() : null);
  225. relationMapper.updateById(relation);
  226. orderDon.setStatus(OrderDon.Status.complete);
  227. orderDonMapper.updateById(orderDon);
  228. });
  229. }
  230. if (relation.getUserId() != 0 && isPrdEnv && relation.getYhsId() == 0) {
  231. //发送短信
  232. smsService.sendSmsToRelationUser(relation.getUserId(), Constant.CHANGE_GROUPS_TRIPS_ACCOUNT, title, account.getAccount());
  233. }
  234. if (verifyCodeList != null && verifyCodeList.size() > 0 && i < verifyCodeList.size()) {
  235. relation.setVerifyCode(verifyCodeList.get(i));
  236. relationMapper.updateById(relation);
  237. }
  238. //设置奈飞用户座位
  239. if (account.getGoodsId() == 1) {
  240. User user = userMapper.selectById(relation.getUserId());
  241. groupsRelationNetflixService.timingSetNetflix(relation.getNum(), user.getNickname(), account.getAccount(), account.getPassword(), relation.getExpiryTime(), null);
  242. }
  243. }
  244. if (groupsRelations.isEmpty() && verifyCodeList != null && verifyCodeList.size() > 0) {
  245. groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()));
  246. for (int i = 0; i < groupsRelations.size(); i++) {
  247. GroupsRelation relation = groupsRelations.get(i);
  248. if (i < verifyCodeList.size()) {
  249. relation.setVerifyCode(verifyCodeList.get(i));
  250. relationMapper.updateById(relation);
  251. }
  252. }
  253. }
  254. groupsTrips.setStatus(GroupsTrips.Status.validity);
  255. GoodsDonSku sku = skuMapper.selectById(groupsTrips.getSkuId());
  256. //下架上过车的POE,PS AI账号 || GPT Plus独立规格 下架
  257. if (Constant.DOWN_GOODS_PAY.contains(sku.getGoodsId()) || skuMapper.selectSingleSkuIdsByGoodsId(Constant.AI_goodsIds.get(0)).contains(groupsTrips.getSkuId())) {
  258. Number number = beanSearcher.searchCount(GroupsRelation.class, MapUtils.builder()
  259. .field(GroupsRelation::getGroupsId, groupsTrips.getId())
  260. .field(GroupsRelation::getUserId, 0).op(Operator.NotEqual)
  261. .field(GroupsRelation::getStatus, GroupsRelation.Status.validity.name())
  262. .build());
  263. if (number.intValue() > 0) {
  264. groupsTrips.setStatus(GroupsTrips.Status.down);
  265. }
  266. }
  267. updateById(groupsTrips);
  268. GoodsDon goodsDon = goodsDonMapper.selectById(account.getGoodsId());
  269. userSysMsgNotifyService.recordUserAccountSetSysMsg(account.getGoodsId(), groupsTrips.getSkuId(), groupsRelations, account.getAccount(), goodsDon.getTitle());
  270. Integer alertCount = alertMapper.selectCount(Wrappers.lambdaQuery(GroupsRelationAlert.class)
  271. .eq(GroupsRelationAlert::getSkuId, sku.getId())
  272. .last("limit 1"));
  273. if (alertCount > 0) {
  274. alertMapper.update(null, Wrappers.lambdaUpdate(GroupsRelationAlert.class)
  275. .set(GroupsRelationAlert::getIsReset, true)
  276. .eq(GroupsRelationAlert::getSkuId, sku.getId())
  277. .eq(GroupsRelationAlert::getIsReset, false));
  278. }
  279. }
  280. @Override
  281. @Transactional(rollbackFor = Throwable.class)
  282. public void replaceTripsAccount(ReplaceTripsAccountReq req) {
  283. Long groupsId = req.getGroupsId();
  284. String account = req.getAccount().trim();
  285. String password = req.getPassword().trim();
  286. String remark = req.getRemark();
  287. Date afterStartTime = req.getStartTime();
  288. Date afterExpiryTime = req.getExpiryTime();
  289. GroupsAccountView groupsAccountView = beanSearcher.searchFirst(GroupsAccountView.class, MapUtils.builder()
  290. .field(GroupsAccountView::getGroupsId, groupsId)
  291. .build());
  292. if (groupsId == null) {
  293. throw BusinessRuntimeException.getInstance("车队已不存在");
  294. }
  295. Long accountId = groupsAccountView.getAccountId();
  296. if (accountId == null) {
  297. throw BusinessRuntimeException.getInstance("该车队暂未配置账号");
  298. }
  299. Long goodsId = groupsAccountView.getGoodsId();
  300. if (!account.contains("客服")) {
  301. if (accountCommonService.checkIsDupAccount(goodsId, account)) {
  302. throw BusinessRuntimeException.getInstance("该平台下已生成该账号");
  303. }
  304. }
  305. //Claude3只允许替换一次
  306. if (Constant.CALUDE_GOODS_ID.equals(groupsAccountView.getGoodsId())) {
  307. Integer count = replaceRecordMapper.selectCount(Wrappers.lambdaQuery(GroupsTripsAccountReplaceRecord.class)
  308. .eq(GroupsTripsAccountReplaceRecord::getGroupsId, groupsAccountView.getGroupsId()));
  309. if (count > 0) throw BusinessRuntimeException.getInstance("Claude3账号只允许替换一次");
  310. }
  311. if (afterStartTime == null) {
  312. afterStartTime = groupsAccountView.getStartTime();
  313. }
  314. if (afterExpiryTime == null) {
  315. afterExpiryTime = groupsAccountView.getExpiryTime();
  316. }
  317. log.info("原账号accountId:{}", accountId);
  318. //替换账号
  319. int update = accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  320. .set(Account::getAccount, account)
  321. .set(Account::getPassword, password)
  322. .set(Account::getRemark, remark)
  323. .set(Account::getStartTime, afterStartTime)
  324. .set(Account::getExpiryTime, afterExpiryTime)
  325. .set(Account::getGptRefreshToken, StrUtil.EMPTY)
  326. .eq(Account::getId, accountId));
  327. if (update == 0) {
  328. log.info("替换原账号oldAccount:{}至新账号newAccount:{}失败", groupsAccountView.getAccount(), account);
  329. throw BusinessRuntimeException.getInstance("替换账号失败");
  330. }
  331. if (StrUtil.isNotBlank(groupsAccountView.getGptRefreshToken())) {
  332. //清除gpt token
  333. redisService.del(RedisKey.CHATGPT_ACCESS_TOKEN + groupsAccountView.getAccount());
  334. redisService.del(RedisService.key.CHAT_GPT_TOKEN_EXCEPTION_KEY.getName() + groupsAccountView.getAccount());
  335. }
  336. //时间有效
  337. if (afterExpiryTime != null && afterExpiryTime.after(DateTime.now())) {
  338. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  339. .set(GroupsTrips::getStatus, GroupsTrips.Status.validity)
  340. .eq(GroupsTrips::getAccountId, accountId)
  341. .eq(GroupsTrips::getStatus, GroupsTrips.Status.down));
  342. }
  343. //保存替换记录
  344. GroupsTripsAccountReplaceRecord replaceRecord = new GroupsTripsAccountReplaceRecord();
  345. replaceRecord.setGroupsId(groupsId);
  346. replaceRecord.setOldAccountId(accountId);
  347. replaceRecord.setOldAccount(groupsAccountView.getAccount());
  348. replaceRecord.setOldPassword(groupsAccountView.getPassword());
  349. replaceRecord.setOldStartTime(groupsAccountView.getStartTime());
  350. replaceRecord.setOldExpiryTime(groupsAccountView.getExpiryTime());
  351. Long adminId = req.getAdminId();
  352. Optional.ofNullable(cmsUserMapper.selectById(adminId)).ifPresent(admin -> replaceRecord.setOperator(admin.getNickname()));
  353. replaceRecord.setNewAccount(account);
  354. replaceRecord.setNewPassword(password);
  355. replaceRecord.setNewStartTime(afterStartTime);
  356. replaceRecord.setNewExpiryTime(afterExpiryTime);
  357. replaceRecord.setRemark(remark);
  358. replaceRecordMapper.insert(replaceRecord);
  359. TASK_EXECUTOR.execute(() -> sendChangeAccountMsg(groupsId, goodsId, account));;
  360. }
  361. @Override
  362. public Integer getAvailableRelation(Long goodsId, Long skuId) {
  363. //chatGPT
  364. GoodsDonSku sku = skuMapper.selectById(skuId);
  365. goodsId = sku.getGoodsId();
  366. Integer skuNum = sku.getNum();
  367. if (Constant.AI_goodsIds.contains(sku.getGoodsId()) && sku.getNum() > 1) {
  368. Integer extraNum = Constant.AI_EXTRACT_NUM;
  369. //GPT 4个月付
  370. if (skuId == 178l) {
  371. extraNum = 2;
  372. }
  373. Integer maxNum = skuNum + extraNum;
  374. //主账号有效时间
  375. DateTime time = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 25);
  376. Integer available = relationMapper.selectAvailableRelation(skuId, skuNum);
  377. //额外硬塞
  378. Integer special_available = relationMapper.selectSpecialGroupsRelationAvailable(skuId, maxNum, skuNum, extraNum, time);
  379. available += special_available;
  380. //GPT Plus 10人月付规格 4人月付
  381. if (Constant.GPT_PLUS_PARKING_SKU_IDS.contains(skuId)) {
  382. //上面已排除限制车队
  383. //限制车队可用车数位
  384. Integer limitAvailable = Optional.ofNullable(relationMapper.selectLimitAvailableNum(skuId, maxNum)).orElse(0);
  385. if (limitAvailable < 0) {
  386. limitAvailable = 0;
  387. }
  388. available += limitAvailable;
  389. //新车队预备上车 最大可用数
  390. if (Constant.GPT_PLUS_PARKING_SKU_IDS.get(0).equals(skuId)) {
  391. maxNum = 10;
  392. }else {
  393. maxNum = 4;
  394. }
  395. }
  396. available = getAvailableAfterPreAccount(available, skuId, maxNum, goodsId);
  397. //待发车已有人员成功上车
  398. Integer waitingNum = relationMapper.selectNumWaitingGroups(skuId);
  399. available -= waitingNum;
  400. return available;
  401. }
  402. Integer available = relationMapper.selectAvailableRelation(skuId, skuNum);
  403. available = getAvailableAfterPreAccount(available, skuId, skuNum, goodsId);
  404. //待发车已有人员成功上车
  405. Integer waitingNum = relationMapper.selectNumWaitingGroups(skuId);
  406. available -= waitingNum;
  407. return available;
  408. }
  409. @Override
  410. public void sendChangeAccountMsg(Long groupsId, Long goodsId, String account) {
  411. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  412. String title = goodsDon != null ? goodsDon.getTitle() : "车票";
  413. DateTime now = DateTime.now();
  414. Boolean isPrd = envCommonService.isPrdEnv();
  415. List<GroupsRelationView> groupsRelations = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder().field(GroupsRelationView::getGroupsId, groupsId).build());
  416. if (CollUtil.isEmpty(groupsRelations)) {
  417. return;
  418. }
  419. Long skuId = groupsMapper.selectById(groupsId).getSkuId();
  420. userSysMsgNotifyService.recordUserAccountUpdateSysMsg(goodsId, skuId, groupsRelations, account, goodsDon.getTitle());
  421. groupsRelations.forEach(relation -> {
  422. if (relation.getExpiryTime() == null) {
  423. return;
  424. }
  425. //账号有效期小于5天的人,不发账号替换/改密码短信提醒
  426. if (now.after(DateUtil.offsetDay(relation.getExpiryTime(), -5))) {
  427. return;
  428. }
  429. //去掉GPT过滤平台
  430. if (Constant.CHANGE_ACCOUNT_PWD_FILTER_GIDS.get(1) == goodsId) {
  431. return;
  432. }
  433. if (relation.getUserId() != 0 && isPrd && relation.getYhsId() == 0) {
  434. try {
  435. smsService.sendSmsToRelationUser(relation.getUserId(), Constant.CHANGE_GROUPS_TRIPS_ACCOUNT, title, account);
  436. } catch (Exception e) {
  437. }
  438. }
  439. });
  440. }
  441. public Integer getAvailableAfterPreAccount(Integer available, Long skuId, Integer maxNum, Long goodsId) {
  442. MapBuilder builder = MapUtils.builder();
  443. if (goodsId == 26) {
  444. //是否在预备规格集合中
  445. String extra_sql = String.format("FIND_IN_SET(%s,a.pre_sku_ids) > 0", skuId);
  446. builder.put("extra_sql", extra_sql);
  447. }
  448. Number preAccount = beanSearcher.searchCount(AccountView.class, builder
  449. .field(AccountView::getGoodsId, goodsId)
  450. .field(AccountView::getAccountType, 2)
  451. .field(AccountView::getGroupsId).op(Operator.IsNull)
  452. .build());
  453. //计算上预发布账号车位数
  454. if (preAccount.intValue() > 0) {
  455. available += preAccount.intValue() * maxNum;
  456. }
  457. return available;
  458. }
  459. }