Browse Source

库存预警

shish 4 năm trước cách đây
mục cha
commit
185f1baa2f

+ 67 - 1
biz-ghs/product/classes/ProductClass.php

@@ -7,6 +7,7 @@ use biz\item\classes\PtItemClass;
 use biz\item\classes\PtItemClassClass;
 use biz\item\classes\UnitClass;
 use biz\shop\services\ShopService;
+use biz\wx\classes\WxMessageClass;
 use bizGhs\base\classes\BaseClass;
 use bizGhs\item\classes\ItemClass;
 use bizGhs\item\classes\ItemClassClass;
@@ -15,6 +16,7 @@ use bizGhs\order\classes\CheckOrderClass;
 use bizGhs\order\classes\CheckOrderItemClass;
 use bizGhs\order\traits\OrderTrait;
 use bizGhs\stock\services\StockRecordService;
+use common\components\dict;
 use common\components\imgUtil;
 use common\components\orderSn;
 use common\components\stringUtil;
@@ -1025,7 +1027,7 @@ class ProductClass extends BaseClass
 
             //小菊的结构
             $ptItemId = $data['itemId'] ?? 0;
-            $ptItemInfo = PtItemClass::getById($ptItemId,true);
+            $ptItemInfo = PtItemClass::getById($ptItemId, true);
             $data['variety'] = $ptItemInfo->variety ?? 0;
 
             //根据sjId, 找到所有的shopId,都添加
@@ -1135,4 +1137,68 @@ class ProductClass extends BaseClass
         $data = self::getGhsItemAll($sjId, 'classId,itemId');
         return $data;
     }
+
+    //库存预警通知 shish 20210909
+    public static function stockWarningAddQueue($product, $stock)
+    {
+        $warningNum = $product['stockWarning'] ?? 0;
+        if ($warningNum <= 0) {
+            return false;
+        }
+        $date = date("Y-m-d");
+        $productId = $product['id'] ?? 0;
+        $key = 'stockWarning_' . $productId . '_' . $date;
+
+        //当天已经放入队列通知的不再通知
+        $hasWarning = Yii::$app->redis->executeCommand('GET', [$key]);
+        if (isset($hasWarning) && !empty($hasWarning)) {
+            return false;
+        }
+
+        if ($stock <= $warningNum) {
+            Yii::$app->redis->executeCommand('SETEX', [$key, 86400, 'hasWarning']);
+
+            $shopId = $product['shopId'] ?? 0;
+            if (empty($shopId)) {
+                return false;
+            }
+
+            //队列方式去处理
+            $stockWarningListKey = 'stockWarningList_' . $shopId;
+            Yii::$app->redis->executeCommand('LPUSH', [$stockWarningListKey, $productId]);
+        }
+
+    }
+
+    //所有门店进行库存通知 shish 20210909
+    public static function allShopStockWarningFromQueue()
+    {
+        $ghs = dict::getDict('ptStyle', 'ghs');
+        $shopList = ShopClass::getAllByCondition(['ptStyle' => $ghs], null, '*', null, true);
+        if (!empty($shopList)) {
+            foreach ($shopList as $key => $shop) {
+                $ids = [];
+                $shopId = $shop->id ?? 0;
+                $stockWarningListKey = 'stockWarningList_' . $shopId;
+                echo $stockWarningListKey."\n";
+                while ($count = Yii::$app->redis->executeCommand('LLEN', [$stockWarningListKey])) {
+                    $productId = Yii::$app->redis->executeCommand('RPOP', [$stockWarningListKey]);
+                    if (is_numeric($productId)) {
+                        $ids[] = $productId;
+                    }
+                }
+                if (empty($ids)) {
+                    continue;
+                }
+                $ids = array_unique(array_filter($ids));
+                $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
+                if (!empty($productList)) {
+                    foreach ($productList as $product) {
+                        WxMessageClass::stockWarningInform($shop, $product);
+                    }
+                }
+            }
+        }
+    }
+
 }

+ 11 - 16
biz-ghs/stock/classes/StockRecordClass.php

@@ -66,7 +66,16 @@ class StockRecordClass extends BaseClass
 
     public static function addRecord($data)
     {
-        return self::add($data);
+        $respond = self::add($data);
+
+        //库存预警通知放入队列
+        $productId = $data['productId'] ?? 0;
+        $currentProduct = ProductClass::getById($productId);
+        if (!empty($currentProduct)) {
+            ProductClass::stockWarningAddQueue($currentProduct, $data['newStock']);
+        }
+
+        return $respond;
     }
 
     //根据order 记录流水
@@ -91,22 +100,8 @@ class StockRecordClass extends BaseClass
             $record['recordType'] = $type;
             $record['productId'] = $productId;
             $record['newStock'] = $v['newStock'];// 最新库存
-//            switch ($type){
-//                case self::STOCK_RECORD_TYPE_OUT_STOCK:
-//                    //出库
-//                case self::STOCK_RECORD_TYPE_OUT_STOCK_CANCEL:
-//                    //取消入库
-//                    $record['newStock'] = $productData[$productId]['stock'];
-//                    break;
-//                case self::STOCK_RECORD_TYPE_IN_STOCK:
-//                    //入库
-//                case self::STOCK_RECORD_TYPE_IN_STOCK_CANCEL:
-//                    //取消出库
-//                    $record['newStock'] = $productData[$productId]['stock'];
-//                    break;
-//            }
             //记录流水
-            StockRecordService::add($record);
+            self::addRecord($record);
         }
     }
 

+ 3 - 5
biz-ghs/stock/services/StockRecordService.php

@@ -7,13 +7,10 @@ use bizGhs\stock\classes\StockRecordClass;
 
 class StockRecordService extends BaseService
 {
+
     public static $baseFile = '\bizGhs\stock\classes\StockRecordClass';
-    //采购订单流水记录
 
-    /**
-     * @param $data  sjId,shopId,orderSn,itemId,itemNum,oldStock(原库存),newStock(订单完成后新库存)
-     * @return mixed
-     */
+    //采购订单流水记录
     public static function addPurchaseOrderRecord($data)
     {
         $data['recordType'] = StockRecordClass::STOCK_RECORD_TYPE_PURCHASE;
@@ -67,4 +64,5 @@ class StockRecordService extends BaseService
         $data['recordType'] = StockRecordClass::STOCK_RECORD_TYPE_SELL_STOCK_CANCEL;
         return StockRecordClass::addRecord($data);
     }
+
 }

+ 45 - 1
biz/wx/classes/WxMessageClass.php

@@ -47,6 +47,8 @@ class WxMessageClass extends BaseClass
                 'OPENTM411207151' => '提现成功通知',
                 'OPENTM416881153' => '发货通知',
                 'OPENTM407280253' => '退款成功通知',
+                'OPENTM206773265' => '库存预警通知',
+                'OPENTM203447669' => '新客户加入通知',
             ],
             //商城
             dict::getDict('ptStyle', 'mall') => [
@@ -125,7 +127,7 @@ class WxMessageClass extends BaseClass
         $first = "您有新的订单,请注意查收";
         $customName = $order->customName ?? '';
         if (!empty($customName)) {
-            $first = '下单客户:'.$customName;
+            $first = '下单客户:' . $customName;
         }
 
         $adminList = ShopAdminClass::getRemainAdmin($shopId);
@@ -529,4 +531,46 @@ class WxMessageClass extends BaseClass
         }
     }
 
+    //库存预警通知 shish 20210909
+    public static function stockWarningInform($shop, $product)
+    {
+        $stock = $product->stock ?? 0;
+        $shopId = $shop->id;
+        $ptStyle = $shop->ptStyle;
+        $wxBase = WxOpenClass::getWxBase();
+        $wx = $wxBase[$ptStyle] ?? [];
+        $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
+        $shortTempId = 'OPENTM206773265';
+        $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
+        if (empty($tempId)) {
+            return false;
+        }
+        $name = $product->name ?? '';
+        $shopName = $shop->shopName ?? '';
+        $date = date("Y-m-d H:i:s");
+        $adminList = ShopAdminClass::getRemainAdmin($shopId);
+        foreach ($adminList as $admin) {
+            if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
+                continue;
+            }
+            $openId = isset($admin['openId']) ? $admin['openId'] : '';
+            $data = [
+                "touser" => $openId,
+                "template_id" => $tempId,
+                "url" => Yii::$app->params['ghsDomain'],
+                "data" => [
+                    "first" => ["value" => "【{$name}】", "color" => "#173177"],
+                    "keyword1" => ["value" => $shopName, "color" => "#173177"],
+                    "keyword2" => ["value" => $name, "color" => "#D52B4D"],
+                    "keyword3" => ["value" => floatval($stock), "color" => "#173177"],
+                    "keyword4" => ["value" => $date, "color" => "#173177"],
+                    "remark" => ["value" => '点击查看详情', "color" => "#173177"]
+                ]
+            ];
+            $data['miniprogram']['appid'] = $wx['miniAppId'];
+            $data['miniprogram']['pagepath'] = 'pagesStorehouse/stockWarn/manage';
+            wxUtil::sendTaskInform($data, $wx, $ptStyle);
+        }
+    }
+
 }

+ 25 - 0
console/controllers/GlobalController.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace console\controllers;
+
+use bizGhs\product\classes\ProductClass;
+use Yii;
+use yii\console\Controller;
+
+/**
+ * 全局脚本
+ */
+class GlobalController extends Controller
+{
+
+    // yii global/run
+    public function actionRun()
+    {
+
+        //所有门店进行库存通知
+        ProductClass::allShopStockWarningFromQueue();
+
+    }
+
+
+}