【产品名称】:CRMEB多店版
【产品版本】:v2.6
【部署方式】:linux宝塔面板
【部署环境】: 线上环境
【php版本】:7.3
【Mysql版本】:5.7
【使用终端】:例:小程序/ H5 / 公众号/ PC / APP / 后台
代码位置:
/**
* 随机奖品
* @param array $data
* @return array|mixed
*/
public function getLuckPrize(array $data)
{
$prize = [];
if (!$data) return $prize;
$coupon = [];
$coupon_ids = array_unique(array_column($data, 'coupon_id'));
if ($coupon_ids) {
/** @var StoreCouponIssueServices $couponServices */
$couponServices = app()->make(StoreCouponIssueServices::class);
$coupon = $couponServices->getGiveCoupon([['id', 'IN', $coupon_ids]]);
if ($coupon) $coupon = array_combine(array_column($coupon, 'id'), $coupon);
}
$totalChance = (int)array_sum(array_column($data, 'chance'));
if (!$totalChance) return $prize;
$startChance = 0;
mt_srand();
$prizeChance = rand(0, $totalChance - 1);
$newPrize = array_combine(array_column($data, 'type'), $data);
foreach ($data as $item) {
$newStartChance = (int)bcadd((string)$item['chance'], (string)$startChance);
//随机数在这个基数端内 且该商品数量大于0 中奖
if ($prizeChance >= $startChance && $prizeChance < $newStartChance) {
//奖品权重<=0 || 随机到不是未中奖奖品-》设置了奖品数量-》数量不足时 返回未中奖奖品 || 抽到优惠券 数量不足
if ((int)$item['chance'] <= 0 ||
(
$item['type'] != 1 &&
$item['total'] != -1 &&
$item['total'] <= 0 &&
CacheService::popStock((string)$item['id'], 1, 6)
)
||
(
$item['coupon_id'] &&
$coupon &&
!isset($coupon[$item['coupon_id']])
)
) {
$prize = $newPrize[1] ?? [];
} else {
$prize = $item;
}
break;
}
$startChance = $newStartChance;
}
return $prize;
}
改成如下:
/**
* 随机奖品
* @param array $data
* @return array|mixed
*/
public function getLuckPrize(array $data)
{
$prize = [];
if (!$data) return $prize;
$coupon = [];
$coupon_ids = array_unique(array_column($data, 'coupon_id'));
if ($coupon_ids) {
/** @var StoreCouponIssueServices $couponServices */
$couponServices = app()->make(StoreCouponIssueServices::class);
$coupon = $couponServices->getGiveCoupon([['id', 'IN', $coupon_ids]]);
if ($coupon) $coupon = array_combine(array_column($coupon, 'id'), $coupon);
}
$totalChance = (int)array_sum(array_column($data, 'chance'));
if (!$totalChance) return $prize;
$startChance = 0;
mt_srand();
$prizeChance = rand(0, $totalChance - 1);
$newPrize = array_combine(array_column($data, 'type'), $data);
foreach ($data as $item) {
$newStartChance = (int)bcadd((string)$item['chance'], (string)$startChance);
//随机数在这个基数端内 且该商品数量大于0 中奖
if ($prizeChance >= $startChance && $prizeChance < $newStartChance) {
//奖品权重<=0 || 随机到不是未中奖奖品-》设置了奖品数量-》数量不足时 返回未中奖奖品 || 抽到优惠券 数量不足
if ((int)$item['chance'] <= 0 ||
(
$item['type'] != 1 &&
$item['total'] != -1 &&
$item['total'] <= 0
)
||
(
$item['coupon_id'] &&
$coupon &&
!isset($coupon[$item['coupon_id']])
)
) {
$prize = $newPrize[1] ?? [];
} else {
if (CacheService::popStock((string)$item['id'], 1, 6)){
$prize = $item;
}else{
$prize = [];
}
}
break;
}
$startChance = $newStartChance;
}
return $prize;
}
{{item.user_info.nickname ? item.user_info.nickname : item.user_name}}
作者 管理员 企业
{{itemf.name}}
{{itemc.user_info.nickname}}
{{itemc.user_name}}
回复 {{itemc.comment_user_info.nickname}}
{{itemf.name}}