132 lines
3.3 KiB
PHP
132 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* PageRepo.php
|
|
*
|
|
* @copyright 2022 beikeshop.com - All Rights Reserved
|
|
* @link https://beikeshop.com
|
|
* @author Edward Yang <yangjin@guangda.work>
|
|
* @created 2022-07-26 21:08:07
|
|
* @modified 2022-07-26 21:08:07
|
|
*/
|
|
|
|
namespace Beike\Admin\Repositories;
|
|
|
|
use Beike\Models\Inquiry;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class InquiryRepo
|
|
{
|
|
/**
|
|
* 获取列表页数据
|
|
*
|
|
* @return LengthAwarePaginator
|
|
*/
|
|
public static function getList(): LengthAwarePaginator
|
|
{
|
|
$builder = Inquiry::query()->with([
|
|
'productsku',
|
|
])->orderByDesc('updated_at');
|
|
|
|
return $builder->paginate(perPage());
|
|
}
|
|
|
|
public static function findByPageId($pageId)
|
|
{
|
|
$page = Inquiry::query()->findOrFail($pageId);
|
|
$page->load(['descriptions']);
|
|
|
|
return $page;
|
|
}
|
|
|
|
public static function getDescriptionsByLocale($pageId)
|
|
{
|
|
$page = self::findByPageId($pageId);
|
|
|
|
return $page->descriptions->keyBy('locale')->toArray();
|
|
}
|
|
|
|
public static function createOrUpdate($data)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
$region = self::pushPage($data);
|
|
DB::commit();
|
|
|
|
return $region;
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public static function pushPage($data)
|
|
{
|
|
$id = $data['id'] ?? 0;
|
|
if ($id) {
|
|
$page = Inquiry::query()->findOrFail($id);
|
|
} else {
|
|
$page = new Inquiry();
|
|
}
|
|
$page->fill([
|
|
'page_category_id' => (int) ($data['page_category_id'] ?? 0),
|
|
'position' => (int) ($data['position'] ?? 0),
|
|
'active' => (bool) ($data['active'] ?? true),
|
|
'author' => $data['author'] ?? '',
|
|
'views' => (int) ($data['views'] ?? 0),
|
|
]);
|
|
|
|
$page->saveOrFail();
|
|
|
|
$page->descriptions()->delete();
|
|
$page->descriptions()->createMany($data['descriptions']);
|
|
|
|
$products = $data['products'] ?? [];
|
|
if ($products) {
|
|
$items = [];
|
|
foreach ($products as $item) {
|
|
$items[] = [
|
|
'product_id' => $item,
|
|
];
|
|
}
|
|
$page->pageProducts()->delete();
|
|
$page->pageProducts()->createMany($items);
|
|
}
|
|
|
|
$page->load(['descriptions', 'pageProducts']);
|
|
|
|
return $page;
|
|
}
|
|
|
|
public static function deleteById($id)
|
|
{
|
|
$page = Inquiry::query()->findOrFail($id);
|
|
$page->delete();
|
|
}
|
|
|
|
/**
|
|
* 页面内容自动完成
|
|
*
|
|
* @param $name
|
|
* @return array
|
|
*/
|
|
public static function autocomplete($name): array
|
|
{
|
|
$pages = Inquiry::query()->with('description')
|
|
->whereHas('description', function ($query) use ($name) {
|
|
$query->where('title', 'like', "{$name}%");
|
|
})->limit(10)->get();
|
|
$results = [];
|
|
foreach ($pages as $page) {
|
|
$results[] = [
|
|
'id' => $page->id,
|
|
'name' => $page->description->title,
|
|
'status' => $page->active,
|
|
];
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
}
|