Update source into github

This commit is contained in:
pbao9
2025-07-24 16:18:27 +07:00
parent 41419358dd
commit 69851ac7bc
2642 changed files with 284095 additions and 0 deletions
@@ -0,0 +1,66 @@
<?php
namespace App\Repositories\Ticket;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
class TicketCachedRepository extends BaseCachedRepository implements TicketRepositoryInterface
{
public function __construct(
public TicketRepository $ticketRepository
) {
parent::__construct();
}
public function getModel()
{
return $this->ticketRepository->getModel();
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->ticketRepository->filter($data, $relationships, $withCount, $select);
});
}
public function findWithRelationshipsByKey(string $key = '', string|int $value = '', array $relationships = [], array $withCount = [], array $withSum = [])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findWithRelationshipsByKey' => [
'key' => $key,
'value' => $value,
'relationships' => $relationships,
'withCount' => $withCount,
'withSum' => $withSum,
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($key, $value, $relationships, $withCount, $withSum) {
return $this->ticketRepository->findWithRelationshipsByKey($key, $value, $relationships, $withCount, $withSum);
});
}
public function findBy(string $key = '', string $value)
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findBy' => [
'key' => $key,
'value' => $value
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($key, $value) {
return $this->ticketRepository->findBy($key, $value);
});
}
public function updateBy(string $key = '', string $value = '', array $data = [])
{
$this->ticketRepository->updateBy($key, $value, $data);
$this->clearCache();
return $this;
}
}
@@ -0,0 +1,100 @@
<?php
namespace App\Repositories\Ticket;
use App\Models\Ticket;
use App\Repositories\BaseRepository;
class TicketRepository extends BaseRepository implements TicketRepositoryInterface
{
public function getModel()
{
return Ticket::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model->with($relationships)->select($select);
if (isset($data['status']) && ! empty($data['status']) && $data['status'] != 'full') {
$model = $model->where("status", match (strtolower($data['status'])) {
'pending' => 1,
'close' => 0,
default => 1
});
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if (isset($data['page']) && $data['page']) {
return $model->paginate(15);
}
return $model->get();
}
public function findWithRelationshipsByKey(string $key = '', string|int $value = '', array $relationships = [], array $withCount = [], array $withSum = [])
{
if (empty($key) || empty($value)) {
return false;
}
$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->where($key, $value)->latest('id')->get();
}
public function findBy(string $key = '', string $value)
{
if (empty($key) || empty($value)) {
return false;
}
return $this->model->where($key, $value)->first();
}
public function updateBy(string $key = '', string $value = '', array $data = [])
{
if (empty($key) || empty($value) || count($data) < 1) {
return false;
}
return $this->model->where($key, $value)->update($data);
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Repositories\Ticket;
use App\Repositories\BaseRepositoryInterface;
interface TicketRepositoryInterface extends BaseRepositoryInterface{
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
public function findWithRelationshipsByKey(string $key = '',string|int $value = '', array $relationships = [],array $withCount = [], array $withSum = []);
public function findBy(string $key = '',string $value);
public function updateBy(string $key = '', string $value = '', array $data = []);
}