Files
dafuweng-saiadmin6.x/server/app/dice/model/player/DicePlayer.php

171 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 $is_up 倍率
* @property $t1_wight T1池权重
* @property $t2_wight T2池权重
* @property $t3_wight T3池权重
* @property $t4_wight T4池权重
* @property $t5_wight T5池权重
* @property $total_draw_count 总抽奖次数
* @property $paid_draw_count 购买抽奖次数
* @property $free_draw_count 赠送抽奖次数
* @property $created_at 创建时间
* @property $updated_at 更新时间
* @property $deleted_at 删除时间
*/
class DicePlayer extends BaseModel
{
/**
* 数据表主键
* @var string
*/
protected $pk = 'id';
/**
* 数据库表名称
* @var string
*/
protected $table = 'dice_player';
/**
* 新增前:生成唯一 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_wightt5_wight 作为玩家未设置时的默认值
*/
protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void
{
$config = DiceLotteryConfig::where('type', 0)->find();
if (!$config) {
return;
}
$fields = ['t1_wight', 't2_wight', 't3_wight', 't4_wight', 't5_wight'];
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) {
// 忽略字段不存在
}
}
}
}
/**
* 生成唯一标识 uid12 位十六进制)
*/
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);
}
}
/**
* 倍率 搜索
*/
public function searchIs_upAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('is_up', '=', $value);
}
}
}