You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/job/service/goods/Collector.php

754 lines
31 KiB

11 months ago
<?php
6 months ago
11 months ago
declare (strict_types=1);
namespace app\job\service\goods;
use app\store\service\Upload as UploadService;
use app\common\service\BaseService;
use app\common\model\Spec as SpecModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsSku as GoodsSkuModel;
use app\common\model\GoodsImage as GoodsImageModel;
8 months ago
use app\store\model\GoodsImage as GoodsImageModel1;
11 months ago
use app\common\model\store\Setting as SettingModel;
use app\common\model\GoodsSpecRel as GoodsSpecRelModel;
use app\common\model\goods\Collector as GoodsCollectorModel;
use app\common\model\GoodsCategoryRel as GoodsCategoryRelModel;
use app\common\enum\Setting as SettingEnum;
use app\common\enum\file\FileType as FileTypeEnum;
use app\common\enum\goods\SpecType as GoodsSpecTypeEnum;
use app\common\enum\goods\DeductStockType as DeductStockTypeEnum;
use app\common\enum\goods\CollectorStatus as GoodsCollectorStatusEnum;
use app\common\library\helper;
use app\common\library\Download;
use app\common\library\collector\tools\UrlStore;
use app\common\library\collector\tools\UrlItemId;
use app\common\library\collector\Facade as CollectorFacade;
use cores\exception\BaseException;
/**
* 服务类:商品批量采集
* Class Collector
* @package app\job\service\goods
*/
class Collector extends BaseService
{
/**
* 商品采集记录
* @var GoodsCollectorModel|null
*/
private ?GoodsCollectorModel $record = null;
/**
* 错误日志记录
* @var array
*/
private array $errorLog = [];
/**
* 采集成功的数量
* @var int
*/
private int $successCount = 0;
/**
* 图片url临时记录 (同一个网络图片仅入库一次)
* @var array
*/
private array $dictImageUrls = [];
/**
* 批量采集商品
* @param array $urls 采集的商品url集
* @param array $form 用户提交的表单
* @param int $recordId 采集记录id
* @param int $storeId 当前商城id
* @return bool
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function batch(array $urls, array $form, int $recordId, int $storeId): bool
{
// 检查采集记录状态是否异常
9 months ago
if (!$this->checkStatusFail($recordId)) {
11 months ago
foreach ($urls as $url) {
try {
// 采集第三方商品数据
$original = $this->collector($url, $storeId);
9 months ago
if ($original['spec_type'] == 20) {
$original['spec_type'] = 10;
$skuList = array_column($original['specData']['skuList'], null, "goods_sku_no");
$goods_price = $skuList[$original['goods_sku_no']]['goods_price'] ?? 0;
$original['goods_price'] = $goods_price;
$original['line_price'] = $goods_price;
$original['data_type'] = 1;
$original['link'] = $url;
unset($original['specData']);
}
11 months ago
// 下载远程商品图片
$original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId);
} catch (\Throwable $e) {
tre($e->getTraceAsString());
$this->errorLog[] = ['url' => trim($url), 'message' => $e->getMessage()];
continue;
}
// 生成商品数据(用于写入数据库)
$data = $this->createData($original, $form, $storeId);
// 事务处理:添加商品
$model = new GoodsModel();
$model->transaction(function () use ($model, $data, $storeId) {
// 添加商品
$model->save($data);
9 months ago
$model->where('goods_id', (int)$model['goods_id'])->update(['spu_id' => (int)$model['goods_id']]);
11 months ago
// 新增商品与分类关联
GoodsCategoryRelModel::increased((int)$model['goods_id'], $data['categoryIds'], $storeId);
// 新增商品与图片关联
GoodsImageModel::increased((int)$model['goods_id'], $data['imagesIds'], $storeId);
// 新增商品与规格关联
GoodsSpecRelModel::increased((int)$model['goods_id'], $data['newSpecList'], $storeId);
// 新增商品sku信息
GoodsSkuModel::add((int)$model['goods_id'], $data['spec_type'], $data['newSkuList'], $storeId);
});
// 记录采集成功
$this->successCount++;
}
9 months ago
}
11 months ago
// 更新采集记录
$this->updateRecord($recordId, \count($urls));
return true;
}
6 months ago
/**
* 后台单个采集
* [single description]
* @param string $url [description]
* @param array $form [description]
* @param int $storeId [description]
* @return [type] [description]
*/
public function single1(string $url, array $form, int $storeId): bool
{
try {
//var_dump($url);
// 采集第三方商品数据
$original = $this->collector1($url, $storeId);
if ($original['spec_type'] == 20) {
$original['spec_type'] = 10;
unset($original['specData']);
}
$original['goods_price'] = $form['goods_price'] ?? 0.00;
$original['line_price'] = $form['goods_price'] ?? 0.00;
$original['cost_price'] = $form['cost_price'] ?? 0.00;
$original['stock_num'] = $form['stock_num'] ?? 0;
$original['remark'] = $form['remark'] ?? "";
$original['cmmdty_model'] = $form['cmmdty_model'] ?? "";
$original['goods_no'] = $form['goods_no'] ?? "";
$original['region'] = $form['region'] ?? "";
$original['region_text'] = $form['region_text'] ?? "";
$original['delivery_time'] = $form['delivery_time'] ?? "";
$original['is_check'] = $form['is_check'] ?? 0;
$original['goods_source'] = $form['goods_source'] ?? "";
$original['data_type'] = 1;
$original['link'] = $url;
// echo "<pre>";
// print_r($original);
// exit();
// 下载远程商品图片
$original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId);
} catch (\Throwable $e) {
// var_dump($e->getMessage());
// exit;
tre($e->getTraceAsString());
$this->errorLog[] = ['url' => trim($url), 'message' => $e->getMessage()];
return false;
}
// 生成商品数据(用于写入数据库)
$data = $this->singleCreateData($original, $form, $storeId);
// echo "<pre>";
// print_r($data);
// exit();
// 事务处理:添加商品
$model = new GoodsModel();
$model->transaction(function () use ($model, $data, $storeId) {
// 添加商品
$model->save($data);
$model->where('goods_id', (int)$model['goods_id'])->update(['spu_id' => (int)$model['goods_id']]);
// 新增商品与分类关联
GoodsCategoryRelModel::increased((int)$model['goods_id'], $data['categoryIds'], $storeId);
// 新增商品与图片关联
GoodsImageModel::increased((int)$model['goods_id'], $data['imagesIds'], $storeId);
// 新增商品与规格关联
GoodsSpecRelModel::increased((int)$model['goods_id'], $data['newSpecList'], $storeId);
// 新增商品sku信息
GoodsSkuModel::add((int)$model['goods_id'], $data['spec_type'], $data['newSkuList'], $storeId);
});
return true;
}
9 months ago
/**
* 后台单个采集
* [single description]
* @param string $url [description]
* @param array $form [description]
* @param int $storeId [description]
* @return [type] [description]
*/
9 months ago
public function single(string $url, array $form, int $storeId): bool
{
try {
6 months ago
//var_dump($url);
9 months ago
// 采集第三方商品数据
$original = $this->collector($url, $storeId);
9 months ago
if ($original['spec_type'] == 20) {
$original['spec_type'] = 10;
6 months ago
if (isset($original['specData'])) {
unset($original['specData']);
}
9 months ago
}
$original['goods_price'] = $form['goods_price'] ?? 0.00;
$original['line_price'] = $form['goods_price'] ?? 0.00;
$original['cost_price'] = $form['cost_price'] ?? 0.00;
$original['stock_num'] = $form['stock_num'] ?? 0;
$original['remark'] = $form['remark'] ?? "";
$original['cmmdty_model'] = $form['cmmdty_model'] ?? "";
$original['goods_no'] = $form['goods_no'] ?? "";
8 months ago
$original['region'] = $form['region'] ?? "";
$original['region_text'] = $form['region_text'] ?? "";
6 months ago
$original['delivery_time'] = $form['delivery_time'] ?? 2;
7 months ago
$original['is_check'] = $form['is_check'] ?? 0;
6 months ago
$original['goods_source'] = $form['goods_source'] ?? "GC";
9 months ago
$original['data_type'] = 1;
$original['link'] = $url;
7 months ago
// echo "<pre>";
// print_r($original);
// exit();
9 months ago
// 下载远程商品图片
$original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId);
} catch (\Throwable $e) {
// var_dump($e->getMessage());
// exit;
tre($e->getTraceAsString());
$this->errorLog[] = ['url' => trim($url), 'message' => $e->getMessage()];
return false;
}
// 生成商品数据(用于写入数据库)
9 months ago
$data = $this->singleCreateData($original, $form, $storeId);
9 months ago
// echo "<pre>";
// print_r($data);
// exit();
9 months ago
// 事务处理:添加商品
$model = new GoodsModel();
$model->transaction(function () use ($model, $data, $storeId) {
// 添加商品
$model->save($data);
9 months ago
$model->where('goods_id', (int)$model['goods_id'])->update(['spu_id' => (int)$model['goods_id']]);
9 months ago
// 新增商品与分类关联
GoodsCategoryRelModel::increased((int)$model['goods_id'], $data['categoryIds'], $storeId);
// 新增商品与图片关联
GoodsImageModel::increased((int)$model['goods_id'], $data['imagesIds'], $storeId);
// 新增商品与规格关联
GoodsSpecRelModel::increased((int)$model['goods_id'], $data['newSpecList'], $storeId);
// 新增商品sku信息
GoodsSkuModel::add((int)$model['goods_id'], $data['spec_type'], $data['newSkuList'], $storeId);
});
return true;
}
8 months ago
/**
* 后台单个采集
* [single description]
* @param string $url [description]
* @param array $form [description]
* @param int $storeId [description]
* @return [type] [description]
*/
public function updateGoods(string $url, array $form, int $storeId): bool
{
8 months ago
$goods = GoodsModel::where('goods_id', $form['goods_id'])->find();
8 months ago
8 months ago
//商品不存在
8 months ago
if (!$goods || !$url) {
7 months ago
return true;
8 months ago
}
6 months ago
//新阙通信的抓取过了,就不抓取了;京东价拖抓取过了就不抓取了
if (($goods['channel'] == 'xqtx' && $goods['content']) || $goods->goods_no_other) {
8 months ago
return true;
}
8 months ago
try {
8 months ago
8 months ago
// 采集第三方商品数据
$original = $this->collector($url, $storeId);
8 months ago
// echo "<pre>";
// print_r($original);
// exit();
8 months ago
if ($original['spec_type'] == 20) {
$original['spec_type'] = 10;
6 months ago
//兼容99api和lt数据源
if (isset($original['specData'])) {
$skuList = array_column($original['specData']['skuList'], null, "goods_sku_no");
$goods_price = $skuList[$original['goods_sku_no']]['goods_price'] ?? 0;
unset($original['specData']);
} else{
$goods_price = $original['goods_price'];
}
8 months ago
$original['goods_price'] = $goods_price;
$original['line_price'] = $goods_price;
8 months ago
$original['data_type'] = 1;//苏宁的数据变成的人工数据了,不再使用苏宁接口更新数据
8 months ago
$original['link_other'] = $url;
6 months ago
8 months ago
}
8 months ago
$form['imageStorage'] = 20;
8 months ago
// 下载远程商品图片
$original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId);
} catch (\Throwable $e) {
// var_dump($e->getMessage());
// exit;
tre($e->getTraceAsString());
$this->errorLog[] = ['url' => trim($url), 'message' => $e->getMessage()];
return false;
}
6 months ago
// if ($original['goods_price'] == 0) {
// return false;
// }
8 months ago
$original['cost_price'] = $form['cost_price_min'];
$data['link_other'] = $url;
6 months ago
8 months ago
$data['content'] = $original['content'];
//重新计算利润和利润率
6 months ago
$data['goods_source'] = $form['goods_source'];
$data['cmmdty_model'] = $form['cmmdty_model'];
$data['delivery_time'] = $form['delivery_time'];
$data['is_check'] = $form['is_check'];
8 months ago
$data['goods_price_min'] = $original['goods_price'];
8 months ago
$data['goods_price_max'] = $original['goods_price'];
8 months ago
$data['line_price_min'] = $original['goods_price'];
8 months ago
$data['line_price_max'] = $original['goods_price'];
8 months ago
$data['profit'] = $original['goods_price'] - $original['cost_price'];
$profit_rate = (float)$original['goods_price'] > 0 ? ($original['goods_price'] - $original['cost_price']) / $original['goods_price'] : 0.00;
$profit_rate = $profit_rate > 0.0001 ? bcmul((string)$profit_rate, "100", 2) : 0.00;
$data['profit_rate'] = $profit_rate;
8 months ago
8 months ago
// echo "<pre>";
// print_r($data);
// exit();
// 事务处理:添加商品
$model = new GoodsModel();
8 months ago
$goodsSku = [
'goods_price' => $original['goods_price'],
'goods_sku_no' => $original['goods_sku_no'],
];
//新阙通信的数据
if ($goods->channel == "xqtx") {
6 months ago
$data['goods_no'] = $original['goods_sku_no'];
8 months ago
//goods
$data['stock_total'] = $form['stock_total'];
8 months ago
$data['goods_name'] = $original['goods_name'];
8 months ago
//设置当前商品价格
$data['profit'] = $original['goods_price'] - $original['cost_price'];
$profit_rate = (float)$original['goods_price'] > 0 ? ($original['goods_price'] - $original['cost_price']) / $original['goods_price'] : 0.00;
$profit_rate = $profit_rate > 0.0001 ? bcmul((string)$profit_rate, "100", 2) : 0.00;
$data['profit_rate'] = $profit_rate;
6 months ago
$data['link'] = $data['link_other'];
8 months ago
unset($data['link_other']);
//sku
$goodsSku['stock_num'] = $form['stock_total'];
8 months ago
//unset($goodsSku['goods_price']);
6 months ago
} else {
//京东价拖
$data['goods_no_other'] = $original['goods_sku_no'];
8 months ago
}
// var_dump($data);
// var_dump($goodsSku);
// exit();
$model->transaction(function () use ($form, $data, $original, $goodsSku) {
8 months ago
// 添加商品
GoodsModel::where('goods_id', $form['goods_id'])->update($data);
// 新增商品与图片关联
GoodsImageModel1::updates((int)$form['goods_id'], $original['imagesIds']);
//更新sku信息
8 months ago
GoodsSkuModel::where('goods_id', $form['goods_id'])->update($goodsSku);
8 months ago
});
return true;
}
11 months ago
/**
* 检查采集记录状态是否异常
* @param int $recordId
* @return bool
*/
private function checkStatusFail(int $recordId): bool
{
$model = $this->getRecord($recordId);
return $model['status'] == GoodsCollectorStatusEnum::FAIL || $model['is_delete'];
}
6 months ago
/**
6 months ago
* 抓取第三方商品内容-lt数据源
6 months ago
* @param string $url
* @param int $storeId
* @return array
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
6 months ago
public function collector1(string $url, int $storeId): array
6 months ago
{
// 获取商品来源
$store = $this->getStore($url);
// 获取url中的商品ID
$itemId = $this->getItemId($url, $store);
6 months ago
$res = getJdGoodsBySku([$itemId]);
$item = $res[$itemId] ?? [];
$item['spec_type'] = 20;//设置默认走多规格的逻辑
$item['goodsImages'] = $item['mainImages'];
$item['goods_price'] = $item['proxyPrice'];
$item['goods_name'] = $item['name'];
$item['imageStorage'] = 10;
11 months ago
6 months ago
// echo "<pre>";
// print_r($item);
6 months ago
// exit();
6 months ago
$item['goods_sku_no'] = $itemId;
return $item;
}
11 months ago
/**
6 months ago
* 抓取第三方商品内容-99api数据源
11 months ago
* @param string $url
* @param int $storeId
* @return array
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
6 months ago
public function collector(string $url, int $storeId): array
11 months ago
{
// 获取商品来源
$store = $this->getStore($url);
// 获取url中的商品ID
$itemId = $this->getItemId($url, $store);
// 商城采集设置
$config = SettingModel::getItem(SettingEnum::COLLECTOR, $storeId);
// 请求API查询商品详情
9 months ago
$item = CollectorFacade::store($store)
11 months ago
->setOptions($config['config'][$config['provider']])
->detail($itemId);
9 months ago
$item['goods_sku_no'] = $itemId;
return $item;
11 months ago
}
/**
* 根据url获取商品来源
* @param string $url
* @return string
* @throws BaseException
*/
private function getStore(string $url): string
{
$UrlStore = new UrlStore($url);
if (!$store = $UrlStore->getStore()) {
throwError('不支持该网址的采集,请检查url是否正确');
}
return $store;
}
/**
* 获取url中的商品ID
* @param string $url
* @param string $store
* @return string
* @throws BaseException
*/
private function getItemId(string $url, string $store): string
{
$UrlItemId = new UrlItemId($url, $store);
if (!$itemId = $UrlItemId->getItemId()) {
throwError('未能获取到商品ID,请检查url是否正确');
}
return $itemId;
}
/**
* 更新采集记录
* @param int $recordId 商品采集记录ID
* @param int $currentCount 当前任务采集的商品总量
* @return void
*/
private function updateRecord(int $recordId, int $currentCount)
{
// 获取采集记录
$model = $this->getRecord($recordId);
// 计算采集失败的数量
$failCount = $currentCount - $this->successCount;
// 更新采集记录
$model->save([
'success_count' => $model['success_count'] + $this->successCount,
'fail_count' => $model['fail_count'] + $failCount,
'fail_log' => array_merge($model['fail_log'], $this->errorLog),
]);
// 判断是否为最后一次队列
if ($model['total_count'] <= ($model['success_count'] + $model['fail_count'])) {
$model->save(['end_time' => \time(), 'status' => GoodsCollectorStatusEnum::COMPLETED]);
}
}
/**
* 获取采集记录
* @param int $recordId 商品采集记录ID
* @return GoodsCollectorModel|array|null
*/
private function getRecord(int $recordId)
{
if (!$this->record) {
$this->record = GoodsCollectorModel::detail($recordId);
}
return $this->record;
}
9 months ago
/**
* 生成商品数据(用于写入数据库)
* @param array $original 商品原始数据
* @param array $form 用户提交的表单
* @param int $storeId 当前商城id
* @return array
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function singleCreateData(array $original, array $form, int $storeId): array
{
// 整理商品数据
$data = [
'goods_type' => $form['goods_type'],
9 months ago
'delivery_id' => $form['delivery_id'] ?? 0,
9 months ago
'goods_name' => $original['goods_name'],
8 months ago
'goods_no' => $original['goods_sku_no'] ?? "",
7 months ago
'is_check' => $original['is_check'] ?? 0,
'delivery_time' => $original['delivery_time'] ?? 0,
'goods_source' => $original['goods_source'] ?? "",
9 months ago
'data_type' => $original['data_type'],//数据类型
'link' => $original['link'],//采集地址
9 months ago
'spec_type' => $original['spec_type'],
'delivery_id' => $form['delivery_id'] ?? 0,
'content' => $original['content'] ?? '',
'sort' => 100,
'deduct_stock_type' => DeductStockTypeEnum::CREATE,
'status' => $form['goods_status'],
'imagesIds' => $original['imagesIds'],
'categoryIds' => $form['categoryIds'],
// 下面是默认数据, 没有会报错
'alone_grade_equity' => [],
'newSpecList' => [],
'newSkuList' => [],
'store_id' => $storeId,
6 months ago
'merchant_id' => $form['merchant_id'] ?? 0,
9 months ago
];
if (isset($form['channel'])) {
$data['channel'] = $form['channel'];
}
9 months ago
$data['unicode'] = $data['channel']."-0-".$original['goods_sku_no'];
9 months ago
// 整理商品的价格和库存总量
if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) {
$data['stock_total'] = GoodsSkuModel::getStockTotal($original['specData']['skuList']);
[$data['goods_price_min'], $data['goods_price_max']] = GoodsSkuModel::getGoodsPrices($original['specData']['skuList']);
[$data['line_price_min'], $data['line_price_max']] = GoodsSkuModel::getLinePrices($original['specData']['skuList']);
} elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) {
$data['goods_price_min'] = $data['goods_price_max'] = $original['goods_price'];
$data['line_price_min'] = $data['line_price_max'] = $original['goods_price'];
$data['cost_price_min'] = $original['cost_price'] ?? 0.00;
8 months ago
$data['profit'] = (float)$original['goods_price'] - (float)$original['cost_price'];
$profit_rate = (float)$original['goods_price'] > 0 ? ((float)$original['goods_price'] - (float)$original['cost_price']) / (float)$original['goods_price'] : 0.00;
9 months ago
$profit_rate = $profit_rate > 0.0001 ? bcmul((string)$profit_rate, "100", 2) : 0.00;
$data['profit_rate'] = $profit_rate;
$data['stock_total'] = $original['stock_num'];
$data['remark'] = $original['remark'] ?? "";
$data['cmmdty_model'] = $original['cmmdty_model'] ?? "";
$data['goods_no'] = $original['goods_no'] ?? "";
8 months ago
$data['region'] = $original['region'] ?? "";
$data['region_text'] = $original['region_text'] ?? "";
9 months ago
}
// 规格和sku数据处理
if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) {
// 验证规格值是否合法
SpecModel::checkSpecData($original['specData']['specList']);
// 生成多规格数据 (携带id)
$data['newSpecList'] = SpecModel::getNewSpecList($original['specData']['specList'], $storeId);
// 生成skuList (携带goods_sku_id)
$data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $original['specData']['skuList']);
} elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) {
// 生成skuItem
9 months ago
$data['newSkuList'] = helper::pick($original, ['goods_sku_no','goods_price', 'line_price', 'cost_price','stock_num', 'goods_weight']);
9 months ago
}
return $data;
}
11 months ago
/**
* 生成商品数据(用于写入数据库)
* @param array $original 商品原始数据
* @param array $form 用户提交的表单
* @param int $storeId 当前商城id
* @return array
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function createData(array $original, array $form, int $storeId): array
{
// 整理商品数据
$data = [
'goods_type' => $form['goods_type'],
'goods_name' => $original['goods_name'],
8 months ago
'goods_no' => $original['goods_sku_no'] ?? "",
9 months ago
'data_type' => $original['data_type'],//数据类型
'link' => $original['link'],//采集地址
11 months ago
'spec_type' => $original['spec_type'],
'delivery_id' => $form['delivery_id'] ?? 0,
'content' => $original['content'] ?? '',
'sort' => 100,
'deduct_stock_type' => DeductStockTypeEnum::CREATE,
'status' => $form['goods_status'],
'imagesIds' => $original['imagesIds'],
'categoryIds' => $form['categoryIds'],
// 下面是默认数据, 没有会报错
'alone_grade_equity' => [],
'newSpecList' => [],
'newSkuList' => [],
'store_id' => $storeId,
6 months ago
'merchant_id' => $form['merchant_id'] ?? 0,
11 months ago
];
9 months ago
if (isset($form['channel'])) {
9 months ago
$data['channel'] = $form['channel'];
}
11 months ago
// 整理商品的价格和库存总量
if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) {
$data['stock_total'] = GoodsSkuModel::getStockTotal($original['specData']['skuList']);
[$data['goods_price_min'], $data['goods_price_max']] = GoodsSkuModel::getGoodsPrices($original['specData']['skuList']);
[$data['line_price_min'], $data['line_price_max']] = GoodsSkuModel::getLinePrices($original['specData']['skuList']);
} elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) {
$data['goods_price_min'] = $data['goods_price_max'] = $original['goods_price'];
$data['line_price_min'] = $data['line_price_max'] = $original['line_price'];
9 months ago
$data['line_price_min'] = $data['line_price_max'] = $original['line_price'];
9 months ago
$data['stock_total'] = $original['stock_num'] ?? 100;
11 months ago
}
// 规格和sku数据处理
if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) {
// 验证规格值是否合法
SpecModel::checkSpecData($original['specData']['specList']);
// 生成多规格数据 (携带id)
$data['newSpecList'] = SpecModel::getNewSpecList($original['specData']['specList'], $storeId);
// 生成skuList (携带goods_sku_id)
$data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $original['specData']['skuList']);
} elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) {
// 生成skuItem
9 months ago
$data['newSkuList'] = helper::pick($original, ['goods_sku_no','goods_price', 'line_price', 'stock_num', 'goods_weight']);
11 months ago
}
return $data;
}
/**
* 下载远程商品图片
* @param array $original 商品信息
* @param int $imageStorage 商品主图 (10下载到本地、20使用源图片url)
* @param int $storeId
* @return array
* @throws BaseException
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function thirdPartyImages(array $original, int $imageStorage, int $storeId): array
{
// 保存商品主图
$original['imagesIds'] = [];
foreach ($original['goodsImages'] as $imageUrl) {
$original['imagesIds'][] = $imageStorage === 10 ? $this->downImage($imageUrl, $storeId)
: $this->externalImage($imageUrl, $storeId);
}
// 保存商品SKU封面图
if ($original['spec_type'] === GoodsSpecTypeEnum::MULTI) {
foreach ($original['specData']['skuList'] as &$skuItem) {
if (!empty($skuItem['imageUrl'])) {
$skuItem['image_id'] = $imageStorage === 10 ? $this->downImage($skuItem['imageUrl'], $storeId)
: $this->externalImage($skuItem['imageUrl'], $storeId);
}
}
}
return $original;
}
/**
* 将远程图片外链保存到文件库中
* @param string $imageUrl 远程图片url
* @param int $storeId
* @return mixed
* @throws BaseException
*/
private function externalImage(string $imageUrl, int $storeId)
{
// 如果已存在该文件则直接返回文件ID
if (isset($this->dictImageUrls[$imageUrl])) {
return $this->dictImageUrls[$imageUrl];
}
// 文件外链记录到文件库
$UploadService = (new UploadService)->setStoreId($storeId);
if (!$UploadService->uploadByExternal(FileTypeEnum::IMAGE, $imageUrl)) {
throwError('图片记录失败:' . $UploadService->getError());
}
// 记录文件ID
$fileId = $UploadService->getFileInfo()['file_id'];
$this->dictImageUrls[$imageUrl] = $fileId;
return $fileId;
}
/**
* 将远程图片下载并保存到文件库中
* @param string $imageUrl 远程图片url
* @param int $storeId
* @return mixed
* @throws BaseException
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function downImage(string $imageUrl, int $storeId)
{
// 如果已存在该文件则直接返回文件ID
if (isset($this->dictImageUrls[$imageUrl])) {
return $this->dictImageUrls[$imageUrl];
}
// 下载远程图片
$filePath = (new Download)->saveTempImage($storeId, $imageUrl);
// 文件上传到文件库
$UploadService = (new UploadService)->setStoreId($storeId);
if (!$UploadService->uploadByLocal(FileTypeEnum::IMAGE, $filePath)) {
throwError('图片上传失败:' . $UploadService->getError());
}
// 记录文件ID
$fileId = $UploadService->getFileInfo()['file_id'];
$this->dictImageUrls[$imageUrl] = $fileId;
return $fileId;
}
}