| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924 |
- package com.cyksj.service.relation.impl;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.collection.CollectionUtil;
- import cn.hutool.core.date.BetweenFormater;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.lang.Assert;
- import cn.hutool.core.lang.Validator;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.cyksj.common.constant.Constant;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.cyksj.common.snowflake.Sequence;
- import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
- import com.cyksj.common.util.Jsons;
- import com.cyksj.common.util.StringUtil;
- import com.cyksj.enums.GatewayApiCode;
- import com.cyksj.mapper.*;
- import com.cyksj.mapper.manage.distribute.DistributeGeneralSkuRateMapper;
- import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
- import com.cyksj.mapper.manage.distribute.UserPlatDistributeRateMapper;
- import com.cyksj.mapper.manage.distribute.UserPlatSkuDistributeRateMapper;
- import com.cyksj.mapper.mirrorshop.UserMirrorShopMapper;
- import com.cyksj.mapper.renew.RenewUpgradePackageMapper;
- import com.cyksj.mapper.sys.SysConfigMapper;
- import com.cyksj.mapper.sys.SysEmailMapper;
- import com.cyksj.mapper.vip.VipGoodsSkuMapper;
- import com.cyksj.mapper.vip.VipUserMapper;
- import com.cyksj.model.dto.ErrorNetflixChangeOtherSkuTicketDto;
- import com.cyksj.model.dto.TicketChangeDto;
- import com.cyksj.model.entity.*;
- import com.cyksj.model.manage.views.AccountView;
- import com.cyksj.model.manage.views.GroupsRelationView;
- import com.cyksj.model.request.ErrorNetflixChangeReq;
- import com.cyksj.model.request.OrderRefundReq;
- import com.cyksj.model.request.SpecialGoodsTicketReq;
- import com.cyksj.model.request.TicketLoginReq;
- import com.cyksj.model.response.NetflixErrorAccountStatus;
- import com.cyksj.model.views.*;
- import com.cyksj.redis.RedisService;
- import com.cyksj.service.chatgpt.ChatGptAccountService;
- import com.cyksj.service.chatgpt.ChatGptAuthService;
- import com.cyksj.service.claude.ClaudeService;
- import com.cyksj.service.groups.GroupsFuncService;
- import com.cyksj.service.luma.LumaService;
- import com.cyksj.service.mail.CommonMailService;
- import com.cyksj.service.mail.MailService;
- import com.cyksj.service.mange.coupon.CouponCommonService;
- import com.cyksj.service.midjourney.MidjourneyAccountService;
- import com.cyksj.service.order.OrderRefundService;
- import com.cyksj.service.relation.GroupRelationClearService;
- import com.cyksj.service.relation.GroupRelationFrontService;
- import com.cyksj.service.relation.GroupsRelationChangeService;
- import com.cyksj.service.relation.GroupsRelationNetflixService;
- import com.cyksj.service.user.UserBindRelationService;
- import com.cyksj.service.user.UserLoginRecordService;
- import com.ejlchina.searcher.BeanSearcher;
- import com.ejlchina.searcher.SearchResult;
- import com.ejlchina.searcher.param.Operator;
- import com.ejlchina.searcher.util.MapBuilder;
- import com.ejlchina.searcher.util.MapUtils;
- import com.github.rholder.retry.*;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.dao.DuplicateKeyException;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.servlet.http.HttpServletRequest;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.*;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.stream.Collectors;
- import static com.cyksj.enums.GatewayApiCode.GROUP_INDEX_ALREADY_BE_USER;
- import static com.cyksj.enums.GatewayApiCode.GROUP_INDEX_NOT_FIND;
- /*
- *项目名: netflix
- *文件名: GroupRelationFrontServiceImpl
- *创建者: JavaZou
- *创建时间:2022/12/29 15:42
- */
- @Service
- @RequiredArgsConstructor
- @Slf4j
- public class GroupRelationFrontServiceImpl implements GroupRelationFrontService {
- private final UserBindRelationService userBindRelationService;
- private final UserMapper userMapper;
- private final BeanSearcher beanSearcher;
- private final CouponCommonService couponCommonService;
- private final GroupsRelationMapper groupsRelationMapper;
- private final GroupsMapper groupsMapper;
- private final ChatGptAuthService chatGptAuthService;
- private final RedisService redisService;
- private final MailService mailService;
- private final OrderDonMapper orderDonMapper;
- private final GroupsRelationExpiryNotifyRecordMapper groupsRelationExpiryNotifyRecordMapper;
- private final HttpServletRequest request;
- private final GroupsFuncService groupsFuncService;
- private final OrderDiscountPlusPurchaseSkuRelationMapper orderDiscountPlusPurchaseSkuRelationMapper;
- private final GroupsRelationLoginCodeRecordMapper groupsRelationLoginCodeRecordMapper;
- private final SysEmailMapper sysEmailMapper;
- private final GoodsDonSkuMapper goodsDonSkuMapper;
- private final UserFuncPropertyMapper userFuncPropertyMapper;
- private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
- private final UserLoginRecordService userLoginRecordService;
- private final UserCertRecordMapper userCertRecordMapper;
- private final MidjourneyAccountService midjourneyAccountService;
- private final ChatGptAccountService chatGptAccountService;
- private final UpgradePackageMapper upgradePackageMapper;
- private final LumaService lumaService;
- private final GroupsRelationNetflixService groupsRelationNetflixService;
- private final UserDistributeMapper userDistributeMapper;
- private final UserPlatDistributeRateMapper userPlatDistributeRateMapper;
- private final GoodsDonMapper goodsDonMapper;
- private final UserPlatSkuDistributeRateMapper userPlatSkuDistributeRateMapper;
- private final DistributeGeneralSkuRateMapper distributeGeneralSkuRateMapper;
- private final RenewUpgradePackageMapper renewUpgradePackageMapper;
- private final ClaudeService claudeService;
- private final UserDistributePosterRecordMapper userDistributePosterRecordMapper;
- private final OrderDonRenewRecordMapper orderDonRenewRecordMapper;
- private final AccountMapper accountMapper;
- private final UserBenefitsMapper userBenefitsMapper;
- private final GroupsRelationChangeService changeTickerRelation;
- private final NetflixTempAccountUserMapper netflixTempAccountUserMapper;
- private final NetflixUserAccountStatusRecordMapper netflixUserAccountStatusRecordMapper;
- private final AccountNetflixStateMapper accountNetflixStateMapper;
- private final UserMirrorShopMapper userMirrorShopMapper;
- private final CommonMailService commonMailService;
- private final SysConfigMapper sysConfigMapper;
- private final GptMirrorDayCardGiveawayActivityMapper gptMirrorDayCardGiveawayActivityMapper;
- private final MirrorTicketPopupsRecordMapper mirrorTicketPopupsRecordMapper;
- private final VipUserMapper vipUserMapper;
- private final VipGoodsSkuMapper vipGoodsSkuMapper;
- private static final Sequence SEQUENCE = new Sequence(0);
- private final NetflixUserChangeOtherTicketRecordMapper netflixUserChangeOtherTicketRecordMapper;
- private final NetflixUserAccountSubmitRecoverRecordMapper netflixUserAccountSubmitRecoverRecordMapper;
- private final OrderRefundService orderRefundService;
- private final GroupRelationClearService clearService;
- @Override
- public SearchResult<RenewalView> getMyTicket(long userId, Boolean isLoginPopularize, String customId, String account, String orderNo, Integer cmt) {
- User u = userMapper.selectById(userId);
- if (u == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
- if (u.getIsBlack()) {
- return new SearchResult<>();
- }
- List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
- MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
- if (cmt != null && cmt == 2 && StrUtil.isNotBlank(customId)) {
- UserMirrorShop userMirrorShop = userMirrorShopMapper.selectOne(Wrappers.lambdaQuery(UserMirrorShop.class)
- .eq(UserMirrorShop::getCustomId, customId)
- .last("limit 1"));
- if (userMirrorShop != null) {
- builder.field(RenewalView::getCmt, cmt).field(RenewalView::getYhsId, userMirrorShop.getId());
- }
- }
- if (cmt == null || cmt != 2) {
- builder.field(RenewalView::getCmt, 2).op(Operator.NotEqual);
- }
- // Long yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
- DateTime now = DateTime.now();
- if (StrUtil.isNotBlank(account)) {
- builder.field(RenewalView::getAccount, account).op(Operator.Contain);
- }
- String conditionSql = String.format("(gr.expiry_time is null or gr.expiry_time > '%s'", now);
- //镜像车票 过期7天后清除
- //个人店铺和AI店铺 暂不支持过期续费
- if (cmt == null && StrUtil.isEmpty(customId)) {
- conditionSql += String.format(" or (sku.is_mirror and gr.expiry_time > '%s')", DateUtil.offsetDay(now, -7));
- }
- conditionSql += ")";
- if (StrUtil.isNotEmpty(orderNo)) {
- conditionSql += String.format(" and exists(select 1 from order_don o where o.relation_id = gr.id and o.user_id = gr.user_id and o.order_no = %s and o.status not in ('close','noPayment','refund'))", orderNo);
- }
- SearchResult<RenewalView> list = beanSearcher.search(RenewalView.class, builder
- .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
- .field(RenewalView::getUserId, userIds).op(Operator.InList)
- .put("condition", conditionSql)
- .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
- .orderBy(RenewalView::getExpiryTime).asc()
- .build());
- if (list.getDataList().size() > 0) {
- //过滤邀请制、已失效的车位
- Integer size = list.getTotalCount().intValue();
- //赠送GPT日卡 参与平台ids
- List<Long> renewReduceSkuIds = userBenefitsMapper.selectList(Wrappers.lambdaQuery(UserBenefits.class)
- .eq(UserBenefits::getType, UserBenefits.Type.RENEW).eq(UserBenefits::getStatus, 1).select(UserBenefits::getSkuId)).stream().map(UserBenefits::getSkuId).collect(Collectors.toList());
- List<Long> gptBuyGids = getGptMirrorGiveawayBuyGids();
- //gpt日卡活动
- GptMirrorDayCardGiveawayActivity gptMirrorDayCardGiveawayActivity = gptMirrorDayCardGiveawayActivityMapper.selectOne(Wrappers.lambdaQuery(GptMirrorDayCardGiveawayActivity.class)
- .last("limit 1"));
- SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
- .eq(SysConfig::getSysKey, Constant.EMAIL_PWD_SHOW_GID_KEY)
- .last("limit 1"));
- List<Long> showEmailPwdGids = new ArrayList<>();
- if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
- try {
- showEmailPwdGids.addAll(Jsons.parseList(sysConfig.getSysValue(), Long.class));
- } catch (Exception e) {
- }
- }
- List<Long> vipSkuIds = vipGoodsSkuMapper.selectList(null).stream().map(VipGoodsSku::getSkuId).collect(Collectors.toList());
- Integer vipUserCount = vipUserMapper.selectCount(Wrappers.lambdaQuery(VipUser.class)
- .in(VipUser::getUserId, userIds)
- .gt(VipUser::getExpiryTime, now));
- list.getDataList().forEach(ticket -> {
- if (ticket.getSkuId() == null || ticket.getGoodsId() == null) {
- return;
- }
- if (ticket.getGoodsId() == 18 && ticket.getSkuNum() > 1) {
- ticket.setPassword(null);
- }
- ticket.setIsMulti(false);
- if (ticket.getSkuNum() > 1) {
- ticket.setIsMulti(true);
- }
- //chatGPT直连
- if (ticket.getGoodsId().equals(42l)) {
- if (StrUtil.isNotBlank(ticket.getAccount())) {
- String accessToken;
- try {
- accessToken = chatGptAuthService.getAccessToken(ticket.getAccount());
- ticket.setGptRefreshToken(accessToken);
- } catch (Exception e) {
- redisService.set(RedisService.key.CHAT_GPT_TOKEN_EXCEPTION_KEY.getName() + ticket.getAccount(), ticket.getAccount());
- }
- }
- ticket.setAccount(null);
- ticket.setPassword(null);
- } else {
- ticket.setGptRefreshToken(null);
- }
- if (ticket.getSecondType() == 1) {
- ticket.setAccount(null);
- ticket.setPassword(null);
- }
- //gpt 2月的独立账号可看到邮箱密码
- if (ticket.getGoodsId() == 18l && ticket.getSkuNum() == 1 && ticket.getAccount() != null && ticket.getCreatedTime().compareTo(DateUtil.parse("2025-02-01")) > 0) {
- } else {
- if (!showEmailPwdGids.contains(ticket.getGoodsId())) {
- ticket.setGptEmailPwd(null);
- }
- }
- if (isLoginPopularize != null && isLoginPopularize) {
- GoodsDonViews views = beanSearcher.searchFirst(GoodsDonViews.class, MapUtils.builder().field(GoodsDonViews::getId, ticket.getGoodsId()).build());
- views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
- views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, views.getId()).field(GoodsDonSkuFrontView::getStatus, true).build()));
- ticket.setGoodsDonViews(views);
- }
- if (ticket.getMonths() != null && ticket.getMonths() >= 300) {
- ticket.setExpiryTime(null);
- }
- if (ticket.getExpiryTime() != null && ticket.getGoodsType() == 1) {
- String remainDays = DateUtil.formatBetween(now, ticket.getExpiryTime(), BetweenFormater.Level.DAY);
- if ("0天".equals(remainDays)) {
- remainDays = "小于1天";
- } else {
- //当天也算
- int days = Integer.parseInt(remainDays.substring(0, remainDays.indexOf("天")));
- remainDays = days + 1 + "天";
- }
- ticket.setRemainDays(remainDays);
- ticket.setTotal(DateUtil.betweenDay(ticket.getStartTime(), ticket.getExpiryTime(), false) + 1);
- }
- OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
- .eq(OrderDon::getRelationId, ticket.getRelationId())
- .eq(OrderDon::getUserId, ticket.getUserId())
- .eq(OrderDon::getOrderType, 1)
- .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
- .orderByDesc(OrderDon::getId)
- .last("limit 1"));
- if (orderDon != null) {
- ticket.setOrderNo(orderDon.getOrderNo());
- ticket.setOrderId(orderDon.getId());
- ticket.setTransactionId(orderDon.getTransactionId());
- if (ticket.getSecondType() == 1) {
- //是否完成邀请 根据订单状态
- ticket.setIsComplete(orderDon.getStatus() == OrderDon.Status.complete ? true : false);
- }
- }
- if (size > 1 && ticket.getGoodsId() != 18) {
- Date payTime = null;
- if (orderDon != null) {
- payTime = Optional.ofNullable(orderDon.getPayTime()).orElse(orderDon.getCreatedTime());
- }
- //加购订单
- if (orderDon == null) {
- List<OrderDiscountPlusPurchaseSkuRelation> plusPurchaseSkuRelations = orderDiscountPlusPurchaseSkuRelationMapper.selectPlusRelationByIdAndUserId(ticket.getRelationId(), ticket.getUserId());
- for (OrderDiscountPlusPurchaseSkuRelation data : plusPurchaseSkuRelations) {
- String relationIds = data.getRelationIds();
- if (StrUtil.isEmpty(relationIds)) {
- return;
- }
- try {
- List<Long> relationList = Jsons.parseList(relationIds, Long.class);
- if (relationList.contains(ticket.getRelationId())) {
- OrderDon plusOrder = orderDonMapper.selectById(data.getOrderId());
- if (plusOrder != null) {
- payTime = Optional.ofNullable(plusOrder.getPayTime()).orElse(plusOrder.getCreatedTime());
- }
- break;
- }
- } catch (Exception e) {
- }
- }
- }
- ticket.setPayTime(payTime);
- }
- if (ticket.getSkuNum() != null && ticket.getSkuNum() == 1) {
- }
- //奈飞、gpt
- if (ticket.getGoodsId() == 1 || ticket.getGoodsId() == 18 || ticket.getGoodsId() == 33l) {
- ticket.setEmail(null);
- }
- //设置镜像车票信息
- setMirrorTicketInfo(ticket);
- // List<GroupsRelationUserView> relationUserViews = groupsRelationMapper.getUserRelationUserViews(ticket.getGroupId(), ticket.getRelationId(), ticket.getUserId(), List.of(GroupsRelation.Status.validity, GroupsRelation.Status.outside));
- // ticket.setRelationViews(relationUserViews);
- if (ticket.getExpiryTime() != null) {
- ticket.setExpiryTimeStr(DateUtil.format(ticket.getExpiryTime(), "yyyy-MM-dd"));
- }
- //查看双重验证码
- if (StrUtil.isNotEmpty(ticket.getApiSecret())) {
- Boolean isHasVerifyCode = StrUtil.isNotBlank(ticket.getVerifyCode()) ? true : false;
- Integer codeNum = getUserCodeNumBySkuId(ticket.getUserId(), ticket.getSkuId(), isHasVerifyCode);
- ticket.setCodeNum(codeNum);
- }
- Long qaId = groupsRelationMapper.selectGoodsDonQaByGoodsId(ticket.getGoodsId());
- ticket.setQa(qaId > 0 ? true : false);
- //是否存在海报
- ticket.setPosterInfo(setGoodsPoster(ticket, u, userIds));
- //续费升级折扣
- setRenewDiscount(ticket);
- if (CollUtil.isNotEmpty(renewReduceSkuIds) && renewReduceSkuIds.contains(ticket.getSkuId())) {
- ticket.setIsRenewReduce(true);
- }
- if (CollUtil.isNotEmpty(gptBuyGids) && gptBuyGids.contains(ticket.getGoodsId())) {
- if (gptMirrorDayCardGiveawayActivity != null && orderDon != null && orderDon.getCreatedTime().compareTo(gptMirrorDayCardGiveawayActivity.getCreatedTime()) > 0) {
- //是否领取过GPT日卡
- Long orderId = orderDon.getId();
- Number count = beanSearcher.searchCount(GptMirrorDayCardActivityRecord.class, MapUtils.builder().field(GptMirrorDayCardActivityRecord::getOrderId, orderId).build());
- ticket.setReceivedGptGift(count.intValue() > 0);
- }
- }
- markVipDeductTicket(userIds, ticket, vipSkuIds, vipUserCount > 0);
- });
- }
- userLoginRecordService.saveUserLoginDayRecord(userId);
- return list;
- }
- @Override
- public Boolean isJoinCorpWx(Long userId, Boolean isJoin, Boolean noCoupon) {
- if (!isJoin) {
- User user = userMapper.selectById(userId);
- if (user == null || StrUtil.isEmpty(user.getUnionid())) {
- return true;
- }
- // CorpUser corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
- // .ne(CorpUser::getUnionid, "")
- // .eq(CorpUser::getUnionid, user.getUnionid())
- // .last("limit 1")
- // .select(CorpUser::getId)
- // );
- CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder()
- .field(CorpUserFollowView::getUnionId, user.getUnionid())
- .field(CorpUserFollowView::getStatus, 1).build());
- if (corpUserFollowView == null) {
- return false;
- }
- }
- //用户是否买了奈飞车票
- Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
- .field(RenewalView::getUserId, userId)
- .field(RenewalView::getGoodsId, 1)
- .field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
- .build());
- if (number.intValue() > 0 && !noCoupon) {
- couponCommonService.sendCouponToUser(userId, 34l, 0l, 0l, 0l, CouponUser.Channel.wxCorp);
- }
- return true;
- }
- @Override
- public GroupsRelation getSimilarExpiredGroupRelation(GoodsDonSku sku, List<Long> errorsRelationIds, String ip) {
- Long skuId = sku.getId();
- Integer days = sku.getDays();
- Integer months = sku.getMonths();
- Integer maxNum = sku.getNum();
- DateTime expiryTime = null;
- if (days != null && days > 0) {
- expiryTime = DateUtil.offsetDay(DateTime.now(), days);
- } else {
- expiryTime = DateUtil.offsetMonth(DateTime.now(), months);
- }
- Integer extraNum = 5;
- if (sku.getId() == 178l) {
- extraNum = 2;
- }
- maxNum += extraNum;
- DateTime minExpiryTime = DateUtil.offsetDay(expiryTime, -5);
- Long groupsId = groupsRelationMapper.selectSimilarExpiredRelation(skuId, minExpiryTime, maxNum, ip);
- if (groupsId == null) {
- log.info("暂无相似过期时间车队");
- return null;
- }
- GroupsRelation relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
- .notIn(errorsRelationIds != null && errorsRelationIds.size() > 0, GroupsRelation::getId, errorsRelationIds)
- .eq(GroupsRelation::getGroupsId, groupsId)
- .eq(GroupsRelation::getStatus, GroupsRelation.Status.none)
- .eq(GroupsRelation::getUserId, 0)
- .orderByAsc(GroupsRelation::getNum)
- .last("limit 1"));
- if (relation != null) {
- log.info("用户userId:{},获取相似车队的车位relationId:{}", relation.getId());
- }
- return relation;
- }
- @Override
- public Page<UserRecommendGoodsFrontView> getRecommendGoodsViews(Boolean isPayNf, List<Long> filter_goods_id, Boolean filterCourseGoods, Integer start, Integer limit) {
- return groupsRelationMapper.getRecommendGoodsViews(new Page<>(start, limit), filter_goods_id, isPayNf, filterCourseGoods);
- }
- @Override
- @Transactional(rollbackFor = Throwable.class)
- public void sendCourseWare(SpecialGoodsTicketReq request) throws Exception {
- Long userId = request.getUserId();
- Long relationId = request.getRelationId();
- String email = request.getEmail();
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- RenewalView relation = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getRelationId, relationId).build());
- Assert.notNull(relation, "您的车票不存在");
- Long relationUserId = relation.getUserId();
- if (!userIdList.contains(relationUserId)) {
- throw BusinessRuntimeException.getInstance("您的车票不存在");
- }
- if (relation.getSpecialType() != GoodsDon.SpecialType.courseware) {
- throw BusinessRuntimeException.getInstance("");
- }
- //是否已发送
- if (StrUtil.isNotBlank(relation.getGrAccount())) {
- throw BusinessRuntimeException.getInstance("已领取课件,请前往邮箱查看");
- }
- if (!Validator.isEmail(email)) {
- throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
- }
- int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
- .set(GroupsRelation::getAccount, email)
- .and(qr -> qr.isNull(GroupsRelation::getAccount).or().eq(GroupsRelation::getAccount, StrUtil.EMPTY))
- .eq(GroupsRelation::getId, relationId));
- if (update > 0) {
- orderDonMapper.update(null, Wrappers.lambdaUpdate(OrderDon.class)
- .set(OrderDon::getStatus, OrderDon.Status.complete)
- .eq(OrderDon::getRelationId, relationId)
- .eq(OrderDon::getUserId, userId)
- .eq(OrderDon::getStatus, OrderDon.Status.hasPayment));
- mailService.sendCoursewareByGoodsId(relation.getGoodsId(), relation.getRelationId(), relation.getUserId(), relation.getTitle(), email);
- }
- }
- @Override
- public GroupsRelation getRelation(Long payRelationId, Long userId, GoodsDonSku sku, String ip) {
- GroupsRelation relation = null;
- Integer maxNum = sku.getNum();
- if (payRelationId != null && payRelationId > 0) {
- relation = groupsRelationMapper.selectById(payRelationId);
- if (relation == null) {
- throw new BusinessRuntimeException(GROUP_INDEX_NOT_FIND.getCode(), GROUP_INDEX_NOT_FIND.getMsg());
- }
- if (relation.getUserId() != null && relation.getUserId() > 0L) {
- throw new BusinessRuntimeException(GROUP_INDEX_ALREADY_BE_USER.getCode(), GROUP_INDEX_ALREADY_BE_USER.getMsg());
- }
- } else {
- GroupsTrips groups = null;
- //ChatGpt Plus、Midjourney非独立规格 上车先获取硬塞座位 硬塞5个座位
- //主账号过期时间25天以外
- //23-12-06 MidJourney 不硬塞
- if (sku.getNum() > 1 && Constant.AI_goodsIds.get(0) == sku.getGoodsId()) {
- Integer extraNum = 5;
- if (sku.getId() == 178l || sku.getId() == 423l) {
- extraNum = 2;
- }
- maxNum += extraNum;
- DateTime time = DateUtil.offsetDay(DateUtil.beginOfDay(DateTime.now()), 25);
- //不塞存在该用户的车队
- Long groupsId = groupsRelationMapper.getOutSideGroupsTripsRelationByGoodsId(sku.getId(), time, extraNum, userId, sku.getNum(), maxNum);
- if (groupsId != null) {
- relation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
- .eq(GroupsRelation::getGroupsId, groupsId)
- .eq(GroupsRelation::getUserId, 0)
- .eq(GroupsRelation::getStatus, GroupsRelation.Status.outside)
- .last("limit 1"));
- if (relation == null) {
- relation = new GroupsRelation();
- relation.setStatus(GroupsRelation.Status.outside);
- relation.setUserId(userId);
- relation.setGroupsId(groupsId);
- GroupsRelation maxNumRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getGroupsId, groupsId).orderByDesc(GroupsRelation::getNum).last("limit 1"));
- Integer nextNum = maxNumRelation.getNum() + 1;
- if (redisService.setNx(RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getNameFormat(groupsId, nextNum), nextNum, RedisService.key.GROUPS_TRIPS_OUTSIDE_RELATION.getTimeout())) {
- relation.setNum(nextNum);
- groupsRelationMapper.insert(relation);
- } else {
- throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
- }
- }
- groups = groupsMapper.selectById(groupsId);
- }
- }
- if (groups == null) {
- //寻找差不多过期时间规格的车位
- relation = getSimilarExpiredGroupRelation(sku, null, ip);
- if (relation == null) {
- //TODO 如果没指定座位,等待逻辑确认
- groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.validity, maxNum, ip);
- if (groups == null) {
- //待发车
- groups = groupsMapper.selectTicketGroups(sku.getId(), GroupsTrips.Status.waiting, maxNum, ip);
- }
- //新增空车队
- if (groups == null) {
- relation = groupsFuncService.createNewGroupsTrips(sku);
- }
- //获取车位
- if (relation == null) {
- //寻找快满编车队进行占座
- relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
- if (relation == null) {
- Long groupsId = groups.getId();
- TASK_EXECUTOR.execute(() -> groupsMapper.updateAvailableNum(groupsId));
- throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
- }
- }
- }
- }
- if (groups == null) {
- groups = groupsMapper.selectById(relation.getGroupsId());
- }
- //车队目前可用停车数
- relation.setGroupsAvailParkingNum(groups.getAvailableParkingNum());
- relation.setMaxNum(maxNum);
- }
- return relation;
- }
- @Override
- public String getAiVerifyCode(TicketLoginReq loginReq) throws Exception {
- Integer type = loginReq.getType();
- RenewalView renewalView;
- String account;
- Long userId = 0l;
- Long relationId = 0l;
- if (loginReq.getMerchants()) {
- renewalView = new RenewalView();
- renewalView.setGoodsId(loginReq.getGoodsId());
- renewalView.setAccount(loginReq.getAccount());
- if (loginReq.getAccountId() != null && loginReq.getAccountId() != 0l) {
- GroupsTrips groupsTrips = groupsMapper.selectOne(Wrappers.lambdaQuery(GroupsTrips.class)
- .eq(GroupsTrips::getAccountId, loginReq.getAccountId())
- .last("limit 1"));
- if (groupsTrips != null) {
- renewalView.setSkuId(groupsTrips.getSkuId());
- }
- }
- renewalView.setUserId(userId);
- account = loginReq.getAccount();
- } else {
- userId = loginReq.getUserId();
- relationId = loginReq.getRelationId();
- List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
- renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getUserId, relationUserIdList).op(Operator.InList)
- .field(RenewalView::getRelationId, relationId).build());
- if (renewalView == null) {
- throw BusinessRuntimeException.getInstance("您的车票不存在...");
- }
- if (StrUtil.isEmpty(renewalView.getAccount())) {
- throw BusinessRuntimeException.getInstance("车票未配置账号");
- }
- account = renewalView.getAccount();
- String password = renewalView.getPassword();
- if (StrUtil.hasEmpty(account, password)) {
- throw BusinessRuntimeException.getInstance("您的车票账号密码异常,请联系客服...");
- }
- }
- if (renewalView.getGoodsId() == 1l) {
- Integer linkType = loginReq.getLinkType();
- if (type == null && linkType == null) {
- throw BusinessRuntimeException.getInstance("系统异常");
- }
- if (linkType != null) {
- String key = RedisService.key.NF_UPGRAD_USER_LIMIT_KEY.getNameFormat(DateUtil.hour(DateTime.now(), true), linkType, renewalView.getUserId());
- if (!redisService.hasKey(key)) {
- redisService.set(key, 0l, RedisService.key.NF_UPGRAD_USER_LIMIT_KEY.getTimeout());
- } else {
- Integer value = (Integer) redisService.get(key);
- if (value >= 5) {
- throw BusinessRuntimeException.getInstance("获取奈飞同步更新装置过于频繁");
- }
- }
- }
- }
- String code = commonMailService.getTicketMailCode(renewalView.getGoodsId(), account, renewalView.getRelateEmail(), renewalView.getGptEmailPwd(), renewalView.getCreatedTime(), type, loginReq.getLinkType(), false);
- GroupsRelationLoginCodeRecord record = new GroupsRelationLoginCodeRecord();
- record.setUserId(userId);
- record.setRelationId(relationId);
- record.setAccount(renewalView.getAccount());
- record.setGoodsId(renewalView.getGoodsId());
- record.setSkuId(renewalView.getSkuId());
- groupsRelationLoginCodeRecordMapper.insert(record);
- //奈飞同步装置
- if (renewalView.getGoodsId() == 1l && loginReq.getLinkType() != null) {
- String key = RedisService.key.NF_UPGRAD_USER_LIMIT_KEY.getNameFormat(DateUtil.hour(DateTime.now(), true), loginReq.getLinkType(), renewalView.getUserId());
- redisService.incr(key, 1L);
- }
- return code;
- }
- @Override
- public void checkGroupsAvailParkingNum(Long skuId, Long groupsId, Integer availableParkingNum, Integer maxNum) {
- //GPT10人月付、4人月付
- if (skuId == 161l || skuId == 178l) {
- //为0时,不参与限制
- if (availableParkingNum == 0) {
- return;
- }
- //目前车队可用停车数
- //目前上车人数
- Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
- .eq(GroupsRelation::getGroupsId, groupsId)
- .ne(GroupsRelation::getUserId, 0)
- .le(GroupsRelation::getNum, maxNum));
- if (currentParkingNum > availableParkingNum) {
- throw BusinessRuntimeException.getInstance("系统繁忙,请重试...");
- }
- }
- }
- @Override
- public void adjustGroupsParkingTime(Long relationId, GoodsDonSku sku) {
- if (sku == null) {
- return;
- }
- Long skuId = sku.getId();
- //更新GPT Plus10人、4人月付车队状态 待优化
- if (skuId == 161l || skuId == 178l || skuId == 422l || skuId == 423l) {
- GroupsRelation relation = groupsRelationMapper.selectById(relationId);
- Long groupsId = relation.getGroupsId();
- GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
- Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
- //为0时 不参与限制
- if (availableParkingNum == 0) {
- return;
- }
- Integer extraNum = 5;
- if (skuId == 178l || skuId == 423l) {
- extraNum = 2;
- }
- Integer maxNum = sku.getNum() + extraNum;
- if (availableParkingNum >= maxNum) {
- return;
- }
- //目前上车人数
- Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
- .eq(GroupsRelation::getGroupsId, groupsId)
- .ne(GroupsRelation::getUserId, 0)
- .le(GroupsRelation::getNum, maxNum));
- if (currentParkingNum >= availableParkingNum) {
- //锁住
- DateTime parkingTime = DateUtil.offsetDay(DateTime.now(), 1);
- groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
- .eq(GroupsTrips::getId, groupsTrips.getId())
- .set(GroupsTrips::getParkingTime, parkingTime)
- .isNull(GroupsTrips::getParkingTime));
- }
- }
- }
- @Override
- public void setGroupsTripsParkingTimeEmpty(GoodsDonSku sku, Long relationId) {
- if (sku == null) {
- return;
- }
- Long skuId = sku.getId();
- if (skuId == 161l || skuId == 178l) {
- GroupsRelation relation = groupsRelationMapper.selectById(relationId);
- Long groupsId = relation.getGroupsId();
- GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
- Integer availableParkingNum = groupsTrips.getAvailableParkingNum();
- if (groupsTrips.getParkingTime() == null || availableParkingNum == 0) {
- return;
- }
- Integer extraNum = 5;
- if (skuId == 178l) {
- extraNum = 2;
- }
- Integer skuNum = sku.getNum();
- if (availableParkingNum >= skuNum + extraNum) {
- return;
- }
- //目前上车人数
- Integer currentParkingNum = groupsRelationMapper.selectCount(Wrappers.lambdaQuery(GroupsRelation.class)
- .eq(GroupsRelation::getGroupsId, groupsId)
- .ne(GroupsRelation::getUserId, 0));
- if (currentParkingNum < availableParkingNum) {
- groupsMapper.update(null, Wrappers.lambdaUpdate(GroupsTrips.class)
- .eq(GroupsTrips::getId, groupsTrips.getId())
- .set(GroupsTrips::getParkingTime, null)
- .isNotNull(GroupsTrips::getParkingTime));
- }
- }
- }
- @Override
- public Integer getUserCodeNumBySkuId(Long userId, Long skuId, Boolean isHasVerifyCode) {
- //查看双重验证码
- UserFuncProperty userFuncProperty = userFuncPropertyMapper.selectOne(Wrappers.lambdaQuery(UserFuncProperty.class).eq(UserFuncProperty::getUserId, userId).last("limit 1"));
- //默认2次
- int num = 2;
- //有安全码查看次数 置为0
- if (isHasVerifyCode) {
- num = 0;
- //存在身份实名认证有两次机会
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- Integer selectCount = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
- .in(UserCertRecord::getUserId, userIdList));
- if (selectCount > 0) {
- num = 2;
- }
- return num;
- }
- //查看双重验证码次数
- GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
- if (sku.getCodeNum() > 0) {
- num = sku.getCodeNum();
- }
- if (userFuncProperty != null) num = userFuncProperty.getVerifyCheckNum();
- return num;
- }
- @Override
- public List<RenewalView> getHasPayGoods(long userId) {
- User user = userMapper.selectById(userId);
- List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, user);
- DateTime now = DateTime.now();
- List<RenewalView> list = groupsRelationMapper.getUserHasPayGoods(userIds, now);
- return list;
- }
- @Override
- public List<RenewalView> getExpiryRenewalNotify(Long userId) {
- DateTime now = DateTime.now();
- DateTime thisZero = DateUtil.beginOfDay(now);
- String thisDate = thisZero.toDateStr();
- Object exists = redisService.get(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId));
- if (exists != null) {
- return null;
- }
- User u = userMapper.selectById(userId);
- if (u == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
- if (u.getIsBlack()) {
- redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
- return null;
- }
- List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
- MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
- List<RenewalView> list = beanSearcher.searchAll(RenewalView.class, builder
- .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
- .field(RenewalView::getUserId, userIds).op(Operator.InList)
- .field(RenewalView::getExpiryTime, now).op(Operator.GreaterThan)
- .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
- .field(RenewalView::getAqType, 1)
- .field(RenewalView::getIsVip, Boolean.TRUE)
- .orderBy(RenewalView::getExpiryTime).asc()
- .build());
- if (list.size() > 5) {
- redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
- return null;
- }
- List<Long> renewReduceSkuIds = userBenefitsMapper.selectList(Wrappers.lambdaQuery(UserBenefits.class)
- .eq(UserBenefits::getType, UserBenefits.Type.RENEW).eq(UserBenefits::getStatus, 1).select(UserBenefits::getSkuId)).stream().map(UserBenefits::getSkuId).collect(Collectors.toList());
- //提前7天
- AtomicInteger day = new AtomicInteger(7);
- List<RenewalView> expiryTickets = list.stream().filter(ticket -> {
- //下架
- if (ticket.getGoodsStatus() == null || ticket.getSkuStatus() == null || !ticket.getGoodsStatus() || !ticket.getSkuStatus()) {
- return false;
- }
- Integer skuMonths = ticket.getMonths();
- if (skuMonths == null) {
- return false;
- }
- Date expiryTime = ticket.getExpiryTime();
- //年付 提前一个月
- if (skuMonths >= 12) {
- day.set(30);
- } else {
- day.set(7);
- }
- if (DateUtil.beginOfDay(expiryTime).compareTo(DateUtil.offsetDay(thisZero, day.get())) <= 0) {
- ticket.setExpiryTimeStr(DateUtil.format(ticket.getExpiryTime(), "yyyy-MM-dd"));
- //GPT镜像信息
- setGptMirrorInfo(ticket);
- ticket.setExpiryTime(null);
- if (CollUtil.isNotEmpty(renewReduceSkuIds) && renewReduceSkuIds.contains(ticket.getSkuId())) {
- ticket.setIsRenewReduce(true);
- }
- return true;
- }
- return false;
- }).collect(Collectors.toList());
- redisService.setNx(RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getNameFormat(thisDate, userId), userId, RedisService.key.TICKET_EXPIRY_NOTIFY_DAY.getTimeout());
- if (CollUtil.isNotEmpty(expiryTickets)) {
- groupsRelationExpiryNotifyRecordMapper.batchInsert(userId, expiryTickets);
- }
- return expiryTickets;
- }
- @Override
- public void getCollegeStudentUserGptTicket(Long userId) {
- Retryer<Boolean> build = RetryerBuilder.<Boolean>newBuilder()
- .retryIfException()
- //运行时异常重试
- .retryIfRuntimeException()
- //false重试
- .retryIfResult(res -> Objects.equals(res, false))
- //1s 间隔
- .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
- //停止策略 : 尝试请求3次
- .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build();
- try {
- build.call(() -> {
- //体验规格
- Long gptMirrorSkuId = Constant.GPT_MIRROR_SKU_ID;
- GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(gptMirrorSkuId);
- GroupsRelation relation = null;
- if (groups != null) {
- relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
- }
- if (relation == null) {
- //生成空车队
- GoodsDonSku sku = goodsDonSkuMapper.selectById(gptMirrorSkuId);
- relation = groupsFuncService.createNewGroupsTrips(sku);
- }
- DateTime startTime = DateTime.now();
- DateTime expiryTime = DateUtil.offsetDay(startTime, 1);
- int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
- .set(GroupsRelation::getUserId, userId)
- .set(GroupsRelation::getStartTime, startTime)
- .set(GroupsRelation::getExpiryTime, expiryTime)
- .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
- .set(GroupsRelation::getAqType, 2)
- .eq(GroupsRelation::getId, relation.getId())
- .eq(GroupsRelation::getUserId, 0));
- if (update > 0) {
- groupsMapper.decrAvailableNum(groups.getId());
- return true;
- }
- log.error("发送用户:{} GPT镜像车票体验失败,进行重试", userId);
- return false;
- });
- } catch (ExecutionException | RetryException e) {
- log.error("发送用户GPT镜像体验失败:{}", StringUtil.getErrorText(e));
- }
- }
- @Override
- public RenewalView getTicketDetail(long userId, Long relationId) {
- User u = userMapper.selectById(userId);
- List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, u);
- MapBuilder builder = MapUtils.builder();
- RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, builder
- .field(RenewalView::getRelationId, relationId)
- .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
- .field(RenewalView::getUserId, userIds).op(Operator.InList)
- .field("condition", String.format("(gr.expiry_time is null or gr.expiry_time > '%s')", DateTime.now()))
- .field(RenewalView::getStatus, List.of("validity", "outside")).op(Operator.InList)
- .orderBy(RenewalView::getExpiryTime).asc()
- .onlySelect(RenewalView::getRelationId, RenewalView::getExpiryTime, RenewalView::getTitle, RenewalView::getSpecVal, RenewalView::getGoodsId, RenewalView::getSkuId, RenewalView::getSkuNum, RenewalView::getSkuStatus, RenewalView::getGoodsStatus)
- .build());
- if (renewalView != null) {
- renewalView.setExpiryTimeStr(DateUtil.format(renewalView.getExpiryTime(), "yyyy-MM-dd"));
- }
- return renewalView;
- }
- @Override
- public GroupsRelation ticketLogin(TicketLoginReq loginReq) {
- String account = loginReq.getAccount();
- if (StrUtil.isEmpty(account)) {
- throw BusinessRuntimeException.getInstance("请输入对应账号");
- }
- if (loginReq.getMerchants()) {
- if (account.contains(Constant.YH_EMAIL_DOMAIN_SUFFIX)) {
- SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
- .eq(SysEmail::getEmail, account)
- .last("limit 1"));
- if (sysEmail == null) {
- throw BusinessRuntimeException.getInstance("账号错误,请联系客服");
- }
- }
- GroupsRelation relation = new GroupsRelation();
- relation.setId(0l);
- return relation;
- }
- String password = loginReq.getPassword();
- Integer num = loginReq.getNum();
- if (ObjectUtil.hasEmpty(password, num)) {
- throw BusinessRuntimeException.getInstance("参数错误");
- }
- Long goodsId = Optional.ofNullable(loginReq.getGoodsId()).orElse(1l);
- GroupsRelation relation = groupsRelationMapper.selectRelationByAccountAndNum(account, password, goodsId, num);
- if (relation == null) {
- log.info("外部入口使用账号account:{},pwd:{},座位num:{}登录失败...", account, password, num);
- if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
- throw BusinessRuntimeException.getInstance("Please enter the correct account information");
- }
- throw BusinessRuntimeException.getInstance("请输入正确的账号信息");
- }
- return relation;
- }
- @Override
- public String outsideTicketVerify(TicketLoginReq loginReq) {
- String account = loginReq.getAccount();
- if (StrUtil.isEmpty(account)) {
- throw BusinessRuntimeException.getInstance("账号错误");
- }
- if (loginReq.getMerchants()) {
- if (account.contains(Constant.YH_EMAIL_DOMAIN_SUFFIX)) {
- SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
- .eq(SysEmail::getEmail, account)
- .last("limit 1"));
- if (sysEmail == null) {
- throw BusinessRuntimeException.getInstance("账号错误,请联系客服");
- }
- }
- loginReq.setGoodsId(1l);
- loginReq.setAccountId(0l);
- } else {
- String password = loginReq.getPassword();
- Integer num = loginReq.getNum();
- if (ObjectUtil.hasEmpty(password, num)) {
- throw BusinessRuntimeException.getInstance("参数错误");
- }
- Long goodsId = Optional.ofNullable(loginReq.getGoodsId()).orElse(1l);
- GroupsRelation relation = groupsRelationMapper.selectRelationByAccountAndNum(account, password, goodsId, num);
- if (relation == null) {
- log.info("外部入口使用账号account:{},pwd:{},座位num:{}同步验证链接失败...", account, password, num);
- if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
- throw BusinessRuntimeException.getInstance("This account is no longer valid");
- }
- throw BusinessRuntimeException.getInstance("该账号已失效");
- }
- loginReq.setUserId(relation.getUserId());
- loginReq.setRelationId(relation.getId());
- loginReq.setGoodsId(goodsId);
- }
- try {
- String aiVerifyCode = getAiVerifyCode(loginReq);
- if (StrUtil.isEmpty(aiVerifyCode)) {
- throw BusinessRuntimeException.getInstance("");
- }
- return aiVerifyCode;
- } catch (Exception e) {
- if (StrUtil.equals(loginReq.getLang(), Constant.EN_US)) {
- throw BusinessRuntimeException.getInstance("Please verify that the link was sent");
- }
- throw BusinessRuntimeException.getInstance("请校验链接是否已发送");
- }
- }
- @Override
- public String getNfHouseholdDevice(long userId, Long relationId) throws Exception {
- List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
- RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder().field(RenewalView::getUserId, relationUserIdList).op(Operator.InList)
- .field(RenewalView::getRelationId, relationId).build());
- if (renewalView == null) {
- throw BusinessRuntimeException.getInstance("您的车票不存在...");
- }
- if (StrUtil.isEmpty(renewalView.getAccount())) {
- throw BusinessRuntimeException.getInstance("车票未配置账号");
- }
- String account = renewalView.getAccount();
- String password = renewalView.getPassword();
- if (StrUtil.hasEmpty(account, password)) {
- throw BusinessRuntimeException.getInstance("您的车票账号密码异常,请联系客服...");
- }
- String code = commonMailService.getTicketMailCode(renewalView.getGoodsId(), account, renewalView.getRelateEmail(), renewalView.getGptEmailPwd(), renewalView.getCreatedTime(), null, 1, true);
- return code;
- }
- @Override
- public String getVerifyCode(String account) throws Exception {
- Account at = accountMapper.selectOne(Wrappers.lambdaQuery(Account.class)
- .eq(Account::getAccount, account)
- .eq(Account::getGoodsId, 18));
- if (at == null) {
- return null;
- }
- String password = at.getPassword();
- if (StrUtil.hasEmpty(account, password)) {
- throw BusinessRuntimeException.getInstance("账号异常");
- }
- String code = commonMailService.getTicketMailCode(at.getGoodsId(), account, at.getRelateEmail(), at.getGptEmailPwd(), at.getCreatedTime(), null, null, false);
- return code;
- }
- @Override
- public void changeTicket(long userId, Long relationId) {
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder()
- .field(RenewalView::getUserId, userIdList).op(Operator.InList)
- .field(RenewalView::getRelationId, relationId)
- .field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
- .build());
- if (renewalView == null) {
- throw BusinessRuntimeException.getInstance("车票不存在");
- }
- if (renewalView.getGoodsId() != 1) {
- throw BusinessRuntimeException.getInstance("只针对奈飞车票");
- }
- AccountNetflixState accountNetflixState = accountNetflixStateMapper.selectOne(Wrappers.lambdaQuery(AccountNetflixState.class)
- .eq(AccountNetflixState::getAccount, renewalView.getAccount())
- .orderByDesc(AccountNetflixState::getId)
- .last("limit 1"));
- if (accountNetflixState == null || accountNetflixState.getState() != 3) {
- throw BusinessRuntimeException.getInstance("只支持异常账号更换车票");
- }
- String key = RedisService.key.NETFLIX_CHANGE_TICKET_KEY.getName() + relationId;
- if (!redisService.setNx(key, relationId, RedisService.key.NETFLIX_CHANGE_TICKET_KEY.getTimeout())) {
- throw BusinessRuntimeException.getInstance("正在更换车票中..");
- }
- Long groupId = renewalView.getGroupId();
- Long skuId = renewalView.getSkuId();
- Date expiryTime = renewalView.getExpiryTime();
- //随机选择同规格得车票
- Long item_groupsId = groupsMapper.selectRandomGroupsTripsBySkuId(groupId, skuId, expiryTime);
- GroupsRelation newRelation = null;
- if (item_groupsId != null) {
- //存在就随机分配一个,没有就不做处理
- newRelation = changeTickerRelation.changeTicket(renewalView.getUserId(), relationId, item_groupsId, renewalView.getSkuId());
- }
- //若无移至空车队
- if (newRelation == null) {
- newRelation = changeTickerRelation.getEmptyGroupsRelation(renewalView.getUserId(), renewalView.getRelationId(), renewalView.getSkuId());
- }
- if (newRelation == null) {
- throw BusinessRuntimeException.getInstance("更换异常,请重新提交");
- }
- NetflixUserAccountStatusRecord netflixUserAccountStatusRecord = netflixUserAccountStatusRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
- .eq(NetflixUserAccountStatusRecord::getRelationId, relationId)
- .last("limit 1"));
- if (netflixUserAccountStatusRecord != null) {
- netflixUserAccountStatusRecord.setDeleted(false);
- netflixUserAccountStatusRecordMapper.updateById(netflixUserAccountStatusRecord);
- }
- redisService.del(key);
- }
- @Override
- public AccountView getTempAccount(long userId, Long relationId) {
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder()
- .field(RenewalView::getRelationId, relationId)
- .field(RenewalView::getUserId, userIdList).op(Operator.InList)
- .field(RenewalView::getGroupsStatus, GroupsTrips.Status.waiting.name())
- .field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
- .build());
- if (renewalView == null) {
- throw BusinessRuntimeException.getInstance("车票不存在");
- }
- AccountView accountView = new AccountView();
- //奈飞超过3帐车票不给看
- Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
- .field(RenewalView::getUserId, userIdList).op(Operator.InList)
- .field(RenewalView::getGoodsId, 1)
- .field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
- .build());
- if (number.intValue() > 2) {
- return accountView;
- }
- NetflixTempAccountUser netflixTempAccountUser = netflixTempAccountUserMapper.selectOne(Wrappers.lambdaQuery(NetflixTempAccountUser.class)
- .eq(NetflixTempAccountUser::getRelationId, relationId)
- .in(NetflixTempAccountUser::getUserId, userIdList)
- .last("limit 1"));
- Account account;
- if (netflixTempAccountUser == null) {
- account = accountMapper.selectNetflixTempAccount();
- if (account != null) {
- netflixTempAccountUser = new NetflixTempAccountUser();
- netflixTempAccountUser.setAccountId(account.getId());
- netflixTempAccountUser.setRelationId(relationId);
- netflixTempAccountUser.setUserId(renewalView.getUserId());
- netflixTempAccountUserMapper.insert(netflixTempAccountUser);
- }
- } else {
- account = accountMapper.selectById(netflixTempAccountUser.getAccountId());
- }
- if (account != null) {
- accountView.setAccount(account.getAccount());
- accountView.setPassword(account.getPassword());
- }
- return accountView;
- }
- @Override
- public void nfErrorAccountSubmit(long userId, Long relationId) {
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- RenewalView renewalView = beanSearcher.searchFirst(RenewalView.class, MapUtils.builder()
- .field(RenewalView::getUserId, userIdList).op(Operator.InList)
- .field(RenewalView::getRelationId, relationId)
- .field(RenewalView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan).build());
- if (renewalView == null) {
- throw BusinessRuntimeException.getInstance("车票不存在");
- }
- if (renewalView.getGoodsId() != 1) {
- throw BusinessRuntimeException.getInstance("只支持奈飞车票");
- }
- String account = renewalView.getAccount();
- String password = renewalView.getPassword();
- Integer selectCount = netflixUserAccountStatusRecordMapper.selectCount(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
- .eq(NetflixUserAccountStatusRecord::getRelationId, relationId)
- .eq(NetflixUserAccountStatusRecord::getUserId, userId)
- .eq(NetflixUserAccountStatusRecord::getAccount, account));
- if (selectCount > 0) {
- throw BusinessRuntimeException.getInstance("该账号正在检测中");
- }
- AccountNetflixState accountNetflixState = accountNetflixStateMapper.selectOne(Wrappers.lambdaQuery(AccountNetflixState.class)
- .eq(AccountNetflixState::getAccount, account)
- .last("limit 1"));
- Integer state = 0;
- if (accountNetflixState != null && accountNetflixState.getState() == 3) {
- state = accountNetflixState.getState();
- }
- NetflixUserAccountStatusRecord netflixUserAccountStatusRecord = new NetflixUserAccountStatusRecord();
- netflixUserAccountStatusRecord.setAccount(account);
- netflixUserAccountStatusRecord.setRelationId(relationId);
- netflixUserAccountStatusRecord.setPassword(password);
- netflixUserAccountStatusRecord.setUserId(renewalView.getUserId());
- netflixUserAccountStatusRecord.setState(state);
- OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
- .eq(OrderDon::getRelationId, renewalView.getRelationId())
- .eq(OrderDon::getUserId, renewalView.getUserId())
- .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
- .orderByDesc(OrderDon::getId)
- .last("limit 1"));
- if (orderDon != null) {
- netflixUserAccountStatusRecord.setOrderId(orderDon.getId());
- }
- netflixUserAccountStatusRecordMapper.insert(netflixUserAccountStatusRecord);
- }
- @Override
- public NetflixErrorAccountStatus getNetflixErrorAccountStatus(long userId, Long relationId) {
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- NetflixUserAccountStatusRecord netflixUseAccountStatusRecord = netflixUserAccountStatusRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
- .eq(NetflixUserAccountStatusRecord::getRelationId, relationId)
- .eq(NetflixUserAccountStatusRecord::getDeleted, 1)
- .in(NetflixUserAccountStatusRecord::getUserId, userIdList)
- .last("limit 1"));
- if (netflixUseAccountStatusRecord == null) {
- throw BusinessRuntimeException.getInstance("未有提交记录");
- }
- Integer state = netflixUseAccountStatusRecord.getState();
- //2是登录失败 3是被封号
- // if (state == 3) {
- // if (netflixUseAccountStatusRecord.getCreatedTime().compareTo(DateUtil.offsetMinute(DateTime.now(), -10)) > 0) {
- // state = 0;
- // }
- // }
- //登录失败不算异常
- if (state == 2) {
- state = 1;
- }
- NetflixErrorAccountStatus status = new NetflixErrorAccountStatus();
- //账号检测状态
- status.setNfStatus(state);
- NetflixUserAccountSubmitRecoverRecord recoverRecord = netflixUserAccountSubmitRecoverRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
- .eq(NetflixUserAccountSubmitRecoverRecord::getUserId, userIdList)
- .eq(NetflixUserAccountSubmitRecoverRecord::getRelationId, relationId)
- .last("limit 1"));
- if (recoverRecord != null) {
- //申请恢复状态
- status.setRecoverStatus(recoverRecord.getStatus());
- }
- return status;
- }
- @Override
- public List<NetflixUserAccountStatusRecord> getUserNetflixSubmitRecord(long userId) {
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- List<NetflixUserAccountStatusRecord> netflixUseAccountStatusRecords = netflixUserAccountStatusRecordMapper.selectList(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
- .in(NetflixUserAccountStatusRecord::getUserId, userIdList)
- .eq(NetflixUserAccountStatusRecord::getDeleted, 1)
- .select(NetflixUserAccountStatusRecord::getOrderId, NetflixUserAccountStatusRecord::getRelationId, NetflixUserAccountStatusRecord::getState, NetflixUserAccountStatusRecord::getCreatedTime));
- return netflixUseAccountStatusRecords;
- }
- @Override
- public Long getTrialTicketByUserIdAndGoodsId(Long userId, Long trialSkuId, Integer ticketType) {
- Retryer<Long> build = RetryerBuilder.<Long>newBuilder()
- .retryIfException()
- //运行时异常重试
- .retryIfRuntimeException()
- //false重试
- .retryIfResult(res -> res == null)
- //1s 间隔
- .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
- //停止策略 : 尝试请求3次
- .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build();
- try {
- return build.call(() -> {
- //体验规格
- GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(trialSkuId);
- GroupsRelation relation = null;
- if (groups != null) {
- relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
- }
- if (relation == null) {
- //生成空车队
- GoodsDonSku sku = goodsDonSkuMapper.selectById(trialSkuId);
- relation = groupsFuncService.createNewGroupsTrips(sku);
- }
- DateTime startTime = DateTime.now();
- DateTime expiryTime = DateUtil.offsetDay(startTime, 1);
- int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
- .set(GroupsRelation::getUserId, userId)
- .set(GroupsRelation::getStartTime, startTime)
- .set(GroupsRelation::getExpiryTime, expiryTime)
- .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
- .set(GroupsRelation::getAqType, ticketType)
- .eq(GroupsRelation::getId, relation.getId())
- .eq(GroupsRelation::getUserId, 0));
- if (update > 0) {
- groupsMapper.decrAvailableNum(groups.getId());
- return relation.getId();
- }
- log.error("发送用户:{} GPT镜像车票体验失败,进行重试", userId);
- return null;
- });
- } catch (ExecutionException | RetryException e) {
- log.error("发送用户GPT镜像体验失败:{}", StringUtil.getErrorText(e));
- return null;
- }
- }
- @Override
- public Boolean getMirrorPopups(long userId) {
- //是否弹窗 默认否
- Boolean flag = false;
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
- .field(RenewalView::getUserId, userIdList).op(Operator.InList)
- .field(RenewalView::getStatus, GroupsRelation.Status.validity.name())
- .field(RenewalView::getIsMirror, true)
- .build());
- if (number.intValue() == 0) {
- flag = true;
- }
- if (!flag) {
- String thisWeekBegin = DateUtil.beginOfWeek(DateTime.now()).toDateStr();
- Integer selectCount = mirrorTicketPopupsRecordMapper.selectCount(Wrappers.lambdaQuery(MirrorTicketPopupsRecord.class)
- .in(MirrorTicketPopupsRecord::getUserId, userIdList)
- .eq(MirrorTicketPopupsRecord::getThisWeek, thisWeekBegin));
- if (selectCount == 0) {
- MirrorTicketPopupsRecord mirrorTicketPopupsRecord = new MirrorTicketPopupsRecord();
- mirrorTicketPopupsRecord.setThisWeek(thisWeekBegin);
- mirrorTicketPopupsRecord.setUserId(userId);
- mirrorTicketPopupsRecordMapper.insert(mirrorTicketPopupsRecord);
- } else {
- flag = true;
- }
- }
- return flag;
- }
- @Override
- public Long getTicket(Long userId, Long skuId, Boolean isVip) {
- Retryer<Long> build = RetryerBuilder.<Long>newBuilder()
- .retryIfException()
- //运行时异常重试
- .retryIfRuntimeException()
- //false重试
- .retryIfResult(res -> res == null)
- //1s 间隔
- .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
- //停止策略 : 尝试请求1次
- .withStopStrategy(StopStrategies.stopAfterAttempt(1)).build();
- try {
- return build.call(() -> {
- GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(skuId);
- GroupsRelation relation = null;
- if (groups != null) {
- relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
- }
- GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
- //生成空车队
- if (relation == null) {
- relation = groupsFuncService.createNewGroupsTrips(sku);
- }
- DateTime startTime = DateTime.now();
- DateTime expiryTime;
- if (sku.getDays() != null) {
- expiryTime = DateUtil.offsetDay(startTime, sku.getDays());
- } else {
- expiryTime = DateUtil.offsetMonth(startTime, sku.getMonths());
- }
- int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
- .set(GroupsRelation::getUserId, userId)
- .set(GroupsRelation::getStartTime, startTime)
- .set(GroupsRelation::getExpiryTime, expiryTime)
- .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
- .set(GroupsRelation::getIsVip, isVip)
- .eq(GroupsRelation::getId, relation.getId())
- .eq(GroupsRelation::getUserId, 0));
- if (update > 0) {
- groupsMapper.decrAvailableNum(groups.getId());
- return relation.getId();
- }
- log.error("发送用户:{} GPT镜像车票失败,进行重试", userId);
- return null;
- });
- } catch (ExecutionException | RetryException e) {
- log.error("发送用户GPT镜像失败:{}", StringUtil.getErrorText(e));
- return null;
- }
- }
- @Override
- public Boolean getVipMirrorTicketIfEmpty(long userId, Long skuId) {
- Integer selectCount = vipGoodsSkuMapper.selectCount(Wrappers.lambdaQuery(VipGoodsSku.class)
- .eq(VipGoodsSku::getSkuId, skuId));
- if (selectCount == 0) {
- return false;
- }
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- VipUser vipUser = vipUserMapper.selectOne(Wrappers.lambdaQuery(VipUser.class)
- .in(VipUser::getUserId, userIdList)
- .gt(VipUser::getExpiryTime, DateTime.now())
- .last("limit 1"));
- //不是会员 或已到期
- if (vipUser == null) {
- return false;
- }
- Number number = beanSearcher.searchCount(GroupsRelationView.class, MapUtils.builder()
- .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
- .field(GroupsRelationView::getSkuId, skuId)
- .field(GroupsRelationView::getIsVip, Boolean.TRUE)
- .build());
- if (number.intValue() == 0) {
- Long relationId = getTicket(vipUser.getUserId(), skuId, Boolean.TRUE);
- //同步时间
- groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
- .set(GroupsRelation::getExpiryTime, vipUser.getExpiryTime())
- .eq(GroupsRelation::getUserId, vipUser.getUserId())
- .eq(GroupsRelation::getId, relationId));
- return true;
- }
- return false;
- }
- @Override
- public Integer netflixSubmitRecover(ErrorNetflixChangeReq req) {
- Long relationId = req.getRelationId();
- Long userId = req.getUserId();
- //校验奈飞账号状态
- GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
- Date expiryTime = netflixRelationView.getExpiryTime();
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- NetflixUserAccountSubmitRecoverRecord recoverRecord = netflixUserAccountSubmitRecoverRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
- .eq(NetflixUserAccountSubmitRecoverRecord::getRelationId, relationId)
- .in(NetflixUserAccountSubmitRecoverRecord::getUserId, userIdList)
- .eq(NetflixUserAccountSubmitRecoverRecord::getStatus, 0)
- .last("limit 1"));
- if (recoverRecord == null) {
- recoverRecord = new NetflixUserAccountSubmitRecoverRecord();
- recoverRecord.setRelationId(relationId);
- recoverRecord.setUserId(netflixRelationView.getUserId());
- recoverRecord.setSkuId(netflixRelationView.getSkuId());
- recoverRecord.setAccount(netflixRelationView.getAccount());
- Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
- recoverRecord.setRemainDays(betweenDay);
- try {
- netflixUserAccountSubmitRecoverRecordMapper.insert(recoverRecord);
- } catch (DuplicateKeyException e) {
- throw BusinessRuntimeException.getInstance("您已申请恢复");
- }
- }
- //等待位数
- Integer selectCount = netflixUserAccountSubmitRecoverRecordMapper.selectCount(Wrappers.lambdaQuery(NetflixUserAccountSubmitRecoverRecord.class)
- .lt(NetflixUserAccountSubmitRecoverRecord::getCreatedTime, recoverRecord.getCreatedTime())
- .eq(NetflixUserAccountSubmitRecoverRecord::getStatus, 0));
- return selectCount + 1;
- }
- @Override
- public void errorNetflixSubmitChangeOtherTicket(ErrorNetflixChangeReq req) throws Exception {
- Long relationId = req.getRelationId();
- Long userId = req.getUserId();
- //校验奈飞账号状态
- GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
- Date expiryTime = netflixRelationView.getExpiryTime();
- //小于15 不可替换其他车票
- Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
- if (betweenDay <= 15) {
- throw BusinessRuntimeException.getInstance("您的车票有效期未超过15天");
- }
- Long skuId = req.getSkuId();
- SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
- .eq(SysConfig::getSysKey, Constant.ERROR_NETFLIX_CHANGE_OTHER_SKU_KEY)
- .last("limit 1"));
- List<ErrorNetflixChangeOtherSkuTicketDto> errorNetflixChangeOtherSkuTicketDtos = Jsons.parseList(sysConfig.getSysValue(), ErrorNetflixChangeOtherSkuTicketDto.class);
- ErrorNetflixChangeOtherSkuTicketDto errorNetflixChangeOtherSkuTicketDto;
- //自定义顺序 直接获取
- if (betweenDay <= 60) {
- //检验规格
- errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(0);
- } else if (betweenDay <= 120) {
- errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(1);
- } else {
- errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(2);
- }
- List<Long> availableSkuIds = errorNetflixChangeOtherSkuTicketDto.getSkuIds();
- if (!availableSkuIds.contains(skuId)) {
- throw BusinessRuntimeException.getInstance("不支持更换该类型车票");
- }
- //替换车票
- TicketChangeDto changeDto = getTicketAndGenerateOrder(userId, skuId, netflixRelationView.getStartTime(), expiryTime);
- //替换记录
- NetflixUserChangeOtherTicketRecord changeOtherTicketRecord = new NetflixUserChangeOtherTicketRecord();
- changeOtherTicketRecord.setUserId(userId);
- changeOtherTicketRecord.setRelationId(relationId);
- changeOtherTicketRecord.setOtherRelationId(changeDto.getOtherRelationId());
- changeOtherTicketRecord.setRelateOrderId(changeDto.getOtherOrderId());
- netflixUserChangeOtherTicketRecordMapper.insert(changeOtherTicketRecord);
- }
- @Override
- public List<GoodsCategorySkuView> getNetflixReplaceOtherSkus(long userId, Long relationId) throws Exception {
- //校验奈飞账号状态
- GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
- Date expiryTime = netflixRelationView.getExpiryTime();
- //小于15 不可替换其他车票
- Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
- SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
- .eq(SysConfig::getSysKey, Constant.ERROR_NETFLIX_CHANGE_OTHER_SKU_KEY)
- .last("limit 1"));
- List<ErrorNetflixChangeOtherSkuTicketDto> errorNetflixChangeOtherSkuTicketDtos = Jsons.parseList(sysConfig.getSysValue(), ErrorNetflixChangeOtherSkuTicketDto.class);
- ErrorNetflixChangeOtherSkuTicketDto errorNetflixChangeOtherSkuTicketDto;
- if (betweenDay <= 15) {
- return null;
- }
- //自定义顺序 直接获取
- if (betweenDay <= 60) {
- //检验规格
- errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(0);
- } else if (betweenDay <= 120) {
- errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(1);
- } else {
- errorNetflixChangeOtherSkuTicketDto = errorNetflixChangeOtherSkuTicketDtos.get(2);
- }
- List<GoodsCategorySkuView> goodsCategorySkuViews = beanSearcher.searchAll(GoodsCategorySkuView.class, MapUtils.builder().field(GoodsCategorySkuView::getSkuId, errorNetflixChangeOtherSkuTicketDto.getSkuIds()).build());
- return goodsCategorySkuViews;
- }
- @Override
- public Boolean refundNetflix(ErrorNetflixChangeReq req) throws Exception {
- Long relationId = req.getRelationId();
- Long userId = req.getUserId();
- //校验奈飞账号状态
- GroupsRelationView netflixRelationView = checkNetflixErrorStatus(relationId, userId);
- Date expiryTime = netflixRelationView.getExpiryTime();
- //小于15
- Long betweenDay = DateUtil.betweenDay(DateTime.now(), expiryTime, false) + 1;
- if (betweenDay > 15) {
- throw BusinessRuntimeException.getInstance("车票有效期不超过15天可退款");
- }
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
- .eq(OrderDon::getRelationId, relationId)
- .in(OrderDon::getUserId, userIdList)
- .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
- .orderByDesc(OrderDon::getId)
- .last("limit 1"));
- if (orderDon == null) {
- throw BusinessRuntimeException.getInstance("对应车票订单不存在,请联系客服");
- }
- Long skuId = netflixRelationView.getSkuId();
- GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
- BigDecimal orderMoney = orderDon.getMoney();
- if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
- return false;
- }
- //每天单价
- BigDecimal dayPrice = sku.getPrice().divide(BigDecimal.valueOf(sku.getMonths() * 30), RoundingMode.DOWN);
- //需要退款金额
- BigDecimal refundMoney = dayPrice.multiply(BigDecimal.valueOf(betweenDay));
- if (refundMoney.compareTo(orderMoney) > 0) {
- return false;
- }
- //调用第三方接口退款
- OrderRefundReq refundReq = new OrderRefundReq();
- refundReq.setRefundMoney(refundMoney);
- refundReq.setRefundType("money");
- String outNo = orderRefundService.centerRefund(refundReq, orderDon);
- orderDon.setStatus(OrderDon.Status.refund);
- orderDon.setRefundMoney(refundMoney);
- GoodsDon goodsDon = goodsDonMapper.selectById(orderDon.getGoodsId());
- orderRefundService.refundRecord(orderDon, refundMoney, "系统", goodsDon, sku, outNo, orderDon.getType().name(), null);
- //清除车票
- GroupsRelation relation = groupsRelationMapper.selectById(relationId);
- clearService.clearTicket(relation, UserTicketClearedRecord.Source.refund);
- return true;
- }
- private DistributePosterGoodsDetailView setGoodsPoster(RenewalView ticket, User user, List<Long> userIds) {
- DistributePosterGoodsDetailView distributePosterGoodsDetailView = beanSearcher.searchFirst(DistributePosterGoodsDetailView.class, MapUtils.flatBuilder(request.getParameterMap())
- .field(DistributePosterGoodsFrontView::getGoodsId, ticket.getGoodsId()).build());
- if (distributePosterGoodsDetailView == null) {
- return null;
- }
- distributePosterGoodsDetailView.setNickname(user.getNickname());
- UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
- .eq(UserDistribute::getUserId, user.getId())
- .eq(UserDistribute::getDeleted, 1)
- .last("limit 1"));
- if (userDistribute != null) {
- UserPlatDistributeRate userPlatDistributeRate = userPlatDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatDistributeRate.class)
- .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
- .eq(UserPlatDistributeRate::getGoodsId, ticket.getGoodsId())
- .last("limit 1"));
- if (userPlatDistributeRate != null) {
- distributePosterGoodsDetailView.setRate(userPlatDistributeRate.getRate());
- }
- }
- GoodsDon goodsDon = goodsDonMapper.selectById(ticket.getGoodsId());
- if (goodsDon.getIsSkuDistribute()) {
- if (userDistribute != null) {
- UserPlatSkuDistributeRate userPlatSkuDistributeRate = userPlatSkuDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
- .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId())
- .eq(UserPlatSkuDistributeRate::getSkuId, ticket.getSkuId())
- .last("limit 1"));
- if (userPlatSkuDistributeRate != null) {
- distributePosterGoodsDetailView.setRate(userPlatSkuDistributeRate.getRate());
- }
- } else {
- DistributeGeneralSkuRate distributeGeneralSkuRate = distributeGeneralSkuRateMapper.selectOne(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
- .eq(DistributeGeneralSkuRate::getSkuId, ticket.getSkuId())
- .last("limit 1"));
- if (distributeGeneralSkuRate != null) {
- distributePosterGoodsDetailView.setRate(distributeGeneralSkuRate.getRate());
- }
- }
- }
- //用户推广海报记录id
- Optional.ofNullable(userDistributePosterRecordMapper.selectOne(Wrappers.lambdaQuery(UserDistributePosterRecord.class)
- .eq(UserDistributePosterRecord::getPosterId, distributePosterGoodsDetailView.getId())
- .in(UserDistributePosterRecord::getUserId, userIds)
- .last("limit 1"))).ifPresent(re -> distributePosterGoodsDetailView.setRid(re.getId()));
- return distributePosterGoodsDetailView;
- }
- private void setRenewDiscount(RenewalView ticket) {
- //镜像或者独立规格
- if (ticket.getIsMirror() || (ticket.getSkuNum() != null && ticket.getSkuNum() == 1)) {
- RenewUpgradePackage renewUpgradePackage = renewUpgradePackageMapper.selectOne(Wrappers.lambdaQuery(RenewUpgradePackage.class)
- .eq(RenewUpgradePackage::getGoodsId, ticket.getGoodsId())
- .eq(RenewUpgradePackage::getStatus, 1)
- .eq(RenewUpgradePackage::getDeleted, 1)
- .eq(RenewUpgradePackage::getSkuId, ticket.getSkuId())
- .orderByAsc(RenewUpgradePackage::getDiscount)
- .last("limit 1")
- .select(RenewUpgradePackage::getId, RenewUpgradePackage::getDiscount));
- if (renewUpgradePackage != null) {
- ticket.setDiscount(renewUpgradePackage.getDiscount());
- }
- }
- }
- private void setMirrorTicketInfo(RenewalView ticket) {
- //设置MJ镜像信息
- setMjMirrorInfo(ticket);
- //设置GPT镜像信息
- setGptMirrorInfo(ticket);
- //clause pro
- setClaudeProInfo(ticket);
- //luma
- setLumaInfo(ticket);
- }
- private void setMjMirrorInfo(RenewalView ticket) {
- if (ticket.getGoodsId() == 26 && ticket.getIsMirror()) {
- List<UpgradePackage> upgradePackages = upgradePackageMapper.selectList(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + ticket.getSkuId() + "\"')").eq(UpgradePackage::getStatus, Boolean.TRUE).eq(UpgradePackage::getDeleted, Boolean.TRUE));
- if (CollectionUtil.isNotEmpty(upgradePackages)) {
- upgradePackages.stream().map(e -> e.getPrice().divide(BigDecimal.valueOf(e.getNum()), 2, RoundingMode.HALF_UP)).min(BigDecimal::compareTo).ifPresent(ticket::setMjPackagePrice);
- }
- try {
- MidjourneyUserView search = beanSearcher.searchFirst(MidjourneyUserView.class, MapUtils.flatBuilder(request.getParameterMap())
- .field(MidjourneyUserView::getUserId, ticket.getUserId()).field(MidjourneyUserView::getRelationId, ticket.getRelationId())
- .onlySelect(MidjourneyUserView::getMjUserRelaxNum, MidjourneyUserView::getMjUserFastNum,MidjourneyUserView::getMjUserUpgradeNum, MidjourneyUserView::getIsCar)
- .build());
- if (search == null) {
- MidjourneyUser midjourneyUser = midjourneyAccountService.getMidjourneyUserToken(ticket.getUserId(), ticket.getRelationId());
- ticket.setMjUserFastNum(midjourneyUser.getMjFastNum());
- ticket.setMjUserUpgradeNum(midjourneyUser.getMjUpgradeNum());
- ticket.setMjUserRelaxNum(midjourneyUser.getMjRelaxNum());
- ticket.setIsCar(false);
- } else {
- ticket.setIsCar(search.getIsCar());
- ticket.setMjUserFastNum(search.getMjUserFastNum());
- ticket.setMjUserUpgradeNum(search.getMjUserUpgradeNum());
- ticket.setMjUserRelaxNum(search.getMjUserRelaxNum());
- }
- midjourneyAccountService.updateUserExpiryTime(ticket.getRelationId(), ticket.getExpiryTime());
- } catch (Exception e) {
- }
- }
- }
- public void setGptMirrorInfo(RenewalView ticket) {
- if (ticket.getGoodsId() == 18 && ticket.getIsCar() && ticket.getExpiryTime() != null) {
- try {
- //gpt用户镜像
- ChatGptUserView userInfo = chatGptAccountService.getUserInfo(ticket.getUserId(), ticket.getRelationId());
- ticket.setGptUserLimitNum(userInfo.getLimitNum());
- ticket.setGptUserLimitTime(userInfo.getLimitTime());
- ticket.setUserDsLimitNum(userInfo.getDsLimitNum());
- ticket.setUserDsLimitTime(userInfo.getDsLimitTime());
- ticket.setUserOtherLimitNum(userInfo.getOtherLimitNum());
- ticket.setUserOtherLimitTime(userInfo.getOtherLimitTime());
- ticket.setUse(userInfo.getUse());
- ticket.setModel(userInfo.getModel());
- Integer count = upgradePackageMapper.selectCount(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + ticket.getSkuId() + "\"')")
- .gt(UpgradePackage::getNum, userInfo.getLimitNum())
- .eq(UpgradePackage::getStatus, Boolean.TRUE)
- .eq(UpgradePackage::getGptType, 0)
- .eq(UpgradePackage::getDeleted, Boolean.TRUE));
- ticket.setUpgrade(count > 0 ? true : false);
- ticket.setPackageId(userInfo.getPackageId());
- if (userInfo.getPackageId() != 0) {
- UpgradePackage upgradePackage = upgradePackageMapper.selectById(userInfo.getPackageId());
- ticket.setPackagePrice(upgradePackage.getPrice());
- Long upgradeOrderDonSkuId = upgradePackage.getUpgradeSkuId();
- GoodsDonSkuRenewView renewSku = beanSearcher.searchFirst(GoodsDonSkuRenewView.class, MapUtils.builder().field(GoodsDonSkuRenewView::getSkuId, upgradeOrderDonSkuId).build());
- renewSku.setPackageId(userInfo.getPackageId());
- ticket.setRenewSku(renewSku);
- }
- ticket.setIsRenewPackage(userInfo.getIsRenewPackage());
- Long renewSkuId = userInfo.getRenewSkuId();
- if (renewSkuId != null) {
- GoodsDonSkuRenewView upgradeSkuRenewView = ticket.getRenewSku();
- GoodsDonSkuRenewView renewSku = beanSearcher.searchFirst(GoodsDonSkuRenewView.class, MapUtils.builder().field(GoodsDonSkuRenewView::getSkuId, renewSkuId).build());
- //续费升级是更高级规格
- if (upgradeSkuRenewView == null || (renewSku.getGptUserLimitNum() >= upgradeSkuRenewView.getGptUserLimitNum() && renewSku.getMonths() >= userInfo.getMonths())) {
- Optional.ofNullable(orderDonRenewRecordMapper.selectOne(Wrappers.lambdaQuery(OrderDonRenewRecord.class)
- .eq(OrderDonRenewRecord::getOrderId, userInfo.getRenewOrderId())
- .last("limit 1"))).ifPresent(e -> renewSku.setOriginalOrderId(e.getOrderId()));
- ticket.setRenewSku(renewSku);
- }
- }
- } catch (Exception e) {
- }
- }
- }
- public void setClaudeProInfo(RenewalView ticket) {
- //clause pro
- if (ticket.getGoodsId() == 75l && ticket.getExpiryTime() != null) {
- try {
- ClaudeUserView userInfo = claudeService.getUserInfo(ticket.getUserId(), ticket.getRelationId());
- ticket.setGptUserLimitNum(userInfo.getLimitNum());
- ticket.setGptUserLimitTime(userInfo.getLimitTime());
- ticket.setUse(userInfo.getUse());
- Long renewSkuId = userInfo.getRenewSkuId();
- if (renewSkuId != null) {
- GoodsDonSkuRenewView renewSku = beanSearcher.searchFirst(GoodsDonSkuRenewView.class, MapUtils.builder().field(GoodsDonSkuRenewView::getSkuId, renewSkuId).build());
- ticket.setRenewSku(renewSku);
- }
- } catch (Exception e) {
- }
- }
- }
- public void setLumaInfo(RenewalView ticket) {
- if (ticket.getGoodsId() == Constant.LUMA_GOODS_ID && ticket.getExpiryTime() != null) {
- try {
- List<UpgradePackage> upgradePackages = upgradePackageMapper.selectList(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + ticket.getSkuId() + "\"')").eq(UpgradePackage::getStatus, Boolean.TRUE).eq(UpgradePackage::getDeleted, Boolean.TRUE));
- if (CollectionUtil.isNotEmpty(upgradePackages)){
- upgradePackages.stream().map(e -> e.getPrice().divide(BigDecimal.valueOf(e.getNum()), 2, RoundingMode.HALF_UP)).min(BigDecimal::compareTo).ifPresent(ticket::setLumaPackagePrice);
- }
- LumaUser lumaUser = lumaService.getLumaUser(ticket.getUserId(), ticket.getRelationId());
- ticket.setLumaUserNum(lumaUser.getNum());
- } catch (Exception e) {
- }
- }
- }
- /**
- * 获取gpt镜像天卡活动参与平台ids
- * @return
- */
- private List<Long> getGptMirrorGiveawayBuyGids() {
- GptMirrorDayCardGiveawayActivity gptMirrorDayCardGiveawayActivity = beanSearcher.searchFirst(GptMirrorDayCardGiveawayActivity.class, MapUtils.builder().build());
- if (gptMirrorDayCardGiveawayActivity != null) {
- String buyGoodsIds = gptMirrorDayCardGiveawayActivity.getBuyGoodsIds();
- try {
- return Jsons.parseList(buyGoodsIds, Long.class);
- } catch (Exception e) {
- }
- }
- return null;
- }
- /**
- * 标记是否可抵扣会员车票
- */
- private void markVipDeductTicket(List<Long> userIds, RenewalView ticket, List<Long> vipSkuIds, Boolean isVip) {
- if (CollUtil.isNotEmpty(vipSkuIds) && !isVip) {
- Long skuId = ticket.getSkuId();
- if (vipSkuIds.contains(skuId)) {
- //是否时长最长
- Number number = beanSearcher.searchCount(RenewalView.class, MapUtils.builder()
- .field(RenewalView::getRelationId, ticket.getRelationId()).op(Operator.NotEqual)
- .field(RenewalView::getSkuId, skuId)
- .field(RenewalView::getUserId, userIds).op(Operator.InList)
- .field(RenewalView::getExpiryTime, ticket.getExpiryTime()).op(Operator.GreaterThan)
- .build());
- if (number.intValue() == 0) {
- ticket.setIsVipDeduct(true);
- }
- }
- }
- }
- /**
- * 检验奈飞账号状态
- */
- public GroupsRelationView checkNetflixErrorStatus(Long relationId, Long userId) {
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- NetflixUserAccountStatusRecord netflixUseAccountStatusRecord = netflixUserAccountStatusRecordMapper.selectOne(Wrappers.lambdaQuery(NetflixUserAccountStatusRecord.class)
- .eq(NetflixUserAccountStatusRecord::getRelationId, relationId)
- .eq(NetflixUserAccountStatusRecord::getDeleted, 1)
- .in(NetflixUserAccountStatusRecord::getUserId, userIdList)
- .last("limit 1"));
- if (netflixUseAccountStatusRecord == null) {
- throw BusinessRuntimeException.getInstance("请先提交账号检测");
- }
- Integer state = netflixUseAccountStatusRecord.getState();
- if (state == 0) {
- throw BusinessRuntimeException.getInstance("您的账号检测中");
- }
- //2是登录失败 3是被封号
- if (state != 3) {
- throw BusinessRuntimeException.getInstance("您的账号未封禁");
- }
- GroupsRelationView netflixRelationView = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder()
- .field(GroupsRelationView::getId, relationId)
- .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
- .field(GroupsRelationView::getGoodsId, Constant.NETFLIX_GID)
- .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
- .build());
- if (netflixRelationView == null) {
- throw BusinessRuntimeException.getInstance("奈飞车票已过期");
- }
- return netflixRelationView;
- }
- /**
- * 获取车票 并生成订单
- */
- public TicketChangeDto getTicketAndGenerateOrder(Long userId, Long skuId, Date startTime, Date expiryTime) {
- Retryer<TicketChangeDto> build = RetryerBuilder.<TicketChangeDto>newBuilder()
- .retryIfException()
- //运行时异常重试
- .retryIfRuntimeException()
- //false重试
- .retryIfResult(res -> res == null)
- //1s 间隔
- .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
- //停止策略 : 尝试请求1次
- .withStopStrategy(StopStrategies.stopAfterAttempt(1)).build();
- if (startTime == null) {
- startTime = DateTime.now();
- }
- GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
- if (expiryTime == null) {
- if (sku.getDays() != null) {
- expiryTime = DateUtil.offsetDay(startTime, sku.getDays());
- } else {
- expiryTime = DateUtil.offsetMonth(startTime, sku.getMonths());
- }
- }
- Date finalStartTime = startTime;
- Date finalExpiryTime = expiryTime;
- try {
- return build.call(() -> {
- GroupsTrips groups = groupsMapper.selectCollegeStudentUserGptTicket(skuId);
- GroupsRelation relation = null;
- if (groups != null) {
- relation = groupsRelationMapper.selectRandomOneRelationByGroupsId(groups.getId());
- }
- //生成空车队
- if (relation == null) {
- relation = groupsFuncService.createNewGroupsTrips(sku);
- }
- int update = groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
- .set(GroupsRelation::getUserId, userId)
- .set(GroupsRelation::getStartTime, finalStartTime)
- .set(GroupsRelation::getExpiryTime, finalExpiryTime)
- .set(GroupsRelation::getStatus, GroupsRelation.Status.validity)
- .eq(GroupsRelation::getId, relation.getId())
- .eq(GroupsRelation::getUserId, 0));
- if (update > 0) {
- groupsMapper.decrAvailableNum(groups.getId());
- Long relationId = relation.getId();
- //生成新的订单
- OrderDon otherOrderDon = generateNewTicketOrder(relationId, userId, sku);
- TicketChangeDto ticketChangeDto = new TicketChangeDto();
- ticketChangeDto.setOtherRelationId(relationId);
- ticketChangeDto.setOtherOrderId(otherOrderDon.getId());
- return ticketChangeDto;
- }
- log.error("发送用户:{} GPT镜像车票失败,进行重试", userId);
- return null;
- });
- } catch (ExecutionException | RetryException e) {
- log.error("发送用户GPT镜像失败:{}", StringUtil.getErrorText(e));
- return null;
- }
- }
- private OrderDon generateNewTicketOrder(Long relationId, Long userId, GoodsDonSku sku) {
- OrderDon orderDon = new OrderDon();
- orderDon.setUserId(userId);
- orderDon.setOrderNo(SEQUENCE.nextId().toString());
- orderDon.setMoney(BigDecimal.ZERO);
- orderDon.setRelationId(relationId);
- orderDon.setGoodsId(sku.getGoodsId());
- orderDon.setSkuId(sku.getId());
- orderDon.setType(OrderDon.Type.TICKET_REPLACE);
- orderDon.setStatus(OrderDon.Status.complete);
- orderDonMapper.insert(orderDon);
- return orderDon;
- }
- }
|