refactor: 产品详细兼容性信息输入及更新
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 6s
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 6s
This commit is contained in:
@@ -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('操作成功');
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user