108 lines
2.6 KiB
PHP
108 lines
2.6 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\admin\model\v1;
|
|
|
|
use app\common\model\ProductBaseModel;
|
|
use think\model\concern\SoftDelete;
|
|
|
|
/**
|
|
* 产品模型
|
|
* @mixin \think\Model
|
|
*/
|
|
class ProductModel extends ProductBaseModel
|
|
{
|
|
// 启用软删除
|
|
use SoftDelete;
|
|
// 软件字段
|
|
protected $deleteTime = 'deleted_at';
|
|
|
|
// 分类关联查询
|
|
public function category()
|
|
{
|
|
return $this->belongsTo(ProductCategoryModel::class, 'category_id', 'id');
|
|
}
|
|
|
|
// 搜索器名称
|
|
public function searchNameNullableAttr($query, $value, $data)
|
|
{
|
|
if (is_null($value)) {
|
|
return;
|
|
}
|
|
$query->where('name', 'like', '%' . $value . '%');
|
|
}
|
|
|
|
// 搜索spu
|
|
public function searchSpuNullableAttr($query, $value, $data)
|
|
{
|
|
if (is_null($value)) {
|
|
return;
|
|
}
|
|
$query->where('spu', 'like', '%' . $value . '%');
|
|
}
|
|
|
|
// 搜索发布时间
|
|
public function searchCreatedAtNullableAttr($query, $value, $data)
|
|
{
|
|
if (is_array($value)) {
|
|
if (count($value) == 2) {
|
|
$query->whereBetweenTime('created_at', $value[0], $value[1]);
|
|
} else {
|
|
$query->whereTime('created_at', '>=', $value[0]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 所属语言查询
|
|
public function scopeLanguage($query, $value)
|
|
{
|
|
$query->where('language_id', '=', $value);
|
|
}
|
|
|
|
// 产品分类查询
|
|
public function scopeCategoryNullable($query, $value)
|
|
{
|
|
if (is_null($value)) {
|
|
return;
|
|
}
|
|
if (is_array($value) || str_contains($value, ',')) {
|
|
$query->whereIn('category_id', $value);
|
|
return;
|
|
}
|
|
$query->where('category_id', '=', $value);
|
|
}
|
|
public function scopeCategory($query, $value)
|
|
{
|
|
if (is_array($value) || str_contains($value, ',')) {
|
|
$query->whereIn('category_id', $value);
|
|
return;
|
|
}
|
|
$query->where('category_id', '=', $value);
|
|
}
|
|
|
|
// 规格型号查询
|
|
public function scopeSpu($query, $spu)
|
|
{
|
|
$query->where('spu', '=', $spu);
|
|
}
|
|
|
|
// 启用状态查询
|
|
public function scopeEnabled($query)
|
|
{
|
|
$query->where('status', '=', 1);
|
|
}
|
|
|
|
// 上架状态查询
|
|
public function scopeIsShowNullable($query, bool|null $value)
|
|
{
|
|
if (is_null($value)) {
|
|
return;
|
|
}
|
|
$query->where('is_show', '=', (int)$value);
|
|
}
|
|
public function scopeIsShow($query, bool $value)
|
|
{
|
|
$query->where('is_show', '=', (int)$value);
|
|
}
|
|
}
|