CmsAccountServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.redis.RedisService;
  22. import com.cyksj.service.mange.AccountCommonService;
  23. import com.cyksj.service.mange.CmsAccountService;
  24. import com.cyksj.service.mange.CmsGroupService;
  25. import com.cyksj.service.sys.SysConfigService;
  26. import com.ejlchina.searcher.BeanSearcher;
  27. import com.ejlchina.searcher.param.Operator;
  28. import com.ejlchina.searcher.util.MapUtils;
  29. import com.google.common.collect.Lists;
  30. import lombok.RequiredArgsConstructor;
  31. import lombok.extern.slf4j.Slf4j;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.transaction.annotation.Transactional;
  34. import java.util.*;
  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. private final RedisService redisService;
  50. private final AccountCommonService accountCommonService;
  51. private final SysConfigService sysConfigService;
  52. @Override
  53. 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) {
  54. return accountMapper.getExpiredAccountView(isCorpWx, customerService, goodsId, skuId, account, renewStatus, handleStatus, cnAccount, usAccount, userId, startTime, endTime, now, new Page<>(offset, limit));
  55. }
  56. @Override
  57. 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) {
  58. return accountMapper.getExpiredAccountViewNoPage(isCorpWx, customerService, goodsId, skuId, account, renewStatus, handleStatus, cnAccount, usAccount, userId, startTime, endTime, now);
  59. }
  60. @Override
  61. public List<OrderDon> getNoAppleOneOrderDon(List<Long> skuIds, Date yesZeroDate, Date thisZeroDate, List<String> noOrderStatus) {
  62. return accountMapper.getNoAppleOneOrderDon(skuIds, yesZeroDate, thisZeroDate, noOrderStatus);
  63. }
  64. @Override
  65. public List<AccountView> getExpiredAllAccount() {
  66. return accountMapper.getExpiredAllAccount();
  67. }
  68. @Override
  69. public void saveRecord(Long operateUserId, Long accountId, String oldPassword, String newPassword, String type) {
  70. AccountUpdatePasswordRecord record = new AccountUpdatePasswordRecord();
  71. record.setAccountId(accountId);
  72. record.setOperateUserId(operateUserId);
  73. Optional.ofNullable(cmsUserMapper.selectById(operateUserId))
  74. .ifPresent(admin -> record.setOperator(admin.getNickname()));
  75. record.setOldPassword(oldPassword);
  76. record.setNewPassword(newPassword);
  77. record.setType(type);
  78. accountUpdatePasswordRecordMapper.insert(record);
  79. }
  80. @Override
  81. public void recordAutoUpdatePwd(Long accountId, String account, String oldPwd, String newPwd, String operateIp) {
  82. AutoUpdateAccountPwdLog log = new AutoUpdateAccountPwdLog();
  83. log.setAccountId(accountId);
  84. log.setAccount(account);
  85. log.setOldPwd(oldPwd);
  86. log.setNewPwd(newPwd);
  87. log.setOperateLocation(StringUtil.getRealAddressByIP(operateIp));
  88. log.setOperateIp(operateIp);
  89. autoUpdateAccountPwdLogMapper.insert(log);
  90. }
  91. @Override
  92. public void assignDeleteCustomerAccount(Long customerServiceId) {
  93. List<AccountView> accountViews = beanSearcher.searchAll(AccountView.class, MapUtils.builder()
  94. .field(AccountView::getCustomerServiceId, customerServiceId)
  95. .build());
  96. if (accountViews.size() > 0) {
  97. List<CmsUserVO> customers = beanSearcher.searchAll(CmsUserVO.class, MapUtils.builder()
  98. .field(CmsUserVO::getId, customerServiceId).op(Operator.NotEqual)
  99. .field(CmsUserVO::getRoleNames, "客服").op(Operator.Contain)
  100. .build());
  101. if (customers.size() > 0) {
  102. log.info("删除客服id:{},分配其未修改的过期账号,账号数量:{}", customerServiceId, accountViews.size());
  103. assignCustomerAccount(customers, accountViews);
  104. }
  105. }
  106. }
  107. @Override
  108. public void assignCustomerAccount(List<CmsUserVO> customers, List<AccountView> accountViews) {
  109. int customerSize = customers.size();
  110. int accountSize = accountViews.size();
  111. if (accountSize < customerSize) {
  112. accountViews.forEach(account -> {
  113. int randomIndex = RandomUtil.randomInt(customerSize);
  114. CmsUserVO customer = customers.get(randomIndex);
  115. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  116. .set(Account::getCustomerServiceId, customer.getId())
  117. .eq(Account::getId, account.getId()));
  118. });
  119. return;
  120. }
  121. int num = accountSize / customerSize;
  122. int remain = accountSize % customerSize;
  123. List<List<AccountView>> partition = Lists.partition(accountViews, num);
  124. for (int i = 0; i < customerSize; i++) {
  125. List<AccountView> expiredAccounts = partition.get(i);
  126. CmsUserVO customer = customers.get(i);
  127. expiredAccounts.forEach(ea -> {
  128. Long accountId = ea.getId();
  129. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  130. .set(Account::getCustomerServiceId, customer.getId())
  131. .eq(Account::getId, accountId));
  132. });
  133. }
  134. if (remain > 0) {
  135. log.info("分配多余的过期账号给客服");
  136. List<AccountView> remainExpiredAccount = partition.get(customerSize);
  137. remainExpiredAccount.forEach(remainAccount -> {
  138. int randomIndex = RandomUtil.randomInt(customerSize);
  139. log.info("获取的随机下标:{}", randomIndex);
  140. CmsUserVO customer = customers.get(randomIndex);
  141. log.info("随机分配的客服是:{}", customer.getNickname());
  142. Long accountId = remainAccount.getId();
  143. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  144. .set(Account::getCustomerServiceId, customer.getId())
  145. .eq(Account::getId, accountId));
  146. });
  147. }
  148. }
  149. @Override
  150. @Transactional(rollbackFor = Throwable.class)
  151. public void analysisChatGPTAccount(Long goodsId, Long skuId, List<Object> accountList) {
  152. insertAccount(goodsId, skuId, accountList);
  153. }
  154. @Override
  155. public void analysisAmericaAppidAccount(Long goodsId, Long skuId, List<Object> accountList) {
  156. insertAccount(goodsId, skuId, accountList);
  157. }
  158. /**
  159. *
  160. * @param goodsId
  161. * @param skuId
  162. * @param accountList
  163. */
  164. public void insertAccount(Long goodsId, Long skuId, List<Object> accountList) {
  165. if (CollUtil.isEmpty(accountList)) {
  166. throw BusinessRuntimeException.getInstance("文件数据为空");
  167. }
  168. String cache = RedisService.key.IMPORT_ACCOUNT_CACHE_KEY.getNameFormat(goodsId);
  169. if (!redisService.setNx(cache, goodsId, RedisService.key.IMPORT_ACCOUNT_CACHE_KEY.getTimeout())) {
  170. throw BusinessRuntimeException.getInstance("后台程序正在导入账号中...");
  171. }
  172. List<Long> special_email_goods_ids = new ArrayList<>();
  173. SysConfig sysConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  174. .eq(SysConfig::getSysKey, Constant.SPECIAL_GOODS_ACCOUNT_IMPORT_KEY).last("limit 1"));
  175. if (sysConfig != null) {
  176. try {
  177. special_email_goods_ids.addAll(Jsons.parseList(sysConfig.getSysValue(), Long.class));
  178. } catch (Exception e) {
  179. }
  180. }
  181. String regex;
  182. if (goodsId == 29l) {
  183. regex = "\\|";
  184. } else {
  185. regex = "----";
  186. }
  187. try {
  188. accountList.forEach(accountData -> {
  189. Map<String, Object> map = Jsons.toMap(accountData);
  190. String[] part = map.get("0").toString().split(regex);
  191. if (part.length >= 2) {
  192. String account = part[0];
  193. String password = part[1];
  194. //重复不再重新插入
  195. if (accountCommonService.checkIsDupAccount(goodsId, account)) {
  196. return;
  197. }
  198. Account insert = new Account();
  199. insert.setAccount(account);
  200. insert.setPassword(password);
  201. if (part.length > 2 && part.length < 9) {
  202. //google36、辅助邮箱
  203. //46 Google Bard
  204. if (special_email_goods_ids.contains(goodsId)) {
  205. String email = part[2];
  206. insert.setEmail(email);
  207. } else {
  208. String apiKey = part[2];
  209. insert.setApiKey(apiKey);
  210. }
  211. }
  212. insert.setGoodsId(goodsId);
  213. if (goodsId.equals(31l)) {
  214. if (part.length >= 9) {
  215. StringBuilder sb = new StringBuilder();
  216. String country = part[part.length - 1];
  217. if (country.contains("国")) {
  218. sb.append(part[part.length - 1]);
  219. sb.append(Constant.ONE_EMPTY);
  220. sb.append(part[part.length - 2]);
  221. sb.append(Constant.ONE_EMPTY);
  222. }
  223. for (int i = 2; i < part.length; i++) {
  224. if (part[i].contains("朋友")) {
  225. sb.append("朋友");
  226. sb.append(Constant.ONE_EMPTY);
  227. sb.append(part[i + 1]);
  228. i++;
  229. }
  230. if (part[i].contains("工作")) {
  231. sb.append("工作");
  232. sb.append(Constant.ONE_EMPTY);
  233. sb.append(part[i + 1]);
  234. i++;
  235. }
  236. if (part[i].contains("父母")) {
  237. sb.append("父母");
  238. sb.append(Constant.ONE_EMPTY);
  239. sb.append(part[i + 1]);
  240. i++;
  241. }
  242. }
  243. insert.setSecret(sb.toString());
  244. } else {
  245. insert.setSecret(Constant.AMERICA_APPID_SECRET);
  246. }
  247. }
  248. this.save(insert);
  249. //是否存在未发车的车队
  250. GroupsTrips waiting = cmsGroupService.getOne(Wrappers.lambdaQuery(GroupsTrips.class)
  251. .eq(GroupsTrips::getSkuId, skuId)
  252. .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting).last("limit 1"));
  253. if (waiting != null) {
  254. waiting.setAccountId(insert.getId());
  255. try {
  256. cmsGroupService.setAccount(waiting);
  257. } catch (Exception e) {
  258. //自动配置车队
  259. setGroupsTrips(skuId, insert.getId());
  260. }
  261. } else {
  262. //自动配置车队
  263. setGroupsTrips(skuId, insert.getId());
  264. }
  265. }
  266. });
  267. } catch (Exception e) {
  268. log.error(StringUtil.getErrorText(e));
  269. }
  270. redisService.del(cache);
  271. }
  272. public void setGroupsTrips(Long skuId, Long accountId) {
  273. //自动配置车队
  274. GroupsTrips groupsTrips = new GroupsTrips();
  275. groupsTrips.setSkuId(skuId);
  276. groupsTrips.setAccountId(accountId);
  277. cmsGroupService.issuance(groupsTrips, null);
  278. }
  279. }