| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- <?php
- namespace addons\QiShangTong\common\logic;
- use addons\QiShangTong\common\enums\QiShangTongEnums;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\model\GoodsDetailModel;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\request\GoodsDetailRequest;
- use addons\QiShangTong\common\helper\RequestHelper;
- use addons\QiShangTong\common\models\QiShangTongCate;
- use addons\QiShangTong\common\models\QiShangTongGoods;
- use addons\QiShangTong\common\service\CategoryService;
- use addons\QiShangTong\common\service\SettingService;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\goods\GoodsAttr;
- use Exception;
- use yii\helpers\Json;
- class GoodsLogic
- {
- protected $goodsModel = null;
- protected $mall_id = 0;
- protected $config = [];
- protected $config_prices = [];
- public function __construct(QiShangTongGoods $goodsModel, $config)
- {
- $this->goodsModel = $goodsModel;
- $this->mall_id = $goodsModel->mall_id;
- $this->config = $config;
- $this->config_prices = !empty($config['price']) ? Json::decode($config['price']) : [];
- }
- /**
- * @return array
- * @throws Exception
- */
- protected function goods()
- {
- $contentModel = new GoodsDetailModel();
- $contentModel->id = $this->goodsModel->third_goods_id;
- return RequestHelper::send(new GoodsDetailRequest(), $contentModel, $this->goodsModel->mall_id);
- }
- public function execute()
- {
- try {
- $goods = $this->goods();
- $specs_name = [
- '1' => $goods['goods']['spec_name_1'],
- '2' => $goods['goods']['spec_name_2'],
- '3' => $goods['goods']['spec_name_3'],
- '4' => $goods['goods']['spec_name_4'],
- ];
- $goods['specs_name'] = array_filter($specs_name, function ($val) {
- return !empty($val);
- });
- if (empty($goods['specs_name'])) $goods['specs_name'] = ['1' => '规格'];
- // 获取商城商品字段
- $goods_attr = $this->format($goods);
- // 获取规格属性
- $attr_groups = $this->group($goods['goods']['spec'], $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['goods']['spec'], $goods);
- $is_attr = count($specs_attr) > 1 ? 1 : 0;
- $prices = array_column(array_values($goods['goods']['spec']), 'price');
- $gonghuojia_prices = array_column(array_values($goods['goods']['spec']), 'gonghuojia');
- $retail_prices = array_column(array_values($goods['goods']['spec']), 'retail_price');
- $price_arr['price'] = count($prices) == 1 ? $prices[0] : min(...$prices);
- $price_arr['gonghuojia'] = count($gonghuojia_prices) == 1 ? $gonghuojia_prices[0] : min(...$gonghuojia_prices);
- $price_arr['retail_price'] = count($retail_prices) == 1 ? $retail_prices[0] : min(...$retail_prices);
- $_prices = [
- 'price' => $this->goodsModel->price,
- 'gonghuojia' => $this->goodsModel->gonghuojia,
- 'retail_price' => $this->goodsModel->retail_price,
- ];
- $cats = CategoryService::firstCate($this->mall_id, $goods['goods']['cate_id_1'] ?? 0, $goods['goods']['cate_id_2'] ?? 0);
- if (is_string($cats)) throw new Exception($cats);
- $attr = [
- // 'cats' => [$this->getCats($goods_info)], // 分类
- 'attr' => $specs_attr, // 商品规格属性
- 'bannar_pic' => $this->images($goods['img']),
- 'cover_pic' => $goods['goods']['default_image'], // 主图
- 'price' => SettingService::getPrice($this->mall_id, SettingService::PRICE, $_prices, $this->config_prices), // 销售价
- 'original_price' => SettingService::getPrice($this->mall_id, SettingService::ORIGINAL_PRICE, $_prices, $this->config_prices), // 原价
- 'cost_price' => SettingService::getPrice($this->mall_id, SettingService::COST_PRICE, $_prices, $this->config_prices), // 成本价
- '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' => QiShangTongEnums::ADDONS_NAME,
- 'goods_no' => $is_attr ? '' : "{$goods['goods']['spec'][0]['spec_id']}",
- 'is_attr' => $is_attr,
- 'is_on_sale' => $goods['goods']['if_sale'] == StatusEnum::ENABLED ? StatusEnum::ENABLED : StatusEnum::DISABLED,
- // 'free_shipping_num' => $goods['goods']['free_shipping_num'],
- 'goods_weight' => $goods['goods']['weight'],
- 'third_price' => $_prices['price'], // 销售价
- 'retail_price' => $_prices['retail_price'], // 原价
- 'gonghuojia' => $_prices['gonghuojia'], // 成本价
- 'cats' => $cats,
- ];
- // 商品规格为空 不添加
- if (empty($goods['goods']['spec'])) {
- 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['gonghuojia'], 'price_rate'), // 价格 sale_price cost_price market_price guide_price
- // 'original_price' => $this->price($sku['gonghuojia'], 'market_price_rate'), // 价格 sale_price cost_price market_price guide_price
- // 'cost_price' => $this->price($sku['gonghuojia'], ''), // 价格 sale_price cost_price market_price guide_price
- 'price' => SettingService::getPrice($this->mall_id, SettingService::PRICE, $sku, $this->config_prices), // 销售价
- 'original_price' => SettingService::getPrice($this->mall_id, SettingService::ORIGINAL_PRICE, $sku, $this->config_prices), // 原价
- 'cost_price' => SettingService::getPrice($this->mall_id, SettingService::COST_PRICE, $sku, $this->config_prices), // 成本价
- 'no' => '',
- 'weight' => 0,
- 'pic_url' => $sku['spec_image'],
- 'distributionLevelList' => [],
- 'member_price' => [],
- 'levelPrice' => [],
- // 'sku_id' => $sku['original_sku_id'],
- 'goods_no' => $sku['spec_id'],
- 'third_spec_id' => $sku['spec_id'],
- 'third_goods_id' => $this->goodsModel->third_goods_id,
- 'third_price' => $sku['price'],
- 'retail_price' => $sku['retail_price'],
- 'gonghuojia' => $sku['gonghuojia']
- ];
- 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, 1, 2) : bcmul(bcmul($price, $this->config[$type] ?? 0, 2), 0.01, 2);
- }
- /**
- * @param $images
- * @return array
- */
- protected function images($images)
- {
- $items = [];
- foreach ($images as $image) {
- $items[] = ['id' => 0, 'pic_url' => $image['image_url']];
- }
- return $items;
- }
- }
|