93 lines
3.1 KiB
PHP
93 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace app\common\model\lottery;
|
||
|
||
use app\common\model\BaseModel;
|
||
|
||
class LotteryNumberMapping extends BaseModel
|
||
{
|
||
protected $name = 'lottery_number_mapping';
|
||
|
||
// 类型常量
|
||
const TYPE_ZODIAC = 1; // 生肖
|
||
const TYPE_COLOR = 2; // 波色
|
||
const TYPE_ELEMENT = 3; // 五行
|
||
|
||
protected $json = ['numbers'];
|
||
protected $jsonAssoc = true;
|
||
|
||
/**
|
||
* 获取指定年份的全部映射数据
|
||
*/
|
||
public static function getByYear(int $year): array
|
||
{
|
||
$list = self::where('year', $year)->order('type asc, sort asc')->select()->toArray();
|
||
$result = ['zodiac' => [], 'color' => [], 'element' => []];
|
||
foreach ($list as $item) {
|
||
$item['numbers'] = is_string($item['numbers']) ? json_decode($item['numbers'], true) : $item['numbers'];
|
||
match ((int)$item['type']) {
|
||
self::TYPE_ZODIAC => $result['zodiac'][] = $item,
|
||
self::TYPE_COLOR => $result['color'][] = $item,
|
||
self::TYPE_ELEMENT => $result['element'][] = $item,
|
||
};
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 获取指定年份某类型的映射(按attr_name索引)
|
||
*/
|
||
public static function getMapByType(int $year, int $type): array
|
||
{
|
||
$list = self::where('year', $year)->where('type', $type)->order('sort asc')->select()->toArray();
|
||
$map = [];
|
||
foreach ($list as $item) {
|
||
$nums = is_string($item['numbers']) ? json_decode($item['numbers'], true) : $item['numbers'];
|
||
$map[$item['attr_name']] = $nums;
|
||
}
|
||
return $map;
|
||
}
|
||
|
||
/**
|
||
* 根据号码反查属性名(如:号码1 → 生肖"马"、波色"红波"、五行"水")
|
||
*/
|
||
public static function lookupNumber(int $year, int $number): array
|
||
{
|
||
$all = self::where('year', $year)->order('type asc, sort asc')->select()->toArray();
|
||
$result = ['zodiac' => '', 'color' => '', 'element' => ''];
|
||
foreach ($all as $item) {
|
||
$nums = is_string($item['numbers']) ? json_decode($item['numbers'], true) : $item['numbers'];
|
||
if (in_array($number, $nums)) {
|
||
match ((int)$item['type']) {
|
||
self::TYPE_ZODIAC => $result['zodiac'] = $item['attr_name'],
|
||
self::TYPE_COLOR => $result['color'] = $item['attr_name'],
|
||
self::TYPE_ELEMENT => $result['element'] = $item['attr_name'],
|
||
};
|
||
}
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 从上一年份复制映射数据到新年份(用于年度轮转后微调)
|
||
*/
|
||
public static function copyToNextYear(int $fromYear, int $toYear): int
|
||
{
|
||
$exists = self::where('year', $toYear)->count();
|
||
if ($exists > 0) {
|
||
return 0;
|
||
}
|
||
$list = self::where('year', $fromYear)->select()->toArray();
|
||
$count = 0;
|
||
foreach ($list as $item) {
|
||
unset($item['id']);
|
||
$item['year'] = $toYear;
|
||
$item['create_time'] = time();
|
||
$item['update_time'] = time();
|
||
self::create($item);
|
||
$count++;
|
||
}
|
||
return $count;
|
||
}
|
||
}
|