shish 4 years ago
parent
commit
1c8e488b4d
1 changed files with 124 additions and 0 deletions
  1. 124 0
      console/controllers/StatKhCgController.php

+ 124 - 0
console/controllers/StatKhCgController.php

@@ -0,0 +1,124 @@
+<?php
+
+namespace console\controllers;
+
+use biz\shop\classes\ShopClass;
+use biz\stat\classes\StatKdClass;
+use biz\stat\classes\StatRefundClass;
+use bizGhs\order\classes\OrderClass;
+use bizGhs\order\classes\OrderItemClass;
+use bizGhs\order\classes\RefundOrderClass;
+use bizGhs\order\classes\RefundOrderItemClass;
+use bizGhs\order\models\Order;
+use bizGhs\order\models\RefundOrder;
+use yii\console\Controller;
+use Yii;
+
+class StatKhCgController extends Controller
+{
+
+    // ./yii stat-kh-cg/bc
+    public function actionBc()
+    {
+        $query = new \yii\db\Query();
+        $query->from(Order::tableName());
+
+        foreach ($query->batch() as $batch) {
+            foreach ($batch as $order) {
+                $id = $order['id'] ?? 0;
+                $orderSn = $order['orderSn'] ?? '';
+                $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
+                if (empty($itemList)) {
+                    continue;
+                }
+                $totalNum = 0;
+                foreach ($itemList as $key => $item) {
+                    $num = $item['num'] ?? 0;
+                    $totalNum = bcadd($totalNum, $num, 2);
+                }
+                OrderClass::updateById($id, ['itemNum' => $totalNum]);
+            }
+        }
+
+        $query = new \yii\db\Query();
+        $query->from(RefundOrder::tableName());
+
+        foreach ($query->batch() as $batch) {
+            foreach ($batch as $refund) {
+                $id = $refund['id'] ?? 0;
+                $orderSn = $refund['orderSn'] ?? '';
+                $relateOrderSn = $refund['relateOrderSn'] ?? '';
+                $current = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
+                $customId = $current->customId ?? 0;
+                $itemList = RefundOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
+                if (empty($itemList)) {
+                    continue;
+                }
+                $totalNum = 0;
+                foreach ($itemList as $item) {
+                    $num = $item['itemNum'] ?? 0;
+                    $totalNum = bcadd($totalNum, $num, 2);
+                }
+                RefundOrderClass::updateById($id, ['itemNum' => $totalNum, 'customId' => $customId]);
+            }
+        }
+
+    }
+
+    // ./yii stat-kd/update
+    public function actionUpdate()
+    {
+        ini_set('memory_limit', '2045M');
+        set_time_limit(0);
+
+        $sql = 'TRUNCATE `xhStatKd`;TRUNCATE `xhStatKdMonth`;';
+        Yii::$app->db->createCommand($sql)->execute();
+
+        $query = new \yii\db\Query();
+        $query->from(Order::tableName());
+
+        foreach ($query->batch() as $batch) {
+
+            foreach ($batch as $order) {
+                $status = $order['status'] ?? 0;
+                if ($status == 1 || $status == 5) {
+                    continue;
+                }
+                $payWay = $order['payWay'] ?? 0;
+                $amount = $order['actPrice'] ?? 0;
+                $refundPrice = $order['refundPrice'] ?? 0;
+                if ($refundPrice > 0) {
+                    $amount = bcadd($amount, $refundPrice, 2);
+                }
+                $shopId = $order['shopId'] ?? 0;
+                $shop = ShopClass::getLockById($shopId);
+                if (empty($shop)) {
+                    continue;
+                }
+                $date = date('Ymd', strtotime($order['addTime']));
+                StatKdClass::replace($shop, $amount, $payWay, $date);
+            }
+        }
+
+        $sql = 'TRUNCATE `xhStatRefund`;TRUNCATE `xhStatRefundMonth`;';
+        Yii::$app->db->createCommand($sql)->execute();
+
+        $query = new \yii\db\Query();
+        $query->from(RefundOrder::tableName());
+
+        foreach ($query->batch() as $batch) {
+            foreach ($batch as $refund) {
+                $relateOrderSn = $refund['relateOrderSn'] ?? '';
+                $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
+                $payWay = $order->payWay ?? 0;
+                $shopId = $refund['shopId'] ?? 0;
+                $shop = ShopClass::getLockById($shopId);
+                $amount = $refund['refundPrice'] ?? 0;
+                $date = date('Ymd', strtotime($refund['addTime']));
+                StatRefundClass::replace($shop, $payWay, $amount, $date);
+            }
+        }
+    }
+
+
+}