giftcon_dev/app/Repositories/Product/ProductRepository.php
sungro815 b0545ab5b9 관리자 상품관리 완료
웹사이트 상품리스트 상세보기 작업중
2026-02-20 18:11:03 +09:00

112 lines
3.6 KiB
PHP

<?php
namespace App\Repositories\Product;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;
class ProductRepository
{
/**
* 왼쪽 메뉴용 활성 카테고리 목록 조회
*/
public function getActiveCategories()
{
return DB::table('gc_categories')
->where('is_active', 1)
->orderBy('sort_order', 'asc')
->get();
}
/**
* 카테고리별/전체 상품 목록 조회 (판매 기간 및 상태 검증 포함)
*/
// app/Repositories/Product/ProductRepository.php
public function getActiveProducts($categoryIdOrSlug = null, $search = null)
{
$now = now();
$query = DB::table('gc_products as p')
->select('p.*', 'm.file_path as thumb_path')
->leftJoin('gc_media_library as m', 'p.thumbnail_media_id', '=', 'm.id')
->where('p.status', 'ACTIVE');
// 1. 검색어가 있을 경우 (통합 검색)
if ($search) {
$query->where(function ($q) use ($search) {
// 상품명 검색
$q->where('p.name', 'LIKE', "%{$search}%");
// 카테고리 연관 검색 (이름, 슬러그, 해시태그 키워드)
$q->orWhereExists(function ($sub) use ($search) {
$sub->select(DB::raw(1))
->from('gc_categories as c')
->whereColumn('c.id', 'p.category_id')
->where(function ($sq) use ($search) {
$sq->where('c.name', 'LIKE', "%{$search}%")
->orWhere('c.slug', 'LIKE', "%{$search}%")
->orWhere('c.search_keywords', 'LIKE', "%{$search}%"); // # 제거된 검색어도 LIKE로 매칭
});
});
});
}
if ($categoryIdOrSlug && !$search) {
$categoryQuery = DB::table('gc_categories');
if (is_numeric($categoryIdOrSlug)) {
$categoryQuery->where('id', $categoryIdOrSlug);
} else {
$categoryQuery->where('slug', $categoryIdOrSlug);
}
$targetCategory = $categoryQuery->first();
if ($targetCategory) {
$categoryIds = DB::table('gc_categories')
->where('id', $targetCategory->id)
->orWhere('parent_id', $targetCategory->id)
->pluck('id');
$query->whereIn('p.category_id', $categoryIds);
}
}
return $query->orderBy('p.id', 'desc')->get();
}
public function getProductById(int $id)
{
return DB::table('gc_products as p')
->select('p.*', 'm.file_path as thumb_path')
->leftJoin('gc_media_library as m', 'p.thumbnail_media_id', '=', 'm.id')
->where('p.id', $id)
->where('p.status', 'ACTIVE')
->first();
}
/**
* 상품에 속한 권종(SKU) 목록 조회
*/
public function getSkusByProductId(int $productId)
{
return DB::table('gc_product_skus')
->where('product_id', $productId)
->where('is_active', 1)
->orderBy('sort_order', 'asc')
->get();
}
/**
* 특정 결제 수단 정보 조회
*/
public function getPaymentMethodsByIds(array $ids)
{
if (empty($ids)) return collect();
return DB::table('gc_payment_methods')
->whereIn('id', $ids)
->where('is_active', 1)
->orderBy('sort_order', 'asc')
->get();
}
}