Files
amlhc/application/admin/model/History.php
T
2026-04-21 23:02:15 +08:00

144 lines
3.8 KiB
PHP

<?php
namespace app\admin\model;
use think\Model;
use app\admin\model\Num;
class History extends Model
{
// 表名
protected $name = 'history';
// 自动写入时间戳字段
protected $autoWriteTimestamp = false;
// 定义时间戳字段名
protected $createTime = false;
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
];
/**
* 计算遗漏号码
* @param int $periods 查询最近多少期
* @param string $type 查询类型 all=全部号码 special=仅特码
* @return array [{num: int, omit: int, color: string}, ...]
*/
public function getMissingNumbers($periods = 10, $type = 'all')
{
// 查询最近 $periods 期开奖数据
if ($type === 'special') {
$history = $this
->field('expect,num7')
->order('openTime', 'desc')
->limit($periods)
->select();
} else {
$history = $this
->field('expect,num1,num2,num3,num4,num5,num6,num7')
->order('openTime', 'desc')
->limit($periods)
->select();
}
// 收集最近 $periods 期出现过的号码
$appeared = [];
foreach ($history as $row) {
if ($type === 'special') {
$fields = ['num7'];
} else {
$fields = ['num1', 'num2', 'num3', 'num4', 'num5', 'num6', 'num7'];
}
foreach ($fields as $field) {
if ($row[$field] !== null && $row[$field] !== '') {
$appeared[(int)$row[$field]] = true;
}
}
}
// 获取遗漏号码(1-49中未出现的)
$missing = [];
for ($num = 1; $num <= 49; $num++) {
if (!isset($appeared[$num])) {
$missing[] = $num;
}
}
// 查询更多历史数据用于计算遗漏期数
if ($type === 'special') {
$allHistory = $this
->field('num7')
->order('openTime', 'desc')
->limit(500)
->select();
} else {
$allHistory = $this
->field('num1,num2,num3,num4,num5,num6,num7')
->order('openTime', 'desc')
->limit(500)
->select();
}
// 查询波色映射
$num_model = new Num();
$colorMap = $num_model->column('color', 'num');
// 计算遗漏期数并组装结果
$result = [];
foreach ($missing as $num) {
$omitCount = $this->calcOmitCount($num, $allHistory, $type);
$result[] = [
'num' => $num,
'omit' => $omitCount,
'color' => $colorMap[$num] ?? '—'
];
}
// 按遗漏期数降序排序
usort($result, function ($a, $b) {
return $b['omit'] - $a['omit'];
});
return $result;
}
/**
* 计算某个号码的遗漏期数
* @param int $num 号码
* @param array $allHistory 历史数据(已按openTime DESC排序)
* @param string $type 查询类型 all=全部号码 special=仅特码
* @return int 遗漏期数
*/
private function calcOmitCount($num, $allHistory, $type = 'all')
{
foreach ($allHistory as $idx => $row) {
if ($type === 'special') {
if ((int)$row['num7'] === $num) {
return $idx;
}
} else {
for ($i = 1; $i <= 7; $i++) {
if ((int)$row['num' . $i] === $num) {
return $idx;
}
}
}
}
return count($allHistory);
}
}