|
|
|
@ -116,12 +116,34 @@ class Collector extends BaseService |
|
|
|
|
$this->updateRecord($recordId, \count($urls)); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 后台单个采集 |
|
|
|
|
* [single description] |
|
|
|
|
* @param string $url [description] |
|
|
|
|
* @param array $form [description] |
|
|
|
|
* @param int $storeId [description] |
|
|
|
|
* @return [type] [description] |
|
|
|
|
*/ |
|
|
|
|
public function single(string $url, array $form, int $storeId): bool |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
// 采集第三方商品数据 |
|
|
|
|
$original = $this->collector($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 = $this->thirdPartyImages($original, $form['imageStorage'], $storeId); |
|
|
|
|
} catch (\Throwable $e) { |
|
|
|
@ -132,7 +154,7 @@ class Collector extends BaseService |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
// 生成商品数据(用于写入数据库) |
|
|
|
|
$data = $this->createData($original, $form, $storeId); |
|
|
|
|
$data = $this->singleCreateData($original, $form, $storeId); |
|
|
|
|
// 事务处理:添加商品 |
|
|
|
|
$model = new GoodsModel(); |
|
|
|
|
$model->transaction(function () use ($model, $data, $storeId) { |
|
|
|
@ -252,7 +274,74 @@ class Collector extends BaseService |
|
|
|
|
} |
|
|
|
|
return $this->record; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 生成商品数据(用于写入数据库) |
|
|
|
|
* @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'], |
|
|
|
|
'goods_name' => $original['goods_name'], |
|
|
|
|
'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, |
|
|
|
|
]; |
|
|
|
|
if (isset($form['channel'])) { |
|
|
|
|
$data['channel'] = $form['channel']; |
|
|
|
|
} |
|
|
|
|
// 整理商品的价格和库存总量 |
|
|
|
|
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['line_price_min'] = $data['line_price_max'] = $original['goods_price']; |
|
|
|
|
$data['cost_price_min'] = $original['cost_price'] ?? 0.00; |
|
|
|
|
$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; |
|
|
|
|
|
|
|
|
|
$data['stock_total'] = $original['stock_num']; |
|
|
|
|
$data['remark'] = $original['remark'] ?? ""; |
|
|
|
|
$data['cmmdty_model'] = $original['cmmdty_model'] ?? ""; |
|
|
|
|
$data['goods_no'] = $original['goods_no'] ?? ""; |
|
|
|
|
} |
|
|
|
|
// 规格和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 |
|
|
|
|
$data['newSkuList'] = helper::pick($original, ['goods_price', 'line_price', 'cost_price','stock_num', 'goods_weight']); |
|
|
|
|
} |
|
|
|
|
return $data; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* 生成商品数据(用于写入数据库) |
|
|
|
|
* @param array $original 商品原始数据 |
|
|
|
@ -295,6 +384,7 @@ class Collector extends BaseService |
|
|
|
|
} 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']; |
|
|
|
|
$data['line_price_min'] = $data['line_price_max'] = $original['line_price']; |
|
|
|
|
$data['stock_total'] = $original['stock_num']; |
|
|
|
|
} |
|
|
|
|
// 规格和sku数据处理 |
|
|
|
|