From af8cd7db30e347bf4a9d4753e352abe9106bdb09 Mon Sep 17 00:00:00 2001 From: puhanshu <a9236326> Date: 星期二, 11 一月 2022 18:06:54 +0800 Subject: [PATCH] 商业街1/11代码提交 --- springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsGameServiceImpl.java | 138 +++++++++++++++++++++++++++++++++++----------- 1 files changed, 105 insertions(+), 33 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsGameServiceImpl.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsGameServiceImpl.java index 7af4dd0..336ecf9 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsGameServiceImpl.java +++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsGameServiceImpl.java @@ -4,24 +4,20 @@ import static java.util.Objects.nonNull; import static org.apache.commons.lang3.StringUtils.isNotEmpty; +import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Random; import javax.annotation.Resource; -import com.alibaba.fastjson.JSONObject; import com.panzhihua.common.model.vos.community.microCommercialStreet.McsCouponVO; -import com.panzhihua.common.model.vos.community.microCommercialStreet.McsEvaluateVO; -import com.panzhihua.common.model.vos.community.microCommercialStreet.McsMerchantVO; -import com.panzhihua.common.utlis.QRCodeUtils; import com.panzhihua.common.utlis.Snowflake; import com.panzhihua.service_community.dao.McsEvaluateDAO; import com.panzhihua.service_community.entity.McsCoupon; -import com.panzhihua.service_community.entity.McsEvaluate; import com.panzhihua.service_community.util.NearbyUtil; import com.panzhihua.service_community.util.QRCodeUtil; import com.spatial4j.core.shape.Rectangle; -import org.apache.commons.lang3.RandomUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; @@ -379,6 +375,11 @@ Integer allocation = mcsGame.getAllocation(); if (allocation.equals(1)) { //随机分配 + Integer surplusCoins = mcsGame.getSurplusCoins(); + Integer coin = getRandomCoins(surplusCoins, mcsGame.getSurplusCoupons()); + mcsCoupon.setCoin(coin); + mcsCoupon.setAward("戳戳币:" + coin); + mcsGame.setSurplusCoins(surplusCoins - coin); } else { //固定值 mcsCoupon.setCoin(mcsGame.getCoins()); @@ -448,35 +449,106 @@ } public static void main(String[] args) { - int total = 300; + int total = 500; int people = 10; - dispath(total, people, 30); - - } - - public static void dispath(int total, int people, int min) { - for (int i = 0; i < people - 1; i++) { - int leftPeople = people - i; - - double avg = Double.valueOf(total) / leftPeople; - - double ratio1 = RandomUtils.nextInt(0, 3); - - double ratio2 = RandomUtils.nextInt(0, 1); - - double ratio = ratio1 + ratio2; - - int cur = (int) Math.floor(ratio * avg) > min ? (int) Math.floor(ratio * avg) : min; - - // 扣减总额阅点数目 - - total = total - cur; - - System.out.format("第 %d 个红包: %d 阅点,剩下: %d 阅点\n", i + 1, cur,total); + for (int i = 1;i <= 10; i++) { + Integer decrease = getRandomCoins(total, people); + total -= decrease; + people --; + System.out.println("第:" + i + "个人领取到:" + decrease + "戳戳币,总数还剩下:" + total); } - // 剩余的就是最后一个用户的阅点额度 - - System.out.format("第 %d 个红包: %d 阅点,剩下: 0 阅点\n", people, total); } + public static Integer getRandomCoins(int totalCoins, int peopleTotal) { + List<Integer> list = new ArrayList<>(peopleTotal); + if (peopleTotal == 1) { + return totalCoins; + } + Random random = new Random(); + // 平均分配 + int average = totalCoins / peopleTotal; + int max = (int) Math.floor(totalCoins * 0.6); + int min = (int) Math.floor(average * 0.5); + float thresh = 0.95F; + int rest = totalCoins - average * peopleTotal; + for (int i = 0; i < peopleTotal; i++) { + if (i < rest) { + list.add(average + 1); + } else { + list.add(average); + } + } + // 根据阀值进行数据随机处理 + int randOfRange = 0; + int randRom = 0; + int nextIndex = 0; + int nextValue = 0; + //多余 + int surplus = 0; + //缺少 + int lack = 0; + for (int i = 0; i < peopleTotal - 1; i++) { + nextIndex = i + 1; + int itemThis = list.get(i); + int itemNext = list.get(nextIndex); + boolean isLt = itemThis < itemNext; + int rangeThis = isLt ? max - itemThis : itemThis - min; + int rangeNext = isLt ? itemNext - min : max - itemNext; + int rangeFinal = (int) Math.ceil(thresh * (Math.min(rangeThis, rangeNext) + 1)); + randOfRange = random.nextInt(rangeFinal); + randRom = isLt ? 1 : -1; + int iValue = list.get(i) + randRom * randOfRange; + nextValue = list.get(nextIndex) + randRom * randOfRange * -1; + if (iValue > max) { + surplus += (iValue - max); + list.set(i, max); + } else if (iValue < min) { + list.set(i, min); + lack += (min - iValue); + } else { + list.set(i, iValue); + } + list.set(nextIndex, nextValue); + } + if (nextValue > max) { + surplus += (nextValue - max); + list.set(nextIndex, max); + } + if (nextValue < min) { + lack += (min - nextValue); + list.set(nextIndex, min); + } + if (surplus - lack > 0) { + //分少了 给低于max的凑到max + for (int i = 0; i < list.size(); i++) { + int value = list.get(i); + if (value < max) { + int tmp = max - value; + if (surplus >= tmp) { + surplus -= tmp; + list.set(i, max); + } else { + list.set(i, value + surplus); + return list.get(random.nextInt(list.size() - 1)); + } + } + } + } else if (lack - surplus > 0) { + //分多了 给超过高于min的人凑到min + for (int i = 0; i < list.size(); i++) { + int value = list.get(i); + if (value > min) { + int tmp = value - min; + if (lack >= tmp) { + lack -= tmp; + list.set(i, min); + } else { + list.set(i, min + tmp - lack); + return list.get(random.nextInt(list.size() - 1)); + } + } + } + } + return list.get(random.nextInt(list.size() - 1)); + } } -- Gitblit v1.7.1