lqmac 10 months ago
parent f863b630d9
commit a0b0c01c77
  1. 34
      app/admin/controller/Category.php
  2. 17
      app/admin/model/Store.php
  3. 3
      app/api/controller/Checkout.php
  4. 56
      app/api/model/Goods.php
  5. 108
      app/command/UpdateCollectGoodsPrice.php
  6. 7
      app/common/model/Category.php
  7. 13
      app/common/model/Goods.php
  8. 4
      app/common/model/GoodsSku.php
  9. 27
      app/common/model/GoodsSpecRel.php
  10. 7
      app/common/model/Store.php
  11. 4
      app/job/service/goods/AdminImport.php
  12. 45
      app/job/service/goods/Collector.php
  13. 5
      app/store/model/Category.php
  14. 49
      app/store/model/Goods.php
  15. 42
      app/store/model/GoodsSku.php
  16. 7
      app/store/model/goods/Collector.php
  17. 16
      app/store/model/goods/Import.php
  18. 1
      config/console.php

@ -87,7 +87,7 @@ class Category extends Controller
return $this->renderError($model->getError() ?: '更新失败'); return $this->renderError($model->getError() ?: '更新失败');
} }
public function copyCategory(int $new_store_id){ public function copyCategory(int $new_store_id){
$store_id = 0; $store_id = 10001;
$model = new CategoryModel; $model = new CategoryModel;
$list = $model->getList(['store_id' =>$store_id]); $list = $model->getList(['store_id' =>$store_id]);
if (!$list) { if (!$list) {
@ -99,6 +99,7 @@ class Category extends Controller
$value['store_id'] = $new_store_id; $value['store_id'] = $new_store_id;
$value['create_time'] = time(); $value['create_time'] = time();
$value['update_time'] = time(); $value['update_time'] = time();
$value['image_id'] = $value['image_id'] ? $this->copyImage($value['image_id'], $new_store_id) : 0;
$temp = $value; $temp = $value;
unset($temp['children']); unset($temp['children']);
unset($temp['image']); unset($temp['image']);
@ -114,6 +115,7 @@ class Category extends Controller
$value1['store_id'] = $new_store_id; $value1['store_id'] = $new_store_id;
$value1['create_time'] = time(); $value1['create_time'] = time();
$value1['update_time'] = time(); $value1['update_time'] = time();
$value1['image_id'] = $value1['image_id'] ? $this->copyImage($value1['image_id'], $new_store_id) : 0;
$temp1 = $value1; $temp1 = $value1;
unset($temp1['children']); unset($temp1['children']);
unset($temp1['image']); unset($temp1['image']);
@ -129,17 +131,45 @@ class Category extends Controller
$value2['store_id'] = $new_store_id; $value2['store_id'] = $new_store_id;
$value2['create_time'] = time(); $value2['create_time'] = time();
$value2['update_time'] = time(); $value2['update_time'] = time();
$value2['image_id'] = $value2['image_id'] ? $this->copyImage($value2['image_id'], $new_store_id) : 0;
$temp2 = $value2; $temp2 = $value2;
unset($temp2['children']); unset($temp2['children']);
unset($temp2['image']); unset($temp2['image']);
unset($temp2['category_id']); unset($temp2['category_id']);
Db::table('yoshop_category')->insertGetId($temp2); Db::table('yoshop_category')->insertGetId($temp2);
if (!isset($value2['children']) || !$value2['children']) {
continue;
}
foreach ($value2['children'] as $value3) {
$value3 = $value3->toArray();
$value3['parent_id'] = $secondid;
$value3['store_id'] = $new_store_id;
$value3['create_time'] = time();
$value3['update_time'] = time();
$value3['image_id'] = $value3['image_id'] ? $this->copyImage($value3['image_id'], $new_store_id) : 0;
$temp3 = $value3;
unset($temp3['children']);
unset($temp3['image']);
unset($temp3['category_id']);
Db::table('yoshop_category')->insertGetId($temp3);
}
} }
} }
} }
return $this->renderSuccess($list); return $this->renderSuccess($list);
} }
public function copyImage($image_id, $store_id){
$upload_file = DB::table("yoshop_upload_file")->where('file_id', $image_id)->find();
if (!$upload_file) {
return 0;
}
$upload_file['store_id'] = $store_id;
$upload_file['create_time'] = time();
unset($upload_file['file_id']);
$new_image_id = DB::table("yoshop_upload_file")->insertGetId($upload_file);
return $new_image_id;
}
} }

@ -127,6 +127,23 @@ class Store extends StoreModel
unset($temp2['category_id']); unset($temp2['category_id']);
Db::table('yoshop_category')->insertGetId($temp2); Db::table('yoshop_category')->insertGetId($temp2);
if (!isset($value2['children']) || !$value2['children']) {
continue;
}
foreach ($value2['children'] as $value3) {
$value3 = $value3->toArray();
$value3['parent_id'] = $secondid;
$value3['store_id'] = $new_store_id;
$value3['create_time'] = time();
$value3['update_time'] = time();
$value3['image_id'] = $value3['image_id'] ? $this->copyImage($value3['image_id'], $new_store_id) : 0;
$temp3 = $value3;
unset($temp3['children']);
unset($temp3['image']);
unset($temp3['category_id']);
Db::table('yoshop_category')->insertGetId($temp3);
}
} }
} }
} }

@ -92,6 +92,9 @@ class Checkout extends Controller
); );
// 获取订单确认信息 // 获取订单确认信息
$orderInfo = $Checkout->onCheckout($goodsList); $orderInfo = $Checkout->onCheckout($goodsList);
// echo "<pre>";
// print_r($orderInfo);
// exit();
if ($this->request->isGet()) { if ($this->request->isGet()) {
return $this->renderSuccess([ return $this->renderSuccess([
'order' => $orderInfo, 'order' => $orderInfo,

@ -284,8 +284,10 @@ class Goods extends GoodsModel
->where([ ->where([
'a.store_id' => request()->header()['storeid'], 'a.store_id' => request()->header()['storeid'],
'a.status' => 10, 'a.status' => 10,
'c.parent_id' => 0, //'c.parent_id' => 0,
])->whereIn('a.goods_id', $goods)->field('c.*,a.sales_initial,a.sales_actual')->select()->toArray(); ])->whereIn('a.goods_id', $goods)->field('c.*,a.sales_initial,a.sales_actual')->select()->toArray();
// var_dump($list);
// exit();
$list = $this->removeDuplicatesByField($list, 'category_id'); $list = $this->removeDuplicatesByField($list, 'category_id');
$info['end_time'] = strtotime($info->p_time) - time(); $info['end_time'] = strtotime($info->p_time) - time();
$res['info'] = $info; $res['info'] = $info;
@ -348,32 +350,32 @@ class Goods extends GoodsModel
$goodsInfo->specifications = []; $goodsInfo->specifications = [];
$skuList = []; $skuList = [];
$specList = []; $specList = [];
if ($goodsInfo->spu_id > 0) { // if ($goodsInfo->spu_id > 0) {
//查同规格的商品 // //查同规格的商品
$spe = GoodsModel::where([ // $spe = GoodsModel::where([
'spu_id' => $goodsInfo->spu_id, // 'spu_id' => $goodsInfo->spu_id,
'store_id' => request()->header()['storeid'], // 'store_id' => request()->header()['storeid'],
'is_delete' => 0 // 'is_delete' => 0
])->column('goods_id'); // ])->column('goods_id');
foreach ($spe as $k => $v) { // foreach ($spe as $k => $v) {
//$sku1 = $this->getSpecData($v)['skuList']->toArray()[0]; // //$sku1 = $this->getSpecData($v)['skuList']->toArray()[0];
// $skuList[] = $sku1; // // $skuList[] = $sku1;
$spe1 = $this->getSpecData($v)['specList'][0]; // $spe1 = $this->getSpecData($v)['specList'][0];
$spe1['valueList'][0]['goods_id'] = $v; // $spe1['valueList'][0]['goods_id'] = $v;
$aa = 0; // $aa = 0;
foreach ($specList as &$v2) { // foreach ($specList as &$v2) {
if ($v2['spec_name'] == $spe1['spec_name']) { // if ($v2['spec_name'] == $spe1['spec_name']) {
$aa = 1; // $aa = 1;
$v2['valueList'][] = $spe1['valueList']; // $v2['valueList'][] = $spe1['valueList'];
break; // break;
} // }
} // }
if ($aa == 0) { // if ($aa == 0) {
$specList[] = $spe1; // $specList[] = $spe1;
} // }
} // }
} // }
$goodsInfo = $goodsInfo->toArray(); $goodsInfo = $goodsInfo->toArray();

@ -0,0 +1,108 @@
<?php
declare (strict_types=1);
namespace app\command;
use think\console\Command;
use think\console\Output;
use think\console\Input;
use think\facade\Db;
use app\common\model\Goods;
use app\common\model\GoodsSku;
// /www/server/php/74/bin/php /server/wwwroot/yanzong/think test
class UpdateCollectGoodsPrice extends Command
{
protected function configure()
{
// 指令配置
$this->setName('UpdateCollectGoodsPrice')->setDescription('自动更新商品的价格');
$this->addArgument("goods_id");
$this->addArgument("div");
$this->addArgument("mod");
}
protected function execute(Input $input, Output $output)
{
$goods_id = $input->getArgument("goods_id");
$div = $input->getArgument("div");
$mod = $input->getArgument("mod");
$limit = 10;
$service = new \app\job\service\goods\Collector();
while (TRUE) {
$sql = "SELECT goods_id,link,goods_price_min,cost_price_min,profit,profit_rate,store_id FROM `yoshop_goods` WHERE goods_id > " . $goods_id . " AND goods_id % ". $div . " = ". $mod ." AND store_id = 0 AND is_delete = 0 AND status = 10 AND link != '' ";
$list = Db::query($sql);
if (!$list) {
return false;
}
foreach ($list as $item) {
try {
$sku = GoodsSku::where('goods_id', $item['goods_id'])->field('goods_sku_no,cost_price,goods_price')->find();
if (!$sku || !$sku['goods_sku_no']) {
echo $item['link'].":sku不存在".PHP_EOL;
continue;
}
$data = $service->collector($item['link'], (int)$item['store_id']);
if (!$data || !isset($data['specData']['skuList']) || !$data['specData']['skuList']) {
echo $item['link'].":没有抓取到sku信息".PHP_EOL;
continue;
}
$skuList = array_column($data['specData']['skuList'], null, "goods_sku_no");
var_dump($skuList);
$goods_price = $skuList[$sku['goods_sku_no']]['goods_price'] ?? 0;
var_dump($goods_price);
//exit();
if (!$goods_price) {
echo $item['link'].":商品价格没有抓取到".PHP_EOL;
continue;
}
$cost_price = $item['cost_price_min'];
$profit = $goods_price - $cost_price;
$profit_rate = (float)$goods_price > 0 ? ($goods_price - $cost_price) / $goods_price : 0.00;
$profit_rate = $profit_rate > 0.0001 ? bcmul($profit_rate, 100, 2) : 0.00;
//更新商品价格
$goodsData = [
'goods_price_min' => $goods_price,
'profit' => $profit,
'profit_rate' => $profit_rate,
'update_time' => time(),
];
Goods::where('goods_id', $item['goods_id'])->update($goodsData);
//更新sku价格
$skuData = [
'goods_price' => $goods_price,
'update_time' => time(),
];
Goods::where('goods_id', $item['goods_id'])->update($goodsData);
} catch (Exception $e) {
echo $e->getMessage().PHP_EOL;
continue;
}
}
// var_dump($list);
// exit();
}
// $page = 1;
// $limit = 1000;
// while (TRUE) {
// $list = Goods::where('spu_id',0)->field('goods_id')->page($page)->limit($limit)->select();
// foreach ($list as $key => $value) {
// $ret = Goods::where('goods_id', $value['goods_id'])->update(['spu_id' => $value['goods_id']]);
// $ret = GoodsSku::where('goods_id', $value['goods_id'])->update(['spu_id' => $value['goods_id']]);
// var_dump($ret);
// }
// $page++;
// }
//var_dump($list);
}
}

@ -37,7 +37,10 @@ class Category extends BaseModel
{ {
return $this->hasOne('UploadFile', 'file_id', 'image_id'); return $this->hasOne('UploadFile', 'file_id', 'image_id');
} }
public function rankimage(): HasOne
{
return $this->hasOne('UploadFile', 'file_id', 'rank_image_id');
}
public function price(): HasMany public function price(): HasMany
{ {
$model = "app\\common\\model\\goods\\GoodsPrice"; $model = "app\\common\\model\\goods\\GoodsPrice";
@ -112,7 +115,7 @@ class Category extends BaseModel
} }
// 查询列表数据 // 查询列表数据
return $this->with(['image']) return $this->with(['image','rankimage'])
->where($filter) ->where($filter)
->order(['sort', 'create_time']) ->order(['sort', 'create_time'])
->select(); ->select();

@ -130,9 +130,16 @@ class Goods extends BaseModel
*/ */
public function skuList(): HasMany public function skuList(): HasMany
{ {
return $this->hasMany('GoodsSku')->order(['id' => 'asc']); return $this->hasMany('GoodsSku','spu_id')->order(['id' => 'asc']);
}
/**
* 关联商品规格表
* @return HasMany
*/
public function skuList1(): HasMany
{
return $this->hasMany('GoodsSku','goods_id')->order(['id' => 'asc']);
} }
/** /**
* 关联商品规格关系表 * 关联商品规格关系表
* @return HasMany * @return HasMany
@ -470,6 +477,8 @@ class Goods extends BaseModel
*/ */
public static function detail(int $goodsId, array $with = []) public static function detail(int $goodsId, array $with = [])
{ {
// var_dump($with);
// exit();
return static::get($goodsId, $with); return static::get($goodsId, $with);
} }
} }

@ -102,7 +102,8 @@ class GoodsSku extends BaseModel
public static function getSkuList(int $goodsId, bool $withImage = false): \think\Collection public static function getSkuList(int $goodsId, bool $withImage = false): \think\Collection
{ {
return (new static)->with($withImage ? ['image'] : []) return (new static)->with($withImage ? ['image'] : [])
->where('goods_id', '=', $goodsId) //->where('goods_id', '=', $goodsId)
->where('spu_id', '=', $goodsId)
->select(); ->select();
} }
@ -173,6 +174,7 @@ class GoodsSku extends BaseModel
if ($specType === SpecTypeEnum::SINGLE) { if ($specType === SpecTypeEnum::SINGLE) {
return (new static)->save(array_merge($newSkuList, [ return (new static)->save(array_merge($newSkuList, [
'goods_id' => $goodsId, 'goods_id' => $goodsId,
'spu_id' => $goodsId,
'goods_sku_id' => 0, 'goods_sku_id' => 0,
'store_id' => $storeId ?: self::$storeId 'store_id' => $storeId ?: self::$storeId
])); ]));

@ -14,7 +14,8 @@ namespace app\common\model;
use cores\BaseModel; use cores\BaseModel;
use think\model\relation\BelongsTo; use think\model\relation\BelongsTo;
use app\common\model\Spec;
use app\common\model\SpecValue;
/** /**
* 商品规格关系模型 * 商品规格关系模型
* Class GoodsSpecRel * Class GoodsSpecRel
@ -77,8 +78,8 @@ class GoodsSpecRel extends BaseModel
($item['spec_id'] == $group['spec_id']) && $group['valueList'][] = [ ($item['spec_id'] == $group['spec_id']) && $group['valueList'][] = [
'key' => isset($group['valueList']) ? count($group['valueList']) : 0, 'key' => isset($group['valueList']) ? count($group['valueList']) : 0,
'groupKey' => $groupKey, 'groupKey' => $groupKey,
'spec_value_id' => $item['specValue']['spec_value_id'], 'spec_value_id' => $item['specValue']['spec_value_id'] ?? 0,
'spec_value' => $item['specValue']['spec_value'] 'spec_value' => $item['specValue']['spec_value'] ?? ""
]; ];
} }
} }
@ -96,6 +97,26 @@ class GoodsSpecRel extends BaseModel
private static function getList(int $goodsId) private static function getList(int $goodsId)
{ {
$list = (new static)->where('goods_id', '=', $goodsId)->select(); $list = (new static)->where('goods_id', '=', $goodsId)->select();
// $spec_ids = array_column($list, "spec_id");
// $spec_value_ids = array_column($list, "spec_value_id");
// $specs = Spec::whereIn('spec_id', $spec_ids)->select()->toArray();
// $specs = array_column($specs, null, "spec_id");
// $spec_values = SpecValue::whereIn('spec_value_id', $spec_value_ids)->select()->toArray();
// $spec_values = array_column($spec_values, null, "spec_id");
// foreach ($list as $key => &$value) {
// $value['spec'] = $specs[$value['spec_id']] ?? [];
// $value['specValue'] = $spec_values[$value['spec_value_id']] ?? [];
// }
// // echo "<pre>";
// // print_r($list);
// // exit();
// return $list;
// echo "<pre>";
// print_r($list->toArray());
//exit();
self::$storeId = 0;
app()->request->setStoreId(0);
return static::preload($list, ['spec', 'specValue']); return static::preload($list, ['spec', 'specValue']);
} }

@ -71,6 +71,13 @@ class Store extends BaseModel
}else{ }else{
$list['newPeopleOrderImg'] = null; $list['newPeopleOrderImg'] = null;
} }
if($list['presale_img_id']){
$presale_img_id = explode(",",$list['presale_img_id']);
$file3 = UploadFile::where('file_id','in' ,$presale_img_id)->select();
$list['presaleImg'] = $file3 ?: null;
}else{
$list['presaleImg'] = null;
}
} }
return $list ?? null; return $list ?? null;
} catch (\Exception $e) { } catch (\Exception $e) {

@ -217,12 +217,14 @@ class AdminImport extends BaseService
'cost_price' => $original["G"], 'cost_price' => $original["G"],
'stock_num' => $original["H"], 'stock_num' => $original["H"],
'remark' => $original["I"], 'remark' => $original["I"],
//'link' => $original["D"],
'categoryIds' => $this->ids2array($original["E"]), 'categoryIds' => $this->ids2array($original["E"]),
'imageStorage' => 10,//下载图片到本地 'imageStorage' => 10,//下载图片到本地
'goods_type' => 10,//实物 'goods_type' => 10,//实物
'goods_status' => 10,//上架 'goods_status' => 10,//上架
'store_id' => $storeId, 'store_id' => $storeId,
'sale_areas' => implode("、", $arr) 'sale_areas' => $arr ? implode("、", $arr) : "",
'import' => 1,//是否是导入采集
]; ];
// echo "<pre>"; // echo "<pre>";
// print_r($data); // print_r($data);

@ -80,11 +80,25 @@ class Collector extends BaseService
public function batch(array $urls, array $form, int $recordId, int $storeId): bool public function batch(array $urls, array $form, int $recordId, int $storeId): bool
{ {
// 检查采集记录状态是否异常 // 检查采集记录状态是否异常
if (!$this->checkStatusFail($recordId)) { //if (!$this->checkStatusFail($recordId)) {
foreach ($urls as $url) { foreach ($urls as $url) {
try { try {
// 采集第三方商品数据 // 采集第三方商品数据
$original = $this->collector($url, $storeId); $original = $this->collector($url, $storeId);
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']);
}
// 下载远程商品图片 // 下载远程商品图片
$original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId); $original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId);
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -99,6 +113,7 @@ class Collector extends BaseService
$model->transaction(function () use ($model, $data, $storeId) { $model->transaction(function () use ($model, $data, $storeId) {
// 添加商品 // 添加商品
$model->save($data); $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); GoodsCategoryRelModel::increased((int)$model['goods_id'], $data['categoryIds'], $storeId);
// 新增商品与图片关联 // 新增商品与图片关联
@ -111,7 +126,7 @@ class Collector extends BaseService
// 记录采集成功 // 记录采集成功
$this->successCount++; $this->successCount++;
} }
} //}
// 更新采集记录 // 更新采集记录
$this->updateRecord($recordId, \count($urls)); $this->updateRecord($recordId, \count($urls));
return true; return true;
@ -144,6 +159,8 @@ class Collector extends BaseService
$original['cmmdty_model'] = $form['cmmdty_model'] ?? ""; $original['cmmdty_model'] = $form['cmmdty_model'] ?? "";
$original['goods_no'] = $form['goods_no'] ?? ""; $original['goods_no'] = $form['goods_no'] ?? "";
$original['sale_areas'] = $form['sale_areas'] ?? ""; $original['sale_areas'] = $form['sale_areas'] ?? "";
$original['data_type'] = 1;
$original['link'] = $url;
// 下载远程商品图片 // 下载远程商品图片
$original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId); $original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId);
@ -156,11 +173,15 @@ class Collector extends BaseService
} }
// 生成商品数据(用于写入数据库) // 生成商品数据(用于写入数据库)
$data = $this->singleCreateData($original, $form, $storeId); $data = $this->singleCreateData($original, $form, $storeId);
// echo "<pre>";
// print_r($data);
// exit();
// 事务处理:添加商品 // 事务处理:添加商品
$model = new GoodsModel(); $model = new GoodsModel();
$model->transaction(function () use ($model, $data, $storeId) { $model->transaction(function () use ($model, $data, $storeId) {
// 添加商品 // 添加商品
$model->save($data); $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); GoodsCategoryRelModel::increased((int)$model['goods_id'], $data['categoryIds'], $storeId);
// 新增商品与图片关联 // 新增商品与图片关联
@ -194,18 +215,22 @@ class Collector extends BaseService
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
private function collector(string $url, int $storeId): array public function collector(string $url, int $storeId): array
{ {
// 获取商品来源 // 获取商品来源
$store = $this->getStore($url); $store = $this->getStore($url);
// 获取url中的商品ID // 获取url中的商品ID
$itemId = $this->getItemId($url, $store); $itemId = $this->getItemId($url, $store);
// 商城采集设置 // 商城采集设置
$config = SettingModel::getItem(SettingEnum::COLLECTOR, $storeId); $config = SettingModel::getItem(SettingEnum::COLLECTOR, $storeId);
// 请求API查询商品详情 // 请求API查询商品详情
return CollectorFacade::store($store) $item = CollectorFacade::store($store)
->setOptions($config['config'][$config['provider']]) ->setOptions($config['config'][$config['provider']])
->detail($itemId); ->detail($itemId);
$item['goods_sku_no'] = $itemId;
return $item;
} }
/** /**
@ -292,7 +317,10 @@ class Collector extends BaseService
$data = [ $data = [
'goods_type' => $form['goods_type'], 'goods_type' => $form['goods_type'],
'goods_name' => $original['goods_name'], 'goods_name' => $original['goods_name'],
'data_type' => $original['data_type'],//数据类型
'link' => $original['link'],//采集地址
'spec_type' => $original['spec_type'], 'spec_type' => $original['spec_type'],
'unicode' => $original['spec_type'],
'delivery_id' => $form['delivery_id'] ?? 0, 'delivery_id' => $form['delivery_id'] ?? 0,
'content' => $original['content'] ?? '', 'content' => $original['content'] ?? '',
'sort' => 100, 'sort' => 100,
@ -309,6 +337,7 @@ class Collector extends BaseService
if (isset($form['channel'])) { if (isset($form['channel'])) {
$data['channel'] = $form['channel']; $data['channel'] = $form['channel'];
} }
$data['unicode'] = $data['channel']."-0-".$original['goods_sku_no'];
// 整理商品的价格和库存总量 // 整理商品的价格和库存总量
if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) { if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) {
$data['stock_total'] = GoodsSkuModel::getStockTotal($original['specData']['skuList']); $data['stock_total'] = GoodsSkuModel::getStockTotal($original['specData']['skuList']);
@ -340,7 +369,7 @@ class Collector extends BaseService
$data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $original['specData']['skuList']); $data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $original['specData']['skuList']);
} elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) { } elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) {
// 生成skuItem // 生成skuItem
$data['newSkuList'] = helper::pick($original, ['goods_price', 'line_price', 'cost_price','stock_num', 'goods_weight']); $data['newSkuList'] = helper::pick($original, ['goods_sku_no','goods_price', 'line_price', 'cost_price','stock_num', 'goods_weight']);
} }
return $data; return $data;
} }
@ -361,6 +390,8 @@ class Collector extends BaseService
$data = [ $data = [
'goods_type' => $form['goods_type'], 'goods_type' => $form['goods_type'],
'goods_name' => $original['goods_name'], 'goods_name' => $original['goods_name'],
'data_type' => $original['data_type'],//数据类型
'link' => $original['link'],//采集地址
'spec_type' => $original['spec_type'], 'spec_type' => $original['spec_type'],
'delivery_id' => $form['delivery_id'] ?? 0, 'delivery_id' => $form['delivery_id'] ?? 0,
'content' => $original['content'] ?? '', 'content' => $original['content'] ?? '',
@ -387,7 +418,7 @@ class Collector extends BaseService
$data['goods_price_min'] = $data['goods_price_max'] = $original['goods_price']; $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['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']; $data['stock_total'] = $original['stock_num'] ?? 100;
} }
// 规格和sku数据处理 // 规格和sku数据处理
if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) { if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) {
@ -399,7 +430,7 @@ class Collector extends BaseService
$data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $original['specData']['skuList']); $data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $original['specData']['skuList']);
} elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) { } elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) {
// 生成skuItem // 生成skuItem
$data['newSkuList'] = helper::pick($original, ['goods_price', 'line_price', 'stock_num', 'goods_weight']); $data['newSkuList'] = helper::pick($original, ['goods_sku_no','goods_price', 'line_price', 'stock_num', 'goods_weight']);
} }
return $data; return $data;
} }

@ -29,6 +29,8 @@ class Category extends CategoryModel
public function add($data): bool public function add($data): bool
{ {
$data['store_id'] = $data['store_id'] ?? self::$storeId; $data['store_id'] = $data['store_id'] ?? self::$storeId;
$cate = $this->where('category_id', $data['parent_id'])->find();
$data['level'] = $cate ? $cate['level'] + 1 : 1;
return $this->save($data); return $this->save($data);
} }
@ -50,7 +52,10 @@ class Category extends CategoryModel
$this->error = '上级分类不允许设置为当前子分类'; $this->error = '上级分类不允许设置为当前子分类';
return false; return false;
} }
$cate = $this->where('category_id', $data['parent_id'])->find();
$data['level'] = $cate ? $cate['level'] + 1 : 1;
} }
// 是否删除图片 // 是否删除图片
!isset($data['image_id']) && $data['image_id'] = 0; !isset($data['image_id']) && $data['image_id'] = 0;
return $this->save($data) !== false; return $this->save($data) !== false;

@ -24,7 +24,7 @@ use app\store\service\Goods as GoodsService;
use app\common\enum\goods\SpecType as GoodsSpecTypeEnum; use app\common\enum\goods\SpecType as GoodsSpecTypeEnum;
use app\common\enum\goods\Status as GoodsStatusEnum; use app\common\enum\goods\Status as GoodsStatusEnum;
use cores\exception\BaseException; use cores\exception\BaseException;
use app\common\model\Region;
/** /**
* 商品模型 * 商品模型
* Class Goods * Class Goods
@ -45,6 +45,10 @@ class Goods extends GoodsModel
{ {
// 获取商品基础信息 // 获取商品基础信息
$goodsInfo = $this->getBasic($goodsId); $goodsInfo = $this->getBasic($goodsId);
if (isset($goodsInfo['skuList1'])) {
$goodsInfo['skuList'] = $goodsInfo['skuList1'];
unset($goodsInfo['skuList1']);
}
// 分类ID集 // 分类ID集
$goodsInfo['categoryIds'] = GoodsCategoryRelModel::getCategoryIds($goodsInfo['goods_id']); $goodsInfo['categoryIds'] = GoodsCategoryRelModel::getCategoryIds($goodsInfo['goods_id']);
// 商品多规格属性列表 // 商品多规格属性列表
@ -67,10 +71,18 @@ class Goods extends GoodsModel
*/ */
public function getBasic(int $goodsId) public function getBasic(int $goodsId)
{ {
$info = $this->field('spec_type,goods_id')->find($goodsId);
// 关联查询 // 关联查询
$with = ['images.file', 'skuList.image', 'video', 'videoCover']; if ($info->spec_type == GoodsSpecTypeEnum::SINGLE) {
$with = ['images.file', 'skuList1.image', 'video', 'videoCover'];
} else {
$with = ['images.file', 'skuList.image', 'video', 'videoCover'];
}
// 获取商品记录 // 获取商品记录
$goodsInfo = static::detail($goodsId, $with); $goodsInfo = static::detail($goodsId, $with);
// echo "<pre>";
// print_r($goodsInfo->toArray());
// exit();
empty($goodsInfo) && throwError('很抱歉,商品信息不存在'); empty($goodsInfo) && throwError('很抱歉,商品信息不存在');
// 整理商品数据并返回 // 整理商品数据并返回
return parent::setGoodsData($goodsInfo); return parent::setGoodsData($goodsInfo);
@ -91,6 +103,7 @@ class Goods extends GoodsModel
$data = $this->createData($data); $data = $this->createData($data);
// 事务处理 // 事务处理
$this->transaction(function () use ($data) { $this->transaction(function () use ($data) {
$data['data_type'] = $data['data_type'] ?? 1;
// 添加商品 // 添加商品
$this->save($data); $this->save($data);
// 新增商品与分类关联 // 新增商品与分类关联
@ -118,12 +131,17 @@ class Goods extends GoodsModel
*/ */
public function edit(array $data): bool public function edit(array $data): bool
{ {
// 创建商品数据 // 创建商品数据
$data = $this->createData($data); $data = $this->createData($data);
// 事务处理 // 事务处理
$this->transaction(function () use ($data) { $this->transaction(function () use ($data) {
// var_dump($data);
// exit(); //如果是编辑多规格商品
if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) {
$data['spu_id'] = $this['goods_id'];
}
// 更新商品 // 更新商品
$this->save($data); $this->save($data);
// 更新商品与分类关联 // 更新商品与分类关联
@ -137,6 +155,7 @@ class Goods extends GoodsModel
// 更新服务与承诺关联 // 更新服务与承诺关联
GoodsServiceRelModel::updates((int)$this['goods_id'], $data['serviceIds']); GoodsServiceRelModel::updates((int)$this['goods_id'], $data['serviceIds']);
}); });
//exit();
return true; return true;
} }
@ -200,6 +219,16 @@ class Goods extends GoodsModel
*/ */
private function createData(array $data): array private function createData(array $data): array
{ {
$arr = [];
$sale_areas = (isset($data["sale_areas"]) && $data["sale_areas"]) ? explode("、", $data["sale_areas"]) : [];
if ($sale_areas) {
foreach ($sale_areas as $value) {
$regions = Region::whereIn('name', explode(".", $value))->select()->toArray();
$regions = array_column($regions, null, "level");
$arr[] = $regions[1]['id'].".".$regions[2]['id'];
}
}
// 默认数据 // 默认数据
$data = array_merge($data, [ $data = array_merge($data, [
'line_price' => $data['line_price'] ?? 0, 'line_price' => $data['line_price'] ?? 0,
@ -207,20 +236,20 @@ class Goods extends GoodsModel
'newSpecList' => [], 'newSpecList' => [],
'newSkuList' => [], 'newSkuList' => [],
'store_id' => self::$storeId, 'store_id' => self::$storeId,
'sale_areas' => $arr ? implode("、", $arr) : "",
]); ]);
// var_dump($data);
// exit();
// 整理商品的价格和库存总量 // 整理商品的价格和库存总量
if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) { if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) {
$data['stock_total'] = GoodsSkuModel::getStockTotal($data['specData']['skuList']); // $data['stock_total'] = GoodsSkuModel::getStockTotal($data['specData']['skuList']);
[$data['goods_price_min'], $data['goods_price_max']] = GoodsSkuModel::getGoodsPrices($data['specData']['skuList']); // [$data['goods_price_min'], $data['goods_price_max']] = GoodsSkuModel::getGoodsPrices($data['specData']['skuList']);
[$data['line_price_min'], $data['line_price_max']] = GoodsSkuModel::getLinePrices($data['specData']['skuList']); // [$data['line_price_min'], $data['line_price_max']] = GoodsSkuModel::getLinePrices($data['specData']['skuList']);
} elseif ($data['spec_type'] == GoodsSpecTypeEnum::SINGLE) { } elseif ($data['spec_type'] == GoodsSpecTypeEnum::SINGLE) {
$data['goods_price_min'] = $data['goods_price_max'] = $data['goods_price']; $data['goods_price_min'] = $data['goods_price_max'] = $data['goods_price'];
//$data['line_price_min'] = $data['line_price_max'] = $data['line_price']; //$data['line_price_min'] = $data['line_price_max'] = $data['line_price'];
$data['line_price_min'] = $data['line_price_max'] = $data['goods_price']; $data['line_price_min'] = $data['line_price_max'] = $data['goods_price'];
$data['stock_total'] = $data['stock_num']; $data['stock_total'] = $data['stock_num'];
$data['cost_price_min'] = $data['cost_price']; $data['cost_price_min'] = $data['cost_price']??0;
} }
// 规格和sku数据处理 // 规格和sku数据处理
if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) { if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) {

@ -13,6 +13,7 @@ declare (strict_types=1);
namespace app\store\model; namespace app\store\model;
use app\common\model\GoodsSku as GoodsSkuModel; use app\common\model\GoodsSku as GoodsSkuModel;
use app\common\model\Goods as GoodsModel;
use app\common\enum\goods\SpecType as SpecTypeEnum; use app\common\enum\goods\SpecType as SpecTypeEnum;
/** /**
@ -31,9 +32,42 @@ class GoodsSku extends GoodsSkuModel
*/ */
public static function edit(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $skuList = []) public static function edit(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $skuList = [])
{ {
// 删除所有的sku记录 // echo "<pre>";
static::deleteAll(['goods_id' => $goodsId]); // print_r($skuList);
// 新增商品sku记录 // exit();
return static::add($goodsId, $specType, $skuList); //单规格修改还按照之前的逻辑
if ($specType == SpecTypeEnum::SINGLE) {
// 删除所有的sku记录
static::deleteAll(['goods_id' => $goodsId]);
// 新增商品sku记录
return static::add($goodsId, $specType, $skuList);
} else {
//设置spu_id为当前商品id的商品的spu_id=0; by liuqing
GoodsModel::where('spu_id', $goodsId)->update(['spu_id' => 0]);
//更新sku列表里面的商品id的spu_id
$goodsIds = array_column($skuList, "goods_id");
GoodsModel::whereIn('goods_id', $goodsIds)->update(['spu_id' => $goodsId]);
foreach ($skuList as $skuItem) {
$temp = [
'goods_props' => json_encode($skuItem['goodsProps'], JSON_UNESCAPED_UNICODE),
'spec_value_ids' => json_encode($skuItem['specValueIds'], JSON_UNESCAPED_UNICODE),
'spu_id' => $goodsId
];
if ($skuItem['goods_id'] == $goodsId) {
$temp['goods_price'] = $skuItem['goods_price'] ?? 0.01;
$temp['cost_price'] = $skuItem['cost_price'] ?? 0.00;
$temp['goods_sku_no'] = $skuItem['goods_sku_no'] ?? '';
$temp['stock_num'] = $skuItem['stock_num'] ?? 0;
$temp['goods_weight'] = $skuItem['goods_weight'] ?? 0;
}
GoodsSku::where('goods_id', $skuItem['goods_id'])->update($temp);
}
}
} }
} }

@ -66,8 +66,15 @@ class Collector extends CollectorModel
$urls = $this->getUrls($form); $urls = $this->getUrls($form);
// 验证采集的商品数量是否合法 // 验证采集的商品数量是否合法
$this->checkLimit($urls); $this->checkLimit($urls);
//exit;
// 新增商品采集记录 // 新增商品采集记录
$recordId = $this->addRecord(\count($urls)); $recordId = $this->addRecord(\count($urls));
// $obj = new \app\job\service\goods\Collector();
// $obj->batch($urls, $form, $recordId, self::$storeId);
// exit();
// 调度计划任务 // 调度计划任务
$this->dispatchJob($urls, $recordId, $form); $this->dispatchJob($urls, $recordId, $form);
return true; return true;

@ -75,15 +75,15 @@ class Import extends ImportModel
$this->checkLimit($execlData); $this->checkLimit($execlData);
self::$storeId = 0; self::$storeId = 0;
$obj = new \app\job\service\goods\AdminImport(); // $obj = new \app\job\service\goods\AdminImport();
$service = new \app\job\service\goods\Collector(); // $service = new \app\job\service\goods\Collector();
foreach ($execlData as $item) { // foreach ($execlData as $item) {
$data = $obj->createData($item, self::$storeId); // $data = $obj->createData($item, self::$storeId);
// $service->single($item['D'], $data, $storeId); // $service->single($item['D'], $data, self::$storeId);
// // 记录导入成功 // // // 记录导入成功
// $this->successCount++; // // $this->successCount++;
} // }
// 新增商品导入记录 // 新增商品导入记录
$recordId = $this->addRecord(\count($execlData)); $recordId = $this->addRecord(\count($execlData));

@ -10,5 +10,6 @@ return [
//test脚本 //test脚本
'test' => 'app\command\test', 'test' => 'app\command\test',
'CalDealerTime' => 'app\command\CalDealerTime', 'CalDealerTime' => 'app\command\CalDealerTime',
'UpdateCollectGoodsPrice' => 'app\command\UpdateCollectGoodsPrice',
], ],
]; ];

Loading…
Cancel
Save