183 lines
4.7 KiB
PHP
183 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* CountryRepo.php
|
|
*
|
|
* @copyright 2022 beikeshop.com - All Rights Reserved
|
|
* @link https://beikeshop.com
|
|
* @author TL <mengwb@guangda.work>
|
|
* @created 2022-06-30 15:22:05
|
|
* @modified 2022-06-30 15:22:05
|
|
*/
|
|
|
|
namespace Beike\Repositories;
|
|
|
|
use Beike\Models\Country;
|
|
use Beike\Models\RegionZone;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class CountryRepo
|
|
{
|
|
/**
|
|
* @param $data
|
|
* @return array
|
|
*/
|
|
private static function handleParams($data)
|
|
{
|
|
return [
|
|
'name' => $data['name'] ?? '',
|
|
'icon' => $data['icon'] ?? '',
|
|
'code' => $data['code'] ?? '',
|
|
'sort_order' => (int) $data['sort_order'] ?? 0,
|
|
'status' => (bool) $data['status'] ?? 0,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 创建一个country记录
|
|
* @param $data
|
|
* @return mixed
|
|
*/
|
|
public static function create($data)
|
|
{
|
|
$data = self::handleParams($data);
|
|
|
|
return Country::query()->create($data);
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @param $data
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
public static function update($id, $data)
|
|
{
|
|
$data = self::handleParams($data);
|
|
$country = Country::query()->find($id);
|
|
if (! $country) {
|
|
throw new \Exception("国家id {$id} 不存在");
|
|
}
|
|
$country->update($data);
|
|
|
|
return $country;
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public static function find($id)
|
|
{
|
|
return Country::query()->find($id);
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return void
|
|
*/
|
|
public static function delete($id)
|
|
{
|
|
$country = Country::query()->find($id);
|
|
if ($country) {
|
|
$country->delete();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $data
|
|
* @return LengthAwarePaginator
|
|
*/
|
|
public static function list($data): LengthAwarePaginator
|
|
{
|
|
$builder = Country::query();
|
|
|
|
if (isset($data['name'])) {
|
|
$builder->where('countries.name', 'like', "%{$data['name']}%");
|
|
}
|
|
if (isset($data['code'])) {
|
|
$builder->where('countries.code', 'like', "%{$data['email']}%");
|
|
}
|
|
if (isset($data['status'])) {
|
|
$builder->where('countries.status', $data['status']);
|
|
}
|
|
|
|
return $builder->paginate(perPage())->withQueryString();
|
|
}
|
|
|
|
/**
|
|
* 获取已启用国家列表
|
|
* @return Builder[]|Collection
|
|
*/
|
|
public static function listEnabled(): Collection|array
|
|
{
|
|
return Country::query()->where('status', true)->select('id', 'name')->get();
|
|
}
|
|
|
|
/**
|
|
* 获取所有国家列表
|
|
* @return Builder[]|Collection
|
|
*/
|
|
public static function all()
|
|
{
|
|
return Country::query()->select('id', 'name')->get();
|
|
}
|
|
|
|
/**
|
|
* Common: 查询国家列表
|
|
* Author: wu-hui
|
|
* Time: 2023/08/21 16:10
|
|
* @param $name
|
|
* @param int $onlyActive
|
|
* @return Builder[]|Collection
|
|
*/
|
|
public static function autocomplete($name, $onlyActive = 1){
|
|
// 参数获取
|
|
$pageSize = request()->input('page_size',10);
|
|
$regionsId = (int)request()->input('regions_id');
|
|
// 列表获取
|
|
$builder = Country::query()
|
|
->where('name', 'like', "$name%")
|
|
->when($regionsId > 0,function($query) use ($regionsId){
|
|
$ids = RegionZone::query()
|
|
->where('region_id',$regionsId)
|
|
->where('zone_id',0)
|
|
->pluck('country_id');
|
|
$ids = $ids ? $ids->toArray() : [];
|
|
if(is_array($ids) && count($ids) > 0) $query->whereIn('id',$ids);
|
|
})
|
|
->select('id', 'name', 'icon', 'status', 'code')
|
|
->orderBy('sort_order','ASC')
|
|
->orderBy('id','ASC');
|
|
// if ($onlyActive) {
|
|
// $builder->where('status', 1);
|
|
// }
|
|
if($pageSize == 'all') return $builder->get();
|
|
else return $builder->limit($pageSize)->get();
|
|
}
|
|
/**
|
|
* Common: 根据国家ID 获取对应的列表
|
|
* Author: wu-hui
|
|
* Time: 2023/08/21 16:13
|
|
* @param array $ids
|
|
* @return array|mixed[]
|
|
*/
|
|
public static function getInList(array $ids){
|
|
$list = Country::query()
|
|
->whereIn('id', $ids)
|
|
->select('id', 'name', 'code')
|
|
->orderBy('sort_order','ASC')
|
|
->orderBy('id','ASC')
|
|
->get();
|
|
if($list) {
|
|
$list = $list->toArray();
|
|
|
|
return array_column($list,null,'id');
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
}
|