shish 5 years ago
parent
commit
86540cc83e
1 changed files with 48 additions and 24 deletions
  1. 48 24
      console/controllers/PdController.php

+ 48 - 24
console/controllers/PdController.php

@@ -23,33 +23,57 @@ class PdController extends Controller
                 $orderSn = $item->orderSn ?? '';
                 $shopId = $item->shopId ?? 0;
                 $addTime = $item->addTime ?? '';
-                $shop = ShopClass::getById($shopId, true);
-                if (empty($shop)) {
-                    echo '没有找到店铺 ' . $shopId . ' ' . $orderSn . "\n";
-                    continue;
-                }
-                $productList = CheckOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
-                if (!empty($productList)) {
-                    $add = 0;
-                    $sub = 0;
-                    foreach ($productList as $product) {
-                        $num = $product->profitLossNum ?? 0;
-                        $cost = $product->itemCost ?? 0;
-                        $res = bcmul($num, $cost, 2);
-                        if ($res > 0) {
-                            $add = bcadd($add, $res, 2);
-                        } else {
-                            $sub = bcadd($add, abs($res), 2);
-                        }
-                    }
-                    $date = date("Ymd", strtotime($addTime));
-                    if ($add > 0) {
-                        StatPdAddClass::replace($shop, $add, $date);
+
+                $connection = Yii::$app->db;//事务处理
+                $transaction = $connection->beginTransaction();
+
+                try {
+
+                    $shop = ShopClass::getLockById($shopId);
+                    if (empty($shop)) {
+                        echo '没有找到店铺 ' . $shopId . ' ' . $orderSn . "\n";
+                        continue;
                     }
-                    if ($sub > 0) {
-                        StatPdSubClass::replace($shop, $sub, $date);
+                    $productList = CheckOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
+                    if (!empty($productList)) {
+
+
+                        $add = 0;
+                        $sub = 0;
+                        foreach ($productList as $product) {
+                            $num = $product->profitLossNum ?? 0;
+                            $cost = $product->itemCost ?? 0;
+                            $res = bcmul($num, $cost, 2);
+                            if ($res > 0) {
+                                $add = bcadd($add, $res, 2);
+                            } else {
+                                $sub = bcadd($add, abs($res), 2);
+                            }
+                        }
+                        $totalAdd = bcadd($shop->totalPdAdd, $add, 2);
+                        $totalSub = bcadd($shop->totalPdSub, $sub, 2);
+                        $shop->totalPdAdd = $totalAdd;
+                        $shop->totalPdSub = $totalSub;
+                        $shop->save();
+
+                        $date = date("Ymd", strtotime($addTime));
+                        if ($add > 0) {
+                            StatPdAddClass::replace($shop, $add, $date);
+                        }
+                        if ($sub > 0) {
+                            StatPdSubClass::replace($shop, $sub, $date);
+                        }
+
                     }
+
+                    $transaction->commit();
+
+                } catch (\Exception $e) {
+                    $transaction->rollBack();
+                    $msg = $e->getMessage();
+                    echo "报错了:" . $msg . "\n";
                 }
+
             }
         }
     }