modify redpack send rule

master
wangning 7 years ago
parent f7774cf14f
commit 54ae201cde

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

@ -17,7 +17,11 @@
<select id="getRedPacketsByOpenid" resultType="int"> <select id="getRedPacketsByOpenid" resultType="int">
SELECT count(1) SELECT count(1)
FROM act_red_packets_customer_orders FROM act_red_packets_customer_orders
WHERE open_id = #{open_id} AND date(event_time) = date(#{event_date}) AND status != 0 AND act_id = #{act_id} WHERE open_id = #{open_id}
<if test="event_date != null">
AND date(event_time) = date(#{event_date})
</if>
AND status != 0 AND act_id = #{act_id}
</select> </select>
<select id="listOrders" resultType="com.alibaba.fastjson.JSONObject"> <select id="listOrders" resultType="com.alibaba.fastjson.JSONObject">

Loading…
Cancel
Save