商品、订单部分相关代码生成,下一步先把商品搞完再搞订单
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Service\CodeGeneration;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use App\Models\ProductImageModel;
|
||||
use App\Models\ProductLabelRelationsModel;
|
||||
use App\Models\ProductModel;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -19,7 +20,12 @@ class ProductService extends BaseService
|
||||
$this->queryField = ['title' => 'like','mall_id' => '=','classification_id' => '=,status='];
|
||||
$this->model = ProductModel::class;
|
||||
$this->with = [
|
||||
'images:id,product_id,url'
|
||||
'images:id,product_id,url',
|
||||
'mall:id,name,avatar',
|
||||
'user:id,nick_name,avatar',
|
||||
'labels',
|
||||
'classification',
|
||||
'skus:id,product_id,name,cover,price,inventory',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -34,6 +40,8 @@ class ProductService extends BaseService
|
||||
$result = $this->getPageList();
|
||||
foreach ($result['items'] as &$v) {
|
||||
$v['images'] = array_column($v['images'], 'url');
|
||||
$v['labels_name'] = array_column($v['labels'], 'name');
|
||||
$v['labels'] = array_column($v['labels'], 'id');
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
@@ -46,7 +54,14 @@ class ProductService extends BaseService
|
||||
*/
|
||||
public function detail($id)
|
||||
{
|
||||
return $this->getDetail($id);
|
||||
$result = $this->getDetail($id);
|
||||
$result = $result->toArray();
|
||||
$items = [];
|
||||
foreach ($result['images'] as $v) {
|
||||
$items[] = $v['url'];
|
||||
}
|
||||
$result['images'] = $items;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,7 +70,7 @@ class ProductService extends BaseService
|
||||
*/
|
||||
public function option()
|
||||
{
|
||||
$this->optionField = ['id', 'name'];
|
||||
$this->optionField = ['id', 'title'];
|
||||
return $this->getOption();
|
||||
}
|
||||
|
||||
@@ -68,8 +83,10 @@ class ProductService extends BaseService
|
||||
public function create($params): mixed
|
||||
{
|
||||
$images = $params['images'];
|
||||
unset($params['images']);
|
||||
$labels = $params['labels'];
|
||||
unset($params['images'], $params['labels']);
|
||||
$params['user_id'] = $this->userId;
|
||||
$params['mall_id'] = $this->mallId;
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
|
||||
@@ -81,7 +98,20 @@ class ProductService extends BaseService
|
||||
'url' => $v
|
||||
];
|
||||
}
|
||||
|
||||
$imageModel = ProductImageModel::insert($insertData);
|
||||
|
||||
$labelInsertData = [];
|
||||
foreach ($labels as $v) {
|
||||
$labelInsertData[] = [
|
||||
'product_id' => $result,
|
||||
'label_id' => $v
|
||||
];
|
||||
}
|
||||
$labelModel = ProductLabelRelationsModel::insert($labelInsertData);
|
||||
if (!$imageModel || !$labelModel) {
|
||||
$this->utils->errorThrow('操作失败');
|
||||
}
|
||||
DB::commit();
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
@@ -102,7 +132,8 @@ class ProductService extends BaseService
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$images = $params['images'];
|
||||
unset($params['images']);
|
||||
$labels = $params['labels'];
|
||||
unset($params['images'], $params['labels']);
|
||||
// 1. 更新产品基本信息
|
||||
$model = $this->save($id, $params);
|
||||
|
||||
@@ -139,6 +170,39 @@ class ProductService extends BaseService
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 处理标签逻辑
|
||||
if (isset($images)) {
|
||||
$newLabels = $labels;
|
||||
|
||||
// 获取当前产品已有的图片URL列表
|
||||
$existingLabels = ProductLabelRelationsModel::where('product_id', $id)
|
||||
->pluck('label_id')
|
||||
->toArray();
|
||||
|
||||
// 计算新增的图片和被删除的图片
|
||||
$addedLabels = array_diff($newLabels, $existingLabels);
|
||||
$deletedLabels = array_diff($existingLabels, $newLabels);
|
||||
|
||||
// 删除不再需要的图片记录
|
||||
if (!empty($deletedLabels)) {
|
||||
ProductLabelRelationsModel::where('product_id', $id)
|
||||
->whereIn('label_id', $deletedLabels)
|
||||
->delete();
|
||||
}
|
||||
|
||||
// 添加新图片记录
|
||||
if (!empty($addedLabels)) {
|
||||
$insertData = [];
|
||||
foreach ($addedLabels as $url) {
|
||||
$insertData[] = [
|
||||
'product_id' => $id,
|
||||
'label_id' => $url
|
||||
];
|
||||
}
|
||||
ProductLabelRelationsModel::insert($insertData);
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Reference in New Issue
Block a user