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.
50 lines
1.1 KiB
50 lines
1.1 KiB
<?php
|
|
|
|
namespace app\model;
|
|
|
|
use \think\Model;
|
|
|
|
class ZoneAmountParam extends Model
|
|
{
|
|
|
|
/**
|
|
* 获取随机金额
|
|
* @param $data
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public static function getRandAmount($data = [])
|
|
{
|
|
if (empty($data)) {
|
|
$model = new ZoneAmountParam();
|
|
$data = $model->select()->toArray();
|
|
}
|
|
|
|
$totalWeight = 0;
|
|
foreach ($data as $pattern) {
|
|
$totalWeight += $pattern['weight'];
|
|
}
|
|
|
|
// 随机金额
|
|
$amount = '0.00';
|
|
|
|
// 生成随机数
|
|
$randomNumber = rand(1, $totalWeight);
|
|
|
|
foreach ($data as $pattern) {
|
|
$randomNumber -= $pattern['weight'];
|
|
if ($randomNumber <= 0) {
|
|
$amount = $pattern['amount'];
|
|
break;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'data' => $data,
|
|
'amount' => $amount
|
|
];
|
|
}
|
|
|
|
}
|