Files
orico-official-website/app/admin/model/v1/ProductCompatModel.php
jsasg 8f6e7d4826
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 5s
refactor: 产品兼容性相关功能
2026-08-11 17:00:26 +08:00

94 lines
2.5 KiB
PHP

<?php
declare (strict_types = 1);
namespace app\admin\model\v1;
use app\common\model\ProductCompatBaseModel;
use think\model\concern\SoftDelete;
/**
* @mixin \think\Model
*/
class ProductCompatModel extends ProductCompatBaseModel
{
// 启用软件删除
use SoftDelete;
// 软件删除时间字段
protected $deleteTime = 'deleted_at';
// 关联产品类型
public function types()
{
return $this->hasOne(ProductCompatTypesModel::class, 'id', 'type_id');
}
// 关联配件类型
public function parts()
{
return $this->hasOne(ProductCompatPartsModel::class, 'id', 'part_id');
}
// 关联兼容型号
public function compatibleModels()
{
return $this->hasMany(ProductCompatModelsModel::class, 'compat_id');
}
// 按所属语言查询
public function scopeLanguage(\think\db\Query $query, int $value)
{
$query->where('language_id', $value);
}
// 按类型查询
public function scopeTypeId(\think\db\Query $query, int $value)
{
$query->where('type_id', '=', $value);
}
// 按配件查询
public function scopePartId(\think\db\Query $query, int $value)
{
$query->where('part_id', '=', $value);
}
// 按型号查询
public function scopeModelName(\think\db\Query $query, string|array $value)
{
if (is_array($value)) {
$query->where('model', 'in', $value);
return;
}
$query->where('model', '=', $value);
}
// 按品牌名称搜索
public function searchBrandNameAttr(\think\db\Query $query, string|null $value, array $data)
{
if (is_null($value)) return;
$query->where('brand_name', 'like', '%' . $value . '%');
}
// 按型号搜索
public function searchModelAttr(\think\db\Query $query, string|null $value, array $data)
{
if (is_null($value)) return;
$query->where('model', 'like', '%' . $value . '%');
}
// 按兼容型号搜索
public function searchCompatibleModelAttr(\think\db\Query $query, string|null $value, array $data)
{
if (is_null($value)) return;
$table = $this->getTable();
$relation_table = ProductCompatModelsModel::getTable();
$where = ProductCompatModelsModel::where($relation_table . '.compatible_model', 'like', '%' . $value . '%')
->where('compat_id', '=', $table . '.id')
->fetchSql(true)
->select();
$query->whereExists($where);
}
}