selectField = ["id", "title", "mall_id", "user_id", "introduction", "cover", "description", "quantity_sold", "price", "classification_id", "status", "created_at", "updated_at", "deleted_at"]; $this->queryField = ['title' => 'like','mall_id' => '=','classification_id' => '=,status=']; $this->model = ProductModel::class; $this->with = [ 'images:id,product_id,url', 'mall:id,name,avatar', 'user:id,nick_name,avatar', 'labels', 'classification', 'skus:id,product_id,name,cover,price,inventory', ]; } /** * 获取产品列表 * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function list(): array { $this->with = [ 'images:id,product_id,url', 'mall:id,name,avatar', 'user:id,nick_name,avatar', 'labels', 'classification', 'skus:id,product_id,name,cover,price,inventory', ]; $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; } /** * 获取产品详情 * @param $id * @return mixed * @throws Exception */ public function detail($id) { $result = $this->getDetail($id); $result = $result->toArray(); $items = []; foreach ($result['images'] as $v) { $items[] = $v['url']; } $result['images'] = $items; return $result; } /** * 产品下拉列表 * @return mixed */ public function option() { $this->optionField = ['id', 'title']; return $this->getOption(); } /** * 创建产品 * @param $params * @return mixed * @throws Exception */ public function create($params): mixed { $images = $params['images']; $labels = $params['labels']; unset($params['images'], $params['labels']); $params['user_id'] = $this->userId; $params['mall_id'] = $this->mallId; DB::beginTransaction(); try { $result = $this->insert($params); $insertData = []; foreach ($images as $v) { $insertData[] = [ 'product_id' => $result, '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(); $this->utils->errorThrow($e->getMessage()); } return $imageModel; } /** * 编辑产品 * @param $id * @param $params * @return mixed * @throws Exception */ public function update($id, $params): mixed { DB::beginTransaction(); try { $images = $params['images']; $labels = $params['labels']; unset($params['images'], $params['labels']); // 1. 更新产品基本信息 $model = $this->save($id, $params); // 2. 处理图片逻辑 if (isset($images)) { $newImages = $images; // 获取当前产品已有的图片URL列表 $existingImages = ProductImageModel::where('product_id', $id) ->pluck('url') ->toArray(); // 计算新增的图片和被删除的图片 $addedImages = array_diff($newImages, $existingImages); $deletedImages = array_diff($existingImages, $newImages); // 删除不再需要的图片记录 if (!empty($deletedImages)) { ProductImageModel::where('product_id', $id) ->whereIn('url', $deletedImages) ->delete(); } // 添加新图片记录 if (!empty($addedImages)) { $insertData = []; foreach ($addedImages as $url) { $insertData[] = [ 'product_id' => $id, 'url' => $url ]; } ProductImageModel::insert($insertData); } } // 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(); $this->utils->errorThrow($e->getMessage()); } // 返回更新后的产品信息 return $model; } /** * 删除产品 * @param $ids * @return mixed|true * @throws Exception */ public function delete($ids): mixed { return $this->del($ids); } }