refactor: 产品详细兼容性信息输入及更新
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 6s

This commit is contained in:
2026-07-08 17:45:48 +08:00
parent 36261488b4
commit d8a584e9b7
2 changed files with 34 additions and 7 deletions

View File

@@ -120,6 +120,19 @@ class Product
->bindAttr('product', ['spu'])
->hidden(['product']);
// 兼容性信息
$product->compat = ProductCompatRelModel::withoutField([
'created_at',
'updated_at',
])
->with(['compat' => function($query) {
$query->field(['id', 'label_name']);
}])
->productId($product->id)
->select()
?->bindAttr('compat', ['label_name'])
?->hidden(['compat']);
return success('获取成功', $product);
}
@@ -253,15 +266,23 @@ class Product
// 更新兼容性信息
if (!empty($put['compat'])) {
$compat = $put['compat'];
$compat['product_id'] = $id;
$compat['compat_id'] = explode(',', str_replace('', ',', $compat['compat_id']));
// 删除原先的兼容信息
ProductCompatRelModel::productId($id)->delete();
$rel_id = ProductCompatRelModel::productId($id)->value('id');
if (!empty($rel_id)) {
$compat['id'] = $rel_id;
$compat = [];
$rel_compat = json_decode(data_get($put['compat'], 'compat_id') ?? [], true);
foreach ($rel_compat as $compat_id) {
$compat[] = [
'product_id' => $id,
'compat_id' => $compat_id,
'series_name' => data_get($put['compat'], 'series_name'),
'show_series_name' => data_get($put['compat'], 'show_series_name') ?? 0,
'remark' => data_get($put['compat'], 'remark')
];
}
if (!empty($compat)) {
ProductCompatRelModel::insertAll($compat);
}
(new ProductCompatRelModel)->save($compat);
}
return success('操作成功');

View File

@@ -10,6 +10,12 @@ use app\common\model\ProductCompatRelBaseModel;
*/
class ProductCompatRelModel extends ProductCompatRelBaseModel
{
// 关联兼容性数据
public function compat()
{
return $this->hasOne(ProductCompatModel::class, 'id', 'compat_id');
}
// 按所属产品查询
public function scopeProductId(\think\db\Query $query, int $id)
{