Files
hyperf_service/app/Model/UserSite.php
2025-02-27 17:34:32 +08:00

88 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use App\Constants\Common\SiteCode;
use Hyperf\Database\Concerns\BuildsQueries;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property int $site_id
* @property int $user_id
* @property int $is_default
* @property string $create_time
* @property string $update_time
*/
class UserSite extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'user_site';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'site_id' => 'integer', 'user_id' => 'integer', 'is_default' => 'integer'];
const string CREATED_AT = 'created_time';
const string UPDATED_AT = 'updated_time';
/**
* @param int $id
* @return UserSite|UserSite[]|Collection|\Hyperf\Database\Model\Model|null
*/
public function getInfoById(int $id): Collection|\Hyperf\Database\Model\Model|UserSite|array|null
{
return $this->find($id);
}
/**
* @param int $userId
* @param int $siteId
* @return Collection
*/
public function getInfoByUserIdAndSiteId(int $userId, int $siteId): Collection
{
return $this->where('user_id', $userId)->where('site_id', $siteId)->first();
}
/**
* @param int $userId
* @return array
*/
public function getSiteIdsByUserId(int $userId): array
{
return $this->where('user_id', $userId)->pluck('site_id')->toArray();
}
/**
* @param int $userId
* @return int
*/
public function getUserDefaultIdByUserId(int $userId): int
{
return $this->where('user_id', $userId)->where('is_default',SiteCode::USER_DEFAULT)->value('site_id') ?? 0;
}
/**
* @param int $userId
* @return BuildsQueries|Builder|\Hyperf\Database\Model\Model|object|null
*/
public function getUserNoDefaultIdByUserId(int $userId)
{
return $this->where('user_id', $userId)->where('is_default',SiteCode::USER_NO_DEFAULT)->first();
}
}