shish 1 год назад
Родитель
Сommit
c51fcc56a5
1 измененных файлов с 117 добавлено и 108 удалено
  1. 117 108
      app-ghs/controllers/OrderController.php

+ 117 - 108
app-ghs/controllers/OrderController.php

@@ -2639,123 +2639,132 @@ class OrderController extends BaseController
         $newFile = $preFileName . ".xls";
         $fullPathFile = $path . $newFile;
         if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
-            $arr = [];
-            $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
-            require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');
-            $spreadsheet = \PHPExcel_IOFactory::load($fullPathFile);
-            // 获取活动工作表
-            $worksheet = $spreadsheet->getActiveSheet();
-            // 遍历每一行
-            foreach ($worksheet->getRowIterator() as $key => $row) {
-                $cellIterator = $row->getCellIterator();
-                // 这里设置为迭代所有单元格,包括空单元格
-                $cellIterator->setIterateOnlyExistingCells(FALSE);
-                // 遍历每列
-                $rowData = [];
-                foreach ($cellIterator as $cell) {
-                    $rowData[] = $cell->getValue();
+
+            $connection = Yii::$app->db;
+            $transaction = $connection->beginTransaction();
+            try {
+
+                $arr = [];
+                $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
+                require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');
+                $spreadsheet = \PHPExcel_IOFactory::load($fullPathFile);
+                // 获取活动工作表
+                $worksheet = $spreadsheet->getActiveSheet();
+                // 遍历每一行
+                foreach ($worksheet->getRowIterator() as $key => $row) {
+                    $cellIterator = $row->getCellIterator();
+                    // 这里设置为迭代所有单元格,包括空单元格
+                    $cellIterator->setIterateOnlyExistingCells(FALSE);
+                    // 遍历每列
+                    $rowData = [];
+                    foreach ($cellIterator as $cell) {
+                        $rowData[] = $cell->getValue();
+                    }
+                    $arr[] = $rowData;
                 }
-                $arr[] = $rowData;
-            }
-            array_shift($arr);
-            $productIds = [];
-            foreach ($arr as $k => $v) {
-                $productId = $v[0] ?? 0;
-                $productId = trim($productId);
-                if (is_numeric($productId) && $productId > 0) {
-                    $productIds[] = $productId;
+                array_shift($arr);
+                $productIds = [];
+                foreach ($arr as $k => $v) {
+                    $productId = $v[0] ?? 0;
+                    $productId = trim($productId);
+                    if (is_numeric($productId) && $productId > 0) {
+                        $productIds[] = $productId;
+                    }
                 }
-            }
 
-            $productInfo = ProductClass::getByIds($productIds, null, 'id');
-
-            $mat = [];
-            foreach ($arr as $k => $v) {
-                $productId = $v[0] ?? 0;
-                $productId = trim($productId);
-                $itemName = $v[1] ?? '';
-                $itemName = trim($itemName);
-                $num = $v[2] ?? 0;
-                $num = trim($num);
-                $unitPrice = $v[3] ?? 0;
-                $unitPrice = trim($unitPrice);
-                $remark = $v['5'] ?? '';
-                $remark = trim($remark);
-                if (is_numeric($productId) && $productId > 0 && $num > 0 && $unitPrice > 0) {
-                    $product = $productInfo[$productId] ?? [];
-                    $ptItemId = $product['itemId'] ?? 0;
-                    $mat[] = [
-                        'productId' => $productId,
-                        'bigNum' => $num,
-                        'itemId' => $ptItemId,
-                        'smallNum' => 0,
-                        'price' => $unitPrice,
-                        'remark' => $remark,
-                    ];
+                $productInfo = ProductClass::getByIds($productIds, null, 'id');
+
+                $mat = [];
+                foreach ($arr as $k => $v) {
+                    $productId = $v[0] ?? 0;
+                    $productId = trim($productId);
+                    $num = $v[2] ?? 0;
+                    $num = trim($num);
+                    $unitPrice = $v[3] ?? 0;
+                    $unitPrice = trim($unitPrice);
+                    $remark = $v['5'] ?? '';
+                    $remark = trim($remark);
+                    if (is_numeric($productId) && $productId > 0 && $num > 0 && $unitPrice > 0) {
+                        $product = $productInfo[$productId] ?? [];
+                        $ptItemId = $product['itemId'] ?? 0;
+                        $mat[] = [
+                            'productId' => $productId,
+                            'bigNum' => $num,
+                            'itemId' => $ptItemId,
+                            'smallNum' => 0,
+                            'price' => $unitPrice,
+                            'remark' => $remark,
+                        ];
+                    }
                 }
-            }
-
-            $modifyPrice = 0;
-            foreach ($mat as $ma) {
-                $cNum = $ma['bigNum'] ?? 0;
-                $cPrice = $ma['price'] ?? 0;
-                $cTotal = bcmul($cNum, $cPrice, 2);
-                $modifyPrice = bcadd($modifyPrice, $cTotal, 2);
-            }
-
-            $customName = $customRes->name ?? '';
-            $customMobile = $customRes->mobile ?? '';
-            $customPy = $customRes->py ?? '';
-            $ghsId = $customRes->ghsId ?? 0;
-
-            $hasPay = 2;
-            $custom = CustomClass::getCustom($customId);
-            $post = [];
-            $post['modifyPrice'] = $modifyPrice;
-            $post['shopId'] = $shopId;
-            $post['sendType'] = 0;
-            $post['transType'] = 0;
-            $post['sendSide'] = 0;
-            $post['sendCost'] = 0;
-            $post['packCost'] = 0;
-            $post['customId'] = $customId;
-            $post['customName'] = $customName;
-            $post['payWay'] = 0;
-            $post['needPrint'] = 2;
-            $post['hasPay'] = 2;
-            $post['product'] = $mat;
-            $post['deadline'] = date("Y-m-d H:i:s", time() + 86400);
-            $post['debt'] = 1;
-            $post['mainId'] = $mainId;
-            $post['fromType'] = 1;
-            $post['expressId'] = 0;
-            $post['sjId'] = $sjId;
 
-            $staff = $this->shopAdmin;
-            $staffId = $staff->id ?? 0;
-            $staffName = $staff->name ?? '';
-            $post['shopAdminId'] = $staffId;
-            $post['shopAdminName'] = $staffName;
-            $post['getStaffId'] = $staffId;
-            $post['getStaffName'] = $staffName;
-
-            $post['customMobile'] = $customMobile;
-            $post['customNamePy'] = $customPy;
-            $post['customAvatar'] = '';
-
-            $post['newVersion'] = 1;
-            $post['book'] = 1;
-            $post['wlName'] = '';
-            $post['dealPrice'] = 1;
-            $post['ghsId'] = $ghsId;
+                $modifyPrice = 0;
+                foreach ($mat as $ma) {
+                    $cNum = $ma['bigNum'] ?? 0;
+                    $cPrice = $ma['price'] ?? 0;
+                    $cTotal = bcmul($cNum, $cPrice, 2);
+                    $modifyPrice = bcadd($modifyPrice, $cTotal, 2);
+                }
 
-            //多处有用到此方法,需要同步修改,搜索关键词create_new_order
-            OrderService::createNewOrder($post, $custom, $hasPay);
+                $customName = $customRes->name ?? '';
+                $customMobile = $customRes->mobile ?? '';
+                $customPy = $customRes->py ?? '';
+                $ghsId = $customRes->ghsId ?? 0;
+
+                $hasPay = 2;
+                $custom = CustomClass::getCustom($customId);
+                $post = [];
+                $post['modifyPrice'] = $modifyPrice;
+                $post['shopId'] = $shopId;
+                $post['sendType'] = 0;
+                $post['transType'] = 0;
+                $post['sendSide'] = 0;
+                $post['sendCost'] = 0;
+                $post['packCost'] = 0;
+                $post['customId'] = $customId;
+                $post['customName'] = $customName;
+                $post['payWay'] = 0;
+                $post['needPrint'] = 2;
+                $post['hasPay'] = 2;
+                $post['product'] = $mat;
+                $post['deadline'] = date("Y-m-d H:i:s", time() + 86400);
+                $post['debt'] = 1;
+                $post['mainId'] = $mainId;
+                $post['fromType'] = 1;
+                $post['expressId'] = 0;
+                $post['sjId'] = $sjId;
+
+                $staff = $this->shopAdmin;
+                $staffId = $staff->id ?? 0;
+                $staffName = $staff->name ?? '';
+                $post['shopAdminId'] = $staffId;
+                $post['shopAdminName'] = $staffName;
+                $post['getStaffId'] = $staffId;
+                $post['getStaffName'] = $staffName;
+
+                $post['customMobile'] = $customMobile;
+                $post['customNamePy'] = $customPy;
+                $post['customAvatar'] = '';
+
+                $post['newVersion'] = 1;
+                $post['book'] = 1;
+                $post['wlName'] = '';
+                $post['dealPrice'] = 1;
+                $post['ghsId'] = $ghsId;
+
+                //多处有用到此方法,需要同步修改,搜索关键词create_new_order
+                OrderService::createNewOrder($post, $custom, $hasPay);
 
-            util::complete('提交成功');
+                $transaction->commit();
+                util::complete('提交成功');
 
+            } catch (\Exception $e) {
+                $transaction->rollBack();
+                Yii::info("导入失败了," . $e->getMessage());
+                util::fail('导入失败了');
+            }
         } else {
-            util::fail('上传失败');
+            util::fail('上传失败了哦');
         }
     }