Quellcode durchsuchen

fix 定时清除过期子账号

zoujiajian vor 3 Jahren
Ursprung
Commit
c798034a78

+ 2 - 0
netflix-dao/src/main/java/com/cyksj/mapper/GroupsRelationMapper.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.cyksj.model.entity.GroupsRelation;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -14,4 +15,5 @@ public interface GroupsRelationMapper extends BaseMapper<GroupsRelation> {
 
     List<Long> getAccountIdsByUserIdOrNickNameOrSubmitAccount(@Param("userId") Long userId, @Param("nickName") String nickName, @Param("submitAccount") String submitAccount);
 
+    Integer updateExpiryTime(@Param("entity") GroupsRelation relation, @Param("expireTime") Date expireTime);
 }

+ 0 - 5
netflix-dao/src/main/java/com/cyksj/model/views/BackgroundDataView.java

@@ -44,11 +44,6 @@ public class BackgroundDataView {
 	 */
 	private Integer totalExpiredAccountNum;
 
-	/**
-	 * 到期子账号总数
-	 */
-	private Integer totalNumOfExpiredSubAccount;
-
 	/**
 	 * apple one数据
 	 */

+ 5 - 0
netflix-dao/src/main/java/com/cyksj/model/views/ExpiredAccountDataView.java

@@ -29,6 +29,11 @@ public class ExpiredAccountDataView {
 	 */
 	private String account;
 
+	/**
+	 * 主账号密码
+	 */
+	private String password;
+
 	/**
 	 * 类型
 	 */

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

@@ -6,6 +6,7 @@
         select gr.id as relationId,
                a.title as platform,
                a.account,
+               a.password,
                a.us_account,
                a.cn_account,
                concat(gdk.spec_val,'¥',cast(gdk.price/100 as decimal(10,2))) as 'type',
@@ -49,7 +50,7 @@
             <if test="endTime != null">
                 and gr.expiry_time &lt; #{endTime}
             </if>
-                and gr.expiry_time &lt; #{now}
+                and gr.expiry_time > #{now}
                 order by expiry_time
         </where>
     </select>
@@ -72,6 +73,6 @@
         <foreach collection="noOrderStatus" item="item" open="(" separator="," close=")">
             #{item}
         </foreach>
-        and od.pay_time between #{yesZeroDate} and #{thisZeroDate}
+        and od.created_time between #{yesZeroDate} and #{thisZeroDate}
     </select>
 </mapper>

+ 12 - 0
netflix-dao/src/main/resources/mapper/GroupRelationMapper.xml

@@ -1,6 +1,18 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.cyksj.mapper.GroupsRelationMapper">
+
+    <update id="updateExpiryTime">
+        update groups_relation
+        set start_time   = null,
+            expiry_time  = null,
+            status       = #{entity.status},
+            renew_status = #{entity.renewStatus},
+            is_handle    = #{entity.isHandle},
+            user_id      = #{entity.userId}
+        where id = #{entity.id} and expiry_time = #{expireTime}
+    </update>
+
     <select id="getAccountIdsByUserIdOrNickNameOrSubmitAccount" resultType="java.lang.Long">
         select gr.groups_id
         from groups_relation gr,

+ 1 - 1
netflix-dao/src/main/resources/mapper/OrderDonMapper.xml

@@ -28,7 +28,7 @@
         <foreach collection="noOrderStatus" item="item" open="(" separator="," close=")">
             #{item}
         </foreach>
-        and pay_time between #{yesZeroDate} and #{thisZeroDate})s
+        and created_time between #{yesZeroDate} and #{thisZeroDate})s
         inner join `groups_relation` gr
         on gr.user_id = s.user_id
         inner join `groups_trips` gt

+ 0 - 3
netflix-service/src/main/java/com/cyksj/service/mange/statistics/impl/StatisticsDataServiceImpl.java

@@ -163,8 +163,5 @@ public class StatisticsDataServiceImpl implements StatisticsDataService {
 		//已到期主账号
 		List<Account> expiredAccounts = Optional.ofNullable(accountService.list(Wrappers.lambdaQuery(Account.class).le(Account::getExpiryTime, now))).orElse(new ArrayList<>());
 		backgroundDataView.setTotalExpiredAccountNum(expiredAccounts.size());
-		//已到期子账号
-		List<GroupsRelation> expiredSubAccounts = Optional.ofNullable(groupsRelationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).le(GroupsRelation::getExpiryTime, now))).orElse(new ArrayList<>());
-		backgroundDataView.setTotalNumOfExpiredSubAccount(expiredSubAccounts.size());
 	}
 }

+ 30 - 4
netflix-service/src/main/java/com/cyksj/task/Scheduler.java

@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateField;
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.common.util.Jsons;
 import com.cyksj.mapper.*;
 import com.cyksj.model.dto.WxMpTemplateData;
@@ -15,6 +16,7 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.Date;
 import java.util.List;
@@ -46,11 +48,16 @@ public class Scheduler {
 
 
     /**
-     * 统计前一天数据
+     * 每5分钟清理过期账号
      */
-    //@Scheduled(cron = "0 0 0 * * ?")
-    public void statics() {
-
+    @Scheduled(cron = "0 0/5 * * * ?")
+    @Transactional(rollbackFor = Throwable.class)
+    public void clearExpiry() {
+        DateTime now = new DateTime();
+        List<GroupsRelation> expiryList = groupsRelationMapper.selectList(Wrappers.lambdaQuery(GroupsRelation.class).le(GroupsRelation::getExpiryTime, now));
+        log.info("定时清理过期账号start ===> 数量:{}", expiryList.size());
+        //过期账号清楚有效期
+        expiryList.forEach(relation -> delRelation(relation));
     }
 
     @Scheduled(cron = "0 0 9 * * ?")
@@ -111,6 +118,25 @@ public class Scheduler {
 
     }
 
+    private void delRelation(GroupsRelation relation) {
+        Date expireTime = relation.getExpiryTime();
+        relation.setStartTime(null);
+        relation.setExpiryTime(null);
+        relation.setUserId(0l);
+        relation.setStatus(GroupsRelation.Status.none);
+        relation.setIsHandle(false);
+        relation.setRenewStatus(false);
+        Integer success = groupsRelationMapper.updateExpiryTime(relation, expireTime);
+        if (success == 1) {
+            log.info("开始清除过期子账号,座位号:{}", relation.getId());
+            int count = groupsMapper.incrAvailableNum(relation.getGroupsId());
+            if (count == 0) {
+                log.error("车位异常 groupId:{}", relation.getGroupsId());
+                throw new BusinessRuntimeException("车位异常");
+            }
+        }
+    }
+
 
     /**
      * 定时关闭订单

+ 15 - 11
netflix-web/src/main/java/com/cyksj/web/controller/group/GroupRelationController.java

@@ -1,11 +1,11 @@
 package com.cyksj.web.controller.group;
 
+import cn.hutool.core.date.DateTime;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.dto.Result;
 import com.cyksj.enums.GatewayResponse;
 import com.cyksj.mapper.GroupsRelationMapper;
-import com.cyksj.model.entity.BaseEntity;
 import com.cyksj.model.entity.GroupsRelation;
 import com.cyksj.model.entity.OrderDon;
 import com.cyksj.model.request.OrderPayRequest;
@@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author chan
@@ -45,16 +46,19 @@ public class GroupRelationController {
                 .field(RenewalView::getStatus, List.of("validity", "overdue")).op(Operator.InList)
                 .orderBy(RenewalView::getExpiryTime).asc()
                 .build());
-        //过滤邀请制
-        list.forEach((renewalView) -> {
-                    if (renewalView.getSecondType() == 1) {
-                        renewalView.setAccount(null);
-                        renewalView.setPassword(null);
-                    }
-
-                }
-        );
-        return GatewayResponse.SUCCESS.newBuilder().toResult(list);
+        //过滤邀请制、已失效的车位
+        DateTime now = DateTime.now();
+        List<RenewalView> collect = list.stream().filter(ticket -> {
+            if (ticket.getExpiryTime() != null && ticket.getExpiryTime().before(now)) {
+                return false;
+            }
+            if (ticket.getSecondType() == 1) {
+                ticket.setAccount(null);
+                ticket.setPassword(null);
+            }
+            return true;
+        }).collect(Collectors.toList());
+        return GatewayResponse.SUCCESS.newBuilder().toResult(collect);
     }
 
     @PostMapping("/post/renewal")

+ 1 - 1
netflix-web/src/main/java/com/cyksj/web/controller/manage/statistics/StatisticsDataController.java

@@ -34,7 +34,7 @@ public class StatisticsDataController {
 	}
 
 	/**
-	 * 到期账号数列表
+	 * 有效子账号数列表
 	 */
 	@GetMapping("/expired/account")
 	public Result<IPage<ExpiredAccountDataView>> getExpiredAccountView(Integer goodsId, String account, Boolean renewStatus, Boolean handleStatus, String cnAccount, String usAccount, Integer userId, Long startTime, Long endTime, @RequestParam(required = false, defaultValue = "1") Long start, @RequestParam(required = false, defaultValue = "10") Long limit) {