浏览代码

项目提成结算定时任务

wucl 2 年之前
父节点
当前提交
7b4f2e5852

+ 26 - 9
common/src/main/java/com/dayou/configuration/AsyncManager.java

@@ -7,12 +7,14 @@ import com.google.common.cache.Cache;
 import com.dayou.enums.CodeMsgEnumInterface;
 import com.dayou.exception.ErrorCode;
 import com.dayou.utils.LoginContext;
+import com.google.common.cache.LoadingCache;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Component;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -31,9 +33,9 @@ public class AsyncManager {
 
     private static ThreadPoolTaskExecutor threadPoolTaskExecutor;
 
-    @Autowired
-    @Qualifier("lockCache")
-    private Cache<String, Object> lockCache;
+    private static Cache<String, Object> lockCache;
+
+
 
     @Autowired
     @Qualifier("mainThreadPool")
@@ -41,6 +43,16 @@ public class AsyncManager {
         AsyncManager.threadPoolTaskExecutor = threadPoolTaskExecutor;
     }
 
+    @Autowired
+    @Qualifier("lockCache")
+    public void setLockCache(Cache<String, Object> cache) {
+        AsyncManager.lockCache = cache;
+    }
+
+    public static Cache<String, Object> getLockCache() {
+        return lockCache;
+    }
+
     public static ThreadPoolTaskExecutor getThreadPoolTaskExecutor() {
         return threadPoolTaskExecutor;
     }
@@ -215,21 +227,23 @@ public class AsyncManager {
         StringBuilder sb = new StringBuilder();
         String key = null;
         Long userId = LoginContext.getLoginCacheUserBO().getId();
-        if (userId == null) {
-            return;
-        }
         switch (asyncLockStrategy) {
             case LOCK_BIZ:
                 key = prefix + asyncBiz.getCode();
                 break;
             case LOCK_USER:
+                if (userId == null) {
+                    return;
+                }
                 key = prefix + userId;
                 break;
             case LOCK_USER_BIZ:
+                if (userId == null) {
+                    return;
+                }
                 key = prefix + userId + "_" + asyncBiz.getCode();
                 break;
         }
-
         if (lockCache.getIfPresent(key) != null) {
             ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR, asyncLockStrategy.tips);
         }
@@ -240,7 +254,9 @@ public class AsyncManager {
             @Override
             public void run() {
                 try {
-                    bizHandler.doCustom();
+                    if (bizHandler.validBefore()){
+                        bizHandler.doCustom();
+                    }
                 } catch (Exception e) {
                     log.error("异步任务异常", e);
                 } finally {
@@ -283,6 +299,7 @@ public class AsyncManager {
      * 异步业务类型
      */
     public enum AsyncBiz implements CodeMsgEnumInterface<String, String> {
+        BROKERAGE_SETTLE("BROKERAGE_SETTLE","提成结算")
         ;
 
         private String key;
@@ -352,7 +369,7 @@ public class AsyncManager {
          * 前序校验
          * 在异步任务之前
          */
-        void validBefore();
+        Boolean validBefore();
 
         Object doCustom();
     }

+ 1 - 1
dao/src/main/resources/mapper/ItemUserMapper.xml

@@ -30,7 +30,7 @@
                     iu.user_id
                 FROM
                     item_user iu
-                        LEFT JOIN USER u ON u.id = iu.user_id
+                        LEFT JOIN user u ON u.id = iu.user_id
                 WHERE
                     iu.item_id = #{itemStageUserVO.itemId}
                   AND u.deleted = 0

+ 68 - 0
service/src/main/java/com/dayou/brokerage/config/BrokerageSettleSchedule.java

@@ -0,0 +1,68 @@
+package com.dayou.brokerage.config;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.dayou.common.BaseEntity;
+import com.dayou.configuration.AsyncManager;
+import com.dayou.entity.ItemBrokerageSequence;
+import com.dayou.service.IItemBrokerageSequenceService;
+import com.dayou.utils.DateUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+
+import static com.dayou.brokerage.constants.BrokerageMsg.BROKERAGE_SEQUENCE_EMBODY;
+
+/**
+ * 类说明:
+ *
+ * @author: wucl
+ * @since: 2023/3/27
+ * created with IntelliJ IDEA.
+ */
+@Slf4j
+@Component
+public class BrokerageSettleSchedule {
+
+    @Autowired
+    private IItemBrokerageSequenceService itemBrokerageSequenceService;
+
+    @Scheduled(cron = "59 59 23 23 * ?")
+    public void doBrokerageSettle(){
+        AsyncManager.me().execute(new AsyncManager.BizHandler() {
+            @Override
+            public Boolean validBefore() {
+                Date now = new Date();
+                int year = DateUtils.getYear(now);
+                int month = DateUtils.getMonth(now);
+                String embody = BROKERAGE_SEQUENCE_EMBODY.replace("year-month", year + "-" + (month + 1));
+                int count = itemBrokerageSequenceService.count(new LambdaQueryWrapper<ItemBrokerageSequence>().eq(ItemBrokerageSequence::getEmbody, embody).eq(BaseEntity::getDeleted, Boolean.FALSE));
+                if (count!=0){
+                    log.info(embody+"提成已被结算,无需重复结算.");
+                    return Boolean.FALSE;
+                }
+                return Boolean.TRUE;
+            }
+
+            @Override
+            public Object doCustom() {
+                log.info("每月提成结算定时任务开始执行...");
+                log.info("非市场人员固定预提结算程序开始执行...");
+                if (itemBrokerageSequenceService.doNonMarketerAheadSettle()){
+                    log.info("非市场人员固定预提结算程序开始执行完成");
+                    log.info("立即执行市场人员结算程序...");
+                    if (itemBrokerageSequenceService.doAbleItemBrokerageSettle()){
+                        log.info("市场人员结算程序执行完成");
+                    }
+                }
+                return null;
+            }
+        }, AsyncManager.AsyncLockStrategy.LOCK_BIZ, AsyncManager.AsyncBiz.BROKERAGE_SETTLE);
+
+
+
+    }
+}
+