diff --git a/app/admin/controller/Category.php b/app/admin/controller/Category.php index fa618740..3e5ba392 100644 --- a/app/admin/controller/Category.php +++ b/app/admin/controller/Category.php @@ -87,7 +87,7 @@ class Category extends Controller return $this->renderError($model->getError() ?: '更新失败'); } public function copyCategory(int $new_store_id){ - $store_id = 0; + $store_id = 10001; $model = new CategoryModel; $list = $model->getList(['store_id' =>$store_id]); if (!$list) { @@ -99,6 +99,7 @@ class Category extends Controller $value['store_id'] = $new_store_id; $value['create_time'] = time(); $value['update_time'] = time(); + $value['image_id'] = $value['image_id'] ? $this->copyImage($value['image_id'], $new_store_id) : 0; $temp = $value; unset($temp['children']); unset($temp['image']); @@ -114,6 +115,7 @@ class Category extends Controller $value1['store_id'] = $new_store_id; $value1['create_time'] = time(); $value1['update_time'] = time(); + $value1['image_id'] = $value1['image_id'] ? $this->copyImage($value1['image_id'], $new_store_id) : 0; $temp1 = $value1; unset($temp1['children']); unset($temp1['image']); @@ -129,17 +131,45 @@ class Category extends Controller $value2['store_id'] = $new_store_id; $value2['create_time'] = time(); $value2['update_time'] = time(); + $value2['image_id'] = $value2['image_id'] ? $this->copyImage($value2['image_id'], $new_store_id) : 0; $temp2 = $value2; unset($temp2['children']); unset($temp2['image']); unset($temp2['category_id']); 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); } - + 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; + } } diff --git a/app/admin/model/Store.php b/app/admin/model/Store.php index 5d89719e..544e4858 100644 --- a/app/admin/model/Store.php +++ b/app/admin/model/Store.php @@ -127,6 +127,23 @@ class Store extends StoreModel unset($temp2['category_id']); 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); + } } } } diff --git a/app/api/controller/Checkout.php b/app/api/controller/Checkout.php index fd4431cf..d944860d 100644 --- a/app/api/controller/Checkout.php +++ b/app/api/controller/Checkout.php @@ -92,6 +92,9 @@ class Checkout extends Controller ); // 获取订单确认信息 $orderInfo = $Checkout->onCheckout($goodsList); + // echo "
"; + // print_r($orderInfo); + // exit(); if ($this->request->isGet()) { return $this->renderSuccess([ 'order' => $orderInfo, diff --git a/app/api/model/Goods.php b/app/api/model/Goods.php index cbfd4b36..33460bfb 100644 --- a/app/api/model/Goods.php +++ b/app/api/model/Goods.php @@ -284,8 +284,10 @@ class Goods extends GoodsModel ->where([ 'a.store_id' => request()->header()['storeid'], '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(); + // var_dump($list); + // exit(); $list = $this->removeDuplicatesByField($list, 'category_id'); $info['end_time'] = strtotime($info->p_time) - time(); $res['info'] = $info; @@ -348,32 +350,32 @@ class Goods extends GoodsModel $goodsInfo->specifications = []; $skuList = []; $specList = []; - if ($goodsInfo->spu_id > 0) { - //查同规格的商品 - $spe = GoodsModel::where([ - 'spu_id' => $goodsInfo->spu_id, - 'store_id' => request()->header()['storeid'], - 'is_delete' => 0 - ])->column('goods_id'); - foreach ($spe as $k => $v) { - //$sku1 = $this->getSpecData($v)['skuList']->toArray()[0]; - // $skuList[] = $sku1; - $spe1 = $this->getSpecData($v)['specList'][0]; - $spe1['valueList'][0]['goods_id'] = $v; - $aa = 0; - foreach ($specList as &$v2) { - if ($v2['spec_name'] == $spe1['spec_name']) { - $aa = 1; - $v2['valueList'][] = $spe1['valueList']; - break; - } - } - - if ($aa == 0) { - $specList[] = $spe1; - } - } - } + // if ($goodsInfo->spu_id > 0) { + // //查同规格的商品 + // $spe = GoodsModel::where([ + // 'spu_id' => $goodsInfo->spu_id, + // 'store_id' => request()->header()['storeid'], + // 'is_delete' => 0 + // ])->column('goods_id'); + // foreach ($spe as $k => $v) { + // //$sku1 = $this->getSpecData($v)['skuList']->toArray()[0]; + // // $skuList[] = $sku1; + // $spe1 = $this->getSpecData($v)['specList'][0]; + // $spe1['valueList'][0]['goods_id'] = $v; + // $aa = 0; + // foreach ($specList as &$v2) { + // if ($v2['spec_name'] == $spe1['spec_name']) { + // $aa = 1; + // $v2['valueList'][] = $spe1['valueList']; + // break; + // } + // } + + // if ($aa == 0) { + // $specList[] = $spe1; + // } + // } + // } $goodsInfo = $goodsInfo->toArray(); diff --git a/app/command/UpdateCollectGoodsPrice.php b/app/command/UpdateCollectGoodsPrice.php new file mode 100644 index 00000000..4d6de9e0 --- /dev/null +++ b/app/command/UpdateCollectGoodsPrice.php @@ -0,0 +1,108 @@ +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); + + } + + +} diff --git a/app/common/model/Category.php b/app/common/model/Category.php index 4847e2c7..f0ef3646 100644 --- a/app/common/model/Category.php +++ b/app/common/model/Category.php @@ -37,7 +37,10 @@ class Category extends BaseModel { 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 { $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) ->order(['sort', 'create_time']) ->select(); diff --git a/app/common/model/Goods.php b/app/common/model/Goods.php index 5678c382..c57d029c 100644 --- a/app/common/model/Goods.php +++ b/app/common/model/Goods.php @@ -130,9 +130,16 @@ class Goods extends BaseModel */ 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 @@ -470,6 +477,8 @@ class Goods extends BaseModel */ public static function detail(int $goodsId, array $with = []) { + // var_dump($with); + // exit(); return static::get($goodsId, $with); } } diff --git a/app/common/model/GoodsSku.php b/app/common/model/GoodsSku.php index 1587d2ae..fa96724d 100644 --- a/app/common/model/GoodsSku.php +++ b/app/common/model/GoodsSku.php @@ -102,7 +102,8 @@ class GoodsSku extends BaseModel public static function getSkuList(int $goodsId, bool $withImage = false): \think\Collection { return (new static)->with($withImage ? ['image'] : []) - ->where('goods_id', '=', $goodsId) + //->where('goods_id', '=', $goodsId) + ->where('spu_id', '=', $goodsId) ->select(); } @@ -173,6 +174,7 @@ class GoodsSku extends BaseModel if ($specType === SpecTypeEnum::SINGLE) { return (new static)->save(array_merge($newSkuList, [ 'goods_id' => $goodsId, + 'spu_id' => $goodsId, 'goods_sku_id' => 0, 'store_id' => $storeId ?: self::$storeId ])); diff --git a/app/common/model/GoodsSpecRel.php b/app/common/model/GoodsSpecRel.php index 37454fb6..498d6a69 100644 --- a/app/common/model/GoodsSpecRel.php +++ b/app/common/model/GoodsSpecRel.php @@ -14,7 +14,8 @@ namespace app\common\model; use cores\BaseModel; use think\model\relation\BelongsTo; - +use app\common\model\Spec; +use app\common\model\SpecValue; /** * 商品规格关系模型 * Class GoodsSpecRel @@ -77,8 +78,8 @@ class GoodsSpecRel extends BaseModel ($item['spec_id'] == $group['spec_id']) && $group['valueList'][] = [ 'key' => isset($group['valueList']) ? count($group['valueList']) : 0, 'groupKey' => $groupKey, - 'spec_value_id' => $item['specValue']['spec_value_id'], - 'spec_value' => $item['specValue']['spec_value'] + 'spec_value_id' => $item['specValue']['spec_value_id'] ?? 0, + 'spec_value' => $item['specValue']['spec_value'] ?? "" ]; } } @@ -96,6 +97,26 @@ class GoodsSpecRel extends BaseModel private static function getList(int $goodsId) { $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 ""; + // // print_r($list); + // // exit(); + // return $list; + // echo ""; + // print_r($list->toArray()); + //exit(); + self::$storeId = 0; + app()->request->setStoreId(0); return static::preload($list, ['spec', 'specValue']); } diff --git a/app/common/model/Store.php b/app/common/model/Store.php index fecdb945..dca74c01 100644 --- a/app/common/model/Store.php +++ b/app/common/model/Store.php @@ -71,6 +71,13 @@ class Store extends BaseModel }else{ $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; } catch (\Exception $e) { diff --git a/app/job/service/goods/AdminImport.php b/app/job/service/goods/AdminImport.php index ab181125..7cce1f85 100644 --- a/app/job/service/goods/AdminImport.php +++ b/app/job/service/goods/AdminImport.php @@ -217,12 +217,14 @@ class AdminImport extends BaseService 'cost_price' => $original["G"], 'stock_num' => $original["H"], 'remark' => $original["I"], + //'link' => $original["D"], 'categoryIds' => $this->ids2array($original["E"]), 'imageStorage' => 10,//下载图片到本地 'goods_type' => 10,//实物 'goods_status' => 10,//上架 'store_id' => $storeId, - 'sale_areas' => implode("、", $arr) + 'sale_areas' => $arr ? implode("、", $arr) : "", + 'import' => 1,//是否是导入采集 ]; // echo ""; // print_r($data); diff --git a/app/job/service/goods/Collector.php b/app/job/service/goods/Collector.php index 5f51761a..34d560ef 100644 --- a/app/job/service/goods/Collector.php +++ b/app/job/service/goods/Collector.php @@ -80,11 +80,25 @@ class Collector extends BaseService public function batch(array $urls, array $form, int $recordId, int $storeId): bool { // 检查采集记录状态是否异常 - if (!$this->checkStatusFail($recordId)) { + //if (!$this->checkStatusFail($recordId)) { foreach ($urls as $url) { try { // 采集第三方商品数据 $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); } catch (\Throwable $e) { @@ -99,6 +113,7 @@ class Collector extends BaseService $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); // 新增商品与图片关联 @@ -111,7 +126,7 @@ class Collector extends BaseService // 记录采集成功 $this->successCount++; } - } + //} // 更新采集记录 $this->updateRecord($recordId, \count($urls)); return true; @@ -144,6 +159,8 @@ class Collector extends BaseService $original['cmmdty_model'] = $form['cmmdty_model'] ?? ""; $original['goods_no'] = $form['goods_no'] ?? ""; $original['sale_areas'] = $form['sale_areas'] ?? ""; + $original['data_type'] = 1; + $original['link'] = $url; // 下载远程商品图片 $original = $this->thirdPartyImages($original, $form['imageStorage'], $storeId); @@ -156,11 +173,15 @@ class Collector extends BaseService } // 生成商品数据(用于写入数据库) $data = $this->singleCreateData($original, $form, $storeId); + // echo ""; + // 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); // 新增商品与图片关联 @@ -194,18 +215,22 @@ class Collector extends BaseService * @throws \think\db\exception\DbException * @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); + // 获取url中的商品ID $itemId = $this->getItemId($url, $store); // 商城采集设置 $config = SettingModel::getItem(SettingEnum::COLLECTOR, $storeId); // 请求API查询商品详情 - return CollectorFacade::store($store) + $item = CollectorFacade::store($store) ->setOptions($config['config'][$config['provider']]) ->detail($itemId); + + $item['goods_sku_no'] = $itemId; + return $item; } /** @@ -292,7 +317,10 @@ class Collector extends BaseService $data = [ 'goods_type' => $form['goods_type'], 'goods_name' => $original['goods_name'], + 'data_type' => $original['data_type'],//数据类型 + 'link' => $original['link'],//采集地址 'spec_type' => $original['spec_type'], + 'unicode' => $original['spec_type'], 'delivery_id' => $form['delivery_id'] ?? 0, 'content' => $original['content'] ?? '', 'sort' => 100, @@ -309,6 +337,7 @@ class Collector extends BaseService if (isset($form['channel'])) { $data['channel'] = $form['channel']; } + $data['unicode'] = $data['channel']."-0-".$original['goods_sku_no']; // 整理商品的价格和库存总量 if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) { $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']); } elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) { // 生成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; } @@ -361,6 +390,8 @@ class Collector extends BaseService $data = [ 'goods_type' => $form['goods_type'], 'goods_name' => $original['goods_name'], + 'data_type' => $original['data_type'],//数据类型 + 'link' => $original['link'],//采集地址 'spec_type' => $original['spec_type'], 'delivery_id' => $form['delivery_id'] ?? 0, 'content' => $original['content'] ?? '', @@ -387,7 +418,7 @@ class Collector extends BaseService $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']; + $data['stock_total'] = $original['stock_num'] ?? 100; } // 规格和sku数据处理 if ($data['spec_type'] === GoodsSpecTypeEnum::MULTI) { @@ -399,7 +430,7 @@ class Collector extends BaseService $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', 'stock_num', 'goods_weight']); + $data['newSkuList'] = helper::pick($original, ['goods_sku_no','goods_price', 'line_price', 'stock_num', 'goods_weight']); } return $data; } diff --git a/app/store/model/Category.php b/app/store/model/Category.php index 908a4997..8124728f 100644 --- a/app/store/model/Category.php +++ b/app/store/model/Category.php @@ -29,6 +29,8 @@ class Category extends CategoryModel public function add($data): bool { $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); } @@ -50,7 +52,10 @@ class Category extends CategoryModel $this->error = '上级分类不允许设置为当前子分类'; 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; return $this->save($data) !== false; diff --git a/app/store/model/Goods.php b/app/store/model/Goods.php index 7e75cf60..9d5bf907 100644 --- a/app/store/model/Goods.php +++ b/app/store/model/Goods.php @@ -24,7 +24,7 @@ use app\store\service\Goods as GoodsService; use app\common\enum\goods\SpecType as GoodsSpecTypeEnum; use app\common\enum\goods\Status as GoodsStatusEnum; use cores\exception\BaseException; - +use app\common\model\Region; /** * 商品模型 * Class Goods @@ -45,6 +45,10 @@ class Goods extends GoodsModel { // 获取商品基础信息 $goodsInfo = $this->getBasic($goodsId); + if (isset($goodsInfo['skuList1'])) { + $goodsInfo['skuList'] = $goodsInfo['skuList1']; + unset($goodsInfo['skuList1']); + } // 分类ID集 $goodsInfo['categoryIds'] = GoodsCategoryRelModel::getCategoryIds($goodsInfo['goods_id']); // 商品多规格属性列表 @@ -67,10 +71,18 @@ class Goods extends GoodsModel */ 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); + // echo ""; + // print_r($goodsInfo->toArray()); + // exit(); empty($goodsInfo) && throwError('很抱歉,商品信息不存在'); // 整理商品数据并返回 return parent::setGoodsData($goodsInfo); @@ -91,6 +103,7 @@ class Goods extends GoodsModel $data = $this->createData($data); // 事务处理 $this->transaction(function () use ($data) { + $data['data_type'] = $data['data_type'] ?? 1; // 添加商品 $this->save($data); // 新增商品与分类关联 @@ -118,12 +131,17 @@ class Goods extends GoodsModel */ public function edit(array $data): bool { + + // 创建商品数据 $data = $this->createData($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); // 更新商品与分类关联 @@ -137,6 +155,7 @@ class Goods extends GoodsModel // 更新服务与承诺关联 GoodsServiceRelModel::updates((int)$this['goods_id'], $data['serviceIds']); }); + //exit(); return true; } @@ -200,6 +219,16 @@ class Goods extends GoodsModel */ 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, [ 'line_price' => $data['line_price'] ?? 0, @@ -207,20 +236,20 @@ class Goods extends GoodsModel 'newSpecList' => [], 'newSkuList' => [], 'store_id' => self::$storeId, + 'sale_areas' => $arr ? implode("、", $arr) : "", ]); - // var_dump($data); - // exit(); + // 整理商品的价格和库存总量 if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) { - $data['stock_total'] = GoodsSkuModel::getStockTotal($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['stock_total'] = GoodsSkuModel::getStockTotal($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']); } elseif ($data['spec_type'] == GoodsSpecTypeEnum::SINGLE) { $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['goods_price']; $data['stock_total'] = $data['stock_num']; - $data['cost_price_min'] = $data['cost_price']; + $data['cost_price_min'] = $data['cost_price']??0; } // 规格和sku数据处理 if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) { diff --git a/app/store/model/GoodsSku.php b/app/store/model/GoodsSku.php index b0e4c0e4..efa14e6c 100644 --- a/app/store/model/GoodsSku.php +++ b/app/store/model/GoodsSku.php @@ -13,6 +13,7 @@ declare (strict_types=1); namespace app\store\model; use app\common\model\GoodsSku as GoodsSkuModel; +use app\common\model\Goods as GoodsModel; 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 = []) { - // 删除所有的sku记录 - static::deleteAll(['goods_id' => $goodsId]); - // 新增商品sku记录 - return static::add($goodsId, $specType, $skuList); + // echo ""; + // print_r($skuList); + // exit(); + //单规格修改还按照之前的逻辑 + 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); + } + + + } + } } diff --git a/app/store/model/goods/Collector.php b/app/store/model/goods/Collector.php index f68540f4..42a6897b 100644 --- a/app/store/model/goods/Collector.php +++ b/app/store/model/goods/Collector.php @@ -66,8 +66,15 @@ class Collector extends CollectorModel $urls = $this->getUrls($form); // 验证采集的商品数量是否合法 $this->checkLimit($urls); + + + //exit; // 新增商品采集记录 $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); return true; diff --git a/app/store/model/goods/Import.php b/app/store/model/goods/Import.php index bf1d44f8..937ff33e 100644 --- a/app/store/model/goods/Import.php +++ b/app/store/model/goods/Import.php @@ -75,15 +75,15 @@ class Import extends ImportModel $this->checkLimit($execlData); self::$storeId = 0; - $obj = new \app\job\service\goods\AdminImport(); - $service = new \app\job\service\goods\Collector(); - foreach ($execlData as $item) { + // $obj = new \app\job\service\goods\AdminImport(); + // $service = new \app\job\service\goods\Collector(); + // foreach ($execlData as $item) { - $data = $obj->createData($item, self::$storeId); - // $service->single($item['D'], $data, $storeId); - // // 记录导入成功 - // $this->successCount++; - } + // $data = $obj->createData($item, self::$storeId); + // $service->single($item['D'], $data, self::$storeId); + // // // 记录导入成功 + // // $this->successCount++; + // } // 新增商品导入记录 $recordId = $this->addRecord(\count($execlData)); diff --git a/config/console.php b/config/console.php index ff096871..2cedb105 100644 --- a/config/console.php +++ b/config/console.php @@ -10,5 +10,6 @@ return [ //test脚本 'test' => 'app\command\test', 'CalDealerTime' => 'app\command\CalDealerTime', + 'UpdateCollectGoodsPrice' => 'app\command\UpdateCollectGoodsPrice', ], ];