feat: 产品相关接口

This commit is contained in:
2025-01-24 17:54:04 +08:00
parent a64379f188
commit 050d7cec4b
18 changed files with 787 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
<?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';
// 修改自动写入时间格式
protected $autoWriteTimestamp = 'datetime';
// 分类关联查询
public function category()
{
return $this->belongsTo('ProductCategoryModel', '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;
}
$query->where('category_id', '=', $value);
}
// 上架状态查询
public function scopeIsSaleNullable($query, bool|null $value)
{
if (is_null($value)) {
return;
}
$query->where('is_sale', '=', (int)$value);
}
}