85 lines
2.0 KiB
PHP
85 lines
2.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models\Juil;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||
|
|
|
||
|
|
class PmisWorker extends Model
|
||
|
|
{
|
||
|
|
use SoftDeletes;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'tenant_id',
|
||
|
|
'user_id',
|
||
|
|
'name',
|
||
|
|
'login_id',
|
||
|
|
'phone',
|
||
|
|
'email',
|
||
|
|
'department',
|
||
|
|
'position',
|
||
|
|
'role_type',
|
||
|
|
'gender',
|
||
|
|
'company',
|
||
|
|
'profile_photo_path',
|
||
|
|
'options',
|
||
|
|
'last_login_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'options' => 'array',
|
||
|
|
'last_login_at' => 'datetime',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function user(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* SAM 로그인 사용자로부터 PMIS Worker 자동 생성 (최초 접근 시)
|
||
|
|
*/
|
||
|
|
public static function findOrCreateFromUser(User $user, int $tenantId = 1): self
|
||
|
|
{
|
||
|
|
$worker = self::where('tenant_id', $tenantId)
|
||
|
|
->where('user_id', $user->id)
|
||
|
|
->first();
|
||
|
|
|
||
|
|
if ($worker) {
|
||
|
|
return $worker;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 부서명 조회
|
||
|
|
$departments = $user->getDepartmentsForTenant($tenantId);
|
||
|
|
$deptName = $departments->first()?->name ?? null;
|
||
|
|
|
||
|
|
return self::create([
|
||
|
|
'tenant_id' => $tenantId,
|
||
|
|
'user_id' => $user->id,
|
||
|
|
'name' => $user->name,
|
||
|
|
'login_id' => $user->user_id,
|
||
|
|
'phone' => $user->phone,
|
||
|
|
'email' => $user->email,
|
||
|
|
'department' => $deptName,
|
||
|
|
'role_type' => $user->role ?? '사용자',
|
||
|
|
'profile_photo_path' => $user->profile_photo_path,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getOption(string $key, mixed $default = null): mixed
|
||
|
|
{
|
||
|
|
return data_get($this->options, $key, $default);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setOption(string $key, mixed $value): self
|
||
|
|
{
|
||
|
|
$options = $this->options ?? [];
|
||
|
|
$options[$key] = $value;
|
||
|
|
$this->options = $options;
|
||
|
|
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
}
|