forked from fa/breadcrumb-the-shire
70 lines
2.3 KiB
PHP
70 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Repository;
|
|
|
|
use MintyPHP\DB;
|
|
|
|
class TenantDepartmentRepository
|
|
{
|
|
public static function listTenantIdsByDepartmentId(int $departmentId): array
|
|
{
|
|
$rows = DB::select('select tenant_id from tenant_departments where department_id = ?', (string) $departmentId);
|
|
if (!is_array($rows)) {
|
|
return [];
|
|
}
|
|
$ids = [];
|
|
foreach ($rows as $row) {
|
|
$data = $row['tenant_departments'] ?? $row;
|
|
if (is_array($data) && isset($data['tenant_id'])) {
|
|
$ids[] = (int) $data['tenant_id'];
|
|
}
|
|
}
|
|
return array_values(array_unique($ids));
|
|
}
|
|
|
|
public static function listDepartmentIdsByTenantIds(array $tenantIds): array
|
|
{
|
|
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
|
$tenantIds = array_filter($tenantIds, static fn ($id) => $id > 0);
|
|
if (!$tenantIds) {
|
|
return [];
|
|
}
|
|
$placeholders = implode(',', array_fill(0, count($tenantIds), '?'));
|
|
$rows = DB::select(
|
|
'select department_id from tenant_departments where tenant_id in (' . $placeholders . ')',
|
|
...array_map('strval', $tenantIds)
|
|
);
|
|
if (!is_array($rows)) {
|
|
return [];
|
|
}
|
|
$ids = [];
|
|
foreach ($rows as $row) {
|
|
$data = $row['tenant_departments'] ?? $row;
|
|
if (is_array($data) && isset($data['department_id'])) {
|
|
$ids[] = (int) $data['department_id'];
|
|
}
|
|
}
|
|
return array_values(array_unique($ids));
|
|
}
|
|
|
|
public static function replaceForDepartment(int $departmentId, array $tenantIds): bool
|
|
{
|
|
DB::delete('delete from tenant_departments where department_id = ?', (string) $departmentId);
|
|
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
|
$tenantIds = array_filter($tenantIds, static fn ($id) => $id > 0);
|
|
if (!$tenantIds) {
|
|
return true;
|
|
}
|
|
|
|
foreach ($tenantIds as $tenantId) {
|
|
DB::insert(
|
|
'insert into tenant_departments (tenant_id, department_id, created) values (?,?,NOW())',
|
|
(string) $tenantId,
|
|
(string) $departmentId
|
|
);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|