164 lines
4.4 KiB
PHP
164 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Service\CodeGeneration;
|
|
|
|
use App\BaseApp\BaseService;
|
|
use App\Models\ProductImageModel;
|
|
use App\Models\ProductModel;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class ProductService extends BaseService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->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'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取产品列表
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function list(): array
|
|
{
|
|
$result = $this->getPageList();
|
|
foreach ($result['items'] as &$v) {
|
|
$v['images'] = array_column($v['images'], 'url');
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 获取产品详情
|
|
* @param $id
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function detail($id)
|
|
{
|
|
return $this->getDetail($id);
|
|
}
|
|
|
|
/**
|
|
* 产品下拉列表
|
|
* @return mixed
|
|
*/
|
|
public function option()
|
|
{
|
|
$this->optionField = ['id', 'name'];
|
|
return $this->getOption();
|
|
}
|
|
|
|
/**
|
|
* 创建产品
|
|
* @param $params
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function create($params): mixed
|
|
{
|
|
$images = $params['images'];
|
|
unset($params['images']);
|
|
$params['user_id'] = $this->userId;
|
|
DB::beginTransaction();
|
|
try {
|
|
|
|
$result = $this->insert($params);
|
|
$insertData = [];
|
|
foreach ($images as $v) {
|
|
$insertData[] = [
|
|
'product_id' => $result,
|
|
'url' => $v
|
|
];
|
|
}
|
|
$imageModel = ProductImageModel::insert($insertData);
|
|
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'];
|
|
unset($params['images']);
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
}
|