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); } // 创建玩家时:未指定则关联 name=playerDefault(玩家默认彩金池),没有则为 0 try { $lotteryConfigId = $model->getAttr('lottery_config_id'); } catch (\Throwable $e) { $lotteryConfigId = null; } if ($lotteryConfigId === null || $lotteryConfigId === '' || (int) $lotteryConfigId === 0) { $config = self::findPlayerDefaultLotteryConfigForPlayer($model); $model->setAttr('lottery_config_id', $config ? (int) $config->id : 0); } // 展示用权重:从玩家关联的彩金池复制(playerDefault 在抽奖时仍实时读池配置) self::setDefaultWeightsFromLotteryConfig($model); } /** * 按玩家所属渠道查找玩家默认彩金池(name=playerDefault) */ protected static function findPlayerDefaultLotteryConfigForPlayer(DicePlayer $model): ?DiceLotteryPoolConfig { try { $deptId = $model->getAttr('dept_id'); } catch (\Throwable $e) { $deptId = null; } $normalizedDeptId = AdminScopeHelper::resolvePlayerConfigDeptId($model); if ($deptId !== null && $deptId !== '' && (int) $deptId > 0) { $normalizedDeptId = (int) $deptId; } $config = DiceLotteryPoolConfig::findByNameForDept( DiceLotteryPoolConfig::NAME_PLAYER_DEFAULT, $normalizedDeptId ); if ($config) { return $config; } return DiceLotteryPoolConfig::findByNameForDept('default', $normalizedDeptId); } /** * 从玩家关联彩金池(或 playerDefault / default)取 t1_weight~t5_weight 作为未设置时的默认值 */ protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void { $config = null; try { $lotteryConfigId = (int) $model->getAttr('lottery_config_id'); } catch (\Throwable $e) { $lotteryConfigId = 0; } if ($lotteryConfigId > 0) { $config = DiceLotteryPoolConfig::find($lotteryConfigId); } if (!$config) { $config = self::findPlayerDefaultLotteryConfigForPlayer($model); } 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 diceLotteryPoolConfig() { return $this->belongsTo(DiceLotteryPoolConfig::class, 'lottery_config_id', 'id'); } }