Browse Source

数据库模型文件

master
wanghongjun 2 years ago
parent
commit
91b482929e
  1. 18
      app/model/Zone.php
  2. 50
      app/model/ZoneAmountParam.php
  3. 21
      app/model/ZoneGoods.php
  4. 47
      app/model/ZoneGoodsParam.php

18
app/model/Zone.php

@ -0,0 +1,18 @@
<?php
namespace app\model;
use think\Model;
class Zone extends Model
{
public static function getList()
{
$zone = self::where('status',1)->select();
return $zone->toArray();
}
}

50
app/model/ZoneAmountParam.php

@ -0,0 +1,50 @@
<?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
];
}
}

21
app/model/ZoneGoods.php

@ -0,0 +1,21 @@
<?php
namespace app\model;
class ZoneGoods extends \think\Model
{
public static function getList($param)
{
$where = ['status' => 1];
if (isset($param['zone_id'])) $where['zone_id'] = $param['zone_id'];
$field = 'zone_id,title,important,price,cover_image';
$list = self::where($where)->field($field)->order('id desc')->select()->toArray();
foreach ($list as &$item) {
$item['cover_image'] = get_image_url($item['cover_image']);
}
return $list;
}
}

47
app/model/ZoneGoodsParam.php

@ -0,0 +1,47 @@
<?php
namespace app\model;
use \think\Model;
class ZoneGoodsParam extends Model
{
/**
* 获取刮奖参数
* @param $where
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function getList($where = [])
{
$where['status'] = 1;
$list = self::where($where)->field('amount,image,probability,awards')->select()->toArray();
$ZoneAmountParam = [];
foreach ($list as &$item) {
$item['image'] = get_image_url($item['image']);
if ($item['awards'] != 1) {
# 获取无奖项随机金额
$item['amount'] = ZoneAmountParam::getRandAmount($ZoneAmountParam);
$ZoneAmountParam = $item['data'];
}
}
return $list;
}
/**
* 获取中奖金额
* @author whj
* @date 2023-08-28 17:50
*/
public static function getAwardsAmount($id)
{
$data = self::where(['id' => $id,'awards' => 1])->field('amount')->find();
return $data ?: false;
}
}
Loading…
Cancel
Save