CmsAccountServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package com.cyksj.service.mange.account;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.lang.Validator;
  4. import cn.hutool.core.util.RandomUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.baomidou.mybatisplus.core.metadata.IPage;
  7. import com.baomidou.mybatisplus.core.toolkit.Assert;
  8. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  9. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  10. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  11. import com.cyksj.common.constant.Constant;
  12. import com.cyksj.common.exception.BusinessRuntimeException;
  13. import com.cyksj.common.util.Jsons;
  14. import com.cyksj.common.util.StringUtil;
  15. import com.cyksj.mapper.AccountBlockedReplaceRecordMapper;
  16. import com.cyksj.mapper.AccountMapper;
  17. import com.cyksj.mapper.AccountUpdatePasswordRecordMapper;
  18. import com.cyksj.mapper.GoodsDonMapper;
  19. import com.cyksj.mapper.manage.AutoUpdateAccountPwdLogMapper;
  20. import com.cyksj.mapper.manage.cms.CmsUserMapper;
  21. import com.cyksj.mapper.sys.SysEmailMapper;
  22. import com.cyksj.model.entity.*;
  23. import com.cyksj.model.excel.ExcelAccountData;
  24. import com.cyksj.model.manage.views.AccountView;
  25. import com.cyksj.model.views.CmsUserVO;
  26. import com.cyksj.model.views.ExpiredAccountDataView;
  27. import com.cyksj.redis.RedisService;
  28. import com.cyksj.service.mange.AccountCommonService;
  29. import com.cyksj.service.mange.CmsAccountService;
  30. import com.cyksj.service.mange.CmsGroupService;
  31. import com.cyksj.service.sys.SysConfigService;
  32. import com.ejlchina.searcher.BeanSearcher;
  33. import com.ejlchina.searcher.param.Operator;
  34. import com.ejlchina.searcher.util.MapUtils;
  35. import com.google.common.collect.Lists;
  36. import lombok.RequiredArgsConstructor;
  37. import lombok.extern.slf4j.Slf4j;
  38. import org.springframework.stereotype.Service;
  39. import org.springframework.transaction.annotation.Transactional;
  40. import org.springframework.web.multipart.MultipartFile;
  41. import java.io.BufferedReader;
  42. import java.io.InputStream;
  43. import java.io.InputStreamReader;
  44. import java.util.*;
  45. /**
  46. * @author chan
  47. * @date 2022/9/27 6:23 PM
  48. */
  49. @Slf4j
  50. @Service("cms")
  51. @RequiredArgsConstructor
  52. public class CmsAccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements CmsAccountService {
  53. private final AccountMapper accountMapper;
  54. private final AccountUpdatePasswordRecordMapper accountUpdatePasswordRecordMapper;
  55. private final CmsUserMapper cmsUserMapper;
  56. private final AutoUpdateAccountPwdLogMapper autoUpdateAccountPwdLogMapper;
  57. private final BeanSearcher beanSearcher;
  58. private final CmsGroupService cmsGroupService;
  59. private final RedisService redisService;
  60. private final AccountCommonService accountCommonService;
  61. private final SysConfigService sysConfigService;
  62. private final SysEmailMapper sysEmailMapper;
  63. private final GoodsDonMapper goodsDonMapper;
  64. private final AccountBlockedReplaceRecordMapper accountBlockedReplaceRecordMapper;
  65. @Override
  66. 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) {
  67. return accountMapper.getExpiredAccountView(isCorpWx, customerService, goodsId, skuId, account, renewStatus, handleStatus, cnAccount, usAccount, userId, startTime, endTime, now, new Page<>(offset, limit));
  68. }
  69. @Override
  70. 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) {
  71. return accountMapper.getExpiredAccountViewNoPage(isCorpWx, customerService, goodsId, skuId, account, renewStatus, handleStatus, cnAccount, usAccount, userId, startTime, endTime, now);
  72. }
  73. @Override
  74. public List<OrderDon> getNoAppleOneOrderDon(List<Long> skuIds, Date yesZeroDate, Date thisZeroDate, List<String> noOrderStatus) {
  75. return accountMapper.getNoAppleOneOrderDon(skuIds, yesZeroDate, thisZeroDate, noOrderStatus);
  76. }
  77. @Override
  78. public List<AccountView> getExpiredAllAccount() {
  79. return accountMapper.getExpiredAllAccount();
  80. }
  81. @Override
  82. public void saveRecord(Long operateUserId, Long accountId, String oldPassword, String newPassword, String type) {
  83. AccountUpdatePasswordRecord record = new AccountUpdatePasswordRecord();
  84. record.setAccountId(accountId);
  85. record.setOperateUserId(operateUserId);
  86. Optional.ofNullable(cmsUserMapper.selectById(operateUserId))
  87. .ifPresent(admin -> record.setOperator(admin.getNickname()));
  88. record.setOldPassword(oldPassword);
  89. record.setNewPassword(newPassword);
  90. record.setType(type);
  91. accountUpdatePasswordRecordMapper.insert(record);
  92. }
  93. @Override
  94. public void recordAutoUpdatePwd(Long accountId, String account, String oldPwd, String newPwd, String operateIp) {
  95. AutoUpdateAccountPwdLog log = new AutoUpdateAccountPwdLog();
  96. log.setAccountId(accountId);
  97. log.setAccount(account);
  98. log.setOldPwd(oldPwd);
  99. log.setNewPwd(newPwd);
  100. log.setOperateLocation(StringUtil.getRealAddressByIP(operateIp));
  101. log.setOperateIp(operateIp);
  102. autoUpdateAccountPwdLogMapper.insert(log);
  103. }
  104. @Override
  105. public void assignDeleteCustomerAccount(Long customerServiceId) {
  106. List<AccountView> accountViews = beanSearcher.searchAll(AccountView.class, MapUtils.builder()
  107. .field(AccountView::getCustomerServiceId, customerServiceId)
  108. .build());
  109. if (accountViews.size() > 0) {
  110. List<CmsUserVO> customers = beanSearcher.searchAll(CmsUserVO.class, MapUtils.builder()
  111. .field(CmsUserVO::getId, customerServiceId).op(Operator.NotEqual)
  112. .field(CmsUserVO::getRoleNames, "客服").op(Operator.Contain)
  113. .build());
  114. if (customers.size() > 0) {
  115. log.info("删除客服id:{},分配其未修改的过期账号,账号数量:{}", customerServiceId, accountViews.size());
  116. assignCustomerAccount(customers, accountViews);
  117. }
  118. }
  119. }
  120. @Override
  121. public void assignCustomerAccount(List<CmsUserVO> customers, List<AccountView> accountViews) {
  122. int customerSize = customers.size();
  123. int accountSize = accountViews.size();
  124. if (accountSize < customerSize) {
  125. accountViews.forEach(account -> {
  126. int randomIndex = RandomUtil.randomInt(customerSize);
  127. CmsUserVO customer = customers.get(randomIndex);
  128. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  129. .set(Account::getCustomerServiceId, customer.getId())
  130. .eq(Account::getId, account.getId()));
  131. });
  132. return;
  133. }
  134. int num = accountSize / customerSize;
  135. int remain = accountSize % customerSize;
  136. List<List<AccountView>> partition = Lists.partition(accountViews, num);
  137. for (int i = 0; i < customerSize; i++) {
  138. List<AccountView> expiredAccounts = partition.get(i);
  139. CmsUserVO customer = customers.get(i);
  140. expiredAccounts.forEach(ea -> {
  141. Long accountId = ea.getId();
  142. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  143. .set(Account::getCustomerServiceId, customer.getId())
  144. .eq(Account::getId, accountId));
  145. });
  146. }
  147. if (remain > 0) {
  148. log.info("分配多余的过期账号给客服");
  149. List<AccountView> remainExpiredAccount = partition.get(customerSize);
  150. remainExpiredAccount.forEach(remainAccount -> {
  151. int randomIndex = RandomUtil.randomInt(customerSize);
  152. log.info("获取的随机下标:{}", randomIndex);
  153. CmsUserVO customer = customers.get(randomIndex);
  154. log.info("随机分配的客服是:{}", customer.getNickname());
  155. Long accountId = remainAccount.getId();
  156. accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
  157. .set(Account::getCustomerServiceId, customer.getId())
  158. .eq(Account::getId, accountId));
  159. });
  160. }
  161. }
  162. @Override
  163. @Transactional(rollbackFor = Throwable.class)
  164. public void analysisChatGPTAccount(Long goodsId, Long skuId, List<Object> accountList) {
  165. insertAccount(goodsId, skuId, accountList);
  166. }
  167. @Override
  168. public void analysisAmericaAppidAccount(Long goodsId, Long skuId, List<Object> accountList) {
  169. insertAccount(goodsId, skuId, accountList);
  170. }
  171. @Override
  172. public void readFileAndImportEmail(MultipartFile file) throws Exception {
  173. InputStream in = null;
  174. BufferedReader br = null;
  175. String regex = "----";
  176. List<SysEmail> sysEmails = new ArrayList<>();
  177. List<String> error = new ArrayList<>();
  178. try {
  179. in = file.getInputStream();
  180. br = new BufferedReader(new InputStreamReader(file.getInputStream()));
  181. String content = null;
  182. while ((content = br.readLine()) != null) {
  183. String[] split = content.split(regex);
  184. if (split.length > 1) {
  185. String email = split[0];
  186. String password = split[1];
  187. if (!StrUtil.hasEmpty(email, password)) {
  188. SysEmail sysEmail = new SysEmail();
  189. sysEmail.setEmail(email);
  190. sysEmail.setPassword(password);
  191. sysEmails.add(sysEmail);
  192. }
  193. }else {
  194. error.add(split[0]);
  195. }
  196. }
  197. if (CollUtil.isNotEmpty(sysEmails)) {
  198. Lists.partition(sysEmails, 1000).forEach(data -> {
  199. sysEmailMapper.batchInsert(data);
  200. });
  201. }
  202. } finally {
  203. if (br != null) {
  204. br.close();
  205. }
  206. if (in != null) {
  207. in.close();
  208. }
  209. }
  210. System.out.println(Jsons.toJson(error));
  211. }
  212. /**
  213. *
  214. * @param goodsId
  215. * @param skuId
  216. * @param accountList
  217. */
  218. public void insertAccount(Long goodsId, Long skuId, List<Object> accountList) {
  219. if (CollUtil.isEmpty(accountList)) {
  220. throw BusinessRuntimeException.getInstance("文件数据为空");
  221. }
  222. String cache = RedisService.key.IMPORT_ACCOUNT_CACHE_KEY.getNameFormat(goodsId);
  223. if (!redisService.setNx(cache, goodsId, RedisService.key.IMPORT_ACCOUNT_CACHE_KEY.getTimeout())) {
  224. throw BusinessRuntimeException.getInstance("后台程序正在导入账号中...");
  225. }
  226. List<Long> special_email_goods_ids = new ArrayList<>();
  227. SysConfig sysConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  228. .eq(SysConfig::getSysKey, Constant.SPECIAL_GOODS_ACCOUNT_IMPORT_KEY).last("limit 1"));
  229. if (sysConfig != null) {
  230. try {
  231. special_email_goods_ids.addAll(Jsons.parseList(sysConfig.getSysValue(), Long.class));
  232. } catch (Exception e) {
  233. }
  234. }
  235. String regex;
  236. if (goodsId == 29l) {
  237. regex = "\\|";
  238. } else {
  239. regex = "----";
  240. }
  241. try {
  242. accountList.forEach(accountData -> {
  243. Map<String, Object> map = Jsons.toMap(accountData);
  244. Account insert = new Account();
  245. insert.setGoodsId(goodsId);
  246. if (map.keySet().size() >= 5) {
  247. String account = map.get("0").toString();
  248. String password = map.get("1").toString();
  249. String friend = map.get("2").toString();
  250. String work = map.get("3").toString();
  251. String parents = map.get("4").toString();
  252. String secret;
  253. if (map.keySet().size() == 6) {
  254. String country = map.get("5").toString();
  255. secret = String.format(Constant.NEW_APPID_SECRET, friend, work, parents, country);
  256. } else {
  257. secret = String.format(Constant.NEW_APPID_NO_COUNTRY_SECRET, friend, work, parents);
  258. }
  259. insert.setAccount(account);
  260. insert.setPassword(password);
  261. insert.setSecret(secret);
  262. } else {
  263. String[] part = map.get("0").toString().split(regex);
  264. if (part.length >= 2) {
  265. String account = part[0];
  266. String password = part[1];
  267. insert.setAccount(account);
  268. insert.setPassword(password);
  269. if (part.length > 2 && part.length < 9) {
  270. //google36、辅助邮箱
  271. //46 Google Bard
  272. if (special_email_goods_ids.contains(goodsId)) {
  273. String email = part[2];
  274. insert.setEmail(email);
  275. } else {
  276. //chatGPT 邮箱密码
  277. String gptEmailPwd = part[2];
  278. insert.setGptEmailPwd(gptEmailPwd);
  279. }
  280. }
  281. if (goodsId.equals(31l)) {
  282. if (part.length >= 9) {
  283. StringBuilder sb = new StringBuilder();
  284. String country = part[part.length - 1];
  285. if (country.contains("国")) {
  286. sb.append(part[part.length - 1]);
  287. sb.append(Constant.ONE_EMPTY);
  288. sb.append(part[part.length - 2]);
  289. sb.append(Constant.ONE_EMPTY);
  290. }
  291. for (int i = 2; i < part.length; i++) {
  292. if (part[i].contains("朋友")) {
  293. sb.append("朋友");
  294. sb.append(Constant.ONE_EMPTY);
  295. sb.append(part[i + 1]);
  296. i++;
  297. }
  298. if (part[i].contains("工作")) {
  299. sb.append("工作");
  300. sb.append(Constant.ONE_EMPTY);
  301. sb.append(part[i + 1]);
  302. i++;
  303. }
  304. if (part[i].contains("父母")) {
  305. sb.append("父母");
  306. sb.append(Constant.ONE_EMPTY);
  307. sb.append(part[i + 1]);
  308. i++;
  309. }
  310. }
  311. insert.setSecret(sb.toString());
  312. } else {
  313. insert.setSecret(Constant.AMERICA_APPID_SECRET);
  314. }
  315. }
  316. }
  317. }
  318. String account = insert.getAccount();
  319. if (StrUtil.isEmpty(account)) {
  320. return;
  321. }
  322. //重复不再重新插入
  323. if (accountCommonService.checkIsDupAccount(goodsId, account)) {
  324. return;
  325. }
  326. this.save(insert);
  327. //是否存在未发车的车队
  328. GroupsTrips waiting = cmsGroupService.getOne(Wrappers.lambdaQuery(GroupsTrips.class)
  329. .eq(GroupsTrips::getSkuId, skuId)
  330. .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting).last("limit 1"));
  331. if (waiting != null) {
  332. waiting.setAccountId(insert.getId());
  333. try {
  334. cmsGroupService.setAccount(waiting);
  335. } catch (Exception e) {
  336. //自动配置车队
  337. setGroupsTrips(skuId, insert.getId());
  338. }
  339. } else {
  340. //自动配置车队
  341. setGroupsTrips(skuId, insert.getId());
  342. }
  343. });
  344. } catch (Exception e) {
  345. log.error(StringUtil.getErrorText(e));
  346. }
  347. redisService.del(cache);
  348. }
  349. public void setGroupsTrips(Long skuId, Long accountId) {
  350. //自动配置车队
  351. GroupsTrips groupsTrips = new GroupsTrips();
  352. groupsTrips.setSkuId(skuId);
  353. groupsTrips.setAccountId(accountId);
  354. cmsGroupService.issuance(groupsTrips, null);
  355. }
  356. @Override
  357. public void batchImportBlockedAccount(Long goodsId, List<Object> accountBlockedList) {
  358. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  359. Assert.notNull(goodsDon, "平台不存在");
  360. List<AccountBlockedReplaceRecord> list = new ArrayList<>();
  361. accountBlockedList.forEach(accountBlocked->{
  362. Map<String, Object> map = Jsons.toMap(accountBlocked);
  363. String blockedAccount = map.get("0").toString();
  364. if (!Validator.isEmail(blockedAccount)) {
  365. return;
  366. }
  367. AccountBlockedReplaceRecord accountBlockedReplaceRecord = new AccountBlockedReplaceRecord();
  368. accountBlockedReplaceRecord.setGoodsId(goodsId);
  369. accountBlockedReplaceRecord.setAccount(blockedAccount);
  370. list.add(accountBlockedReplaceRecord);
  371. if (list.size() == 400) {
  372. accountBlockedReplaceRecordMapper.batchInsert(list);
  373. list.clear();
  374. }
  375. });
  376. if (list.size() > 0) {
  377. accountBlockedReplaceRecordMapper.batchInsert(list);
  378. }
  379. }
  380. }