CmsGroupServiceImpl.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.cyksj.service.mange.group;
  2. import cn.hutool.core.date.DateField;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.cyksj.common.constant.Constant;
  8. import com.cyksj.common.exception.BusinessRuntimeException;
  9. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  10. import com.cyksj.common.util.SmsUtil;
  11. import com.cyksj.mapper.*;
  12. import com.cyksj.mapper.manage.GroupsTripsAccountReplaceRecordMapper;
  13. import com.cyksj.mapper.manage.cms.CmsUserMapper;
  14. import com.cyksj.mapper.market.task.UserBindDetailMapper;
  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.ejlchina.searcher.BeanSearcher;
  20. import com.ejlchina.searcher.util.MapUtils;
  21. import lombok.RequiredArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.Optional;
  28. /**
  29. * @author chan
  30. * @date 2022/9/27 6:27 PM
  31. */
  32. @Slf4j
  33. @Service
  34. @RequiredArgsConstructor
  35. public class CmsGroupServiceImpl extends ServiceImpl<GroupsMapper,GroupsTrips> implements CmsGroupService {
  36. private final GoodsDonSkuMapper skuMapper;
  37. private final GroupsRelationMapper relationMapper;
  38. private final OrderDonMapper orderDonMapper;
  39. private final AccountMapper accountMapper;
  40. private final BeanSearcher beanSearcher;
  41. private final GroupsTripsAccountReplaceRecordMapper replaceRecordMapper;
  42. private final CmsUserMapper cmsUserMapper;
  43. private final GoodsDonMapper goodsDonMapper;
  44. private UserBindDetailMapper userBindDetailMapper;
  45. private final UserMapper userMapper;
  46. private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
  47. @Override
  48. public GroupsTrips issuance(GroupsTrips groups) {
  49. GoodsDonSku sku = skuMapper.selectById(groups.getSkuId());
  50. if (sku == null) {
  51. throw new BusinessRuntimeException("该商品规格不存在!");
  52. }
  53. groups.setNum(sku.getNum());
  54. groups.setAvailableNum(sku.getNum());
  55. if (groups.getAccountId() == null || accountMapper.selectById(groups.getAccountId()) == null) {
  56. throw new BusinessRuntimeException("该账号不存在");
  57. }
  58. groups.setStatus(GroupsTrips.Status.validity);
  59. this.save(groups);
  60. for (Integer i = 1; i <= groups.getNum(); i++) {
  61. GroupsRelation relation = new GroupsRelation();
  62. relation.setGroupsId(groups.getId());
  63. relation.setStatus(GroupsRelation.Status.none);
  64. relation.setNum(i);
  65. relationMapper.insert(relation);
  66. }
  67. return groups;
  68. }
  69. @Override
  70. public void close(Long groupId) {
  71. GroupsTrips groupsTrips = this.getById(groupId);
  72. if(groupsTrips == null){
  73. throw new BusinessRuntimeException("非法参数.车队不存在");
  74. }
  75. Integer count = relationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
  76. .eq(GroupsRelation::getGroupsId, groupId)
  77. .in(GroupsRelation::getStatus, List.of(GroupsRelation.Status.locking, GroupsRelation.Status.validity)));
  78. if (count > 0) {
  79. throw new BusinessRuntimeException("该车队已有用户购买或锁定.无法删除");
  80. }else {
  81. relationMapper.delete(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId,groupId));
  82. this.removeById(groupId);
  83. }
  84. }
  85. @Override
  86. @Transactional(rollbackFor = Throwable.class)
  87. public void setAccount(GroupsTrips groupsTrips) {
  88. int count = count(Wrappers.lambdaQuery(GroupsTrips.class).eq(GroupsTrips::getAccountId, groupsTrips.getAccountId()));
  89. if (count > 0) {
  90. throw new BusinessRuntimeException("该账号已发车.请选择未发车账户");
  91. }
  92. List<GroupsRelation> groupsRelations = relationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsTrips.getId()).eq(GroupsRelation::getStatus, GroupsRelation.Status.validity));
  93. groupsRelations.forEach((relation)->{
  94. if (relation.getExpiryTime() == null) {
  95. 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));
  96. orderDonList.forEach((orderDon)->{
  97. //如果续费增加时间,如果首次则设置当前时间为初始时间
  98. Date expiryTime = Optional.ofNullable(relation.getExpiryTime()).orElse(new Date());
  99. log.info("设置账号,该用户 {} 初始时间:{} ",orderDon.getUserId(),DateUtil.format(expiryTime,"yyyy-MM-dd HH:mm:ss"));
  100. GoodsDonSku donSku = skuMapper.selectById(orderDon.getSkuId());
  101. Date newDate = DateUtil.offset(expiryTime, DateField.MONTH, donSku.getMonths() * orderDon.getNum());
  102. relation.setExpiryTime(newDate);
  103. log.info("设置账号,该用户 {} 过期时间:{} ",orderDon.getUserId(),DateUtil.format(newDate,"yyyy-MM-dd HH:mm:ss"));
  104. relation.setStartTime(relation.getStartTime() == null ? new Date() : null);
  105. relationMapper.updateById(relation);
  106. orderDon.setStatus(OrderDon.Status.complete);
  107. orderDonMapper.updateById(orderDon);
  108. });
  109. }
  110. });
  111. groupsTrips.setStatus(GroupsTrips.Status.validity);
  112. updateById(groupsTrips);
  113. }
  114. @Override
  115. @Transactional(rollbackFor = Throwable.class)
  116. public void replaceTripsAccount(ReplaceTripsAccountReq req) {
  117. Long groupsId = req.getGroupsId();
  118. String account = req.getAccount().trim();
  119. String password = req.getPassword().trim();
  120. String remark = req.getRemark();
  121. GroupsAccountView groupsAccountView = beanSearcher.searchFirst(GroupsAccountView.class, MapUtils.builder()
  122. .field(GroupsAccountView::getGroupsId, groupsId)
  123. .build());
  124. if (groupsId == null) {
  125. throw BusinessRuntimeException.getInstance("车队已不存在");
  126. }
  127. Long accountId = groupsAccountView.getAccountId();
  128. if (accountId == null) {
  129. throw BusinessRuntimeException.getInstance("该车队暂未配置账号");
  130. }
  131. Long goodsId = groupsAccountView.getGoodsId();
  132. if (!account.contains("客服")) {
  133. int count = accountMapper.selectCount(Wrappers.lambdaQuery(Account.class)
  134. .eq(Account::getAccount, account)
  135. .eq(Account::getGoodsId, goodsId));
  136. if (count > 0) {
  137. throw BusinessRuntimeException.getInstance("该平台下已生成该账号");
  138. }
  139. }
  140. //新增账号
  141. Account replace = new Account();
  142. replace.setAccount(account);
  143. replace.setPassword(password);
  144. replace.setGoodsId(goodsId);
  145. replace.setStartTime(groupsAccountView.getStartTime());
  146. replace.setExpiryTime(groupsAccountView.getExpiryTime());
  147. replace.setRemark(remark);
  148. accountMapper.insert(replace);
  149. //替换车队账号
  150. this.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
  151. .set(GroupsTrips::getAccountId, replace.getId())
  152. .eq(GroupsTrips::getId, groupsId));
  153. //删除旧的账号
  154. accountMapper.deleteById(accountId);
  155. //保存替换记录
  156. GroupsTripsAccountReplaceRecord replaceRecord = new GroupsTripsAccountReplaceRecord();
  157. replaceRecord.setGroupsId(groupsId);
  158. replaceRecord.setOldAccountId(accountId);
  159. replaceRecord.setOldAccount(groupsAccountView.getAccount());
  160. replaceRecord.setOldPassword(groupsAccountView.getPassword());
  161. replaceRecord.setOldStartTime(groupsAccountView.getStartTime());
  162. replaceRecord.setOldExpiryTime(groupsAccountView.getExpiryTime());
  163. Long adminId = req.getAdminId();
  164. Optional.ofNullable(cmsUserMapper.selectById(adminId)).ifPresent(admin -> replaceRecord.setOperator(admin.getNickname()));
  165. replaceRecord.setNewAccountId(replace.getId());
  166. replaceRecord.setNewAccount(replace.getAccount());
  167. replaceRecord.setNewPassword(replace.getPassword());
  168. replaceRecord.setNewStartTime(replace.getStartTime());
  169. replaceRecord.setNewExpiryTime(replace.getExpiryTime());
  170. replaceRecordMapper.insert(replaceRecord);
  171. TASK_EXECUTOR.execute(() -> sendChangeAccountMsg(groupsId, goodsId));;
  172. }
  173. private void sendChangeAccountMsg(Long groupsId,Long goodsId) {
  174. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  175. List<GroupsRelation> groupsRelations = beanSearcher.searchAll(GroupsRelation.class, MapUtils.builder().field(GroupsRelation::getGroupsId, groupsId).build());
  176. groupsRelations.forEach(relation->{
  177. Long userId = relation.getUserId();
  178. User user = userMapper.selectById(userId);
  179. String phone = null;
  180. if (user != null) {
  181. phone = user.getLoginPhone();
  182. if (StrUtil.isEmpty(phone)) {
  183. UserBindDetail userBindDetail = userBindDetailMapper.selectById(userId);
  184. if (userBindDetail != null) {
  185. phone = userBindDetail.getPhone();
  186. }
  187. }
  188. }
  189. if (StrUtil.isNotBlank(phone)) {
  190. String title = goodsDon != null ? goodsDon.getTitle() : "车票";
  191. SmsUtil.sendLuoKey2Msg(phone, String.format(Constant.CHANGE_GROUPS_TRIPS_ACCOUNT, title));
  192. }
  193. });
  194. }
  195. }