CmsAccountServiceImpl.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.cyksj.service.mange.account;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.RandomUtil;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.cyksj.common.constant.Constant;
  9. import com.cyksj.common.exception.BusinessRuntimeException;
  10. import com.cyksj.common.util.Jsons;
  11. import com.cyksj.common.util.StringUtil;
  12. import com.cyksj.mapper.AccountMapper;
  13. import com.cyksj.mapper.AccountUpdatePasswordRecordMapper;
  14. import com.cyksj.mapper.manage.AutoUpdateAccountPwdLogMapper;
  15. import com.cyksj.mapper.manage.cms.CmsUserMapper;
  16. import com.cyksj.model.entity.*;
  17. import com.cyksj.model.excel.ExcelAccountData;
  18. import com.cyksj.model.manage.views.AccountView;
  19. import com.cyksj.model.views.CmsUserVO;
  20. import com.cyksj.model.views.ExpiredAccountDataView;
  21. import com.cyksj.service.mange.CmsAccountService;
  22. import com.cyksj.service.mange.CmsGroupService;
  23. import com.ejlchina.searcher.BeanSearcher;
  24. import com.ejlchina.searcher.param.Operator;
  25. import com.ejlchina.searcher.util.MapUtils;
  26. import com.google.common.collect.Lists;
  27. import lombok.RequiredArgsConstructor;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.Optional;
  35. /**
  36. * @author chan
  37. * @date 2022/9/27 6:23 PM
  38. */
  39. @Slf4j
  40. @Service("cms")
  41. @RequiredArgsConstructor
  42. public class CmsAccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements CmsAccountService {
  43. private final AccountMapper accountMapper;
  44. private final AccountUpdatePasswordRecordMapper accountUpdatePasswordRecordMapper;
  45. private final CmsUserMapper cmsUserMapper;
  46. private final AutoUpdateAccountPwdLogMapper autoUpdateAccountPwdLogMapper;
  47. private final BeanSearcher beanSearcher;
  48. private final CmsGroupService cmsGroupService;
  49. @Override
  50. public IPage<ExpiredAccountDataView> getExpiredAccountView(Boolean isCorpWx, String customerService, Long goodsId, Long skuId, String account, Boolean renewStatus, Boolean handleStatus, String cnAccount, String usAccount, Integer userId, String startTime, String endTime, String now, Long offset, Long limit) {
  51. return accountMapper.getExpiredAccountView(isCorpWx, customerService, goodsId, skuId, account, renewStatus, handleStatus, cnAccount, usAccount, userId, startTime, endTime, now, new Page<>(offset, limit));
  52. }
  53. @Override
  54. public List<ExcelAccountData> getExpiredAccountViewNoPage(Boolean isCorpWx, String customerService, Long goodsId, Long skuId, String account, Boolean renewStatus, Boolean handleStatus, String cnAccount, String usAccount, Integer userId, String startTime, String endTime, String now) {
  55. return accountMapper.getExpiredAccountViewNoPage(isCorpWx, customerService, goodsId, skuId, account, renewStatus, handleStatus, cnAccount, usAccount, userId, startTime, endTime, now);
  56. }
  57. @Override
  58. public List<Account> getNoAppleOneAccount(Long appleOneGoodsId) {
  59. return accountMapper.getNoAppleOneAccount(appleOneGoodsId);
  60. }
  61. @Override
  62. public List<OrderDon> getNoAppleOneOrderDon(List<Long> skuIds, Date yesZeroDate, Date thisZeroDate, List<String> noOrderStatus) {
  63. return accountMapper.getNoAppleOneOrderDon(skuIds, yesZeroDate, thisZeroDate, noOrderStatus);
  64. }
  65. @Override
  66. public List<AccountView> getExpiredAllAccount() {
  67. return accountMapper.getExpiredAllAccount();
  68. }
  69. @Override
  70. public void saveRecord(Long operateUserId, Long accountId, String oldPassword, String newPassword, String type) {
  71. AccountUpdatePasswordRecord record = new AccountUpdatePasswordRecord();
  72. record.setAccountId(accountId);
  73. record.setOperateUserId(operateUserId);
  74. Optional.ofNullable(cmsUserMapper.selectById(operateUserId))
  75. .ifPresent(admin -> record.setOperator(admin.getNickname()));
  76. record.setOldPassword(oldPassword);
  77. record.setNewPassword(newPassword);
  78. record.setType(type);
  79. accountUpdatePasswordRecordMapper.insert(record);
  80. }
  81. @Override
  82. public void recordAutoUpdatePwd(Long accountId, String account, String oldPwd, String newPwd, String operateIp) {
  83. AutoUpdateAccountPwdLog log = new AutoUpdateAccountPwdLog();
  84. log.setAccountId(accountId);
  85. log.setAccount(account);
  86. log.setOldPwd(oldPwd);
  87. log.setNewPwd(newPwd);
  88. log.setOperateLocation(StringUtil.getRealAddressByIP(operateIp));
  89. log.setOperateIp(operateIp);
  90. autoUpdateAccountPwdLogMapper.insert(log);
  91. }
  92. @Override
  93. public void assignDeleteCustomerAccount(Long customerServiceId) {
  94. List<AccountView> accountViews = beanSearcher.searchAll(AccountView.class, MapUtils.builder()
  95. .field(AccountView::getCustomerServiceId, customerServiceId)
  96. .build());
  97. if (accountViews.size() > 0) {
  98. List<CmsUserVO> customers = beanSearcher.searchAll(CmsUserVO.class, MapUtils.builder()
  99. .field(CmsUserVO::getId, customerServiceId).op(Operator.NotEqual)
  100. .field(CmsUserVO::getRoleNames, "客服").op(Operator.Contain)
  101. .build());
  102. if (customers.size() > 0) {
  103. log.info("删除客服id:{},分配其未修改的过期账号,账号数量:{}", customerServiceId, accountViews.size());
  104. assignCustomerAccount(customers, accountViews);
  105. }
  106. }
  107. }
  108. @Override
  109. public void assignCustomerAccount(List<CmsUserVO> customers, List<AccountView> accountViews) {
  110. int customerSize = customers.size();
  111. int accountSize = accountViews.size();
  112. if (accountSize < customerSize) {
  113. accountViews.forEach(account -> {
  114. int randomIndex = RandomUtil.randomInt(customerSize);
  115. CmsUserVO customer = customers.get(randomIndex);
  116. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  117. .set(Account::getCustomerServiceId, customer.getId())
  118. .eq(Account::getId, account.getId()));
  119. });
  120. return;
  121. }
  122. int num = accountSize / customerSize;
  123. int remain = accountSize % customerSize;
  124. List<List<AccountView>> partition = Lists.partition(accountViews, num);
  125. for (int i = 0; i < customerSize; i++) {
  126. List<AccountView> expiredAccounts = partition.get(i);
  127. CmsUserVO customer = customers.get(i);
  128. expiredAccounts.forEach(ea -> {
  129. Long accountId = ea.getId();
  130. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  131. .set(Account::getCustomerServiceId, customer.getId())
  132. .eq(Account::getId, accountId));
  133. });
  134. }
  135. if (remain > 0) {
  136. log.info("分配多余的过期账号给客服");
  137. List<AccountView> remainExpiredAccount = partition.get(customerSize);
  138. remainExpiredAccount.forEach(remainAccount -> {
  139. int randomIndex = RandomUtil.randomInt(customerSize);
  140. log.info("获取的随机下标:{}", randomIndex);
  141. CmsUserVO customer = customers.get(randomIndex);
  142. log.info("随机分配的客服是:{}", customer.getNickname());
  143. Long accountId = remainAccount.getId();
  144. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  145. .set(Account::getCustomerServiceId, customer.getId())
  146. .eq(Account::getId, accountId));
  147. });
  148. }
  149. }
  150. @Override
  151. @Transactional(rollbackFor = Throwable.class)
  152. public void analysisChatGPTAccount(Long goodsId, Long skuId, List<Object> accountList) {
  153. insertAccount(goodsId, skuId, accountList);
  154. }
  155. @Override
  156. public void analysisAmericaAppidAccount(Long goodsId, Long skuId, List<Object> accountList) {
  157. insertAccount(goodsId, skuId, accountList);
  158. }
  159. /**
  160. *
  161. * @param goodsId
  162. * @param skuId
  163. * @param accountList
  164. */
  165. public void insertAccount(Long goodsId, Long skuId, List<Object> accountList) {
  166. if (CollUtil.isEmpty(accountList)) {
  167. throw BusinessRuntimeException.getInstance("文件数据为空");
  168. }
  169. accountList.forEach(accountData -> {
  170. Map<String, Object> map = Jsons.toMap(accountData);
  171. String[] part = map.get("0").toString().split("----");
  172. if (part.length >= 2) {
  173. String account = part[0];
  174. String password = part[1];
  175. int count = this.count(Wrappers.lambdaQuery(Account.class)
  176. .eq(Account::getAccount, account)
  177. .eq(Account::getGoodsId, goodsId));
  178. if (count > 0) {
  179. return;
  180. }
  181. Account insert = new Account();
  182. insert.setAccount(account);
  183. insert.setPassword(password);
  184. if (part.length > 2) {
  185. String apiKey = part[2];
  186. insert.setApiKey(apiKey);
  187. }
  188. insert.setGoodsId(goodsId);
  189. if (goodsId.equals(31l)) {
  190. insert.setSecret(Constant.AMERICA_APPID_SECRET);
  191. }
  192. this.save(insert);
  193. //自动配置车队
  194. GroupsTrips groupsTrips = new GroupsTrips();
  195. groupsTrips.setSkuId(skuId);
  196. groupsTrips.setAccountId(insert.getId());
  197. cmsGroupService.issuance(groupsTrips, null);
  198. }
  199. });
  200. }
  201. }