182 lines
4.8 KiB
PHP
182 lines
4.8 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | saiadmin [ saiadmin快速开发框架 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Author: your name
|
||
// +----------------------------------------------------------------------
|
||
namespace app\dice\model\player;
|
||
|
||
use plugin\saiadmin\basic\think\BaseModel;
|
||
use app\dice\model\lottery_config\DiceLotteryConfig;
|
||
|
||
/**
|
||
* 大富翁-玩家模型
|
||
*
|
||
* dice_player 大富翁-玩家
|
||
*
|
||
* @property $id ID
|
||
* @property $username 用户名
|
||
* @property $phone 手机
|
||
* @property $uid uid
|
||
* @property $name 昵称
|
||
* @property $password 密码
|
||
* @property $status 状态
|
||
* @property $coin 平台币
|
||
* @property $lottery_config_id 彩金池配置ID(0或null时使用自定义权重*_weight)
|
||
* @property $t1_weight T1池权重
|
||
* @property $t2_weight T2池权重
|
||
* @property $t3_weight T3池权重
|
||
* @property $t4_weight T4池权重
|
||
* @property $t5_weight T5池权重
|
||
* @property $total_ticket_count 总抽奖次数
|
||
* @property $paid_ticket_count 购买抽奖次数
|
||
* @property $free_ticket_count 赠送抽奖次数
|
||
* @property $create_time 创建时间
|
||
* @property $update_time 更新时间
|
||
* @property $delete_time 删除时间
|
||
*/
|
||
class DicePlayer extends BaseModel
|
||
{
|
||
/**
|
||
* 数据表主键
|
||
* @var string
|
||
*/
|
||
protected $pk = 'id';
|
||
|
||
/**
|
||
* 数据库表名称
|
||
* @var string
|
||
*/
|
||
protected $table = 'dice_player';
|
||
|
||
protected $createTime = 'create_time';
|
||
|
||
protected $updateTime = 'update_time';
|
||
|
||
/**
|
||
* 新增前:生成唯一 uid,昵称 name 默认使用 uid
|
||
* 用 try-catch 避免表尚未含 uid 时 getAttr/getData 抛 InvalidArgumentException
|
||
*/
|
||
public static function onBeforeInsert($model): void
|
||
{
|
||
parent::onBeforeInsert($model);
|
||
try {
|
||
$uid = $model->getAttr('uid');
|
||
} catch (\Throwable $e) {
|
||
$uid = null;
|
||
}
|
||
if ($uid === null || $uid === '') {
|
||
$uid = self::generateUid();
|
||
$model->setAttr('uid', $uid);
|
||
}
|
||
try {
|
||
$name = $model->getAttr('name');
|
||
} catch (\Throwable $e) {
|
||
$name = null;
|
||
}
|
||
if ($name === null || $name === '') {
|
||
$model->setAttr('name', $uid);
|
||
}
|
||
// 彩金池权重默认取 type=0 的奖池配置
|
||
self::setDefaultWeightsFromLotteryConfig($model);
|
||
}
|
||
|
||
/**
|
||
* 从 DiceLotteryConfig type=0 取 t1_weight~t5_weight 作为玩家未设置时的默认值
|
||
*/
|
||
protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void
|
||
{
|
||
$config = DiceLotteryConfig::where('type', 0)->find();
|
||
if (!$config) {
|
||
return;
|
||
}
|
||
$fields = ['t1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'];
|
||
foreach ($fields as $field) {
|
||
try {
|
||
$val = $model->getAttr($field);
|
||
} catch (\Throwable $e) {
|
||
$val = null;
|
||
}
|
||
if ($val === null || $val === '') {
|
||
try {
|
||
$model->setAttr($field, $config->getAttr($field));
|
||
} catch (\Throwable $e) {
|
||
// 忽略字段不存在
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 生成唯一标识 uid(12 位十六进制)
|
||
*/
|
||
public static function generateUid(): string
|
||
{
|
||
return strtoupper(substr(bin2hex(random_bytes(6)), 0, 12));
|
||
}
|
||
|
||
/**
|
||
* 用户名 搜索
|
||
*/
|
||
public function searchUsernameAttr($query, $value)
|
||
{
|
||
$query->where('username', 'like', '%'.$value.'%');
|
||
}
|
||
|
||
/**
|
||
* 昵称 搜索
|
||
*/
|
||
public function searchNameAttr($query, $value)
|
||
{
|
||
$query->where('name', 'like', '%'.$value.'%');
|
||
}
|
||
|
||
/**
|
||
* 手机号 模糊搜索
|
||
*/
|
||
public function searchPhoneAttr($query, $value)
|
||
{
|
||
if ($value !== '' && $value !== null) {
|
||
$query->where('phone', 'like', '%' . $value . '%');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 状态 搜索
|
||
*/
|
||
public function searchStatusAttr($query, $value)
|
||
{
|
||
if ($value !== '' && $value !== null) {
|
||
$query->where('status', '=', $value);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 平台币 搜索
|
||
*/
|
||
public function searchCoinAttr($query, $value)
|
||
{
|
||
if ($value !== '' && $value !== null) {
|
||
$query->where('coin', '=', $value);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 彩金池配置ID 搜索
|
||
*/
|
||
public function searchLottery_config_idAttr($query, $value)
|
||
{
|
||
if ($value !== '' && $value !== null) {
|
||
$query->where('lottery_config_id', '=', $value);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 关联彩金池配置
|
||
*/
|
||
public function diceLotteryConfig()
|
||
{
|
||
return $this->belongsTo(DiceLotteryConfig::class, 'lottery_config_id', 'id');
|
||
}
|
||
}
|