Selaa lähdekoodia

fix 账号被封禁,重新分配用户车票

zoujiajian 3 vuotta sitten
vanhempi
sitoutus
572e26f0de

+ 1 - 0
netflix-common/src/main/java/com/cyksj/dto/RedisKey.java

@@ -54,5 +54,6 @@ public class RedisKey {
     public static String CHATGPT_ACCESS_TOKEN = "chatgpt:accesstoken:";
 
 
+    public static String DISABLE_ACCOUNT_ASSIGN = "disable_account_assign:";
 
 }

+ 2 - 2
netflix-dao/src/main/resources/mapper/GroupsMapper.xml

@@ -49,10 +49,10 @@
         gr.user_id != 0) <
         <choose>
             <when test="!isOverTen">
-                10
+                20
             </when>
             <otherwise>
-                5
+                10
             </otherwise>
         </choose>
         order by gt.available_num asc limit 1

+ 1 - 0
netflix-service/src/main/java/com/cyksj/redis/RedisService.java

@@ -607,6 +607,7 @@ public class RedisService {
         CPS_BACK_LOGIN_KEY("cps_back_login:", "cps商务后台登录key", 3 * 60),
         CPS_AUTH_SUCCESS_KEY("cps_auth_success_key:%s", "cps后台微信授权", 60 * 10),
         QUESTION_RESPONSE_KEY("question_response_key:%s", "问卷回答key", 60 * 60 * 24L),
+        IMPORT_ACCOUNT_CACHE_KEY("import_account_cache_key:%s", "导入缓存key", 60 * 10L),
         ;
 
         private String name;

+ 8 - 0
netflix-service/src/main/java/com/cyksj/service/mange/account/CmsAccountServiceImpl.java

@@ -19,6 +19,7 @@ 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.CmsAccountService;
 import com.cyksj.service.mange.CmsGroupService;
 import com.ejlchina.searcher.BeanSearcher;
@@ -55,6 +56,8 @@ public class CmsAccountServiceImpl extends ServiceImpl<AccountMapper, Account> i
 
 	private final CmsGroupService cmsGroupService;
 
+	private final RedisService redisService;
+
 	@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));
@@ -193,6 +196,10 @@ public class CmsAccountServiceImpl extends ServiceImpl<AccountMapper, Account> i
 		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("后台程序正在导入账号中...");
+		}
 		accountList.forEach(accountData -> {
 			Map<String, Object> map = Jsons.toMap(accountData);
 			String[] part = map.get("0").toString().split("----");
@@ -225,5 +232,6 @@ public class CmsAccountServiceImpl extends ServiceImpl<AccountMapper, Account> i
 				cmsGroupService.issuance(groupsTrips, null);
 			}
 		});
+		redisService.del(cache);
 	}
 }

+ 2 - 0
netflix-service/src/main/java/com/cyksj/service/scheduler/SchedulerService.java

@@ -8,4 +8,6 @@ package com.cyksj.service.scheduler;
  */
 public interface SchedulerService {
 	void transExpiryAccountValidRelation();
+
+	void assignUserGroupsRelation(Long groupsId);
 }

+ 128 - 95
netflix-service/src/main/java/com/cyksj/service/scheduler/impl/SchedulerServiceImpl.java

@@ -6,10 +6,12 @@ import cn.hutool.core.date.DateUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.constant.Constant;
 import com.cyksj.common.util.StringUtil;
+import com.cyksj.dto.RedisKey;
 import com.cyksj.mapper.*;
 import com.cyksj.model.entity.*;
 import com.cyksj.model.manage.views.GroupsRelationView;
 import com.cyksj.model.request.ChangeRelationReq;
+import com.cyksj.redis.RedisService;
 import com.cyksj.service.error.UserRelationChangeErrorRecordService;
 import com.cyksj.service.groups.GroupsFuncService;
 import com.cyksj.service.mange.CmsOrderDonService;
@@ -61,6 +63,8 @@ public class SchedulerServiceImpl implements SchedulerService {
 
 	private final UserRelationChangeErrorRecordMapper userRelationChangeErrorRecordMapper;
 
+	private final RedisService redisService;
+
 	@Override
 	public void transExpiryAccountValidRelation() {
 		DateTime now = new DateTime();
@@ -81,120 +85,149 @@ public class SchedulerServiceImpl implements SchedulerService {
 					.field(GroupsRelationView::getStatus, GroupsRelation.Status.validity.name())
 					.field(GroupsRelationView::getExpiryTime, nextBeginDay).op(Operator.GreaterEqual)
 					.build());
-			log.info("迁移groupsId:{}未过期用户数量:{}", g_groupsId, groupsRelations.size());
-			groupsRelations.forEach(relationView -> {
-				Long change_relationId = relationView.getId();
-				Long userId = relationView.getUserId();
-				UserRelationChangeErrorRecord exists = userRelationChangeErrorRecordMapper.selectOne(Wrappers.lambdaQuery(UserRelationChangeErrorRecord.class)
-						.eq(UserRelationChangeErrorRecord::getRelationId, change_relationId)
-						.eq(UserRelationChangeErrorRecord::getUserId, userId)
-						.eq(UserRelationChangeErrorRecord::getDeleted, true)
-						.last("limit 1"));
-				if (exists != null) {
-					log.info("存在userId:{}无法转移的车票relationId:{}", userId, change_relationId);
-					return;
-				}
-				Date expiryTime = relationView.getExpiryTime();
-				Long skuId = relationView.getSkuId();
-				//仅针对chatGPT
-				if (relationView.getGoodsId() == 18) {
-					Long bet_day = DateUtil.between(now, expiryTime, DateUnit.DAY);
-					//10天以内
-					if (bet_day <= 10) {
-						//分配到过期账号5天内误差的相同规格车队里
-						DateTime five_day = DateUtil.offsetDay(expiryTime, 5);
-						Long item_groupsId = groupsMapper.selectSameSpecItemGroupsByTime(g_groupsId, skuId, expiryTime, five_day, false);
-						if (item_groupsId == null) {
-							//无对应车队 退款
-							//退款至余额
-							OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
-									.eq(OrderDon::getRelationId, change_relationId)
+			reAssign(g_groupsId, now, groupsRelations);
+		});
+	}
+
+	@Override
+	public void assignUserGroupsRelation(Long groupsId) {
+		GroupsTrips groupsTrips = groupsMapper.selectById(groupsId);
+		if (groupsTrips == null || groupsTrips.getAccountId() == null) return;
+		String key = RedisKey.DISABLE_ACCOUNT_ASSIGN + groupsId;
+		if (!redisService.setNx(key, groupsId, 60 * 3l)) {
+			return;
+		}
+		if (groupsTrips.getStatus() != GroupsTrips.Status.down) {
+			groupsTrips.setStatus(GroupsTrips.Status.down);
+			groupsMapper.updateById(groupsTrips);
+		}
+		DateTime now = DateTime.now();
+		//迁移用户
+		List<GroupsRelationView> groupsRelations = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
+				.field(GroupsRelationView::getGroupsId, groupsId)
+				.field(GroupsRelationView::getStatus, List.of(GroupsRelation.Status.validity.name(), GroupsRelation.Status.outside.name())).op(Operator.InList)
+				.field(GroupsRelationView::getExpiryTime, now).op(Operator.GreaterEqual)
+				.build());
+		reAssign(groupsId, now, groupsRelations);
+		redisService.del(key);
+	}
+
+	/**
+	 * 重新分配车位
+	 */
+	public void reAssign(Long g_groupsId, DateTime now, List<GroupsRelationView> groupsRelations) {
+		log.info("迁移groupsId:{}未过期用户数量:{}", g_groupsId, groupsRelations.size());
+		groupsRelations.forEach(relationView -> {
+			Long change_relationId = relationView.getId();
+			Long userId = relationView.getUserId();
+			UserRelationChangeErrorRecord exists = userRelationChangeErrorRecordMapper.selectOne(Wrappers.lambdaQuery(UserRelationChangeErrorRecord.class)
+					.eq(UserRelationChangeErrorRecord::getRelationId, change_relationId)
+					.eq(UserRelationChangeErrorRecord::getUserId, userId)
+					.eq(UserRelationChangeErrorRecord::getDeleted, true)
+					.last("limit 1"));
+			if (exists != null) {
+				log.info("存在userId:{}无法转移的车票relationId:{}", userId, change_relationId);
+				return;
+			}
+			Date expiryTime = relationView.getExpiryTime();
+			Long skuId = relationView.getSkuId();
+			//仅针对chatGPT
+			if (relationView.getGoodsId() == 18) {
+				Long bet_day = DateUtil.between(now, expiryTime, DateUnit.DAY);
+				//10天以内
+				if (bet_day <= 10) {
+					//分配到过期账号5天内误差的相同规格车队里
+					DateTime five_day = DateUtil.offsetDay(expiryTime, 5);
+					Long item_groupsId = groupsMapper.selectSameSpecItemGroupsByTime(g_groupsId, skuId, expiryTime, five_day, false);
+					if (item_groupsId == null) {
+						//无对应车队 退款
+						//退款至余额
+						OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
+								.eq(OrderDon::getRelationId, change_relationId)
+								.eq(OrderDon::getUserId, userId)
+								.notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
+								.orderByDesc(OrderDon::getId)
+								.last("limit 1"));
+						if (orderDon == null) {
+							//更换过车票
+							orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
+									.eq(OrderDon::getSkuId, skuId)
 									.eq(OrderDon::getUserId, userId)
 									.notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
 									.orderByDesc(OrderDon::getId)
 									.last("limit 1"));
-							if (orderDon == null) {
-								//更换过车票
-								orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
-										.eq(OrderDon::getSkuId, skuId)
-										.eq(OrderDon::getUserId, userId)
-										.notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
-										.orderByDesc(OrderDon::getId)
-										.last("limit 1"));
-							}
-							if (orderDon == null) {
-								log.error("用户userId:{}的车票relationId:{}账号过期后转移错误", userId, change_relationId);
-								changeErrorRecordService.changeTicketErrorRecord(relationView, UserRelationChangeErrorRecord.Source.change);
-								return;
-							}
-							//清除车票
-							GroupsRelation relation = groupsRelationMapper.selectById(change_relationId);
-							groupRelationClearService.clearTicket(relation, UserTicketClearedRecord.Source.timing_change_ticket);
-							//退款至余额
-							//实付金额 + 余额
-							BigDecimal money = orderDon.getMoney().add(orderDon.getBalance());
-							//当月天数
-							Date payTime = orderDon.getPayTime();
-							if (payTime == null) {
-								payTime = orderDon.getCreatedTime();
-							}
-							if (money.compareTo(BigDecimal.ZERO) == 0) {
-								GoodsDonSku sku = skuMapper.selectById(skuId);
-								if (sku != null) {
-									money = sku.getPrice();
-								}
-							}
-							int dayNum = DateUtil.dayOfMonth(DateUtil.endOfMonth(payTime));
-							BigDecimal re_balance = money.divide(BigDecimal.valueOf(dayNum), 0, RoundingMode.DOWN).multiply(BigDecimal.valueOf(bet_day));
-							if (re_balance.compareTo(BigDecimal.ZERO) <= 0) {
-								log.info("user_id:{},relationId:{}返回余额为0", userId, change_relationId);
-								return;
-							}
-							userBenefitsService.addUserBalance(userId, re_balance);
-							log.info("车票未到期清除,退款至用户user_id:{}余额:{}成功", userId, re_balance);
-							userBenefitsService.recordBalanceBySource(userId, re_balance, UserBalanceSourceRecord.Source.clear_valid);
+						}
+						if (orderDon == null) {
+							log.error("用户userId:{}的车票relationId:{}账号过期后转移错误", userId, change_relationId);
+							changeErrorRecordService.changeTicketErrorRecord(relationView, UserRelationChangeErrorRecord.Source.change);
 							return;
 						}
-						changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.timing_change_ticket);
-						return;
-					}
-					//大于10天
-					//分配到过期账号10天内误差的相同规格车队里
-					DateTime ten_day = DateUtil.offsetDay(expiryTime, 10);
-					Long item_groupsId = groupsMapper.selectSameSpecItemGroupsByTime(g_groupsId, skuId, expiryTime, ten_day, true);
-					if (item_groupsId == null) {
-						//无对应车队 生成空车队
-						//寻找空车队
-						item_groupsId = groupsMapper.selectSameSpecEmptyGroups(skuId);
-						if (item_groupsId == null) {
-							//新增空车队
+						//清除车票
+						GroupsRelation relation = groupsRelationMapper.selectById(change_relationId);
+						groupRelationClearService.clearTicket(relation, UserTicketClearedRecord.Source.timing_change_ticket);
+						//退款至余额
+						//实付金额 + 余额
+						BigDecimal money = orderDon.getMoney().add(orderDon.getBalance());
+						//当月天数
+						Date payTime = orderDon.getPayTime();
+						if (payTime == null) {
+							payTime = orderDon.getCreatedTime();
+						}
+						if (money.compareTo(BigDecimal.ZERO) == 0) {
 							GoodsDonSku sku = skuMapper.selectById(skuId);
-							GroupsRelation new_relation = groupsFuncService.createNewGroupsTrips(sku);
-							item_groupsId = new_relation.getGroupsId();
+							if (sku != null) {
+								money = sku.getPrice();
+							}
+						}
+						int dayNum = DateUtil.dayOfMonth(DateUtil.endOfMonth(payTime));
+						BigDecimal re_balance = money.divide(BigDecimal.valueOf(dayNum), 0, RoundingMode.DOWN).multiply(BigDecimal.valueOf(bet_day));
+						if (re_balance.compareTo(BigDecimal.ZERO) <= 0) {
+							log.info("user_id:{},relationId:{}返回余额为0", userId, change_relationId);
+							return;
 						}
-						//新车队
-						changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.new_groups_ticket);
+						userBenefitsService.addUserBalance(userId, re_balance);
+						log.info("车票未到期清除,退款至用户user_id:{}余额:{}成功", userId, re_balance);
+						userBenefitsService.recordBalanceBySource(userId, re_balance, UserBalanceSourceRecord.Source.clear_valid);
 						return;
 					}
 					changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.timing_change_ticket);
 					return;
 				}
-				if (relationView.getGoodsId() == 26) {
-					//移入合适车队
-					Long item_groupsId = groupsMapper.selectMidJourneySameSpecItemGroups(g_groupsId, skuId, expiryTime);
+				//大于10天
+				//分配到过期账号10天内误差的相同规格车队里
+				DateTime ten_day = DateUtil.offsetDay(expiryTime, 10);
+				Long item_groupsId = groupsMapper.selectSameSpecItemGroupsByTime(g_groupsId, skuId, expiryTime, ten_day, true);
+				if (item_groupsId == null) {
+					//无对应车队 生成空车队
+					//寻找空车队
+					item_groupsId = groupsMapper.selectSameSpecEmptyGroups(skuId);
 					if (item_groupsId == null) {
-						//midJourney 无合适车队
-						log.error("midJourney账号过期groupsId:{},用户车票relation_id:{}", g_groupsId, change_relationId);
-						changeErrorRecordService.changeTicketErrorRecord(relationView, UserRelationChangeErrorRecord.Source.change);
-						return;
+						//新增空车队
+						GoodsDonSku sku = skuMapper.selectById(skuId);
+						GroupsRelation new_relation = groupsFuncService.createNewGroupsTrips(sku);
+						item_groupsId = new_relation.getGroupsId();
 					}
-					changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.timing_change_ticket);
+					//新车队
+					changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.new_groups_ticket);
+					return;
+				}
+				changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.timing_change_ticket);
+				return;
+			}
+			if (relationView.getGoodsId() == 26) {
+				//移入合适车队
+				Long item_groupsId = groupsMapper.selectMidJourneySameSpecItemGroups(g_groupsId, skuId, expiryTime);
+				if (item_groupsId == null) {
+					//midJourney 无合适车队
+					log.error("midJourney账号过期groupsId:{},用户车票relation_id:{}", g_groupsId, change_relationId);
+					changeErrorRecordService.changeTicketErrorRecord(relationView, UserRelationChangeErrorRecord.Source.change);
+					return;
 				}
-			});
+				changeTickerRelation(item_groupsId, relationView, UserTicketClearedRecord.Source.timing_change_ticket);
+			}
 		});
 	}
 
-
 	public void changeTickerRelation(Long item_groupsId, GroupsRelationView relationView, UserTicketClearedRecord.Source source) {
 		Long change_relationId = relationView.getId();
 		try {

+ 14 - 3
netflix-web/src/main/java/com/cyksj/web/controller/manage/group/CmsGroupRelationController.java

@@ -1,17 +1,17 @@
 package com.cyksj.web.controller.manage.group;
 
+import com.cyksj.common.annotation.NoSubmit;
 import com.cyksj.dto.Result;
 import com.cyksj.enums.GatewayResponse;
 import com.cyksj.model.entity.GroupsRelation;
 import com.cyksj.model.manage.views.GroupsRelationView;
+import com.cyksj.service.scheduler.SchedulerService;
 import com.ejlchina.searcher.BeanSearcher;
 import com.ejlchina.searcher.param.Operator;
 import com.ejlchina.searcher.util.MapUtils;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.List;
@@ -30,6 +30,8 @@ public class CmsGroupRelationController {
 
     private final HttpServletRequest request;
 
+    private final SchedulerService schedulerService;
+
     @GetMapping("/get")
     public Result<List<GroupsRelationView>> get(){
         List<GroupsRelationView> search = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.flatBuilder(request.getParameterMap())
@@ -38,4 +40,13 @@ public class CmsGroupRelationController {
         return GatewayResponse.SUCCESS.newBuilder().toResult(search);
     }
 
+    /**
+     * 账号被封,重新分配用户车票
+     */
+    @PutMapping("/disable/{groupsId}")
+    @NoSubmit
+    public Result<String> disable(@PathVariable Long groupsId) {
+        schedulerService.assignUserGroupsRelation(groupsId);
+        return GatewayResponse.SUCCESS.newBuilder().toResult("车队账号禁用成功");
+    }
 }