createData($item, $storeId); if (!isset($item['G']) || !$item['G']) { continue; } list($ret, $errorLog) = $service->updateGoods($item['G'], $data, $storeId); if ($ret == false) { $this->errorLog[] = $errorLog; continue; } // 记录导入成功 $this->successCount++; } // 更新导入记录 $this->updateRecord($recordId, \count($list)); return true; } /** * 更新导入记录 * @param int $recordId 商品导入记录ID * @param int $currentCount 当前任务导入的商品总量 * @return void */ private function updateRecord(int $recordId, int $currentCount) { // 获取导入记录 $model = GoodsImportModel::detail($recordId); // 计算导入失败的数量 $failCount = $currentCount - $this->successCount; // 更新导入记录 $model->save([ 'success_count' => $model['success_count'] + $this->successCount, 'fail_count' => $model['fail_count'] + $failCount, 'fail_log' => array_merge($model['fail_log'], $this->errorLog), ]); // 判断是否为最后一次队列 if ($model['total_count'] <= ($model['success_count'] + $model['fail_count'])) { $model->save(['end_time' => \time(), 'status' => GoodsImportStatusEnum::COMPLETED]); } } /** * 生成商品数据(用于写入数据库) * @param array $original * @param int $storeId * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function createData(array $original, int $storeId): array { // 整理商品数据 $data = [ 'goods_id' => $original['A'] ?? 0, 'goods_name' => $original['B'] ?? "", 'cmmdty_model' => $original['C'] ?? "", 'link' => $original['D'] ?? "", 'cost_price_min' => $original['E'] ?? "", 'stock_total' => $original['F'] ?? "", 'link_other' => $original['G'] ?? "", 'goods_price_min' => $original['H'] ?? "", 'goods_source' => $original['J'] ?? "GC", 'delivery_time' => $original['K'] ?? 2, 'is_check' => $original['L'] ?? 0, 'is_use_jd_stock' => $original['M'] ?? 0, 'is_cover' => $original['N'] ?? 0,//是否覆盖 ]; return $data; } /** * ID集字符串转换成数组 * @param string $value * @return array|false|string[] */ private function ids2array(string $value) { return !empty($value) ? \explode(',', $value) : []; } /** * 创建标准的商品SKU数据 * @param array $originalSkuList * @param array $specList * @return array */ private function createSkuList(array $originalSkuList, array $specList): array { $data = []; foreach ($originalSkuList as $item) { // 设置skuKeys数据 foreach ($item['specNames'] as $spec) { $specGroup = helper::arraySearch($specList, 'spec_name', $spec[0]); $specValue = helper::arraySearch($specGroup['valueList'], 'spec_value', $spec[1]); $item['skuKeys'][] = [ 'groupKey' => $specGroup['key'], 'valueKey' => $specValue['key'] ]; } // 整理SKU数据 $data[] = [ 'image_id' => 0, 'goods_price' => $item[$this->mapSku['goods_price']], 'line_price' => $item[$this->mapSku['line_price']], 'stock_num' => $item[$this->mapSku['stock_num']], 'goods_weight' => $item[$this->mapSku['goods_weight']], 'goods_sku_no' => $item[$this->mapSku['goods_sku_no']], 'skuKeys' => $item['skuKeys'], ]; } return $data; } /** * 创建标准的商品规格数据(树状) * @param array $originalSkuList * @return array */ private function createSpecList(array &$originalSkuList): array { // 生成规格数据 $data = []; foreach ($originalSkuList as &$item) { $tempArr1 = \explode(',', $item['H']); // ['颜色:白色', '尺码:小'] foreach ($tempArr1 as $val) { $tempArr2 = \explode(':', $val); // ['颜色','白色'] $data[$tempArr2[0]][$tempArr2[1]] = 1; $item['specNames'][] = $tempArr2; } } // 整理为specList格式 $specList = []; foreach ($data as $specName => $specValues) { $groupKey = \count($specList); $valueList = []; foreach ($specValues as $specValue => $key) { $valueList[] = [ 'key' => \count($valueList), 'groupKey' => $groupKey, 'spec_value' => $specValue, ]; } $specList[] = [ 'key' => $groupKey, 'spec_name' => $specName, 'valueList' => $valueList, ]; } return $specList; } }