goodsModel = $goodsModel; $this->mall_id = $goodsModel->mall_id; $this->config = $config; $this->config_prices = !empty($config['price']) ? Json::decode($config['price']) : []; } /** * @return array|int|string * @throws Exception */ protected function goods() { $contentModel = RequestHelper::contentModelInstance($this->mall_id); $contentModel->content->data = new GoodsDetailV2Model(); $contentModel->content->data->id = $this->goodsModel->third_goods_id; $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE; return RequestHelper::send(new GoodsDetailV2Request(), RequestHelper::enCrypto($contentModel, $this->mall_id), $this->mall_id); } public function execute() { try { $goods = $this->goods(); // 获取商城商品字段 $goods_attr = $this->format($goods); // 获取规格属性 $attr_groups = $this->group($goods['specs'], $goods); // $attr_groups = $this->removeAttrGroups($attr_groups, $goods_attr['attr']); // 匹配规格ID $goods_attr = $this->matchAttrId($goods_attr, $attr_groups); $goods_attr['attr_groups'] = $attr_groups; return $goods_attr; } catch (Exception $e) { \Yii::error(FormatHelper::exception($e, __METHOD__)); return $e->getMessage(); } } /** * 格式化数据 * @param $goods * @return array * @throws Exception */ protected function format($goods) { // 获取匹配的Attr $specs_attr = $this->specForAttr($goods['specs'], $goods); $is_attr = count($specs_attr) > 1 ? 1 : 0; $prices = array_column(array_values($goods['specs']), 'price'); $retail_prices = array_column(array_values($goods['specs']), 'retail_price'); $lowest_prices = array_column(array_values($goods['specs']), 'lowest_price'); $price_arr['price'] = count($prices) == 1 ? $prices[0] : min(...$prices); $price_arr['retail_price'] = count($retail_prices) == 1 ? $retail_prices[0] : min(...$retail_prices); $price_arr['lowest_price'] = count($lowest_prices) == 1 ? $lowest_prices[0] : min(...$lowest_prices); $_prices = [ 'price' => $this->goodsModel->price, 'retail_price' => $this->goodsModel->retail_price, 'lowest_price' => $this->goodsModel->lowest_price, ]; $cat1 = CategoryService::firstCate($this->mall_id, 0, $goods['goods']['cate_id_1'] ?? 0); if (is_string($cat1)) throw new Exception($cat1); $cat2 = CategoryService::firstCate($this->mall_id, $goods['goods']['cate_id_1'] ?? 0, $goods['goods']['cate_id_2'] ?? 0); if (is_string($cat2)) throw new Exception($cat2); $attr = [ // 'cats' => [$this->getCats($goods_info)], // 分类 'attr' => $specs_attr, // 商品规格属性 'bannar_pic' => $this->images($goods['images']), 'cover_pic' => $goods['goods']['default_image'], // 主图 'price' => SettingService::getPrice($this->mall_id, SettingService::PRICE, $_prices, $this->config_prices, 1), // 销售价 'original_price' => SettingService::getPrice($this->mall_id, SettingService::ORIGINAL_PRICE, $_prices, $this->config_prices, 1), // 原价 'cost_price' => SettingService::getPrice($this->mall_id, SettingService::COST_PRICE, $_prices, $this->config_prices, 1), // 成本价 // 'price' => SettingService::getPrice($this->mall_id, SettingService::PRICE, $price_arr, $this->config_prices, 0.01), // 销售价 // 'original_price' => SettingService::getPrice($this->mall_id, SettingService::ORIGINAL_PRICE, $price_arr, $this->config_prices, 0.01), // 原价 // 'cost_price' => SettingService::getPrice($this->mall_id, SettingService::COST_PRICE, $price_arr, $this->config_prices, 0.01), // 成本价 'detail' => empty($goods['goods']['description']) ? $goods['goods']['goods_name'] : $goods['goods']['description'], // 商品详情 'stock' => min(max($goods['goods']['stock'], 0), 99999999), // 总库存 // 'status' => $goods_info['is_display'], // 状态 相同 'goods_name' => $goods['goods']['goods_name'], // 商品名称 'goods_source' => QiDingDongEnums::ADDONS_NAME, 'goods_no' => $is_attr ? '' : "{$goods['specs'][0]['spec_id']}", 'is_attr' => $is_attr, 'is_on_sale' => $goods['goods']['on_off_status'], 'free_shipping_num' => $goods['goods']['free_shipping_num'], 'goods_weight' => $goods['goods']['weight'], 'third_price' => $_prices['price'], // 销售价 'retail_price' => $_prices['retail_price'], // 原价 'lowest_price' => $_prices['lowest_price'], // 成本价 'cats' => [$cat1, $cat2], ]; // 商品规格为空 不添加 if (empty($goods['specs'])) { throw new Exception("商品规格为空, 不能导入"); } if (!empty($this->goodsModel->goods_id)) { $attr['id'] = $this->goodsModel->goods_id; } // if ($this->is_update) { // // 当商品上架时 不需要改变系统商品状态1 // if ($goods['is_display'] == 1) { // unset($goods['is_display']); // } // // $detail = MallGoodsModel::getDetail($this->goods_id); // return $this->mergeDetail($detail, $attr); // } else { // 获取分类 仅新增时添加分类 return array_merge($attr, [ 'goods_type' => 1, // 默认1 'subtitle' => '', // 'labels' => [], // 商品标签 'mchCats' => [], // 多商户分类 'services' => [], // 商品服务 'unit' => '', // 单位 'virtual_sales' => 0, // 虚拟销量 'stock_warning' => 0, 'goods_weight' => 0, // 商品权重 'is_show_stock' => 1, 'is_show_sales' => 0, // 是否显示销量 'freight_type' => [], 'freight_rules_type' => 1, 'freight_id' => '', 'shipping_fee' => 0, 'free_shipping_num' => 0, 'free_shipping_money' => 0, 'is_cross_border' => 0, 'is_id_card' => 0, 'extra' => '', // 额外属性 'coupon_selection' => [], 'goods_setting' => [], 'is_area_limit' => 0, 'area_limit' => [], 'attr_groups' => [], 'mch_id' => 0, 'mall_id' => $this->mall_id, 'is_show' => 1, ]); // } } protected function group($skus, $goods) { $attr_groups = []; $attr_group_id = 1; foreach ($goods['specs_name'] as $spec) { $attr_groups[] = [ 'attr_group_id' => $attr_group_id, 'attr_group_name' => $spec, ]; $attr_group_id++; } $attr_name = []; $attr_id = 1; foreach ($skus as $sku) { foreach ($goods['specs_name'] as $sKey => $spec_name) { $key = "spec_{$sKey}"; if (!empty($sku[$key])) $sku['options'][$spec_name] = $sku[$key]; } if (!empty($sku['options'])) { foreach ($attr_groups as $k => $group) { foreach ($sku['options'] as $g_name => $s_name) { if ($group['attr_group_name'] == $g_name && !in_array($s_name, $attr_name)) { $attr_name[] = $s_name; $attr_groups[$k]['attr_list'][] = [ 'attr_id' => $attr_id, 'attr_name' => $s_name, ]; $attr_id++; } } } } } return $attr_groups; } /** * @param $attrs * @param $groups * @return array */ protected function matchAttrId($attrs, $groups) { foreach ($attrs['attr'] as $k => $sku) { $sign = []; foreach ($sku['attr_list'] as $k1 => $attr) { $attr_group_id = $this->attrGroupId($groups, $attr['attr_group_name']); $attr_id = $this->attrId($groups, $attr['attr_name']); $sign[] = $attr_id; $attrs['attr'][$k]['attr_list'][$k1]['attr_id'] = $attr_id; $attrs['attr'][$k]['attr_list'][$k1]['attr_group_id'] = $attr_group_id; } // 拼接sign_id asort($sign); count($sign) > 1 && $attrs['attr'][$k]['sign_id'] = implode(':', $sign); } return $attrs; } /** * @param $groups * @param $name * @return mixed|void */ protected function attrGroupId($groups, $name) { foreach ($groups as $group) { if ($group['attr_group_name'] == $name) { return $group['attr_group_id']; } } } /** * @param $groups * @param $name * @return mixed|void */ protected function attrId($groups, $name) { foreach ($groups as $group) { foreach ($group['attr_list'] as $attr) { if ($attr['attr_name'] == $name) { return $attr['attr_id']; } } } } /** * @param $skus * @param $goods * @return array * @throws Exception */ protected function specForAttr($skus, $goods) { if (empty($skus)) { throw new Exception('供应链未返回商品规格信息skus'); } $attr = []; foreach ($skus as $sku) { // 仅需要上架的 // if ($sku['is_display'] == 1) { $temp = [ 'attr_list' => $this->attrList($goods['specs_name'], [$sku['spec_1'], $sku['spec_2'], $sku['spec_3'], $sku['spec_4']]), // 规格 'stock' => min(max($sku['stock'], 0), 99999999), // 库存 // 'price' => $this->price($sku['price'], 'price_rate'), // 价格 sale_price cost_price market_price guide_price // 'original_price' => $this->price($sku['price'], 'market_price_rate'), // 价格 sale_price cost_price market_price guide_price // 'cost_price' => $this->price($sku['price'], ''), // 价格 sale_price cost_price market_price guide_price 'price' => SettingService::getPrice($this->mall_id, SettingService::PRICE, $sku, $this->config_prices, 0.01), // 销售价 'original_price' => SettingService::getPrice($this->mall_id, SettingService::ORIGINAL_PRICE, $sku, $this->config_prices, 0.01), // 原价 'cost_price' => SettingService::getPrice($this->mall_id, SettingService::COST_PRICE, $sku, $this->config_prices, 0.01), // 成本价 'no' => '', 'weight' => 0, 'pic_url' => '', 'distributionLevelList' => [], 'member_price' => [], 'levelPrice' => [], // 'sku_id' => $sku['original_sku_id'], 'goods_no' => $sku['spec_id'], 'third_price' => bcmul($sku['price'], 0.01, 2), 'retail_price' => bcmul($sku['retail_price'], 0.01, 2), 'lowest_price' => bcmul($sku['lowest_price'], 0.01, 2) ]; if (!empty($this->goodsModel->goods_id)) { /** * @var $localGoodsAttr GoodsAttr */ $localGoodsAttr = GoodsAttr::getOne([ 'goods_id' => $this->goodsModel->goods_id, 'mall_id' => $this->mall_id, 'goods_no' => $sku['spec_id'] ]); if (!empty($LocalGoodsAttr)) $temp['id'] = $localGoodsAttr->id; } /* // 如果是更新 if ($this->is_update) { $params = [ 'sku_id' => $temp['sku_id'], 'mall_id' => $this->mall_id, 'goods_id' => $this->goods_id, ]; $sku_model = GoodsSkuModel::find()->where($params)->select('attr_id')->one(); if ($sku_model) { $temp['id'] = $sku_model->attr_id; } } // 如果库存大于0 可以上架 if ($sku['stock'] > 0) { $this->goods_status = true; } // 中台ID if (isset($this->supply_id)) { $temp['supply_id'] = $this->supply_id; } // 供应链ID if (isset($this->supply_goods_id)) { $temp['supply_goods_id'] = $this->supply_goods_id; } */ $attr[] = $temp; // } } // 剔除没有规格的商品 foreach ($attr as $k => $v) { if (empty($v['attr_list'])) { unset($attr[$k]); } } return $attr; } /** * @param $specs_names * @param $specs * @return array */ protected function attrList($specs_names, $specs) { $attr = []; foreach ($specs as $index => $item) { if (!empty($item)) { $attr[] = [ 'attr_group_name' => $specs_names[$index+1], 'attr_name' => $item, ]; } } return $attr; } protected function price($price, $type = '') { return empty($type) ? bcmul($price, 0.01, 2) : bcmul(bcmul($price, $this->config[$type] ?? 0, 2), 0.0001, 2); } /** * @param $images * @return array */ protected function images($images) { $items = []; foreach ($images as $image) { $items[] = ['id' => 0, 'pic_url' => $image['image_url']]; } return $items; } }