diff --git a/src/main/java/au/com/royalpay/payment/manage/redpack/core/impls/ActRedPackServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/redpack/core/impls/ActRedPackServiceImpl.java index 963cdcd66..a0bf423ee 100644 --- a/src/main/java/au/com/royalpay/payment/manage/redpack/core/impls/ActRedPackServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/redpack/core/impls/ActRedPackServiceImpl.java @@ -15,13 +15,15 @@ import au.com.royalpay.payment.tools.connections.mpsupport.beans.WechatRedpack; import au.com.royalpay.payment.tools.exceptions.ForbiddenException; import au.com.royalpay.payment.tools.exceptions.NotFoundException; import au.com.royalpay.payment.tools.utils.PageListUtils; + import com.alibaba.fastjson.JSONObject; import com.github.miemiedev.mybatis.paginator.domain.PageBounds; import com.github.miemiedev.mybatis.paginator.domain.PageList; + import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.RandomStringUtils; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.RandomUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateUtils; import org.slf4j.Logger; @@ -30,7 +32,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.Assert; -import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Collections; import java.util.Date; @@ -39,6 +40,8 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import javax.annotation.Resource; + /** * Created by wangning on 2017/8/10. */ @@ -129,37 +132,51 @@ public class ActRedPackServiceImpl implements ActRedPackService { int getTotalCounts = getRedPacketsByOpenid(open_id, new Date(), actId); int ruleMaxNum = redpackCustomerActivity.getIntValue("rule_max_num"); if (getTotalCounts >= ruleMaxNum) { - logger.error("open_id:" + open_id + "当日红包发送数量已到达" + ruleMaxNum + "!跳过"); + logger.debug("open_id:" + open_id + "已达到本次活动发送最大次数" + ruleMaxNum + "!跳过"); return; } - takeRedpack(actId,orderId, client_id, clientApply, currencyAmount, open_id, 0); + + int getDailyTotalCounts = getRedPacketsByOpenid(open_id, new Date(), actId); + int ruleMaxNumLimit = redpackCustomerActivity.getIntValue("rule_max_num_limit"); + if (getDailyTotalCounts >= ruleMaxNumLimit) { + logger.debug("open_id:" + open_id + "已达当日发送最大次数" + ruleMaxNumLimit + "!跳过"); + return; + } + + takeRedpack(actId, orderId, client_id, clientApply, currencyAmount, open_id, 0); } - private void takeRedpack(String actId,String orderId, int client_id, JSONObject clientApply, BigDecimal currencyAmount, String open_id, int tryTimes) + private void takeRedpack(String actId, String orderId, int client_id, JSONObject clientApply, BigDecimal currencyAmount, String open_id, int tryTimes) throws RedPackException { if (tryTimes > 5) { return; } + int random = (int) (Math.random() * 100); + if (random > 80) { + logger.info("未达到触发概率当前概率为:" + random); + return; + } + List prizeTypes = actPrizeTypeCustomerMapper.listAvailableTypes(actId); Collections.shuffle(prizeTypes); int totalWeight = 0; for (JSONObject prizeType : prizeTypes) { totalWeight += prizeType.getIntValue("weight"); } - int ran = RandomUtils.nextInt(0,totalWeight); + int ran = RandomUtils.nextInt(0, totalWeight); for (JSONObject prizeType : prizeTypes) { ran -= prizeType.getIntValue("weight"); if (ran < 0) { String lock = Long.toString(System.currentTimeMillis(), 36) + RandomStringUtils.random(5, true, true); - int lockedCount = actRedPacketsCustomerOrdersMapper.lockPrize(actId,lock, prizeType.getString("type_id"), open_id, client_id, orderId, currencyAmount, - new Date()); + int lockedCount = actRedPacketsCustomerOrdersMapper.lockPrize(actId, lock, prizeType.getString("type_id"), open_id, client_id, orderId, + currencyAmount, new Date()); if (lockedCount > 0) { doSendActLuckyMoney(open_id, lock, clientApply.getString("act_name"), clientApply.getString("send_name"), clientApply.getString("wishing")); break; } else { - takeRedpack(actId,orderId, client_id, clientApply, currencyAmount, open_id, tryTimes + 1); + takeRedpack(actId, orderId, client_id, clientApply, currencyAmount, open_id, tryTimes + 1); break; } } @@ -211,8 +228,8 @@ public class ActRedPackServiceImpl implements ActRedPackService { if (redpack_counts > 0) { for (int i = 0; i < redpack_counts; i++) { BigDecimal amount = amountFrom.equals(amountTo) ? amountFrom - : amountFrom.add(BigDecimal.valueOf(RandomUtils.nextInt(0,amountTo.subtract(amountFrom).multiply(CommonConsts.HUNDRED).intValue())) - .divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_DOWN)); + : amountFrom.add(BigDecimal.valueOf(RandomUtils.nextInt(0, amountTo.subtract(amountFrom).multiply(CommonConsts.HUNDRED).intValue())) + .divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_DOWN)); JSONObject prize = new JSONObject(); String prefix = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"); String red_packet_order_id = prefix + RandomStringUtils.random(28 - prefix.length(), true, true); @@ -277,7 +294,8 @@ public class ActRedPackServiceImpl implements ActRedPackService { pool.execute(() -> { JSONObject clientApply = actClientApplyMapper.findActivity(actId, redpack.getIntValue("client_id")); try { - doSendActLuckyMoney(redpack.getString("open_id"), redpack.getString("lock"), clientApply.getString("act_name"), clientApply.getString("send_name"), clientApply.getString("wishing")); + doSendActLuckyMoney(redpack.getString("open_id"), redpack.getString("lock"), clientApply.getString("act_name"), + clientApply.getString("send_name"), clientApply.getString("wishing")); } catch (RedPackException e) { e.printStackTrace(); } diff --git a/src/main/resources/au/com/royalpay/payment/manage/mappers/redpack/ActRedPacketsCustomerOrdersMapper.xml b/src/main/resources/au/com/royalpay/payment/manage/mappers/redpack/ActRedPacketsCustomerOrdersMapper.xml index d569bb1cc..49f1afa1c 100644 --- a/src/main/resources/au/com/royalpay/payment/manage/mappers/redpack/ActRedPacketsCustomerOrdersMapper.xml +++ b/src/main/resources/au/com/royalpay/payment/manage/mappers/redpack/ActRedPacketsCustomerOrdersMapper.xml @@ -17,7 +17,11 @@