Browse Source

拉取客户

shish 8 months ago
parent
commit
cae60a3dbd

+ 14 - 11
app-ghs/controllers/CustomController.php

@@ -676,12 +676,7 @@ class CustomController extends BaseController
         foreach ($arr as $item) {
             $name = $item['name'] ?? '';
             $mobile = $item['mobile'] ?? '';
-
-            //队列方式去处理
-//            $customerCachedKey = 'batch_create_customer_list';
-//            $value = $name . '_' . $mobile . '_' . $this->sjId . '_' . $this->shop->id . '_' . $this->sj->name . '_' . $this->shopAdminId;
-//            Yii::$app->redis->executeCommand('LPUSH', [$customerCachedKey, $value]);
-
+            //使用消息队列处理
             try {
                 $producer = Yii::$app->rabbitmq->getProducer('customProducer');
                 $msg = serialize(['type' => 'add_custom', 'name' => $name, 'mobile' => $mobile, 'sjId' => $this->sjId,
@@ -692,7 +687,6 @@ class CustomController extends BaseController
                 $remind = "供货商创建新客户,生产消息报错 {$name} {$mobile} {$this->sj->name} {$msg}";
                 noticeUtil::push($remind, '15280215347');
             }
-
         }
         util::complete('已提交,15秒后生效');
     }
@@ -724,10 +718,19 @@ class CustomController extends BaseController
         if ($toSjId != $fromSjId) {
             util::fail('不是您的门店');
         }
-        $cachedKey = 'copy_other_shop_customers';
-        Yii::$app->redis->executeCommand('LPUSH', [$cachedKey, $toShopId . '_' . $fromShopId]);
-        Yii::info("从其他门店同步客户:目标门店id =  " . $toShopId . ' 来源门店id = ' . $fromShopId);
-        util::complete('已提交,1分钟后生效');
+
+        //使用消息队列处理 ssh 20251204
+        try {
+            $producer = Yii::$app->rabbitmq->getProducer('customProducer');
+            $msg = serialize(['type' => 'pull_custom_from_other_shop', 'toShopId' => $toShopId, 'fromShopId' => $fromShopId]);
+            $producer->publish($msg, 'customExchange', 'customRoute');
+        } catch (\Exception $e) {
+            $msg = $e->getMessage();
+            $remind = "供货商拉取其他门店客户,生产消息报错 {$toShopId} {$fromShopId} {$msg}";
+            noticeUtil::push($remind, '15280215347');
+        }
+
+        util::complete('已拉取');
     }
 
     //客户列表 ssh 2021.7.8

+ 18 - 0
biz-ghs/custom/classes/CustomClass.php

@@ -71,6 +71,24 @@ class CustomClass extends BaseClass
         self::LEVEL_ZS => 'zsAddPrice',
     ];
 
+    public static function pullOtherShopCustom($data)
+    {
+        $toShopId = $data['toShopId'] ?? 0;
+        $fromShopId = $data['fromShopId'] ?? 0;
+        //指定当前环境为批发商,重要!!!
+        Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
+        $fromCustomList = CustomClass::getAllByCondition(['ownShopId' => $fromShopId], null, '*');
+        if (!empty($fromCustomList)) {
+            foreach ($fromCustomList as $fromCustomer) {
+                $exist = CustomClass::exists(['ownShopId' => $toShopId, 'shopId' => $fromCustomer['shopId']]);
+                if (!$exist) {
+                    CustomClass::build($toShopId, $fromCustomer['shopId'], $fromCustomer['name']);
+                }
+            }
+        }
+        return true;
+    }
+
     public static function generateCustom($data)
     {
 

+ 3 - 0
common/components/rabbitmq/customConsumer.php

@@ -41,6 +41,9 @@ class customConsumer implements ConsumerInterface
                     //添加新客户
                     $result = CustomClass::generateCustom($data);
                     break;
+                case 'pull_custom_from_other_shop':
+                    $result = CustomClass::pullOtherShopCustom($data);
+                    break;
                 default:
                     noticeUtil::push("客户的消费者报错,不存在的类型: {$type}");
                     $result = false;

+ 2 - 34
console/controllers/CustomController.php

@@ -113,45 +113,13 @@ class CustomController extends Controller
     //批量创建客户
     private function createCustomer()
     {
-
+        //重新写这个方法,用rabbitMQ来实现
     }
 
     //批发端分店从首店同步客户
     private function copyCustomers()
     {
-        $cachedKey = 'copy_other_shop_customers';
-        $shopIdList = [];
-        while ($count = Yii::$app->redis->executeCommand('LLEN', [$cachedKey])) {
-            $shopIdData = Yii::$app->redis->executeCommand('RPOP', [$cachedKey]);
-            $shopIdList[] = $shopIdData;
-        }
-
-        if (empty($shopIdList)) {
-            return;
-        }
-
-        foreach ($shopIdList as $shopIdData) {
-            $shopIdArr = explode('_', $shopIdData);
-            if (count($shopIdArr) != 2) {
-                Yii::info('copy customer 数据出错:' . $shopIdData);
-                noticeUtil::push("从分店同步客户出错了:" . $shopIdData, '15280215347');
-                continue;
-            }
-
-            //指定当前环境为批发商,重要
-            Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
-
-            $toShopId = $shopIdArr[0];
-            $fromShopId = $shopIdArr[1];
-
-            $fromCustomList = CustomClass::getAllByCondition(['ownShopId' => $fromShopId], null, '*');
-            foreach ($fromCustomList as $fromCustomer) {
-                $exist = CustomClass::exists(['ownShopId' => $toShopId, 'shopId' => $fromCustomer['shopId']]);
-                if (!$exist) {
-                    CustomClass::build($toShopId, $fromCustomer['shopId'], $fromCustomer['name']);
-                }
-            }
-        }
+        //重新写这个方法,用rabbitMQ来实现
     }
 
     //从文件批量创建客户 ssh 20240426