You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
5.1 KiB
150 lines
5.1 KiB
<?php
|
|
|
|
namespace app\logic;
|
|
use app\model\ZoneGoodsParam;
|
|
use app\model\ZoneOrder;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 专区逻辑层
|
|
*/
|
|
class Zone
|
|
{
|
|
|
|
/**
|
|
* 余额扣减后 生成订单
|
|
* @param $user_id
|
|
* @param $zone_goods_id
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public static function createOrder($user_id,$zone_goods_id)
|
|
{
|
|
# 获取奖项个参数
|
|
$zoneParamArr = ZoneGoodsParam::getList(['zone_goods_id' => $zone_goods_id]);
|
|
|
|
$data = self::getWinningPrize2($zoneParamArr);
|
|
|
|
# 记录订单
|
|
$zone_order_id = ZoneOrder::saveOrder($user_id,$zone_goods_id,$data);
|
|
|
|
return [
|
|
'data' => $data,
|
|
'zone_order_id' => $zone_order_id
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 权重算法
|
|
* @param $patterns
|
|
* @return array
|
|
*/
|
|
protected static function getWinningPrize($patterns)
|
|
{
|
|
|
|
$temp = [
|
|
['image' => 'test.png', 'weight' => 1], // 奖项图案,权重为5
|
|
['image' => 'prize2.png', 'weight' => 1], // 奖项图案,权重为3
|
|
['image' => 'no_prize1.png', 'weight' => 1], // 非奖项图案,权重为10
|
|
['image' => 'no_prize2.png', 'weight' => 1], // 非奖项图案,权重为8
|
|
['image' => 'no_prize6.png', 'weight' => 1], // 非奖项图案,权重为8
|
|
['image' => 'no_prize7.png', 'weight' => 1], // 非奖项图案,权重为8
|
|
['image' => 'testtest.png', 'weight' => 0.01], // 非奖项图案,权重为8
|
|
];
|
|
$patterns = array_merge($patterns,$temp);
|
|
|
|
// 所有图案的权重相加
|
|
$totalWeight = 0;
|
|
foreach ($patterns as $pattern) {
|
|
$totalWeight += $pattern['weight'];
|
|
}
|
|
|
|
|
|
$imageArr = [];
|
|
$numberOfGroups = 3; // 要生成的图案组数
|
|
$groupsNum = 4; // 要生成的图案组数
|
|
|
|
for ($i = 0; $i < $numberOfGroups; $i++) {
|
|
|
|
// 每组生成 3 个图案
|
|
for ($j = 0; $j < $groupsNum; $j++) {
|
|
|
|
// 输出选中图案
|
|
$selectedPattern = null;
|
|
|
|
// 生成随机数
|
|
$randomNumber = rand(1, $totalWeight);
|
|
|
|
foreach ($patterns as $pattern) {
|
|
$randomNumber -= $pattern['weight'];
|
|
if ($randomNumber <= 0) {
|
|
$selectedPattern = $pattern;
|
|
break;
|
|
}
|
|
}
|
|
|
|
$imageArr[$i][] = $selectedPattern['image'];
|
|
}
|
|
}
|
|
return $imageArr;
|
|
}
|
|
|
|
/**
|
|
* 概率算法
|
|
* @return array
|
|
*/
|
|
protected static function getWinningPrize2($patterns)
|
|
{
|
|
// $patterns = [
|
|
// ['image' => 'prize1.png', 'probability' => 0.3], // 奖项图案,概率为30%
|
|
// ['image' => 'prize2.png', 'probability' => 0.2], // 奖项图案,概率为20%
|
|
// ['image' => 'no_prize1.png', 'probability' => 0.1], // 非奖项图案,概率为10%
|
|
// ['image' => 'no_prize2.png', 'probability' => 0.32], // 非奖项图案,概率为32%
|
|
// ['image' => 'test1.png', 'probability' => 0.07], // 非奖项图案,概率为7%
|
|
// ['image' => 'test2.png', 'probability' => 0.002], // 非奖项图案,概率为0.2%
|
|
// ['image' => 'test3.png', 'probability' => 0.003], // 非奖项图案,概率为0.3%
|
|
// ['image' => 'test4.png', 'probability' => 0.004], // 非奖项图案,概率为0.4%
|
|
// ['image' => 'test5.png', 'probability' => 0.001], // 非奖项图案,概率为0.1%
|
|
// ];
|
|
|
|
// 计算概率总和
|
|
$totalProbability = 0;
|
|
foreach ($patterns as $pattern) {
|
|
$totalProbability += $pattern['probability'];
|
|
}
|
|
|
|
// 归一化处理,并计算累积概率
|
|
$accumulatedProbability = 0;
|
|
foreach ($patterns as &$pattern) {
|
|
$pattern['probability'] /= $totalProbability; // 归一化处理
|
|
$accumulatedProbability += $pattern['probability'];
|
|
$pattern['accumulatedProbability'] = $accumulatedProbability; // 累积概率
|
|
}
|
|
unset($pattern);
|
|
|
|
$numberOfGroups = 3; // 要生成的图案组数
|
|
$groupNum = 4; // 要生成的图案组数
|
|
|
|
$data = [];
|
|
for ($i = 0; $i < $numberOfGroups; $i++) {
|
|
|
|
for ($j = 0; $j < $groupNum; $j++) { // 每组生成 3 个图案
|
|
$selectedPattern = null;
|
|
$randomNumber = mt_rand() / mt_getrandmax(); // 生成 0 到 1 之间的随机数
|
|
|
|
foreach ($patterns as $pattern) {
|
|
if ($randomNumber <= $pattern['accumulatedProbability']) {
|
|
$selectedPattern = $pattern;
|
|
break;
|
|
}
|
|
}
|
|
|
|
$data[$i][] = $selectedPattern['image'];
|
|
}
|
|
|
|
}
|
|
return $data;
|
|
}
|
|
}
|