| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- package com.cyksj.service.mange.account;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.lang.Validator;
- import cn.hutool.core.util.RandomUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.core.toolkit.Assert;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.cyksj.common.constant.Constant;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.cyksj.common.util.Jsons;
- import com.cyksj.common.util.StringUtil;
- import com.cyksj.mapper.AccountBlockedReplaceRecordMapper;
- import com.cyksj.mapper.AccountMapper;
- import com.cyksj.mapper.AccountUpdatePasswordRecordMapper;
- import com.cyksj.mapper.GoodsDonMapper;
- import com.cyksj.mapper.manage.AutoUpdateAccountPwdLogMapper;
- import com.cyksj.mapper.manage.cms.CmsUserMapper;
- import com.cyksj.mapper.sys.SysEmailMapper;
- import com.cyksj.model.entity.*;
- import com.cyksj.model.excel.ExcelAccountData;
- import com.cyksj.model.manage.views.AccountView;
- import com.cyksj.model.views.CmsUserVO;
- import com.cyksj.model.views.ExpiredAccountDataView;
- import com.cyksj.redis.RedisService;
- import com.cyksj.service.mange.AccountCommonService;
- import com.cyksj.service.mange.CmsAccountService;
- import com.cyksj.service.mange.CmsGroupService;
- import com.cyksj.service.sys.SysConfigService;
- import com.ejlchina.searcher.BeanSearcher;
- import com.ejlchina.searcher.param.Operator;
- import com.ejlchina.searcher.util.MapUtils;
- import com.google.common.collect.Lists;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.*;
- /**
- * @author chan
- * @date 2022/9/27 6:23 PM
- */
- @Slf4j
- @Service("cms")
- @RequiredArgsConstructor
- public class CmsAccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements CmsAccountService {
- private final AccountMapper accountMapper;
- private final AccountUpdatePasswordRecordMapper accountUpdatePasswordRecordMapper;
- private final CmsUserMapper cmsUserMapper;
- private final AutoUpdateAccountPwdLogMapper autoUpdateAccountPwdLogMapper;
- private final BeanSearcher beanSearcher;
- private final CmsGroupService cmsGroupService;
- private final RedisService redisService;
- private final AccountCommonService accountCommonService;
- private final SysConfigService sysConfigService;
- private final SysEmailMapper sysEmailMapper;
- private final GoodsDonMapper goodsDonMapper;
- private final AccountBlockedReplaceRecordMapper accountBlockedReplaceRecordMapper;
- @Override
- 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) {
- return accountMapper.getExpiredAccountView(isCorpWx, customerService, goodsId, skuId, account, renewStatus, handleStatus, cnAccount, usAccount, userId, startTime, endTime, now, new Page<>(offset, limit));
- }
- @Override
- 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) {
- return accountMapper.getExpiredAccountViewNoPage(isCorpWx, customerService, goodsId, skuId, account, renewStatus, handleStatus, cnAccount, usAccount, userId, startTime, endTime, now);
- }
- @Override
- public List<OrderDon> getNoAppleOneOrderDon(List<Long> skuIds, Date yesZeroDate, Date thisZeroDate, List<String> noOrderStatus) {
- return accountMapper.getNoAppleOneOrderDon(skuIds, yesZeroDate, thisZeroDate, noOrderStatus);
- }
- @Override
- public List<AccountView> getExpiredAllAccount() {
- return accountMapper.getExpiredAllAccount();
- }
- @Override
- public void saveRecord(Long operateUserId, Long accountId, String oldPassword, String newPassword, String type) {
- AccountUpdatePasswordRecord record = new AccountUpdatePasswordRecord();
- record.setAccountId(accountId);
- record.setOperateUserId(operateUserId);
- Optional.ofNullable(cmsUserMapper.selectById(operateUserId))
- .ifPresent(admin -> record.setOperator(admin.getNickname()));
- record.setOldPassword(oldPassword);
- record.setNewPassword(newPassword);
- record.setType(type);
- accountUpdatePasswordRecordMapper.insert(record);
- }
- @Override
- public void recordAutoUpdatePwd(Long accountId, String account, String oldPwd, String newPwd, String operateIp) {
- AutoUpdateAccountPwdLog log = new AutoUpdateAccountPwdLog();
- log.setAccountId(accountId);
- log.setAccount(account);
- log.setOldPwd(oldPwd);
- log.setNewPwd(newPwd);
- log.setOperateLocation(StringUtil.getRealAddressByIP(operateIp));
- log.setOperateIp(operateIp);
- autoUpdateAccountPwdLogMapper.insert(log);
- }
- @Override
- public void assignDeleteCustomerAccount(Long customerServiceId) {
- List<AccountView> accountViews = beanSearcher.searchAll(AccountView.class, MapUtils.builder()
- .field(AccountView::getCustomerServiceId, customerServiceId)
- .build());
- if (accountViews.size() > 0) {
- List<CmsUserVO> customers = beanSearcher.searchAll(CmsUserVO.class, MapUtils.builder()
- .field(CmsUserVO::getId, customerServiceId).op(Operator.NotEqual)
- .field(CmsUserVO::getRoleNames, "客服").op(Operator.Contain)
- .build());
- if (customers.size() > 0) {
- log.info("删除客服id:{},分配其未修改的过期账号,账号数量:{}", customerServiceId, accountViews.size());
- assignCustomerAccount(customers, accountViews);
- }
- }
- }
- @Override
- public void assignCustomerAccount(List<CmsUserVO> customers, List<AccountView> accountViews) {
- int customerSize = customers.size();
- int accountSize = accountViews.size();
- if (accountSize < customerSize) {
- accountViews.forEach(account -> {
- int randomIndex = RandomUtil.randomInt(customerSize);
- CmsUserVO customer = customers.get(randomIndex);
- accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
- .set(Account::getCustomerServiceId, customer.getId())
- .eq(Account::getId, account.getId()));
- });
- return;
- }
- int num = accountSize / customerSize;
- int remain = accountSize % customerSize;
- List<List<AccountView>> partition = Lists.partition(accountViews, num);
- for (int i = 0; i < customerSize; i++) {
- List<AccountView> expiredAccounts = partition.get(i);
- CmsUserVO customer = customers.get(i);
- expiredAccounts.forEach(ea -> {
- Long accountId = ea.getId();
- accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
- .set(Account::getCustomerServiceId, customer.getId())
- .eq(Account::getId, accountId));
- });
- }
- if (remain > 0) {
- log.info("分配多余的过期账号给客服");
- List<AccountView> remainExpiredAccount = partition.get(customerSize);
- remainExpiredAccount.forEach(remainAccount -> {
- int randomIndex = RandomUtil.randomInt(customerSize);
- log.info("获取的随机下标:{}", randomIndex);
- CmsUserVO customer = customers.get(randomIndex);
- log.info("随机分配的客服是:{}", customer.getNickname());
- Long accountId = remainAccount.getId();
- accountMapper.update(null, Wrappers.lambdaUpdate(Account.class)
- .set(Account::getCustomerServiceId, customer.getId())
- .eq(Account::getId, accountId));
- });
- }
- }
- @Override
- @Transactional(rollbackFor = Throwable.class)
- public void analysisChatGPTAccount(Long goodsId, Long skuId, List<Object> accountList) {
- insertAccount(goodsId, skuId, accountList);
- }
- @Override
- public void analysisAmericaAppidAccount(Long goodsId, Long skuId, List<Object> accountList) {
- insertAccount(goodsId, skuId, accountList);
- }
- @Override
- public void readFileAndImportEmail(MultipartFile file) throws Exception {
- InputStream in = null;
- BufferedReader br = null;
- String regex = "----";
- List<SysEmail> sysEmails = new ArrayList<>();
- List<String> error = new ArrayList<>();
- try {
- in = file.getInputStream();
- br = new BufferedReader(new InputStreamReader(file.getInputStream()));
- String content = null;
- while ((content = br.readLine()) != null) {
- String[] split = content.split(regex);
- if (split.length > 1) {
- String email = split[0];
- String password = split[1];
- if (!StrUtil.hasEmpty(email, password)) {
- SysEmail sysEmail = new SysEmail();
- sysEmail.setEmail(email);
- sysEmail.setPassword(password);
- sysEmails.add(sysEmail);
- }
- }else {
- error.add(split[0]);
- }
- }
- if (CollUtil.isNotEmpty(sysEmails)) {
- Lists.partition(sysEmails, 1000).forEach(data -> {
- sysEmailMapper.batchInsert(data);
- });
- }
- } finally {
- if (br != null) {
- br.close();
- }
- if (in != null) {
- in.close();
- }
- }
- System.out.println(Jsons.toJson(error));
- }
- /**
- *
- * @param goodsId
- * @param skuId
- * @param accountList
- */
- public void insertAccount(Long goodsId, Long skuId, List<Object> accountList) {
- if (CollUtil.isEmpty(accountList)) {
- throw BusinessRuntimeException.getInstance("文件数据为空");
- }
- String cache = RedisService.key.IMPORT_ACCOUNT_CACHE_KEY.getNameFormat(goodsId);
- if (!redisService.setNx(cache, goodsId, RedisService.key.IMPORT_ACCOUNT_CACHE_KEY.getTimeout())) {
- throw BusinessRuntimeException.getInstance("后台程序正在导入账号中...");
- }
- List<Long> special_email_goods_ids = new ArrayList<>();
- SysConfig sysConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
- .eq(SysConfig::getSysKey, Constant.SPECIAL_GOODS_ACCOUNT_IMPORT_KEY).last("limit 1"));
- if (sysConfig != null) {
- try {
- special_email_goods_ids.addAll(Jsons.parseList(sysConfig.getSysValue(), Long.class));
- } catch (Exception e) {
- }
- }
- String regex;
- if (goodsId == 29l) {
- regex = "\\|";
- } else {
- regex = "----";
- }
- try {
- accountList.forEach(accountData -> {
- Map<String, Object> map = Jsons.toMap(accountData);
- Account insert = new Account();
- insert.setGoodsId(goodsId);
- if (map.keySet().size() >= 5) {
- String account = map.get("0").toString();
- String password = map.get("1").toString();
- String friend = map.get("2").toString();
- String work = map.get("3").toString();
- String parents = map.get("4").toString();
- String secret;
- if (map.keySet().size() == 6) {
- String country = map.get("5").toString();
- secret = String.format(Constant.NEW_APPID_SECRET, friend, work, parents, country);
- } else {
- secret = String.format(Constant.NEW_APPID_NO_COUNTRY_SECRET, friend, work, parents);
- }
- insert.setAccount(account);
- insert.setPassword(password);
- insert.setSecret(secret);
- } else {
- String[] part = map.get("0").toString().split(regex);
- if (part.length >= 2) {
- String account = part[0];
- String password = part[1];
- insert.setAccount(account);
- insert.setPassword(password);
- if (part.length > 2 && part.length < 9) {
- //google36、辅助邮箱
- //46 Google Bard
- if (special_email_goods_ids.contains(goodsId)) {
- String email = part[2];
- insert.setEmail(email);
- } else {
- //chatGPT 邮箱密码
- String gptEmailPwd = part[2];
- insert.setGptEmailPwd(gptEmailPwd);
- }
- }
- if (goodsId.equals(31l)) {
- if (part.length >= 9) {
- StringBuilder sb = new StringBuilder();
- String country = part[part.length - 1];
- if (country.contains("国")) {
- sb.append(part[part.length - 1]);
- sb.append(Constant.ONE_EMPTY);
- sb.append(part[part.length - 2]);
- sb.append(Constant.ONE_EMPTY);
- }
- for (int i = 2; i < part.length; i++) {
- if (part[i].contains("朋友")) {
- sb.append("朋友");
- sb.append(Constant.ONE_EMPTY);
- sb.append(part[i + 1]);
- i++;
- }
- if (part[i].contains("工作")) {
- sb.append("工作");
- sb.append(Constant.ONE_EMPTY);
- sb.append(part[i + 1]);
- i++;
- }
- if (part[i].contains("父母")) {
- sb.append("父母");
- sb.append(Constant.ONE_EMPTY);
- sb.append(part[i + 1]);
- i++;
- }
- }
- insert.setSecret(sb.toString());
- } else {
- insert.setSecret(Constant.AMERICA_APPID_SECRET);
- }
- }
- }
- }
- String account = insert.getAccount();
- if (StrUtil.isEmpty(account)) {
- return;
- }
- //重复不再重新插入
- if (accountCommonService.checkIsDupAccount(goodsId, account)) {
- return;
- }
- this.save(insert);
- //是否存在未发车的车队
- GroupsTrips waiting = cmsGroupService.getOne(Wrappers.lambdaQuery(GroupsTrips.class)
- .eq(GroupsTrips::getSkuId, skuId)
- .eq(GroupsTrips::getStatus, GroupsTrips.Status.waiting).last("limit 1"));
- if (waiting != null) {
- waiting.setAccountId(insert.getId());
- try {
- cmsGroupService.setAccount(waiting);
- } catch (Exception e) {
- //自动配置车队
- setGroupsTrips(skuId, insert.getId());
- }
- } else {
- //自动配置车队
- setGroupsTrips(skuId, insert.getId());
- }
- });
- } catch (Exception e) {
- log.error(StringUtil.getErrorText(e));
- }
- redisService.del(cache);
- }
- public void setGroupsTrips(Long skuId, Long accountId) {
- //自动配置车队
- GroupsTrips groupsTrips = new GroupsTrips();
- groupsTrips.setSkuId(skuId);
- groupsTrips.setAccountId(accountId);
- cmsGroupService.issuance(groupsTrips, null);
- }
- @Override
- public void batchImportBlockedAccount(Long goodsId, List<Object> accountBlockedList) {
- GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
- Assert.notNull(goodsDon, "平台不存在");
- List<AccountBlockedReplaceRecord> list = new ArrayList<>();
- accountBlockedList.forEach(accountBlocked->{
- Map<String, Object> map = Jsons.toMap(accountBlocked);
- String blockedAccount = map.get("0").toString();
- if (!Validator.isEmail(blockedAccount)) {
- return;
- }
- AccountBlockedReplaceRecord accountBlockedReplaceRecord = new AccountBlockedReplaceRecord();
- accountBlockedReplaceRecord.setGoodsId(goodsId);
- accountBlockedReplaceRecord.setAccount(blockedAccount);
- list.add(accountBlockedReplaceRecord);
- if (list.size() == 400) {
- accountBlockedReplaceRecordMapper.batchInsert(list);
- list.clear();
- }
- });
- if (list.size() > 0) {
- accountBlockedReplaceRecordMapper.batchInsert(list);
- }
- }
- }
|