Files
portal-api/app/Repositories/Customer/CustomerRepository.php
T
baodev0 fccb0c353a feat: Add DNS management resources and importer
- Implemented DomainCollection, DomainDetailResource, DomainNSCollection, DomainNSResource, and DomainResource for handling DNS domain and record data.
- Created VpsServiceCollection and VpsServiceResource for VPS service management.
- Added DnsManagerNS and DnsManagerUser models to represent DNS records and user associations.
- Developed AdminZoneDnsSqlImporter for importing DNS data from SQL dumps, including customer and DNS domain records.
- Added migration scripts to update the customers table with new fields and sync foreign key references for DNS tables.
- Created a migration preview document outlining the mapping and schema changes for the admin zone DNS import.
2026-05-26 13:54:33 +07:00

163 lines
5.8 KiB
PHP

<?php
namespace App\Repositories\Customer;
use App\Models\Customer;
use App\Repositories\BaseRepository;
class CustomerRepository extends BaseRepository implements CustomerRepositoryInterface
{
private const STATUS_MAP = [
'active' => 1,
'inactive' => 2,
'de_active' => 2,
'pending' => 0,
'closed' => 2,
'suspended' => 3,
'banned' => 3,
];
public function getModel()
{
return Customer::class;
}
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
$model = $this->model->with($relationships)->select($select);
if (isset($data['filter']) && ! empty($data['filter'])) {
$model = $model->where(function ($query) use ($data) {
$filter = trim($data['filter']);
$query->where('fullname', 'LIKE', "%{$filter}%")
->orWhere('username', 'LIKE', "%{$filter}%")
->orWhere('company', 'LIKE', "%{$filter}%")
->orWhere('email', 'LIKE', "%{$filter}%")
->orWhere('phone', 'LIKE', "%{$filter}%");
});
}
if (isset($data['name']) && ! empty($data['name'])) {
$model = $model->where(function ($query) use ($data) {
$name = trim($data['name']);
$query->where("fullname", "LIKE", "%$name%");
$query->orWhere("company", "LIKE", "%$name%");
});
}
if (isset($data['email']) && ! empty($data['email'])) {
$model = $model->where("email", "LIKE", "%" . $data['email'] . "%");
}
if (isset($data['phone']) && ! empty($data['phone'])) {
$model = $model->where("phone", "LIKE", "%" . $data['phone'] . "%");
}
if (isset($data['status']) && ! empty($data['status']) && $data['status'] != 'full') {
$status = (string) $data['status'];
$statusValue = is_numeric($status)
? (int) $status
: (self::STATUS_MAP[$status] ?? null);
if ($statusValue !== null) {
$model = $model->where('status', $statusValue);
}
}
if (isset($data['relations'])) {
foreach ($data['relations'] as $key => $value) {
$model = $model->whereHas($key, function ($query) use ($value) {
if (isset($value['condition'])) {
switch (strtoupper($value['condition'])) {
case "IN":
$query->whereIn($value['key'], $value['value']);
break;
case "NotIn":
$query->whereNotIn($value['key'], $value['value']);
break;
default:
$query->where($value['key'], $value['condition'], $value['value']);
break;
}
} else {
$query->where($value['key'], $value['value']);
}
});
}
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if ($returnQuery) {
return $model;
}
if (isset($data['page'])) {
return $model->paginate(15);
}
return $model->get();
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
$model = $this->model;
if (count($relationships) > 0) {
foreach ($relationships as $relationship) {
if (method_exists($model, $relationship)) {
$model = $model->with($relationship);
}
}
}
// Xử lý withCount
if (count($withCount) > 0) {
foreach ($withCount as $key => $items) {
if (is_array($items)) {
foreach ($items as $alias => $item) {
$model = $model->withCount([
$key . ' as ' . $alias => function ($query) use ($item) {
$query->where($item['key'], $item['condition'], $item['value']);
}
]);
}
} else {
$model = $model->withCount($key);
}
}
}
// Xử lý withSum
if (count($withSum) > 0) {
foreach ($withSum as $key => $items) {
if (is_array($items)) {
foreach ($items as $alias => $item) {
$model = $model->withSum([
$key . ' as ' . $alias => function ($query) use ($item) {
$query->where($item['key'], $item['condition'], $item['value']);
}
], $item['sum']);
}
} else {
$model = $model->withSum($key);
}
}
}
return $model->find($id);
}
public function findBy(string $key = '', string $value = '')
{
if (empty($key) || empty($value)) {
return false;
}
return $this->model->where($key, $value)->first();
}
public function onlyTrashed(array $relationships = [], array $withCount = [])
{
$query = $this->model->with($relationships);
if (count($withCount) > 0) {
$query = $query->withCount($withCount);
}
return $query->onlyTrashed();
}
}