admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/job/RedPackJob.java
@@ -1,89 +1,96 @@
package com.yeshi.fanli.job;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dto.user.PullNewRuleTerm;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
import com.yeshi.fanli.service.inter.redpack.RedPackWinInviteService;
import com.yeshi.fanli.util.StringUtil;
@Component
public class RedPackJob {
   @Resource
   private RedPackWinInviteService redPackWinInviteService;
   @Resource
   private RedPackConfigService redPackConfigService;
   /**
    * 红包每月26到账
    */
   @Scheduled(cron = "0 20 0 26 * ? ")
   public void arriveMoney() {
      try {
         String value = redPackConfigService.getValueByKey("win_redpack_term");
         if (StringUtil.isNullOrEmpty(value)) {
            return;
         }
         Type type = new TypeToken<ArrayList<PullNewRuleTerm>>() {
         }.getType();
         List<PullNewRuleTerm> listRule = new Gson().fromJson(value, type);
         if (listRule == null || listRule.size() == 0) {
            return;
         }
         int start = 0;
         Date date = new Date();
         while(true) {
            // 上月存在邀请的用用户id
            List<Long> list = redPackWinInviteService.listWinUid(start, 1000);
            if (list == null || list.size() == 0) {
               break;
            }
            for (Long uid: list) {
               // 统计上月邀请成功数量
               long count = redPackWinInviteService.countLastMonthByUid(uid);
               if (count == 0)
                  continue;
               BigDecimal money = null;
               for (PullNewRuleTerm term: listRule) {
                  if (count >= term.getMin() && (term.getMax() != null && count <= term.getMax())) {
                     money = term.getMoney();
                  }
               }
               if (money == null) {
                  LogHelper.test("红包奖励未匹配到规则count =" + count + " uid =" + uid);
                  continue;
               }
               try {
                  redPackWinInviteService.arriveMoney(uid, money, date);
               } catch (Exception e) {
                  LogHelper.errorDetailInfo(e);
               }
            }
         }
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
   }
}
package com.yeshi.fanli.job;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.fanli.dto.user.PullNewRuleTerm;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
import com.yeshi.fanli.service.inter.redpack.RedPackWinInviteService;
import com.yeshi.fanli.util.StringUtil;
@Component
public class RedPackJob {
   @Resource
   private RedPackWinInviteService redPackWinInviteService;
   @Resource
   private RedPackConfigService redPackConfigService;
   @XxlJob("RedPackJob-arriveMoney")
   public ReturnT<String> arriveMoney(String param) throws Exception {
      arriveMoney();
      return ReturnT.SUCCESS;
   }
   /**
    * 红包每月26到账
    */
   public void arriveMoney() {
      try {
         String value = redPackConfigService.getValueByKey("win_redpack_term");
         if (StringUtil.isNullOrEmpty(value)) {
            return;
         }
         Type type = new TypeToken<ArrayList<PullNewRuleTerm>>() {
         }.getType();
         List<PullNewRuleTerm> listRule = new Gson().fromJson(value, type);
         if (listRule == null || listRule.size() == 0) {
            return;
         }
         int start = 0;
         Date date = new Date();
         while(true) {
            // 上月存在邀请的用用户id
            List<Long> list = redPackWinInviteService.listWinUid(start, 1000);
            if (list == null || list.size() == 0) {
               break;
            }
            for (Long uid: list) {
               // 统计上月邀请成功数量
               long count = redPackWinInviteService.countLastMonthByUid(uid);
               if (count == 0)
                  continue;
               BigDecimal money = null;
               for (PullNewRuleTerm term: listRule) {
                  if (count >= term.getMin() && (term.getMax() != null && count <= term.getMax())) {
                     money = term.getMoney();
                  }
               }
               if (money == null) {
                  LogHelper.test("红包奖励未匹配到规则count =" + count + " uid =" + uid);
                  continue;
               }
               try {
                  redPackWinInviteService.arriveMoney(uid, money, date);
               } catch (Exception e) {
                  LogHelper.errorDetailInfo(e);
               }
            }
         }
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
   }
}