| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- namespace addons\Store\common\services;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreGoods;
- use addons\Store\common\models\StoreGoodsCate;
- use addons\WuzhouChain\common\models\WuzhouChainStorage;
- use addons\WuzhouChain\common\services\SettingService;
- use common\enums\AddonsEnum;
- use common\enums\GoodsTypeEnum;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- use common\models\goods\GoodsCate;
- use common\models\goods\GoodsReserveSku;
- use common\models\mall\MallAddons;
- class SyncMallGoodsService
- {
- // 同步商品到门店---分类也同步
- public static function syncMallGoods($params)
- {
- try {
- $mall_id = $params['mall_id'];
- $selected_goods_ids = $params['goods_ids'];
- $store_id = $params['store_id'];
- self::checkWuzhouChain($mall_id);
- /**
- * @var $goods_list \common\models\goods\Goods[]
- */
- $goods_list = \common\models\goods\Goods::findAll(['mall_id' => $mall_id, 'id' => $selected_goods_ids]);
- $goods_list = $goods_list ?? [];
- foreach ($goods_list as $goods) {
- $chainStorage = WuzhouChainStorage::findOne(['mall_id' => $mall_id, 'goods_id' => $goods['id'], 'store_id' => $store_id, 'status' => StatusEnum::ENABLED]);
- if ($chainStorage) continue;
- // 商品分类-生成或者获取门店分类
- $cats = self::getCate($mall_id, $goods['id'], $store_id);
- try {
- $store_goods = StoreGoods::findOne(['mall_id' => $mall_id, 'store_id' => $store_id, 'mall_goods_id' => $goods['id'], 'status' => StatusEnum::ENABLED, 'supplier_type' => StoreEnum::SELF_MALL_WUZHOU]);
- if ($store_goods) {
- continue;
- }
- $base_params = [
- 'mall_id' => $mall_id,
- 'store_id' => $store_id,
- 'supplier_id' => $store_id,
- 'supplier_type' => StoreEnum::SELF_MALL_WUZHOU,
- 'cats' => $cats,
- 'mall_goods_id' => $goods['id'],
- 'id' => $store_goods['id'] ?? 0,
- 'coupon_selection' => [],
- 'check_status' => StatusEnum::ENABLED
- ];
- $mall_goods = $goods['attributes'];
- $base_params['extra'] = !empty($goods->extra) ? ArrayHelper::toArray($goods->extra) : [];
- if (isset($base_params['extra']['goods_id'])) unset($base_params['extra']['goods_id']);
- if (empty($mall_goods['services'])) {
- $mall_goods['services'] = [];
- } else {
- $mall_goods['services'] = explode(',', $mall_goods['services']);
- }
- if (empty($mall_goods['labels'])) {
- $mall_goods['labels'] = [];
- } else {
- $mall_goods['labels'] = explode(',', $mall_goods['labels']);
- }
- if (empty($mall_goods['freight_type'])) {
- $mall_goods['freight_type'] = [];
- } else {
- $mall_goods['freight_type'] = explode(',', $mall_goods['freight_type']);
- }
- !empty($mall_goods['attr_groups']) && $mall_goods['attr_groups'] = json_decode($mall_goods['attr_groups'], true);
- !empty($mall_goods['bannar_pic']) && $mall_goods['bannar_pic'] = json_decode($mall_goods['bannar_pic'], true);
- !empty($mall_goods['area_limit']) && $mall_goods['area_limit'] = json_decode($mall_goods['area_limit'], true);
- // 预约商品
- if ($mall_goods['goods_type'] == GoodsTypeEnum::GoodsTypeReserved) {
- $reserve = ArrayHelper::toArray($goods->reserve);
- unset($reserve['id'], $reserve['goods_id']);
- $skus = GoodsReserveSku::lists([
- 'where' => [['goods_id' => $goods['id']]],
- 'with' => ['times' => function ($query) {
- $query->with(['timeSpec']);
- }]
- ]);
- foreach ($skus as &$sku) {
- unset($sku['id'], $sku['goods_id']);
- foreach ($sku['times'] as &$time) {
- unset($time['id'], $time['reserve_sku_id']);
- if (!empty($time['timeSpec'])) {
- foreach ($time['timeSpec'] as &$time_spec) {
- unset($time_spec['id'], $time_spec['reserve_time_id'], $time_spec['reserve_sku_id']);
- }
- $time['time_spec'] = $time['timeSpec'];
- unset($time_spec, $time['timeSpec']);
- }
- }
- unset($time);
- }
- unset($sku);
- $reserve['skus'] = $skus;
- $mall_goods['reserve'] = $reserve;
- } else {
- if ($mall_goods['is_attr'] == 1) {
- $temp_attrs = ArrayHelper::toArray($goods->attr);
- $attr_groups = $mall_goods['attr_groups'];
- $attr = [];
- foreach ($temp_attrs as $val) {
- $val['mall_attr_id'] = $val['id'];
- unset($val['id']);
- unset($val['goods_id']);
- $sku_group_name = explode(',', $val['name']);
- $attr_list = [];
- foreach ($sku_group_name as $name) {
- $name_arr = explode(':', $name);
- foreach ($attr_groups as $group) {
- if ($group['attr_group_name'] == $name_arr[0]) {
- foreach ($group['attr_list'] as $v) {
- if ($v['attr_name'] == $name_arr[1]) {
- $attr_list[] = [
- 'attr_id' => $v['attr_id'],
- 'attr_name' => $v['attr_name'],
- 'pic_url' => '',
- 'attr_group_id' => $group['attr_group_id'],
- 'attr_group_name' => $group['attr_group_name'],
- ];
- break;
- }
- }
- break;
- }
- }
- }
- $val['attr_list'] = $attr_list;
- $attr[] = $val;
- }
- $base_params['attr'] = $attr;
- }
- }
- unset($mall_goods['id']);
- $res = StoreGoods::setData(array_merge($mall_goods, $base_params));
- if ($res === false) throw new \Exception(StoreGoods::getStaticError());
- // 更新导入表
- $data = [
- 'mall_id' => $mall_id,
- 'store_id' => $store_id,
- 'goods_id' => $goods['id'],
- 'status' => StatusEnum::ENABLED,
- 'store_goods_id' => $res->id,
- ];
- $res = WuzhouChainStorage::setData($data);
- if ($res === false) throw new \Exception(WuzhouChainStorage::$static_error);
- } catch (\Exception $e) {
- \Yii::error(self::class . '->' . __FUNCTION__ . '//errMsg2:' . $e->getMessage() . '//params:' . json_encode(array_merge($mall_goods, $base_params)));
- }
- }
- } catch (\Exception $e) {
- \Yii::error(self::class . '->' . __FUNCTION__ . '//errMsg1:' . $e->getMessage());
- return false;
- }
- return true;
- }
- public static function getCate($mall_id, $goods_id, $store_id)
- {
- $cats = \common\models\goods\Goods::find()->where(['id' => $goods_id])->with(['cats'])->asArray()->one();
- $cates = [];
- foreach ($cats['cats'] as $cate) {
- if (!$cate['parent_id']) {
- // 一级分类
- $cates[] = self::checkAndAddCate($mall_id, $store_id, $cate['name']);
- } else {
- // 二级分类 || 三级分类
- $temp = GoodsCate::find()->where(['id' => $cate['parent_id']])->with(['parent'])->asArray()->one();
- if (!$temp) {
- throw new \Exception('分类不存在');
- }
- if (!$temp['parent']) {
- // 二级分类
- // 添加、获取一级分类
- $parent_id = self::checkAndAddCate($mall_id, $store_id, $temp['name']);
- $cates[] = self::checkAndAddCate($mall_id, $store_id, $cate['name'], $parent_id);
- } else {
- // 三级分类
- // 添加、获取一级分类
- $parent_id = self::checkAndAddCate($mall_id, $store_id, $temp['parent']['name']);
- // 添加、获取二级分类
- $grand_parent_id = self::checkAndAddCate($mall_id, $store_id, $temp['name'], $parent_id);
- $cates[] = self::checkAndAddCate($mall_id, $store_id, $cate['name'], $grand_parent_id);
- }
- }
- }
- return $cates;
- }
- public static function formatCate($mall_id, $store_id, $cate_name, $parent_id = 0)
- {
- return [
- 'mall_id' => $mall_id,
- 'store_id' => $store_id,
- 'parent_id' => $parent_id,
- 'name' => $cate_name,
- 'pic' => '',
- 'big_pic' => '',
- 'advert_pic' => '',
- 'advert_url' => '',
- 'advert_open_type' => '',
- 'advert_params' => '',
- 'is_show' => 1,
- ];
- }
- // 检测和添加分类
- public static function checkAndAddCate($mall_id, $store_id, $cate_name, $parent_id = 0)
- {
- $temp = StoreGoodsCate::findOne(['mall_id' => $mall_id, 'name' => $cate_name, 'store_id' => $store_id]);
- if (!$temp) {
- $add_cate = self::formatCate($mall_id, $store_id, $cate_name, $parent_id);
- $res = StoreGoodsCate::setData($add_cate);
- if ($res === false) throw new \Exception(StoreGoodsCate::getStaticError());
- return $res->id;
- }
- return $temp->id;
- }
- // 更新商品
- public static function saveGoods($params)
- {
- try {
- $goods_id = $params['goods_id'];
- $goods = \common\models\goods\Goods::findOne(['id' => $goods_id]);
- if ($goods) {
- $mall_id = $goods->mall_id;
- self::checkWuzhouChain($mall_id);
- // 获取所有门店的这个商品
- $store_goods_list = StoreGoods::find()->where(['mall_id' => $mall_id, 'mall_goods_id' => $goods->id, 'status' => StatusEnum::ENABLED, 'supplier_type' => StoreEnum::SELF_MALL_WUZHOU])->asArray()->all();
- if ($store_goods_list) {
- foreach ($store_goods_list as $store_goods) {
- $store_id = $store_goods['store_id'];
- $cats = self::getCate($mall_id, $goods_id, $store_goods['store_id']);
- try {
- $store_goods = StoreGoods::findOne(['mall_id' => $mall_id, 'store_id' => $store_id, 'mall_goods_id' => $goods_id, 'status' => StatusEnum::ENABLED, 'supplier_type' => StoreEnum::SELF_MALL_WUZHOU]);
- $base_params = [
- 'mall_id' => $mall_id,
- 'store_id' => $store_id,
- 'supplier_id' => $store_id,
- 'supplier_type' => StoreEnum::SELF_MALL_WUZHOU,
- 'cats' => $cats,
- 'mall_goods_id' => $goods_id,
- 'id' => $store_goods['id'] ?? 0,
- 'coupon_selection' => [],
- 'check_status' => StatusEnum::ENABLED
- ];
- $mall_goods = $goods['attributes'];
- $base_params['extra'] = !empty($goods->extra) ? ArrayHelper::toArray($goods->extra) : [];
- if (isset($base_params['extra']['goods_id'])) unset($base_params['extra']['goods_id']);
- if (empty($mall_goods['services'])) {
- $mall_goods['services'] = [];
- } else {
- $mall_goods['services'] = explode(',', $mall_goods['services']);
- }
- if (empty($mall_goods['labels'])) {
- $mall_goods['labels'] = [];
- } else {
- $mall_goods['labels'] = explode(',', $mall_goods['labels']);
- }
- if (empty($mall_goods['freight_type'])) {
- $mall_goods['freight_type'] = [];
- } else {
- $mall_goods['freight_type'] = explode(',', $mall_goods['freight_type']);
- }
- !empty($mall_goods['attr_groups']) && $mall_goods['attr_groups'] = json_decode($mall_goods['attr_groups'], true);
- !empty($mall_goods['bannar_pic']) && $mall_goods['bannar_pic'] = json_decode($mall_goods['bannar_pic'], true);
- !empty($mall_goods['area_limit']) && $mall_goods['area_limit'] = json_decode($mall_goods['area_limit'], true);
- // 预约商品
- if ($mall_goods['goods_type'] == GoodsTypeEnum::GoodsTypeReserved) {
- $reserve = ArrayHelper::toArray($goods->reserve);
- unset($reserve['id'], $reserve['goods_id']);
- $skus = GoodsReserveSku::lists([
- 'where' => [['goods_id' => $goods['id']]],
- 'with' => ['times' => function ($query) {
- $query->with(['timeSpec']);
- }]
- ]);
- foreach ($skus as &$sku) {
- unset($sku['id'], $sku['goods_id']);
- foreach ($sku['times'] as &$time) {
- unset($time['id'], $time['reserve_sku_id']);
- if (!empty($time['timeSpec'])) {
- foreach ($time['timeSpec'] as &$time_spec) {
- unset($time_spec['id'], $time_spec['reserve_time_id'], $time_spec['reserve_sku_id']);
- }
- $time['time_spec'] = $time['timeSpec'];
- unset($time_spec, $time['timeSpec']);
- }
- }
- unset($time);
- }
- unset($sku);
- $reserve['skus'] = $skus;
- $mall_goods['reserve'] = $reserve;
- } else {
- if ($mall_goods['is_attr'] == 1) {
- $temp_attrs = ArrayHelper::toArray($goods->attr);
- $attr_groups = $mall_goods['attr_groups'];
- $attr = [];
- foreach ($temp_attrs as $val) {
- $val['mall_attr_id'] = $val['id'];
- unset($val['id']);
- unset($val['goods_id']);
- $sku_group_name = explode(',', $val['name']);
- $attr_list = [];
- foreach ($sku_group_name as $name) {
- $name_arr = explode(':', $name);
- foreach ($attr_groups as $group) {
- if ($group['attr_group_name'] == $name_arr[0]) {
- foreach ($group['attr_list'] as $v) {
- if ($v['attr_name'] == $name_arr[1]) {
- $attr_list[] = [
- 'attr_id' => $v['attr_id'],
- 'attr_name' => $v['attr_name'],
- 'pic_url' => '',
- 'attr_group_id' => $group['attr_group_id'],
- 'attr_group_name' => $group['attr_group_name'],
- ];
- break;
- }
- }
- break;
- }
- }
- }
- $val['attr_list'] = $attr_list;
- $attr[] = $val;
- }
- $base_params['attr'] = $attr;
- }
- }
- unset($mall_goods['id']);
- $res = StoreGoods::setData(array_merge($mall_goods, $base_params));
- if ($res === false) throw new \Exception(StoreGoods::getStaticError());
- } catch (\Exception $e) {
- \Yii::error(self::class . '->' . __FUNCTION__ . '//errMsg2:' . $e->getMessage() . '//params:' . json_encode(array_merge($mall_goods, $base_params)));
- }
- }
- }
- }
- } catch (\Exception $e) {
- \Yii::error(self::class . '->' . __FUNCTION__ . '//errMsg1:' . $e->getMessage());
- return false;
- }
- return true;
- }
- public static function checkWuzhouChain($mall_id)
- {
- if (!MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_WUZHOU_CHAIN)) {
- throw new \Exception('请先安装五洲供应链插件');
- }
- $serv = new SettingService();
- $data = $serv->getSetting($mall_id);
- if (!$data || empty($data['is_enable'])) {
- throw new \Exception('请先在五洲供应链插件中开启供应链功能');
- }
- }
- // 删除商品
- public static function deleteGoods($goods_id, $mall_id)
- {
- try {
- if (empty($goods_id) || empty($mall_id)) return false;
- self::checkWuzhouChain($mall_id);
- $storage = WuzhouChainStorage::findOne(['store_goods_id' => $goods_id, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
-
- if ($storage) {
- $storage->status = StatusEnum::DELETE;
- if ($storage->save() === false) {
- throw new \Exception($storage->getError());
- }
- }
- } catch (\Exception $e) {
- return false;
- }
- }
- }
|