Преглед на файлове

Merge branch 'master' of http://git.huaml.com/zhh/huahuibao

林琦海 преди 5 години
родител
ревизия
1138cbb295

+ 7 - 21
app-ghs/controllers/ConsoleController.php

@@ -10,6 +10,7 @@ use bizHd\stat\services\StatIncomeService;
 use bizHd\stat\services\StatOrderService;
 use bizHd\stat\services\StatUserService;
 use bizHd\stat\services\StatVisitService;
+use bizMall\stat\classes\StatOrderClass;
 use common\components\dict;
 use common\components\httpUtil;
 use Yii;
@@ -27,35 +28,22 @@ class ConsoleController extends BaseController
         $notice = [
             //['title' => '您的试用期还有7天,点击免费续期1年 ', 'action' => '查看', 'page' => '/admin/recharge/renew'],
         ];
-        //今天访客数
-        $todayVisit = StatVisitService::getTodayVisitNum($this->sjId);
         //今天客户与粉丝数
         $userData = StatUserService::getTodayNum($this->shopId);
-        $todayFansNum = isset($userData['riseFansNum']) ? $userData['riseFansNum'] : 0;
         $todayUserNum = isset($userData['riseUserNum']) ? $userData['riseUserNum'] : 0;
-        //今天订单数
-        $orderData = StatOrderService::getTodayNum($this->shopId);
-        $todayOrder = isset($orderData['riseNum']) ? $orderData['riseNum'] : 0;
         //今天收入
         $incomeData = StatIncomeService::getTodayNum($this->shopId);
         $todayIncome = isset($incomeData['amount']) ? $incomeData['amount'] : 0;
         //2021.4.14 linqh 今日支出
         $outData = StatOutService::getTodayNum($this->shopId);
-        $todayOut = $outData['amount']??0;
-        $todayData = ['visit' => $todayVisit, 'fans' => $todayFansNum, 'user' => $todayUserNum, 'income' => $todayIncome, 'order' => $todayOrder, 'expend' => $todayOut];
-
-        $mainMenu = [
-            ['name' => '开单', 'img' => 'https://img.huaml.com/retail/admin/tab-icon-6.png', 'page' => '/pageA/merchant/getList'],
-            ['name' => '采购', 'img' => 'https://img.huaml.com/retail/admin/tab-icon-6.png', 'page' => '/pageA/merchant/getList'],
-        ];
-        $menu = [
-            ['name' => '库存预警', 'img' => 'https://img.huaml.com/retail/admin/tab-icon-6.png', 'page' => '/pageA/merchant/getList'],
-            ['name' => '改价', 'img' => 'https://img.huaml.com/retail/admin/tab-icon-6.png', 'page' => '/pageA/merchant/getList'],
-            ['name' => '全部', 'img' => 'https://img.huaml.com/retail/admin/tab-icon-6.png', 'page' => '/pageA/merchant/getList'],
-        ];
+        $todayOut = $outData['amount'] ?? 0;
+        //今天开单数
+        $statOrder = \bizHd\stat\classes\StatOrderClass::getByCondition(['shopId' => $this->shopId, 'time' => date('Ymd')]);
+        $orderNum = $statOrder['riseNum'] ?? 0;
+        $todayData = ['income' => $todayIncome, 'order' => $orderNum, 'custom' => $todayUserNum, 'expend' => $todayOut];
 
         $incomeStat = [];
-        if(httpUtil::isMiniProgram() == false){
+        if (httpUtil::isMiniProgram() == false) {
             //当月的收入
             $currentMonthIncome = StatIncomeMonthService::getCurrentMonthIncome($this->shopId);
             //上个月收入
@@ -74,8 +62,6 @@ class ConsoleController extends BaseController
             'asset' => $asset,
             'notice' => $notice,
             'todayData' => $todayData,
-            'mainMenu' => $mainMenu,
-            'menu' => $menu,
             'incomeStat' => $incomeStat,
         ]);
     }

+ 18 - 21
biz-ghs/order/classes/OrderClass.php

@@ -16,7 +16,6 @@ use bizGhs\custom\classes\CustomClass;
 use bizGhs\stat\classes\StatOrderCountClass;
 use bizGhs\stat\classes\StatYjClass;
 use bizHd\stat\classes\StatIncomeClass;
-use bizHd\stat\classes\StatIncomeMonthClass;
 use bizHd\stat\classes\StatOrderClass;
 use bizHd\stat\classes\StatOrderMonthClass;
 use common\components\imgUtil;
@@ -77,7 +76,7 @@ class OrderClass extends BaseClass
     const CLEAR_HAS_GATHERING = 2;
 
     //添加订单 ssh 2019.12.5
-    public static function addOrder($data, $shop, $customOrder = false)
+    public static function addOrder($data, $currentShop, $customOrder = false)
     {
         $sjId = $data['merchantId'] ?? 0;
         $month = date("Ym");
@@ -111,8 +110,14 @@ class OrderClass extends BaseClass
         $data['weight'] = $weight;
 
         //门店总订单数+1
-//        $shopId = $shop['id'];
-//        $shop = Shop::find()->select('*')->where("id = {$shopId} for update")->one();
+        $shopId = $currentShop['id'] ?? 0;
+        $shop = Shop::findBySql('select * from xhShop where id = :id for update', ['id' => $shopId])->one();
+        if (empty($shop)) {
+            util::fail('没有找到门店');
+        }
+        $shop->totalOrder += 1;
+        $shop->unPayOrder += 1;
+        $shop->save();
 
         $return = self::add($data);
 
@@ -516,12 +521,11 @@ class OrderClass extends BaseClass
         //增加业绩
         StatYjClass::addYj($order);
 
-        $shop['unSendOrder']++;
-        $shop['unPayOrder']--;
-        $shop['totalOrderNum']++;
-        $shop['totalIncome'] = bcadd($shop['totalIncome'], $amount, 2);
-        $shopId = $shop['id'];
-        ShopClass::updateById($shopId, ['unSendOrder' => $shop['unSendOrder'], 'unPayOrder' => $shop['unPayOrder'], 'totalIncome' => $shop['totalIncome'], 'totalOrderNum' => $shop['totalOrderNum']]);
+        $shop->unSendOrder += 1;
+        $shop->unPayOrder -= 1;
+        $shop->totalOrderNum += 1;
+        $shop->totalIncome = bcadd($shop->totalIncome, $amount, 2);
+        $shop->save();
 
         $sjId = $order['merchantId'];
         $sjAsset = MerchantAssetClass::getByMerchantId($sjId);
@@ -534,17 +538,10 @@ class OrderClass extends BaseClass
             'totalIncome' => $sjAsset['totalIncome'],
         ]);
 
-//        //每天收入和每月收入
-//        StatIncomeClass::updateOrInsert($return['merchantId'], $return['shopId'], $return['actPrice']);
-//        StatIncomeMonthClass::updateOrInsert($return['merchantId'], $return['shopId'], $return['actPrice']);
-//        //门店总收入和总订单
-//        ShopService::updateTotalIncome($return['shopId'], $return['actPrice']);
-//        ShopService::totalOrderPlus($return['shopId']);
-//        //今日订单+1
-//        StatOrderClass::updateOrInsert($return['merchantId'], $return['shopId']);
-//        //本月订单+1
-//        StatOrderMonthClass::updateOrInsert($return['merchantId'], $return['shopId']);
-
+        //每天和每月收入
+        StatIncomeClass::updateOrInsert($shop, $order['actPrice']);
+        //今日订单+1
+        StatOrderClass::updateOrInsert($shop);
 
         //增加已下单流程
         $time = date("Y-m-d H:i:s");

+ 23 - 31
biz-hd/stat/classes/StatIncomeClass.php

@@ -2,51 +2,43 @@
 
 namespace bizHd\stat\classes;
 
+use bizHd\stat\models\StatIncome;
 use Yii;
 use bizHd\base\classes\BaseClass;
 
 class StatIncomeClass extends BaseClass
 {
 
-	public static $baseFile = '\bizHd\stat\models\StatIncome';
+    public static $baseFile = '\bizHd\stat\models\StatIncome';
 
     //更新当前商家的数据 linqh 2021.4.15
     // 收入更新,若无则新增
-    public static function updateOrInsert($sjId,$shopId,$amount)
+    public static function updateOrInsert($shop, $amount)
     {
         $time = date('Ymd');
+        $shopId = $shop->id;
         $where = [
-            'merchantId'=>$sjId,
-            'shopId'=>$shopId,
-            'time'=>$time,
+            'shopId' => $shopId,
+            'time' => $time,
         ];
-        $model = self::getByCondition($where);
-        if($model){
-            $id = $model['id'];
-            //累加金额 amount
-            $newAmount = bcadd($amount,$model['amount'],2);
-            $totalAmount = bcadd($amount,$model['totalAmount'],2);
-            self::updateById($id,['amount'=>$newAmount,'totalAmount'=>$totalAmount]);
-        }else{
-            //insert
-            $data = $where;
-            $data['amount'] = $amount;
-            //totalAmount = 前一天的 totalAmount
-            $totalAmount = 0;
-            $lastDate = date('Ymd',strtotime("-1 day"));
-            $lastWhere = [
-                'merchantId'=>$sjId,
-                'shopId'=>$shopId,
-                'time'=>$lastDate,
+        $model = self::getByCondition($where, true);
+        if (empty($model)) {
+            $data = [
+                'time' => $time,
+                'amount' => 0,
+                'totalAmount' => $shop->totalIncome,
+                'createTime' => date('Y-m-d H:i:s'),
             ];
-            $lastModel = self::getByCondition($lastWhere);
-            if($lastModel){
-                $totalAmount = $lastModel['totalAmount'];
-            }
-            $data['totalAmount'] = bcadd($amount,$totalAmount,2);
-            $data['createTime'] = date('Y-m-d H:i:s');
-            self::add($data);
+            $model = self::add($data, true);
         }
+        $id = $model->id;
+        //查id行级锁
+        $stat = StatIncome::findBySql('select * from ' . StatIncome::tableName() . ' where id = :id for update', ['id' => $id])->one();
+        $stat->amount = bcadd($amount, $stat->amount, 2);
+        $stat->totalAmount = bcadd($amount, $shop->totalIncome, 2);
+        $stat->save();
+        //每月
+        StatIncomeMonthClass::updateOrInsert($shop, $amount);
     }
 
-}
+}

+ 20 - 34
biz-hd/stat/classes/StatIncomeMonthClass.php

@@ -2,53 +2,39 @@
 
 namespace bizHd\stat\classes;
 
+use bizHd\stat\models\StatIncomeMonth;
 use Yii;
 use bizHd\base\classes\BaseClass;
 
 class StatIncomeMonthClass extends BaseClass
 {
 
-	public static $baseFile = '\bizHd\stat\models\StatIncomeMonth';
+    public static $baseFile = '\bizHd\stat\models\StatIncomeMonth';
 
-    public static function updateOrInsert($sjId,$shopId,$amount)
+    public static function updateOrInsert($shop, $amount)
     {
         $time = date('Ym');
-        $year = date('Y');
-        $month = date('m');
+        $shopId = $shop->id;
         $where = [
-            'merchantId'=>$sjId,
-            'shopId'=>$shopId,
-            'year'=>$year,
-            'month'=>$month,
-            'time'=>$time,
+            'shopId' => $shopId,
+            'time' => $time,
         ];
-        $model = self::getByCondition($where);
-        if($model){
-            $id = $model['id'];
-            //累加金额 amount
-            $newAmount = bcadd($amount,$model['amount'],2);
-            $totalAmount = bcadd($amount,$model['totalAmount'],2);
-            self::updateById($id,['amount'=>$newAmount,'totalAmount'=>$totalAmount]);
-        }else{
-            //insert
-            $data = $where;
-            $data['amount'] = $amount;
-            //totalAmount = 前一天的 totalAmount
-            $totalAmount = 0;
-            $lastTime =  date('Ym',strtotime("-1 month"));
-            $lastWhere = [
-                'merchantId'=>$sjId,
-                'shopId'=>$shopId,
-                'time'=>$lastTime,
+        $model = self::getByCondition($where, true);
+        if (empty($model)) {
+            $data = [
+                'time' => $time,
+                'amount' => 0,
+                'totalAmount' => $shop->totalIncome,
+                'createTime' => date('Y-m-d H:i:s'),
             ];
-            $lastModel = self::getByCondition($lastWhere);
-            if($lastModel){
-                $totalAmount = $lastModel['totalAmount'];
-            }
-            $data['totalAmount'] = bcadd($amount,$totalAmount,2);
-            $data['createTime'] = date('Y-m-d H:i:s');
-            self::add($data);
+            $model = self::add($data, true);
         }
+        $id = $model->id;
+        //查id行级锁
+        $stat = StatIncomeMonth::findBySql('select * from ' . StatIncomeMonth::tableName() . ' where id = :id for update', ['id' => $id])->one();
+        $stat->amount = bcadd($amount, $stat->amount, 2);
+        $stat->totalAmount = bcadd($amount, $shop->totalIncome, 2);
+        $stat->save();
     }
 
 }

+ 18 - 16
biz-hd/stat/classes/StatOrderClass.php

@@ -2,6 +2,7 @@
 
 namespace bizHd\stat\classes;
 
+use bizHd\stat\models\StatOrder;
 use Yii;
 use bizHd\base\classes\BaseClass;
 
@@ -24,27 +25,28 @@ class StatOrderClass extends BaseClass
     }
 
     //今日订单+1
-    public static function updateOrInsert($sjId,$shopId)
+    public static function updateOrInsert($shop)
     {
         $time = date('Ymd');
+        $shopId = $shop->id;
+        $sjId = $shop->merchantId;
         $where = [
-            'merchantId'=>$sjId,
-            'shopId'=>$shopId,
-            'time'=>$time,
+            'shopId' => $shopId,
+            'time' => $time,
         ];
-        $model = self::getByCondition($where);
-        if($model){
-            $id = $model['id'];
-            //+1
-            $riseNum = $model['riseNum'] +1 ;
-            self::updateById($id,['riseNum'=>$riseNum]);
-        }else{
-            //insert
-            $data = $where;
-            $data['riseNum'] = 1;
-            $data['createTime'] = date('Y-m-d H:i:s');
-            self::add($data);
+        $model = self::getByCondition($where, true);
+        if (empty($model)) {
+            $model = self::add(['merchantId' => $sjId, 'time' => $time, 'riseNum' => 0, 'totalNum' => $shop->totalOrderNum], true);
         }
+        $id = $model->id;
+        //查id行级锁
+        $stat = StatOrder::findBySql('select * from ' . StatOrder::tableName() . ' where id = :id for update', ['id' => $id])->one();
+        $stat->riseNum += 1;
+        $stat->totalNum += 1;
+        $stat->save();
+
+        //本月订单+1
+        StatOrderMonthClass::updateOrInsert($shop);
     }
 
 

+ 15 - 20
biz-hd/stat/classes/StatOrderMonthClass.php

@@ -2,6 +2,7 @@
 
 namespace bizHd\stat\classes;
 
+use bizHd\stat\models\StatOrderMonth;
 use Yii;
 use bizHd\base\classes\BaseClass;
 
@@ -23,31 +24,25 @@ class StatOrderMonthClass extends BaseClass
     }
 
     //月订单+1
-    public static function updateOrInsert($sjId,$shopId)
+    public static function updateOrInsert($shop)
     {
         $time = date('Ym');
-        $year = date('Y');
-        $month = date('m');
+        $shopId = $shop->id;
+        $sjId = $shop->merchantId;
         $where = [
-            'merchantId'=>$sjId,
-            'shopId'=>$shopId,
-            'year'=>$year,
-            'month'=>$month,
-            'time'=>$time,
+            'shopId' => $shopId,
+            'time' => $time,
         ];
-        $model = self::getByCondition($where);
-        if($model){
-            $id = $model['id'];
-            //累加金额 amount
-            $riseNum = $model['riseNum'] +1;
-            self::updateById($id,['riseNum'=>$riseNum]);
-        }else{
-            //insert
-            $data = $where;
-            $data['riseNum'] = 1;
-            $data['createTime'] = date('Y-m-d H:i:s');
-            self::add($data);
+        $model = self::getByCondition($where, true);
+        if (empty($model)) {
+            $model = self::add(['merchantId' => $sjId, 'time' => $time, 'riseNum' => 0, 'totalNum' => $shop->totalOrderNum], true);
         }
+        $id = $model->id;
+        //查id行级锁
+        $stat = StatOrderMonth::findBySql('select * from ' . StatOrderMonth::tableName() . ' where id = :id for update', ['id' => $id])->one();
+        $stat->riseNum += 1;
+        $stat->totalNum += 1;
+        $stat->save();
     }
 
 }