CmsGroupServiceImpl.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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.mapper.manage.cms.SysLogMapper;
  18. import com.cyksj.model.entity.*;
  19. import com.cyksj.model.manage.views.AccountView;
  20. import com.cyksj.model.manage.views.GroupsRelationView;
  21. import com.cyksj.model.request.ReplaceTripsAccountReq;
  22. import com.cyksj.model.views.GroupsAccountView;
  23. import com.cyksj.redis.RedisService;
  24. import com.cyksj.service.chatgpt.GptUserRechargeRecordService;
  25. import com.cyksj.service.mange.AccountCommonService;
  26. import com.cyksj.service.mange.CmsGroupService;
  27. import com.cyksj.service.order.MultiQuantityOrderService;
  28. import com.cyksj.service.order.OrderDonTicketRecordService;
  29. import com.cyksj.service.relation.GroupRelationClearService;
  30. import com.cyksj.service.relation.GroupsRelationNetflixService;
  31. import com.cyksj.service.sms.SmsService;
  32. import com.cyksj.service.user.UserBindRelationService;
  33. import com.cyksj.service.user.UserSysMsgNotifyService;
  34. import com.ejlchina.searcher.BeanSearcher;
  35. import com.ejlchina.searcher.param.Operator;
  36. import com.ejlchina.searcher.util.MapBuilder;
  37. import com.ejlchina.searcher.util.MapUtils;
  38. import lombok.RequiredArgsConstructor;
  39. import lombok.extern.slf4j.Slf4j;
  40. import org.springframework.stereotype.Service;
  41. import org.springframework.transaction.annotation.Transactional;
  42. import java.math.BigDecimal;
  43. import java.util.Arrays;
  44. import java.util.Date;
  45. import java.util.List;
  46. import java.util.Optional;
  47. import java.util.concurrent.atomic.AtomicInteger;
  48. import java.util.stream.Collectors;
  49. /**
  50. * @author chan
  51. * @date 2022/9/27 6:27 PM
  52. */
  53. @Slf4j
  54. @Service
  55. @RequiredArgsConstructor
  56. public class CmsGroupServiceImpl extends ServiceImpl<GroupsMapper,GroupsTrips> implements CmsGroupService {
  57. private final GoodsDonSkuMapper skuMapper;
  58. private final GroupsRelationMapper relationMapper;
  59. private final OrderDonMapper orderDonMapper;
  60. private final AccountMapper accountMapper;
  61. private final BeanSearcher beanSearcher;
  62. private final GroupsTripsAccountReplaceRecordMapper replaceRecordMapper;
  63. private final CmsUserMapper cmsUserMapper;
  64. private final GoodsDonMapper goodsDonMapper;
  65. private final SmsService smsService;
  66. private final EnvCommonService envCommonService;
  67. private final RedisService redisService;
  68. private final GroupsMapper groupsMapper;
  69. private final AccountCommonService accountCommonService;
  70. private final GroupsRelationAlertMapper alertMapper;
  71. private final UserSysMsgNotifyService userSysMsgNotifyService;
  72. private final GroupsRelationNetflixService groupsRelationNetflixService;
  73. private final UserMapper userMapper;
  74. private final NetflixUserAccountSubmitRecoverRecordMapper netflixUserAccountSubmitRecoverRecordMapper;
  75. private final GroupRelationClearService clearService;
  76. private final UserBindRelationService userBindRelationService;
  77. private final NetflixUserAccountStatusRecordMapper netflixUserAccountStatusRecordMapper;
  78. private final GptUserRechargeRecordService gptUserRechargeRecordService;
  79. private final SysLogMapper sysLogMapper;
  80. private final OrderDonTicketRecordService orderDonTicketRecordService;
  81. private final MultiQuantityOrderService multiQuantityOrderService;
  82. private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
  83. @Override
  84. public GroupsTrips issuance(GroupsTrips groups, String verifyCodes, Boolean isRecover) {
  85. GoodsDonSku sku = skuMapper.selectById(groups.getSkuId());
  86. if (sku == null) {
  87. throw new BusinessRuntimeException("该商品规格不存在!");
  88. }
  89. List<String> verifyCodeList = null;
  90. if (StrUtil.isNotBlank(verifyCodes)) {
  91. verifyCodes = verifyCodes.replaceAll("\n", "");
  92. String[] verifyCodeArray = verifyCodes.split("\\*");
  93. verifyCodeList = Arrays.stream(verifyCodeArray).filter(str -> StrUtil.isNotBlank(str)).map(str -> str.trim()).collect(Collectors.toList());
  94. }
  95. groups.setNum(sku.getNum());
  96. groups.setAvailableNum(sku.getNum());
  97. if (groups.getAccountId() == null || accountMapper.selectById(groups.getAccountId()) == null) {
  98. throw new BusinessRuntimeException("该账号不存在");
  99. }
  100. groups.setStatus(GroupsTrips.Status.validity);
  101. this.save(groups);
  102. for (Integer i = 1; i <= groups.getNum(); i++) {
  103. GroupsRelation relation = new GroupsRelation();
  104. relation.setGroupsId(groups.getId());
  105. relation.setStatus(GroupsRelation.Status.none);
  106. relation.setNum(i);
  107. if (verifyCodeList != null && verifyCodeList.size() > 0 && verifyCodeList.size() >= i) {
  108. relation.setVerifyCode(verifyCodeList.get(i - 1));
  109. }
  110. NetflixUserAccountSubmitRecoverRecord recoverRecord = null;
  111. if (sku.getGoodsId() == Constant.NETFLIX_GID) {
  112. recoverRecord = updateRelationIfRecoverTicket(isRecover, sku, relation);
  113. }
  114. relationMapper.insert(relation);
  115. if (recoverRecord != null) {
  116. recoverRecord.setReplaceRelationId(relation.getId());
  117. netflixUserAccountSubmitRecoverRecordMapper.updateById(recoverRecord);
  118. }
  119. }
  120. Integer alertCount = alertMapper.selectCount(Wrappers.lambdaQuery(GroupsRelationAlert.class)
  121. .eq(GroupsRelationAlert::getSkuId, sku.getId())
  122. .last("limit 1"));
  123. if (alertCount > 0) {
  124. alertMapper.update(null, Wrappers.lambdaUpdate(GroupsRelationAlert.class)
  125. .set(GroupsRelationAlert::getIsReset, true)
  126. .eq(GroupsRelationAlert::getSkuId, sku.getId())
  127. .eq(GroupsRelationAlert::getIsReset, false));
  128. }
  129. return groups;
  130. }
  131. @Override
  132. public void close(Long groupId) {
  133. GroupsTrips groupsTrips = this.getById(groupId);
  134. if(groupsTrips == null){
  135. throw new BusinessRuntimeException("非法参数.车队不存在");
  136. }
  137. Integer count = relationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  138. .eq(GroupsRelation::getGroupsId, groupId)
  139. .ne(GroupsRelation::getUserId, 0)
  140. .ne(GroupsRelation::getStatus, GroupsRelation.Status.none));
  141. if (count > 0) {
  142. throw BusinessRuntimeException.getInstance("请移除该车队下的用户再进行删掉操作");
  143. }
  144. //可强制删除
  145. //后台记录
  146. // if (count > 0) {
  147. // List<GroupsRelation> groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class)
  148. // .eq(GroupsRelation::getGroupsId, groupId)
  149. // .ne(GroupsRelation::getStatus, GroupsRelation.Status.none));
  150. // if (groupsRelations.size() > 0) {
  151. // Account account = accountMapper.selectById(groupsTrips.getAccountId());
  152. // TASK_EXECUTOR.execute(() -> {
  153. // if (redisService.setNx(String.format("%s-%s", groupId, groupsTrips.getAccount()), groupId, 10l)) {
  154. // groupsRelations.forEach(data -> {
  155. // DelGroupsTripsRecord delGroupsTripsRecord = new DelGroupsTripsRecord();
  156. // delGroupsTripsRecord.setGroupsId(groupId);
  157. // delGroupsTripsRecord.setGroupsStatus(groupsTrips.getStatus().name());
  158. // if (account != null) {
  159. // delGroupsTripsRecord.setAccount(account.getAccount());
  160. // delGroupsTripsRecord.setPassword(account.getPassword());
  161. // }
  162. // delGroupsTripsRecord.setRelationId(data.getId());
  163. // delGroupsTripsRecord.setSkuId(groupsTrips.getSkuId());
  164. // delGroupsTripsRecord.setUserId(data.getUserId());
  165. // delGroupsTripsRecord.setStartTime(data.getStartTime());
  166. // delGroupsTripsRecord.setExpiryTime(data.getExpiryTime());
  167. // delGroupsTripsRecord.setRelationStatus(data.getStatus().name());
  168. // delGroupsTripsRecord.setType("delete");
  169. // delGroupsTripsRecordMapper.insert(delGroupsTripsRecord);
  170. // });
  171. // }
  172. // });
  173. // }
  174. // }
  175. relationMapper.delete(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId,groupId));
  176. this.removeById(groupId);
  177. }
  178. @Override
  179. @Transactional(rollbackFor = Throwable.class)
  180. public void setAccount(GroupsTrips groupsTrips) throws Exception {
  181. int count = count(Wrappers.lambdaQuery(GroupsTrips.class).eq(GroupsTrips::getAccountId, groupsTrips.getAccountId()));
  182. if (count > 0) {
  183. throw new BusinessRuntimeException("该账号已发车.请选择未发车账户");
  184. }
  185. Account account = accountMapper.selectById(groupsTrips.getAccountId());
  186. //将预备账号 类型替换掉
  187. if (account.getType() == 2) {
  188. int update = accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  189. .set(Account::getType, 1)
  190. .eq(Account::getId, account.getId())
  191. .eq(Account::getType, 2));
  192. if (update == 0) {
  193. throw BusinessRuntimeException.getInstance("该账号已配置车队");
  194. }
  195. }
  196. List<GroupsRelation> groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()).eq(GroupsRelation::getStatus, GroupsRelation.Status.validity));
  197. GroupsTrips trips = beanSearcher.searchFirst(GroupsTrips.class, MapUtils.builder().field(GroupsTrips::getId, groupsTrips.getId()).onlySelect(GroupsTrips::getTitle).build());
  198. String title = "车票";
  199. if (trips != null && StrUtil.isNotBlank(trips.getTitle())) {
  200. title = trips.getTitle();
  201. }
  202. //配置midJourney安全码
  203. String verifyCodes = groupsTrips.getVerifyCodes();
  204. List<String> verifyCodeList = null;
  205. if (StrUtil.isNotBlank(verifyCodes)) {
  206. verifyCodes = verifyCodes.replaceAll("\n", "");
  207. String[] verifyCodesArray = verifyCodes.split("\\*");
  208. verifyCodeList = Arrays.stream(verifyCodesArray).filter(str -> StrUtil.isNotBlank(str)).map(str -> str.trim()).collect(Collectors.toList());
  209. }
  210. Boolean isPrdEnv = envCommonService.isPrdEnv();
  211. java.util.Map<Long, OrderDonTicketRecord> multiTicketByRelationId =
  212. orderDonTicketRecordService.listSuccessfulByGroupsId(groupsTrips.getId()).stream()
  213. .collect(Collectors.toMap(OrderDonTicketRecord::getRelationId, record -> record));
  214. for (int i = 0; i < groupsRelations.size(); i++) {
  215. GroupsRelation relation = groupsRelations.get(i);
  216. if (relation.getExpiryTime() == null) {
  217. OrderDonTicketRecord multiTicket = multiTicketByRelationId.get(relation.getId());
  218. List<OrderDon> orderDonList = multiTicket == null
  219. ? orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
  220. .eq(OrderDon::getUserId, relation.getUserId())
  221. .eq(OrderDon::getRelationId, relation.getId())
  222. .eq(OrderDon::getStatus, OrderDon.Status.hasPayment))
  223. : Optional.ofNullable(orderDonMapper.selectById(multiTicket.getOrderId()))
  224. .map(List::of).orElseGet(List::of);
  225. //加购车票
  226. if (orderDonList.isEmpty()) {
  227. GoodsDonSku donSku = skuMapper.selectById(groupsTrips.getSkuId());
  228. Date expiryTime = Optional.ofNullable(relation.getExpiryTime()).orElse(new Date());
  229. Date newDate;
  230. if (donSku.getDays() != null && donSku.getDays() > 0) {
  231. newDate = DateUtil.offsetDay(expiryTime, donSku.getDays() * 1);
  232. } else {
  233. //奈飞月付 按30天计算
  234. if (donSku.getId() == 4) {
  235. newDate = DateUtil.offsetDay(expiryTime, 30);
  236. } else {
  237. newDate = DateUtil.offset(expiryTime, DateField.MONTH, donSku.getMonths() * 1);
  238. }
  239. }
  240. relation.setExpiryTime(newDate);
  241. relation.setStartTime(relation.getStartTime() == null ? new Date() : null);
  242. relationMapper.updateById(relation);
  243. return;
  244. }
  245. orderDonList.forEach((orderDon) -> {
  246. //如果续费增加时间,如果首次则设置当前时间为初始时间
  247. Date expiryTime = Optional.ofNullable(relation.getExpiryTime()).orElse(new Date());
  248. log.info("设置账号,该用户 {} 初始时间:{} ", orderDon.getUserId(), DateUtil.format(expiryTime, "yyyy-MM-dd HH:mm:ss"));
  249. GoodsDonSku donSku = skuMapper.selectById(orderDon.getSkuId());
  250. Date newDate;
  251. int durationQuantity = multiTicket == null ? orderDon.getNum() : 1;
  252. if (donSku.getDays() != null && donSku.getDays() > 0) {
  253. newDate = DateUtil.offsetDay(expiryTime, donSku.getDays() * durationQuantity);
  254. } else {
  255. newDate = DateUtil.offset(expiryTime, DateField.MONTH, donSku.getMonths() * durationQuantity);
  256. }
  257. relation.setExpiryTime(newDate);
  258. log.info("设置账号,该用户 {} 过期时间:{} ", orderDon.getUserId(), DateUtil.format(newDate, "yyyy-MM-dd HH:mm:ss"));
  259. relation.setStartTime(relation.getStartTime() == null ? new Date() : null);
  260. relationMapper.updateById(relation);
  261. if (multiTicket == null) {
  262. orderDon.setStatus(OrderDon.Status.complete);
  263. orderDonMapper.updateById(orderDon);
  264. }
  265. });
  266. }
  267. if (relation.getUserId() != 0 && isPrdEnv && relation.getYhsId() == 0) {
  268. //发送短信
  269. smsService.sendSmsToRelationUser(relation.getUserId(), Constant.CHANGE_GROUPS_TRIPS_ACCOUNT, title, account.getAccount());
  270. }
  271. if (verifyCodeList != null && verifyCodeList.size() > 0 && i < verifyCodeList.size()) {
  272. relation.setVerifyCode(verifyCodeList.get(i));
  273. relationMapper.updateById(relation);
  274. }
  275. //设置奈飞用户座位
  276. if (account.getGoodsId() == 1) {
  277. User user = userMapper.selectById(relation.getUserId());
  278. AtomicInteger start = new AtomicInteger(1);
  279. groupsRelationNetflixService.timingSetNetflix(relation.getNum(), user.getNickname(), account.getAccount(), account.getPassword(), relation.getExpiryTime(), start);
  280. }
  281. }
  282. if (groupsRelations.isEmpty() && verifyCodeList != null && verifyCodeList.size() > 0) {
  283. groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()));
  284. for (int i = 0; i < groupsRelations.size(); i++) {
  285. GroupsRelation relation = groupsRelations.get(i);
  286. if (i < verifyCodeList.size()) {
  287. relation.setVerifyCode(verifyCodeList.get(i));
  288. relationMapper.updateById(relation);
  289. }
  290. }
  291. }
  292. groupsTrips.setStatus(GroupsTrips.Status.validity);
  293. GoodsDonSku sku = skuMapper.selectById(groupsTrips.getSkuId());
  294. //下架上过车的POE,PS AI账号 || GPT Plus独立规格 下架
  295. // if (Constant.DOWN_GOODS_PAY.contains(sku.getGoodsId()) || skuMapper.selectSingleSkuIdsByGoodsId(Constant.AI_goodsIds.get(0)).contains(groupsTrips.getSkuId())) {
  296. // Number number = beanSearcher.searchCount(GroupsRelation.class, MapUtils.builder()
  297. // .field(GroupsRelation::getGroupsId, groupsTrips.getId())
  298. // .field(GroupsRelation::getUserId, 0).op(Operator.NotEqual)
  299. // .field(GroupsRelation::getStatus, GroupsRelation.Status.validity.name())
  300. // .build());
  301. // if (number.intValue() > 0) {
  302. // groupsTrips.setStatus(GroupsTrips.Status.down);
  303. // }
  304. // }
  305. //独立车位 车队下架
  306. if (sku.getNum() != null && sku.getNum() == 1) {
  307. Number number = beanSearcher.searchCount(GroupsRelation.class, MapUtils.builder()
  308. .field(GroupsRelation::getGroupsId, groupsTrips.getId())
  309. .field(GroupsRelation::getUserId, 0).op(Operator.NotEqual)
  310. .field(GroupsRelation::getStatus, GroupsRelation.Status.validity.name())
  311. .build());
  312. if (number.intValue() > 0) {
  313. groupsTrips.setStatus(GroupsTrips.Status.down);
  314. }
  315. }
  316. updateById(groupsTrips);
  317. for (Long orderId : orderDonTicketRecordService
  318. .listSuccessfulOrderIdsByGroupsId(groupsTrips.getId())) {
  319. multiQuantityOrderService.completeIfAllTicketsAvailable(orderId);
  320. }
  321. GoodsDon goodsDon = goodsDonMapper.selectById(account.getGoodsId());
  322. userSysMsgNotifyService.recordUserAccountSetSysMsg(account.getGoodsId(), groupsTrips.getSkuId(), groupsRelations, account.getAccount(), goodsDon.getTitle());
  323. Integer alertCount = alertMapper.selectCount(Wrappers.lambdaQuery(GroupsRelationAlert.class)
  324. .eq(GroupsRelationAlert::getSkuId, sku.getId())
  325. .last("limit 1"));
  326. if (alertCount > 0) {
  327. alertMapper.update(null, Wrappers.lambdaUpdate(GroupsRelationAlert.class)
  328. .set(GroupsRelationAlert::getIsReset, true)
  329. .eq(GroupsRelationAlert::getSkuId, sku.getId())
  330. .eq(GroupsRelationAlert::getIsReset, false));
  331. }
  332. //HBO GO 满员,车队下架
  333. setDownIfFull(goodsDon, groupsTrips);
  334. //是否是GPT独立会员
  335. if (goodsDon.getId() == Constant.GPT_PLUS_GID && sku.getNum() == 1) {
  336. setRelationRechargeRemainNumInfo(groupsTrips.getId(), sku.getMonths(), account.getAccount(), account.getPassword());
  337. }
  338. //坐满是否下架车队
  339. if (sku.getIsDown()) {
  340. try {
  341. if (groupsTrips.getStatus() == GroupsTrips.Status.validity) {
  342. Integer selectCount = relationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()).eq(GroupsRelation::getStatus, GroupsTrips.Status.validity));
  343. if (selectCount == sku.getNum()) {
  344. groupsTrips.setStatus(GroupsTrips.Status.down);
  345. groupsMapper.updateById(groupsTrips);
  346. recordSysOpLog(groupsTrips.getId());
  347. }
  348. }
  349. } catch (Exception e) {
  350. }
  351. }
  352. }
  353. @Override
  354. @Transactional(rollbackFor = Throwable.class)
  355. public void replaceTripsAccount(ReplaceTripsAccountReq req) {
  356. Long groupsId = req.getGroupsId();
  357. String account = req.getAccount().trim();
  358. String password = req.getPassword().trim();
  359. String remark = req.getRemark();
  360. Date afterStartTime = req.getStartTime();
  361. Date afterExpiryTime = req.getExpiryTime();
  362. GroupsAccountView groupsAccountView = beanSearcher.searchFirst(GroupsAccountView.class, MapUtils.builder()
  363. .field(GroupsAccountView::getGroupsId, groupsId)
  364. .build());
  365. if (groupsId == null) {
  366. throw BusinessRuntimeException.getInstance("车队已不存在");
  367. }
  368. Long accountId = groupsAccountView.getAccountId();
  369. if (accountId == null) {
  370. throw BusinessRuntimeException.getInstance("该车队暂未配置账号");
  371. }
  372. Long goodsId = groupsAccountView.getGoodsId();
  373. if (!account.contains("客服")) {
  374. if (accountCommonService.checkIsDupAccount(goodsId, account)) {
  375. throw BusinessRuntimeException.getInstance("该平台下已生成该账号");
  376. }
  377. }
  378. //Claude3只允许替换一次
  379. if (Constant.CALUDE_GOODS_ID.equals(groupsAccountView.getGoodsId())) {
  380. Integer count = replaceRecordMapper.selectCount(Wrappers.lambdaQuery(GroupsTripsAccountReplaceRecord.class)
  381. .eq(GroupsTripsAccountReplaceRecord::getGroupsId, groupsAccountView.getGroupsId()));
  382. if (count > 0) throw BusinessRuntimeException.getInstance("Claude3账号只允许替换一次");
  383. }
  384. if (afterStartTime == null) {
  385. afterStartTime = groupsAccountView.getStartTime();
  386. }
  387. if (afterExpiryTime == null) {
  388. afterExpiryTime = groupsAccountView.getExpiryTime();
  389. }
  390. log.info("原账号accountId:{}", accountId);
  391. //替换账号
  392. int update = accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  393. .set(Account::getAccount, account)
  394. .set(Account::getPassword, password)
  395. .set(Account::getRemark, remark)
  396. .set(StrUtil.isNotBlank(req.getEmail()), Account::getEmail, req.getEmail())
  397. .set(StrUtil.isNotBlank(req.getRelateEmail()), Account::getRelateEmail, req.getRelateEmail())
  398. .set(Account::getStartTime, afterStartTime)
  399. .set(Account::getExpiryTime, afterExpiryTime)
  400. .set(Account::getGptRefreshToken, StrUtil.EMPTY)
  401. .eq(Account::getId, accountId));
  402. if (update == 0) {
  403. log.info("替换原账号oldAccount:{}至新账号newAccount:{}失败", groupsAccountView.getAccount(), account);
  404. throw BusinessRuntimeException.getInstance("替换账号失败");
  405. }
  406. if (StrUtil.isNotBlank(groupsAccountView.getGptRefreshToken())) {
  407. //清除gpt token
  408. redisService.del(RedisKey.CHATGPT_ACCESS_TOKEN + groupsAccountView.getAccount());
  409. redisService.del(RedisService.key.CHAT_GPT_TOKEN_EXCEPTION_KEY.getName() + groupsAccountView.getAccount());
  410. }
  411. //时间有效
  412. if (afterExpiryTime != null && afterExpiryTime.after(DateTime.now())) {
  413. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  414. .set(GroupsTrips::getStatus, GroupsTrips.Status.validity)
  415. .eq(GroupsTrips::getAccountId, accountId)
  416. .eq(GroupsTrips::getStatus, GroupsTrips.Status.down));
  417. }
  418. //保存替换记录
  419. GroupsTripsAccountReplaceRecord replaceRecord = new GroupsTripsAccountReplaceRecord();
  420. replaceRecord.setGroupsId(groupsId);
  421. replaceRecord.setOldAccountId(accountId);
  422. replaceRecord.setOldAccount(groupsAccountView.getAccount());
  423. replaceRecord.setOldPassword(groupsAccountView.getPassword());
  424. replaceRecord.setOldStartTime(groupsAccountView.getStartTime());
  425. replaceRecord.setOldExpiryTime(groupsAccountView.getExpiryTime());
  426. Long adminId = req.getAdminId();
  427. Optional.ofNullable(cmsUserMapper.selectById(adminId)).ifPresent(admin -> replaceRecord.setOperator(admin.getNickname()));
  428. replaceRecord.setNewAccount(account);
  429. replaceRecord.setNewPassword(password);
  430. replaceRecord.setNewStartTime(afterStartTime);
  431. replaceRecord.setNewExpiryTime(afterExpiryTime);
  432. replaceRecord.setRemark(remark);
  433. replaceRecordMapper.insert(replaceRecord);
  434. TASK_EXECUTOR.execute(() -> sendChangeAccountMsg(groupsId, goodsId, account));;
  435. }
  436. @Override
  437. public Integer getAvailableRelation(Long goodsId, Long skuId) {
  438. //chatGPT
  439. GoodsDonSku sku = skuMapper.selectById(skuId);
  440. if (sku == null) {
  441. return BigDecimal.ZERO.intValue();
  442. }
  443. goodsId = sku.getGoodsId();
  444. Integer skuNum = sku.getNum();
  445. if (Constant.AI_goodsIds.contains(sku.getGoodsId()) && sku.getNum() > 1) {
  446. Integer extraNum = Constant.AI_EXTRACT_NUM;
  447. //GPT 4个月付
  448. if (skuId == 178l) {
  449. extraNum = 2;
  450. }
  451. Integer maxNum = skuNum + extraNum;
  452. //主账号有效时间
  453. DateTime time = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 25);
  454. Integer available = relationMapper.selectAvailableRelation(skuId, skuNum);
  455. //额外硬塞
  456. Integer special_available = relationMapper.selectSpecialGroupsRelationAvailable(skuId, maxNum, skuNum, extraNum, time);
  457. available += special_available;
  458. //GPT Plus 10人月付规格 4人月付
  459. if (Constant.GPT_PLUS_PARKING_SKU_IDS.contains(skuId)) {
  460. //上面已排除限制车队
  461. //限制车队可用车数位
  462. Integer limitAvailable = Optional.ofNullable(relationMapper.selectLimitAvailableNum(skuId, maxNum)).orElse(0);
  463. if (limitAvailable < 0) {
  464. limitAvailable = 0;
  465. }
  466. available += limitAvailable;
  467. //新车队预备上车 最大可用数
  468. if (Constant.GPT_PLUS_PARKING_SKU_IDS.get(0).equals(skuId)) {
  469. maxNum = 10;
  470. }else {
  471. maxNum = 4;
  472. }
  473. }
  474. available = getAvailableAfterPreAccount(available, skuId, maxNum, goodsId);
  475. //待发车已有人员成功上车
  476. Integer waitingNum = relationMapper.selectNumWaitingGroups(skuId);
  477. available -= waitingNum;
  478. return available;
  479. }
  480. Integer available = relationMapper.selectAvailableRelation(skuId, skuNum);
  481. available = getAvailableAfterPreAccount(available, skuId, skuNum, goodsId);
  482. //待发车已有人员成功上车
  483. Integer waitingNum = relationMapper.selectNumWaitingGroups(skuId);
  484. available -= waitingNum;
  485. return available;
  486. }
  487. @Override
  488. public void sendChangeAccountMsg(Long groupsId, Long goodsId, String account) {
  489. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  490. String title = goodsDon != null ? goodsDon.getTitle() : "车票";
  491. DateTime now = DateTime.now();
  492. Boolean isPrd = envCommonService.isPrdEnv();
  493. List<GroupsRelationView> groupsRelations = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder().field(GroupsRelationView::getGroupsId, groupsId).build());
  494. if (CollUtil.isEmpty(groupsRelations)) {
  495. return;
  496. }
  497. Long skuId = groupsMapper.selectById(groupsId).getSkuId();
  498. userSysMsgNotifyService.recordUserAccountUpdateSysMsg(goodsId, skuId, groupsRelations, account, goodsDon.getTitle());
  499. groupsRelations.forEach(relation -> {
  500. if (relation.getExpiryTime() == null) {
  501. return;
  502. }
  503. //账号有效期小于5天的人,不发账号替换/改密码短信提醒
  504. if (now.after(DateUtil.offsetDay(relation.getExpiryTime(), -5))) {
  505. return;
  506. }
  507. //去掉GPT过滤平台
  508. if (Constant.CHANGE_ACCOUNT_PWD_FILTER_GIDS.get(1) == goodsId) {
  509. return;
  510. }
  511. if (relation.getUserId() != 0 && isPrd && relation.getYhsId() == 0) {
  512. try {
  513. smsService.sendSmsToRelationUser(relation.getUserId(), Constant.CHANGE_GROUPS_TRIPS_ACCOUNT, title, account);
  514. } catch (Exception e) {
  515. }
  516. }
  517. });
  518. }
  519. public Integer getAvailableAfterPreAccount(Integer available, Long skuId, Integer maxNum, Long goodsId) {
  520. MapBuilder builder = MapUtils.builder();
  521. if (goodsId == 26) {
  522. //是否在预备规格集合中
  523. String extra_sql = String.format("FIND_IN_SET(%s,a.pre_sku_ids) > 0", skuId);
  524. builder.put("extra_sql", extra_sql);
  525. }
  526. Number preAccount = beanSearcher.searchCount(AccountView.class, builder
  527. .field(AccountView::getGoodsId, goodsId)
  528. .field(AccountView::getAccountType, 2)
  529. .field(AccountView::getGroupsId).op(Operator.IsNull)
  530. .build());
  531. //计算上预发布账号车位数
  532. if (preAccount.intValue() > 0) {
  533. available += preAccount.intValue() * maxNum;
  534. }
  535. return available;
  536. }
  537. private void setDownIfFull(GoodsDon goodsDon, GroupsTrips groupsTrips) {
  538. //HBO GO 满员,车队下架
  539. if (goodsDon.getId() == Constant.HBO_GO_GOODS_ID) {
  540. if (groupsTrips.getStatus() == GroupsTrips.Status.validity) {
  541. Integer count2 = relationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  542. .eq(GroupsRelation::getGroupsId, groupsTrips.getId())
  543. .in(GroupsRelation::getStatus, List.of(GroupsRelation.Status.none, GroupsRelation.Status.locking)));
  544. if (count2 == 0) {
  545. log.info("HBO GO车队:{}已满员,自动下架", groupsTrips.getId());
  546. groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  547. .set(GroupsTrips::getStatus, GroupsTrips.Status.down)
  548. .eq(GroupsTrips::getId, groupsTrips.getId()));
  549. }
  550. }
  551. }
  552. }
  553. /**
  554. * 奈飞异常恢复车票
  555. */
  556. private NetflixUserAccountSubmitRecoverRecord updateRelationIfRecoverTicket(Boolean isRecover, GoodsDonSku sku, GroupsRelation relation) {
  557. if (isRecover != null && isRecover) {
  558. NetflixUserAccountSubmitRecoverRecord recoverRecord = netflixUserAccountSubmitRecoverRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
  559. .eq(NetflixUserAccountSubmitRecoverRecord::getSkuId, sku.getId())
  560. .eq(NetflixUserAccountSubmitRecoverRecord::getStatus, 0)
  561. .orderByAsc(NetflixUserAccountSubmitRecoverRecord::getCreatedTime)
  562. .last("limit 1"));
  563. if (recoverRecord != null) {
  564. relation.setUserId(recoverRecord.getUserId());
  565. relation.setStatus(GroupsRelation.Status.validity);
  566. relation.setStartTime(DateTime.now());
  567. relation.setExpiryTime(DateUtil.offsetDay(relation.getStartTime(), recoverRecord.getRemainDays().intValue()));
  568. //清除车票
  569. List<Long> userIdList = userBindRelationService.getRelationUserIdList(recoverRecord.getUserId(), null);
  570. GroupsRelation clearRelation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  571. .eq(GroupsRelation::getId, recoverRecord.getRelationId())
  572. .in(GroupsRelation::getUserId, userIdList)
  573. .last("limit 1"));
  574. if (clearRelation != null) {
  575. clearService.clearTicket(clearRelation, UserTicketClearedRecord.Source.ticket_replace);
  576. }
  577. recoverRecord.setStatus(1);
  578. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  579. .in(OrderDon::getUserId, userIdList)
  580. .eq(OrderDon::getRelationId, recoverRecord.getRelationId())
  581. .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
  582. .orderByDesc(OrderDon::getId)
  583. .last("limit 1"));
  584. if (orderDon != null) {
  585. orderDon.setRelationId(relation.getId());
  586. orderDonMapper.updateById(orderDon);
  587. }
  588. NetflixUserAccountStatusRecord netflixUserAccountStatusRecord = netflixUserAccountStatusRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
  589. .in(NetflixUserAccountStatusRecord::getUserId, userIdList)
  590. .eq(NetflixUserAccountStatusRecord::getRelationId, recoverRecord.getRelationId())
  591. .eq(NetflixUserAccountStatusRecord::getDeleted, 1)
  592. .last("limit 1"));
  593. if (netflixUserAccountStatusRecord != null) {
  594. netflixUserAccountStatusRecord.setDeleted(false);
  595. netflixUserAccountStatusRecordMapper.updateById(netflixUserAccountStatusRecord);
  596. }
  597. return recoverRecord;
  598. }
  599. }
  600. return null;
  601. }
  602. private void setRelationRechargeRemainNumInfo(Long groupsId, Integer addNum, String account, String password) {
  603. GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsId).last("limit 1"));
  604. if (relation.getStatus() == GroupsRelation.Status.validity) {
  605. if (relation.getRechargeStatus() == null) {
  606. relation.setRechargeStatus(GroupsRelation.RechargeStatus.complete);
  607. relation.setRechargeNum(addNum);
  608. relation.setRechargeRemainNum(addNum - 1);
  609. relation.setSubmitTime(DateTime.now());
  610. relation.setSendTime(relation.getSubmitTime());
  611. relation.setAccount(account);
  612. relation.setPassword(password);
  613. relationMapper.updateById(relation);
  614. //记录gpt代充记录
  615. gptUserRechargeRecordService.recordGptUserRechargeNum(null, relation.getUserId(), relation.getId(), relation.getAccount(), relation.getRechargeRemainNum(), null, "下单");
  616. }
  617. }
  618. }
  619. private void recordSysOpLog(Long groupsId) {
  620. try {
  621. //添加操作记录
  622. SysLog sysLog = new SysLog();
  623. sysLog.setModule("过期账号/上下架过期车队");
  624. sysLog.setOperateParams(groupsId.toString());
  625. sysLog.setOperator("坐满系统下架");
  626. sysLogMapper.insert(sysLog);
  627. } catch (Exception e) {
  628. }
  629. }
  630. }