Quellcode durchsuchen

导入平台产品

shish vor 2 Jahren
Ursprung
Commit
90db92520b

+ 15 - 0
app-ghs/controllers/CpController.php

@@ -2,8 +2,10 @@
 
 namespace ghs\controllers;
 
+use biz\cp\classes\PtCpClass;
 use bizGhs\cp\classes\CpClass;
 use bizGhs\cp\classes\CpClassClass;
+use bizGhs\cp\services\CpService;
 use common\components\imgUtil;
 use common\components\stringUtil;
 use common\components\util;
@@ -12,6 +14,19 @@ use Yii;
 class CpController extends BaseController
 {
 
+    //采用平台花材 ssh 20240226
+    public function actionUsePtCp()
+    {
+        $get = Yii::$app->request->get();
+        $ptCpId = $get['id'] ?? 0;
+        $ptCp = PtCpClass::getById($ptCpId, true);
+        if (empty($ptCp)) {
+            util::fail('没有找到产品');
+        }
+        CpService::addPtCp($ptCp, $this->shop);
+        util::success(['info' => []]);
+    }
+
     //根据分类id取集合,带分页 ssh 20231228
     public function actionList()
     {

+ 124 - 26
biz-ghs/cp/classes/CpClass.php

@@ -2,10 +2,15 @@
 
 namespace bizGhs\cp\classes;
 
+use biz\cp\classes\PtCpItemClass;
+use biz\item\classes\PtItemClass;
 use bizGhs\base\classes\BaseClass;
+use bizGhs\item\classes\ItemClassClass;
 use bizGhs\product\classes\ProductClass;
 use bizGhs\shop\classes\ShopClass;
 use common\components\imgUtil;
+use common\components\noticeUtil;
+use common\components\stringUtil;
 use common\components\util;
 use Yii;
 
@@ -14,6 +19,97 @@ class CpClass extends BaseClass
 
     public static $baseFile = '\bizGhs\cp\models\Cp';
 
+    //从平台导入产品 ssh 20240226
+    public static function importPtCp($ptCp, $currentShop)
+    {
+        $ptCpId = $ptCp->id ?? 0;
+        $mainId = $currentShop->mainId ?? 0;
+        $shopCp = CpClass::getByCondition(['ptCpId' => $ptCpId], true);
+        if (empty($shopCp)) {
+            $ptCpClassId = $ptCp->classId ?? 0;
+            $ptCpClass = PtCpClassClass::getById($ptCpClassId, true);
+            if (empty($ptCpClass)) {
+                util::fail('平台产品的分类没有找到');
+            }
+            $ptCpClassName = $ptCpClass->name ?? '';
+            $cpClassList = CpClassClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0], null, "*", null, true);
+            $defaultClassId = 0;
+            $currentClassId = 0;
+            if (!empty($cpClassList)) {
+                foreach ($cpClassList as $cpClass) {
+                    if ($cpClass->isDefault == 1) {
+                        $defaultClassId = $cpClass->id ?? 0;
+                    }
+                    if ($ptCpClassName == $cpClass->name) {
+                        $currentClassId = $cpClass->id ?? 0;
+                    }
+                }
+            }
+            if (empty($defaultClassId) && empty($currentClassId)) {
+                util::fail('没有找到默认分类');
+            }
+            $currentClassId = !empty($currentClassId) ? $currentClassId : $defaultClassId;
+            $arr = $ptCp->attributes;
+            unset($arr['id']);
+            unset($arr['addTime']);
+            unset($arr['updateTime']);
+            unset($arr['classId']);
+            $arr['ptCpId'] = $ptCpId;
+            $arr['classId'] = $currentClassId;
+            $shopCp = self::addCp($arr, $currentShop);
+        }
+        if ($ptCp->auth == 0) {
+            return true;
+        }
+        $cpId = $shopCp->id ?? 0;
+        $ptCpItemList = PtCpItemClass::getAllByCondition(['cpId' => $ptCpId], null, '*', null, true);
+        foreach ($ptCpItemList as $ptCpItem) {
+            if ($ptCpItem->auth == 1) {
+                $ptItemId = $ptCpItem->itemId ?? 0;
+                $productInfo = ProductClass::getByCondition(['mainId' => $mainId, 'itemId' => $ptItemId], true);
+                if (empty($productInfo)) {
+                    $ptItemInfo = PtItemClass::getById($ptItemId);
+                    if (empty($ptItemInfo)) {
+                        util::fail('平台花材没有找到哦');
+                    }
+                    $customDefaultClass = ItemClassClass::getByCondition(['mainId' => $mainId, 'isDefault' => 1], true);
+                    if (empty($customDefaultClass)) {
+                        noticeUtil::push("mainId:{$mainId} 导入平台产品和花材,没有找到花材默认分类", '15280215347');
+                        util::fail('没有找到默认分类哈!');
+                    }
+                    $customDefaultClassId = $customDefaultClass->id ?? 0;
+                    unset($ptItemInfo['id']);
+                    $ptItemInfo['itemId'] = $ptItemId;
+                    $ptItemInfo['hasCpId'] = 1;
+                    $ptItemInfo['skPrice'] = 36;
+                    $ptItemInfo['price'] = 36;
+                    $ptItemInfo['hjPrice'] = 36;
+                    $ptItemInfo['mainId'] = $mainId;
+                    $ptItemInfo['py'] = stringUtil::py($ptItemInfo['name']);
+                    $ptItemInfo['classId'] = $customDefaultClassId;
+                    //有二个添加花材的地方,请搜索关键词add_item_place
+                    $productInfo = ProductClass::add($ptItemInfo, true);
+                }
+                $shopItemId = $productInfo->id ?? 0;
+                $currentData = [
+                    'cpId' => $cpId,
+                    'ptCpId' => $ptCpId,
+                    'productId' => $shopItemId,
+                    'ptItemId' => $ptItemId,
+                    'name' => $productInfo->name ?? '',
+                    'py' => $productInfo->py ?? '',
+                    'cpName' => $shopCp->name ?? '',
+                    'cpPy' => $shopCp->py ?? '',
+                    'mainId' => $mainId,
+                    'inTurn' => $ptCpItem->inTurn ?? 100,
+                    'auth' => $ptCpItem->auth ?? 0,
+                ];
+                CpItemClass::add($currentData,true);
+            }
+        }
+
+    }
+
     //恢复产品 ssh 20240222
     public static function recoverCp($cp, $masterShop)
     {
@@ -205,35 +301,36 @@ class CpClass extends BaseClass
         $connection = Yii::$app->db;
         $transaction = $connection->beginTransaction();
         try {
-
-            $ptData = $data;
-            $ptCpClassId = 0;
-            $ptCpClassDefaultId = 0;
-            $ptCpClassList = PtCpClassClass::getAllByCondition(['delStatus' => 0], null, '*');
-            if (!empty($ptCpClassList)) {
-                foreach ($ptCpClassList as $ptCpClass) {
-                    $currentId = $ptCpClass['id'] ?? 0;
-                    if (isset($ptCpClass['name']) && $ptCpClass['name'] == $shopCpClassName) {
-                        $ptCpClassId = $currentId;
-                    }
-                    if (isset($ptCpClass['isDefault']) && $ptCpClass['isDefault'] == 1) {
-                        $ptCpClassDefaultId = $currentId;
+            if (isset($data['ptCpId']) == false || empty($data['ptCpId'])) {
+                $ptData = $data;
+                $ptCpClassId = 0;
+                $ptCpClassDefaultId = 0;
+                $ptCpClassList = PtCpClassClass::getAllByCondition(['delStatus' => 0], null, '*');
+                if (!empty($ptCpClassList)) {
+                    foreach ($ptCpClassList as $ptCpClass) {
+                        $currentId = $ptCpClass['id'] ?? 0;
+                        if (isset($ptCpClass['name']) && $ptCpClass['name'] == $shopCpClassName) {
+                            $ptCpClassId = $currentId;
+                        }
+                        if (isset($ptCpClass['isDefault']) && $ptCpClass['isDefault'] == 1) {
+                            $ptCpClassDefaultId = $currentId;
+                        }
                     }
                 }
+                if (!empty($ptCpClassDefaultId)) {
+                    $ptData['classId'] = $ptCpClassDefaultId;
+                }
+                if (!empty($ptCpClassId)) {
+                    $ptData['classId'] = $ptCpClassId;
+                }
+                if (empty($ptCpClassDefaultId) && empty($ptCpClassId)) {
+                    util::fail('没有找到平台的分类');
+                }
+                $respond = PtCpClass::addCp($ptData);
+                $ptCpId = $respond->id ?? 0;
+                $data['ptCpId'] = $ptCpId;
             }
-            if (!empty($ptCpClassDefaultId)) {
-                $ptData['classId'] = $ptCpClassDefaultId;
-            }
-            if (!empty($ptCpClassId)) {
-                $ptData['classId'] = $ptCpClassId;
-            }
-            if (empty($ptCpClassDefaultId) && empty($ptCpClassId)) {
-                util::fail('没有找到平台的分类');
-            }
-            $respond = PtCpClass::addCp($ptData);
-
-            $data['ptCpId'] = $respond->id ?? 0;
-            self::add($data);
+            $result = self::add($data, true);
 
             //在首店添加,则找到父级sj下的所有直营和加盟店,都进行同步
             if (isset($currentShop->default) && $currentShop->default == 1) {
@@ -267,6 +364,7 @@ class CpClass extends BaseClass
                 }
             }
             $transaction->commit();
+            return $result;
         } catch (\Exception $exception) {
             $transaction->rollBack();
             Yii::info('添加失败原因:' . $exception->getMessage());

+ 20 - 0
biz-ghs/cp/services/CpService.php

@@ -1,11 +1,31 @@
 <?php
+
 namespace bizGhs\cp\services;
 
 use bizGhs\base\services\BaseService;
+use bizGhs\cp\classes\CpClass;
+use bizGhs\shop\classes\ShopClass;
 
 class CpService extends BaseService
 {
 
     public static $baseFile = '\bizGhs\cp\classes\CpClass';
 
+    //从平台导入产品 ssh 20240227
+    public static function addPtCp($ptCp, $currentShop)
+    {
+        CpClass::importPtCp($ptCp, $currentShop);
+        if (isset($currentShop->default) && $currentShop->default == 1) {
+            $sjId = $currentShop->sjId ?? 0;
+            $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
+            foreach ($shopList as $shop) {
+                $shopId = $shop->id ?? 0;
+                if ($shopId == $currentShop->id) {
+                    continue;
+                }
+                CpClass::importPtCp($ptCp, $shop);
+            }
+        }
+    }
+
 }

+ 1 - 0
biz/cp/services/PtCpService.php

@@ -98,6 +98,7 @@ class PtCpService extends BaseService
                                 $addItem['mainId'] = $mainId;
                                 $addItem['py'] = stringUtil::py($addItem['name']);
                                 $addItem['classId'] = $customDefaultClassId;
+                                //有二个添加花材的地方,请搜索关键词add_item_place
                                 $currentProduct = ProductClass::add($addItem, true);
                             }
                             $productId = $currentProduct->id ?? 0;