lqmac 9 months ago
parent 517519a7a3
commit 4896201878
  1. 4
      app/api/controller/goods/Collector.php
  2. 16
      app/job/service/goods/AdminImport.php
  3. 94
      app/job/service/goods/Collector.php

@ -10,10 +10,10 @@
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\controller\goods;
namespace app\api\controller\goods;
use think\response\Json;
use app\store\controller\Controller;
use app\api\controller\Controller;
use app\store\model\goods\Collector as CollectorModel;
use cores\exception\BaseException;

@ -34,7 +34,7 @@ use app\common\enum\goods\DeductStockType as DeductStockTypeEnum;
use app\common\validate\goods\AdminImport as GoodsAdminImportValidate;
use cores\exception\BaseException;
use think\facade\Log;
use app\common\model\Channel;
/**
* 服务类:商品批量导入
* Class Import
@ -102,6 +102,7 @@ class AdminImport extends BaseService
{
$service = new \app\job\service\goods\Collector();
foreach ($list as $item) {
$data = $this->createData($item, $storeId);
$service->single($item['D'], $data, $storeId);
@ -171,7 +172,7 @@ class AdminImport extends BaseService
{
// 验证商品信息:商品名称、分类ID集、图片ID集、运费模板ID
$validate = new GoodsAdminImportValidate;
Log::record($data);
//Log::record($data);
if (!$validate->scene('goods')->check($data)) {
$this->setError($validate->getError());
return false;
@ -217,13 +218,18 @@ class AdminImport extends BaseService
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function createData(array $original, int $storeId): array
public function createData(array $original, int $storeId): array
{
$channel = Channel::where('name', $original["B"])->find();
// 整理商品数据
$data = [
'cmmdty_model' => $original["A"],
'channel' => "jd",
'channel' => $channel['code'] ?? "zy",
'goods_no' => $original["C"],
'goods_price' => $original["F"],
'cost_price' => $original["G"],
'stock_num' => $original["H"],
'remark' => $original["I"],
'categoryIds' => $this->ids2array($original["E"]),
'imageStorage' => 10,//下载图片到本地
'goods_type' => 10,//实物
@ -231,7 +237,7 @@ class AdminImport extends BaseService
'store_id' => $storeId,
];
// 过滤不存在的ID集数据
//$data['categoryIds'] = CategoryModel::filterCategoryIds($data['categoryIds'], $storeId);
$data['categoryIds'] = CategoryModel::filterCategoryIds($data['categoryIds'], $storeId);
return $data;
}

@ -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数据处理

Loading…
Cancel
Save