WiwibaoProductSync.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use app\common\enum\CommonEnum;
  5. use app\common\enum\store\ProductEnum;
  6. use app\common\repositories\douhuomall\DouhuomallChangeLogRepository;
  7. use app\common\repositories\store\product\ProductRepository;
  8. use app\entity\data\douhuomall\DouhuomallChangeLogEntity;
  9. use app\traits\GongApiRequest;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\Output;
  13. use think\db\exception\DbException;
  14. use think\facade\Db;
  15. use think\facade\Log;
  16. class WiwibaoProductSync extends Command
  17. {
  18. use GongApiRequest;
  19. // 配置参数
  20. protected $config = [
  21. 'page_size' => 100, // 每页拉取数量
  22. 'max_pages' => 0, // 最大页数限制
  23. 'price_change_rate' => 0.0, // 价格变动比率阈值(10%)
  24. 'mer_id' => CommonEnum::DESIGN_MERCHANT_ID['WonderfulLiving']['code']
  25. ];
  26. protected function configure()
  27. {
  28. // 指令配置
  29. $this->setName('wiwibaoproductsync')
  30. ->setDescription('the wiwibaoproductsync command');
  31. }
  32. protected function execute(Input $input, Output $output)
  33. {
  34. $output->writeln('定时任务执行中...');
  35. $this->syncGoodsFromGong();
  36. $output->writeln('定时任务执行完成');
  37. }
  38. /**
  39. * 同步微唯宝商品数据(主方法)
  40. */
  41. public function syncGoodsFromGong()
  42. {
  43. try {
  44. set_time_limit(0);
  45. // 1. 批量拉取所有商品数据
  46. $allProducts = $this->fetchAllProducts();
  47. if (empty($allProducts)) {
  48. Log::info('未拉取到任何商品数据');
  49. return 'success';
  50. }
  51. // 2. 获取本地所有SPU ID
  52. $douhuomallDataMap = [];
  53. $douhuomallDataList = Db::name('douhuomall')
  54. ->select()
  55. ->toArray();
  56. if (!empty($douhuomallDataList)) {
  57. $douhuomallDataMap = array_column($douhuomallDataList, null, 'spu_id');
  58. }
  59. $syncedSpuIds = [];
  60. // 3. 批量处理商品数据
  61. foreach ($allProducts as $product) {
  62. try {
  63. $spuId = $this->processSingleProduct($product, $douhuomallDataMap[$product['goods_id']] ?? []);
  64. if ($spuId) {
  65. $syncedSpuIds[] = $spuId;
  66. }
  67. } catch (\Exception $e) {
  68. $this->logError("处理商品 {$product['goods_id']} 失败: ", $e);
  69. continue;
  70. }
  71. }
  72. // 4. 处理需要下架的商品(本地有但第三方已下架)
  73. $this->handleOfflineProducts($syncedSpuIds, $douhuomallDataMap);
  74. return 'success';
  75. } catch (\Exception $e) {
  76. $this->logError('商品同步失败', $e);
  77. return 'error';
  78. }
  79. }
  80. /**
  81. * 批量拉取所有商品数据
  82. */
  83. protected function fetchAllProducts()
  84. {
  85. $allData = [];
  86. // 第一页
  87. $firstPageData = GongApiRequest::get_goods_list(1, $this->config['page_size']);
  88. if (empty($firstPageData['list'])) {
  89. return [];
  90. }
  91. $allData = $firstPageData['list'];
  92. $totalPages = $firstPageData['pages'] ?? 1;
  93. if (!empty($this->config['max_pages'])) {
  94. $totalPages = min($totalPages, $this->config['max_pages']);
  95. }
  96. // 并行获取剩余页面
  97. for ($page = 2; $page <= $totalPages; $page++) {
  98. $pageData = GongApiRequest::get_goods_list($page, $this->config['page_size']);
  99. if (!empty($pageData['list'])) {
  100. $allData = array_merge($allData, $pageData['list']);
  101. }
  102. }
  103. return $allData;
  104. }
  105. /**
  106. * 处理单个商品
  107. */
  108. protected function processSingleProduct($productData, $douhuomallData)
  109. {
  110. $goodsId = $productData['goods_id'];
  111. // 1. 获取商品详情
  112. $goodsInfo = GongApiRequest::get_goods_info($goodsId);
  113. if (!$goodsInfo) {
  114. return null;
  115. }
  116. // 2. 处理SKU信息
  117. if (empty($goodsInfo['sku_list'])) {
  118. return null;
  119. }
  120. $skuInfo = [];
  121. foreach ($goodsInfo['sku_list'] as $sku) {
  122. if (empty($sku['plat_price'])) {
  123. continue;
  124. }
  125. $processedSku = [
  126. 'sku_id' => $sku['sku_id'] ?? 0,
  127. 'plat_price' => $sku['plat_price'],
  128. 'img_url' => $sku['main_img'],
  129. 'retail_price' => $sku['retail_price'] ?? 0,
  130. 'cost_price' => $this->calculateCostPrice($sku['plat_price']),
  131. 'market_price' => $this->calculateMarketPrice($sku['plat_price']),
  132. 'profit' => $this->calculateProfit($sku['plat_price']),
  133. 'mer_profit' => $this->calculateMerProfit($sku['plat_price']),
  134. 'yanglaojin' => '0.00',
  135. 'main_img' => $sku['main_img'] ?? '',
  136. 'attribute_json' => !empty($sku['attr']) ? $sku['attr'] : [['val' => '默认', 'name' => '默认']]
  137. ];
  138. $skuInfo[] = array_merge($sku, $processedSku);
  139. }
  140. // 4. 构建保存数据
  141. $imageData = [
  142. 'cover_url' => $goodsInfo['main_img'] ?? '',
  143. 'detail_images' => $goodsInfo['detail_img'] ?? [],
  144. 'detail_img_list' => $goodsInfo['detail_img'] ?? [] // 为了兼容以前写的代码,建议保留这个数据项,之前用的是detail_img_list字段,但我感觉他们意义一样
  145. ];
  146. // 构建详情HTML
  147. $detailHtml = $this->buildDetailHtml($imageData['detail_images']);
  148. $saveData = [
  149. 'spu_id' => $goodsInfo['goods_id'],
  150. 'skuId_info' => $skuInfo,
  151. 'sys_id' => '112',
  152. 'goods_id' => $goodsInfo['goods_id'],
  153. 'spuId_info' => array_merge(
  154. $goodsInfo,
  155. [
  156. 'cover_url' => $imageData['cover_url'],
  157. 'detail' => $detailHtml,
  158. 'detail_img_list' => $imageData['detail_img_list']
  159. ]
  160. ),
  161. 'sku_id' => $skuInfo[0]['sku_id'] ?? 0,
  162. 'status' => $goodsInfo['status'] ?? 0,
  163. 'cate_ids' => $goodsInfo['cate_ids'] ?? '',
  164. 'title' => $goodsInfo['spu_name'] ?? '',
  165. 'cxb_cate_id' => 0,
  166. 'third_update_time' => $goodsInfo['update_time'] ?? ''
  167. ];
  168. // 5. 保存到本地(使用原有的add方法逻辑)
  169. try {
  170. $spuId = $this->addToDouhuomall($saveData, $douhuomallData);
  171. } catch (DbException $e) {
  172. return 0;
  173. }
  174. return $spuId;
  175. }
  176. /**
  177. * 构建详情HTML
  178. */
  179. protected function buildDetailHtml($detailImages): string
  180. {
  181. if (empty($detailImages)) {
  182. return '';
  183. }
  184. $detailHtml = '';
  185. foreach ((array)$detailImages as $imgUrl) {
  186. $detailHtml .= "<img src='{$imgUrl}' referrerpolicy='no-referrer' />";
  187. }
  188. return $detailHtml;
  189. }
  190. /**
  191. * @param $data
  192. * @param $findData
  193. * @return mixed
  194. * @throws DbException
  195. */
  196. protected function addToDouhuomall($data, $findData)
  197. {
  198. try {
  199. // 对SKU按成本价排序,获取最低成本价的SKU
  200. $skuInfo = $this->arrSort($data['skuId_info'], 'cost_price', SORT_ASC);
  201. // 计算价格字段
  202. $data['cost_price'] = $skuInfo[0]['cost_price'] ?? 0;
  203. $data['market_price'] = $skuInfo[0]['market_price'] ?? 0;
  204. $data['ot_price'] = $skuInfo[0]['retail_price'] ?? 0;
  205. $data['mer_profit'] = $skuInfo[0]['mer_profit'] ?? 0;
  206. $data['pension'] = $skuInfo[0]['yanglaojin'] ?? 0;
  207. $data['profit'] = (empty($data['cost_price']) || empty($data['market_price']))
  208. ? 0
  209. : bcdiv((string)$data['market_price'], (string)$data['cost_price'], 2);
  210. $now = date('Y-m-d H:i:s');
  211. if (empty($findData)) {
  212. // 新增
  213. $insertData = [
  214. 'spu_id' => $data['spu_id'],
  215. 'skuId_info' => json_encode($data['skuId_info'], JSON_UNESCAPED_UNICODE),
  216. 'sys_id' => $data['sys_id'],
  217. 'goods_id' => $data['goods_id'],
  218. 'spuId_info' => json_encode($data['spuId_info'], JSON_UNESCAPED_UNICODE),
  219. 'create_time' => $now,
  220. 'update_time' => $now,
  221. 'sku_id' => $data['sku_id'],
  222. 'status' => $data['status'],
  223. 'cate_ids' => $data['cate_ids'],
  224. 'title' => $data['title'],
  225. 'cost_price' => $data['cost_price'],
  226. 'market_price' => $data['market_price'],
  227. 'ot_price' => $data['ot_price'],
  228. 'profit' => $data['profit'],
  229. 'mer_profit' => $data['mer_profit'],
  230. 'pension' => $data['pension'],
  231. 'cxb_cate_id' => $data['cxb_cate_id'],
  232. 'third_update_time' => $data['third_update_time']
  233. ];
  234. Db::name('douhuomall')->insert($insertData);
  235. // 创建完商品后立马上架
  236. $this->insert_product($data['spu_id'], $this->config['mer_id']);
  237. } else {
  238. // 更新
  239. if (empty($findData['third_update_time']) || strtotime($data['third_update_time']) > strtotime($findData['third_update_time'])) {
  240. $updateData = [
  241. 'status' => $data['status'],
  242. 'update_time' => $now,
  243. 'cost_price' => $data['cost_price'],
  244. 'market_price' => $data['market_price'],
  245. 'ot_price' => $data['ot_price'],
  246. 'mer_profit' => $data['mer_profit'],
  247. 'pension' => $data['pension'],
  248. 'profit' => $data['profit'],
  249. 'skuId_info' => json_encode($data['skuId_info'], JSON_UNESCAPED_UNICODE),
  250. 'spuId_info' => json_encode($data['spuId_info'], JSON_UNESCAPED_UNICODE),
  251. 'title' => $data['title'],
  252. 'cate_ids' => $data['cate_ids'],
  253. 'cxb_cate_id' => $data['cxb_cate_id'],
  254. 'third_update_time' => $data['third_update_time']
  255. ];
  256. Db::name('douhuomall')
  257. ->where('spu_id', $findData['spu_id'])
  258. ->update($updateData);
  259. $productList = Db::name('store_product')
  260. ->where('spu_id', $findData['spu_id'])
  261. // ->where('is_show', ProductEnum::IS_SHOW['Yes']['code'])
  262. ->where('is_del', ProductEnum::IS_DEL['No']['code'])
  263. // ->where('status', '<>', ProductEnum::STATUS['Withdrawn']['code'])
  264. ->select()
  265. ->toArray();
  266. $productIdList = array_column($productList, 'product_id');
  267. // 将绑定的商品进行下架
  268. Db::name('store_product')
  269. ->whereIn('product_id', $productIdList)
  270. ->update([
  271. // 'is_show' => ProductEnum::IS_SHOW['No']['code'],
  272. 'is_del' => ProductEnum::IS_DEL['Yes']['code'],
  273. // 'status' => ProductEnum::STATUS['Withdrawn']['code'],
  274. // 'is_status' => ProductEnum::IS_STATUS['Automatic']['code']
  275. ]);
  276. /** @var DouhuomallChangeLogEntity $douhuomallChangeLogEntity */
  277. $douhuomallChangeLogEntity = DouhuomallChangeLogEntity::newInstance();
  278. $douhuomallChangeLogEntity->setSpuId($findData['spu_id'])
  279. ->setDataJson($findData)
  280. ->setEffectProductJson($productIdList)
  281. ->setCreateTime(date('Y-m-d H:i:s'));
  282. /** @var DouhuomallChangeLogRepository $douhuomallChangeLogRepository */
  283. $douhuomallChangeLogRepository = app()->make(DouhuomallChangeLogRepository::class);
  284. $douhuomallChangeLogRepository->createByEntity($douhuomallChangeLogEntity);
  285. // 将商品重新上架
  286. // 创建完商品后立马上架
  287. $this->insert_product($data['spu_id'], $this->config['mer_id']);
  288. }
  289. }
  290. return $data['spu_id'];
  291. } catch (\Exception $e) {
  292. $this->logError('保存商品到douhuomall失败: ', $e);
  293. throw $e;
  294. }
  295. }
  296. /**
  297. * 恢复商品上架状态
  298. */
  299. protected function restoreProduct($spuId)
  300. {
  301. Db::name('store_product')
  302. ->where('spu_id', $spuId)
  303. ->update([
  304. 'is_show' => ProductEnum::IS_SHOW['Yes']['code'],
  305. 'status' => ProductEnum::STATUS['Approved']['code']
  306. ]);
  307. Log::info("恢复商品上架(恢复到仓库中)状态: spu_id={$spuId}");
  308. }
  309. /**
  310. * 处理需要下架的商品(第三方已下架)
  311. */
  312. protected function handleOfflineProducts($syncedSpuIds, $douhuomallDataMap)
  313. {
  314. $localSpuIds = array_keys($douhuomallDataMap);
  315. $offlineSpuIds = array_diff($localSpuIds, $syncedSpuIds);
  316. if (empty($offlineSpuIds)) {
  317. return;
  318. }
  319. $productList = Db::name('store_product')
  320. ->whereIn('spu_id', $offlineSpuIds)
  321. // ->where('is_show', ProductEnum::IS_SHOW['Yes']['code'])
  322. ->where('is_del', ProductEnum::IS_DEL['No']['code'])
  323. // ->where('status', '<>', ProductEnum::STATUS['Withdrawn']['code'])
  324. ->field('product_id, spu_id, is_show, is_del, status')
  325. ->select()
  326. ->toArray();
  327. if (empty($productList)) {
  328. return;
  329. }
  330. $productIdList = [];
  331. $spuIdMap = [];
  332. foreach ($productList as $item) {
  333. $productIdList[] = $item['product_id'];
  334. $spuIdMap[$item['spu_id']][] = $item['product_id'];
  335. }
  336. // 将绑定的商品进行下架
  337. Db::name('store_product')
  338. ->whereIn('product_id', $productIdList)
  339. ->update([
  340. // 'is_show' => ProductEnum::IS_SHOW['No']['code'],
  341. 'is_del' => ProductEnum::IS_DEL['Yes']['code'],
  342. // 'status' => ProductEnum::STATUS['Withdrawn']['code'],
  343. // 'is_status' => ProductEnum::IS_STATUS['Automatic']['code']
  344. ]);
  345. foreach ($spuIdMap as $spuId => $productIdListGroupBySpuId) {
  346. /** @var DouhuomallChangeLogEntity $douhuomallChangeLogEntity */
  347. $douhuomallChangeLogEntity = DouhuomallChangeLogEntity::newInstance();
  348. $douhuomallChangeLogEntity->setSpuId($spuId)
  349. ->setDataJson($douhuomallDataMap[$spuId])
  350. ->setEffectProductJson($productIdListGroupBySpuId)
  351. ->setCreateTime(date('Y-m-d H:i:s'));
  352. /** @var DouhuomallChangeLogRepository $douhuomallChangeLogRepository */
  353. $douhuomallChangeLogRepository = app()->make(DouhuomallChangeLogRepository::class);
  354. $douhuomallChangeLogRepository->createByEntity($douhuomallChangeLogEntity);
  355. }
  356. // foreach ($offlineSpuIds as $spuId) {
  357. // $this->offlineProductBySpuId($spuId, 'third_party_offline');
  358. // }
  359. Log::info("下架第三方已下架商品" . json_encode(['count' => count($offlineSpuIds), 'spu_ids' => $offlineSpuIds]));
  360. }
  361. /**
  362. * 数组排序(从原有add方法中提取)
  363. */
  364. protected function arrSort($array, $keys, $sort = SORT_DESC)
  365. {
  366. $keysValue = [];
  367. foreach ($array as $k => $v) {
  368. $keysValue[$k] = $v[$keys] ?? 0;
  369. }
  370. array_multisort($keysValue, $sort, $array);
  371. return $array;
  372. }
  373. /**
  374. * 计算成本价
  375. */
  376. protected function calculateCostPrice($platPrice)
  377. {
  378. return bcadd((string)$platPrice, bcmul((string)$platPrice, '0.03', 4), 2);
  379. }
  380. /**
  381. * 计算市场价
  382. */
  383. protected function calculateMarketPrice($platPrice)
  384. {
  385. $costPrice = $this->calculateCostPrice($platPrice);
  386. return bcadd($costPrice, bcmul($costPrice, '0.2', 4), 2);
  387. }
  388. /**
  389. * 计算利润率
  390. */
  391. protected function calculateProfit($platPrice)
  392. {
  393. $costPrice = $this->calculateCostPrice($platPrice);
  394. $marketPrice = $this->calculateMarketPrice($platPrice);
  395. return bcdiv($marketPrice, $costPrice, 2);
  396. }
  397. /**
  398. * 计算毛利
  399. */
  400. protected function calculateMerProfit($platPrice)
  401. {
  402. $costPrice = $this->calculateCostPrice($platPrice);
  403. $marketPrice = $this->calculateMarketPrice($platPrice);
  404. return bcsub($marketPrice, $costPrice, 2);
  405. }
  406. /**
  407. * 记录错误
  408. */
  409. protected function logError($message, \Exception $e)
  410. {
  411. $errorInfo = [
  412. 'message' => $message,
  413. 'error_message' => $e->getMessage(),
  414. 'error_file' => $e->getFile(),
  415. 'error_line' => $e->getLine(),
  416. 'error_code' => $e->getCode(),
  417. 'exception_class' => get_class($e),
  418. 'trace' => $e->getTraceAsString()
  419. ];
  420. Log::error($message);
  421. Log::error($message . json_encode($errorInfo, JSON_UNESCAPED_UNICODE));
  422. Db::name('log')->insert([
  423. 'data' => json_encode($errorInfo, JSON_UNESCAPED_UNICODE),
  424. 'time' => date('Y-m-d H:i:s')
  425. ]);
  426. }
  427. // 借用 app\controller\merchant\gong\Goods.php 控制器内方法
  428. /**
  429. * 将 微唯宝商品 选品(加入) 到系统内部商户商品表
  430. * @param $spu_id
  431. * @param $mer_id
  432. * @return bool
  433. */
  434. public function insert_product($spu_id, $mer_id)
  435. {
  436. try {
  437. // 1、获取商品信息
  438. $data = Db::name('douhuomall')->where('spu_id', $spu_id)->find();
  439. // 2、是否为多规格
  440. $sku_data = json_decode($data['skuId_info'], true);
  441. $spec_type = count($sku_data);
  442. if ($spec_type > 1) {
  443. $spec_type = 1;
  444. } else {
  445. $spec_type = 0;
  446. }
  447. // 3、商品主图
  448. $spuId_info = json_decode($data['spuId_info'], true);
  449. $slider_image = $spuId_info['detail_img_list'] ?? '';
  450. $image = $spuId_info['cover_url'] ?? '';
  451. if (empty($slider_image)) {
  452. $slider_image = $image;
  453. } else if (is_string($slider_image)) {
  454. $slider_image = implode(',', json_decode($slider_image, true));
  455. } else if (is_array($slider_image)) {
  456. $slider_image = implode(',', $slider_image);
  457. }
  458. $yanglaojin_scale = (!empty($data['pension']) && !empty($data['market_price'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0;
  459. // 4、商品入库数据
  460. $product_insert_data = [
  461. 'mer_id' => $mer_id,
  462. 'image' => $image,
  463. 'slider_image' => $slider_image,
  464. 'store_name' => $data['title'],
  465. 'store_info' => 1,
  466. 'keyword' => mb_substr($data['title'], 0, 3),
  467. 'is_show' => 1,
  468. 'status' => 1,
  469. 'cate_id' => $data['cate_ids'],
  470. 'unit_name' => '个',
  471. 'price' => $data['market_price'],
  472. 'cost' => $data['cost_price'],
  473. 'ot_price' => $data['ot_price'],
  474. 'stock' => '1000',
  475. 'spec_type' => $spec_type,
  476. 'extension_type' => 1,
  477. 'mer_status' => 1,
  478. 'is_used' => 1,
  479. 'old_product_id' => $data['id'],
  480. 'volunteer' => 0,
  481. 'type' => 1,
  482. 'pension' => '0.00',
  483. 'commission' => 5,
  484. 'spu_id' => $data['spu_id'],
  485. 'temp_id' => 105,
  486. 'plate_mer_profit' => $data['mer_profit'],
  487. 'concession_pri' => $data['mer_profit'],
  488. 'yanglaojin_scale' => $yanglaojin_scale
  489. ];
  490. $insert_id = Db::name('store_product')->insertGetId($product_insert_data);
  491. // 5、更新商品详情表
  492. $detail = $spuId_info['detail'];
  493. if (!is_null(json_decode($detail))) {
  494. $detail_list = json_decode($detail);
  495. $detail = '';
  496. foreach ($detail_list as $value) {
  497. $detail .= '<img src="' . $value . '"></img>';
  498. }
  499. }
  500. Db::name('store_product_content')->insert(['content' => $detail, 'product_id' => $insert_id]);
  501. // 6、
  502. $this->insert_sku($insert_id, $sku_data);
  503. return true;
  504. } catch (\Exception $e) {
  505. $this->logError($spu_id . "加入选品失败:", $e);
  506. return false;
  507. }
  508. }
  509. // 借用 app\controller\merchant\gong\Goods.php 控制器内方法
  510. public function insert_sku($id, $data)
  511. {
  512. // 1、获取 所有SKU的 规格属性值
  513. $sku_attr_data = array_column($data, 'attribute_json');
  514. $name = '';
  515. // 2、写入 商品属性表
  516. /** @var ProductRepository $ProductRepository */
  517. $ProductRepository = app()->make(ProductRepository::class);
  518. if (empty($sku_attr_data[0][0])) {
  519. // 写入商品属性表
  520. Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]);
  521. $key = '';
  522. $num = 1;
  523. // 写入 SKU 商品属性值表
  524. foreach ($data as $k => $v) {
  525. Db::name('store_product_attr_value')->insert([
  526. 'product_id' => $id,
  527. 'detail' => json_encode(['规格' => $key]),
  528. 'sku' => $key,
  529. 'image' => $v['img_url'],
  530. 'cost' => $v['cost_price'],
  531. 'ot_price' => $v['retail_price'],
  532. 'price' => $v['market_price'],
  533. 'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0),
  534. 'stock' => 100,
  535. 'cost_price' => $v['cost_price'],
  536. 'extension_one' => 5,
  537. 'gong_sku_id' => $v['sku_id'],
  538. 'gong_mer_profit' => $v['mer_profit'],
  539. 'gong_pension' => $v['yanglaojin'],
  540. 'gong_market_price' => $v['market_price'],
  541. 'plate_mer_profit' => $v['mer_profit']
  542. ]);
  543. $key = '';
  544. $num++;
  545. }
  546. } else {
  547. foreach ($sku_attr_data as $kk => $vv) {
  548. foreach ($vv as $k1 => $v1) {
  549. $name .= $v1['val'];
  550. }
  551. $name .= '-!-';
  552. }
  553. $name = rtrim($name, '-!-');
  554. Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]);
  555. $key = '';
  556. $num = 1;
  557. foreach ($data as $k => $v) {
  558. foreach ($v['attribute_json'] as $k2 => $v2) {
  559. $key .= $v2['val'];
  560. }
  561. Db::name('store_product_attr_value')->insert([
  562. 'product_id' => $id,
  563. 'detail' => json_encode(['规格' => $key]),
  564. 'sku' => $key,
  565. 'image' => $v['img_url'],
  566. 'cost' => $v['cost_price'],
  567. 'ot_price' => $v['retail_price'],
  568. 'price' => $v['market_price'],
  569. 'unique' => $ProductRepository->setUnique((int)$id, $v['sku_id'], 0),
  570. 'stock' => 100,
  571. 'cost_price' => $v['cost_price'],
  572. 'extension_one' => 5,
  573. 'gong_sku_id' => $v['sku_id'],
  574. 'gong_mer_profit' => $v['mer_profit'],
  575. 'gong_pension' => $v['yanglaojin'],
  576. 'gong_market_price' => $v['market_price'],
  577. 'plate_mer_profit' => $v['mer_profit']
  578. ]);
  579. $key = '';
  580. $num++;
  581. }
  582. }
  583. }
  584. }