FengChaoYu пре 1 недеља
родитељ
комит
0ef28f468c

+ 11 - 0
mall-server-api/src/main/java/com/gree/mall/manager/controller/member/UserCompanyCreditController.java

@@ -10,6 +10,7 @@ import com.gree.mall.manager.constant.Constant;
 import com.gree.mall.manager.helper.ResponseHelper;
 import com.gree.mall.manager.logic.user.UserCompanyCreditLogic;
 import com.gree.mall.manager.plus.entity.Storage;
+import com.gree.mall.manager.schedule.UserCompanyCreditBillSchedule;
 import com.gree.mall.manager.zfire.bean.ZfireParamBean;
 import com.gree.mall.manager.zfire.util.FieldUtils;
 import io.swagger.annotations.Api;
@@ -36,6 +37,8 @@ public class UserCompanyCreditController {
     UserCompanyCreditLogic userCompanyCreditLogic;
     @Resource
     RedisLockRegistry redisLockRegistry;
+    @Resource
+    UserCompanyCreditBillSchedule userCompanyCreditBillSchedule;
 
     @ZfireList
     @PostMapping("/list")
@@ -101,4 +104,12 @@ public class UserCompanyCreditController {
             obtain.unlock();
         }
     }
+
+    @PostMapping("/test")
+    @ApiOperation(value = "测试生成账单")
+    public ResponseHelper repay(@RequestParam String inDate) {
+            userCompanyCreditBillSchedule.generateBillsOnBillingDay(inDate);
+            return ResponseHelper.success();
+
+    }
 }

+ 31 - 14
mall-server-api/src/main/java/com/gree/mall/manager/schedule/UserCompanyCreditBillSchedule.java

@@ -19,6 +19,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
@@ -38,6 +39,7 @@ import java.util.stream.Collectors;
 @ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
 @Component
 @Slf4j
+//@Service
 public class UserCompanyCreditBillSchedule {
 
     @Resource
@@ -53,30 +55,37 @@ public class UserCompanyCreditBillSchedule {
      */
     @Transactional
     @Scheduled(cron = "0 0 1 * * ?")
-    public void generateBillsOnBillingDay(){
+    public void generateBillsOnBillingDay(String inDate){
+//        DateTime curDate = DateUtil.parseDate(inDate);
         DateTime curDate = DateUtil.date();
         int currentDay = curDate.dayOfMonth();
 
-        // 处理月末特殊情况
-        if (currentDay == DateUtil.endOfMonth(curDate).dayOfMonth()) {
-            currentDay = 29;
-        }
+        final int lastDayOfMonth = DateUtil.endOfMonth(curDate).dayOfMonth();
+
+        // 直接判断:1-28号 或 实际月末(无论月末是28/29/30/31)
+        if (currentDay >= 1 && currentDay <= 28 || currentDay == lastDayOfMonth) {
+            // 处理月末特殊情况
+            if (currentDay == lastDayOfMonth) {
+                currentDay = 29;
+            }
+            // 获取账单日为今天的所有用户商户授信关系
+            List<UserCompanyCredit> userCompanyCreditList = lockQueryMapper.findByBillingDay(currentDay);
 
-        // 获取账单日为今天的所有用户商户授信关系
-        List<UserCompanyCredit> userCompanyCreditList = lockQueryMapper.findByBillingDay(currentDay);
-        
-        if (CollectionUtil.isNotEmpty(userCompanyCreditList)) {
-            for (UserCompanyCredit credit : userCompanyCreditList) {
-                generateBillForUserCompany(credit);
+            if (CollectionUtil.isNotEmpty(userCompanyCreditList)) {
+                for (UserCompanyCredit credit : userCompanyCreditList) {
+                    generateBillForUserCompany(credit, curDate);
+                }
             }
         }
     }
 
-    private void generateBillForUserCompany(UserCompanyCredit credit) {
+    private void generateBillForUserCompany(UserCompanyCredit credit, DateTime curDate) {
         final String userId = credit.getUserId();
         final String companyWechatId = credit.getCompanyWechatId();
 
-        DateTime today = DateUtil.date();
+        DateTime today = curDate;
+//         DateTime today = DateUtil.date();
+
         // 获取授信记录明细开始时间
         Date startTime;
         // 获取授信记录明细结束时间
@@ -90,6 +99,11 @@ public class UserCompanyCreditBillSchedule {
             startTime = credit.getLastBillingDate();
         }
 
+        if (startTime.getTime() > endTime.getTime()) {
+            // 如果开始时间比结束时间大就返回
+            return;
+        }
+
         // 处理月末情况
         if (credit.getBillingDay() == 29) {
             endTime = DateUtil.endOfDay(DateUtil.offsetDay(DateUtil.endOfMonth(today), -1));
@@ -132,11 +146,13 @@ public class UserCompanyCreditBillSchedule {
         // 创建账单
         UserCompanyCreditBill bill = new UserCompanyCreditBill();
         bill.setBillId(IdWorker.getIdStr())
+                .setUserCompanyCreditId(credit.getId())
                 .setUserId(credit.getUserId())
                 .setCompanyWechatId(credit.getCompanyWechatId())
                 .setCompanyWechatName(credit.getCompanyWechatName())
                 .setAmount(totalAmount)
                 .setRemainingAmount(totalAmount)
+                .setCreateDate(DateUtil.parseDate(DateUtil.formatDate(today)))
                 .setDueDate(dueDate)
                 .setIsPaid(totalAmount.compareTo(BigDecimal.ZERO) <= 0)
                 .setRemark(DateUtil.formatDateTime(startTime) + " - " + DateUtil.formatDateTime(endTime))
@@ -150,7 +166,8 @@ public class UserCompanyCreditBillSchedule {
                 .update();
 
         // 更新用户商户授信记录
-        credit.setLastBillingDate(DateUtil.offsetSecond(endTime, 1))
+        DateTime offsetSecond = DateUtil.beginOfDay(DateUtil.offsetDay(endTime, 1));
+        credit.setLastBillingDate(offsetSecond)
                 .updateById();
     }
 }