shish 5 years ago
parent
commit
2c4697e6b7
2 changed files with 84 additions and 71 deletions
  1. 53 42
      app-ghs/controllers/ProductController.php
  2. 31 29
      biz-ghs/product/classes/ProductClass.php

+ 53 - 42
app-ghs/controllers/ProductController.php

@@ -3,19 +3,13 @@
  * User: admin
  * Date Time: 2021/1/15 20:36
  */
-
 namespace ghs\controllers;
 
-
 use biz\admin\classes\AdminRoleClass;
-use bizGhs\admin\classes\AdminClass;
-use bizGhs\item\classes\ItemClass;
 use bizGhs\item\classes\ItemClassClass;
 use bizGhs\merchant\classes\ShopClass;
 use bizGhs\product\classes\ProductClass;
 use bizGhs\product\services\ProductService;
-use bizGhs\item\services\ItemService;
-use common\components\noticeUtil;
 use common\components\stringUtil;
 use common\components\util;
 use Yii;
@@ -154,12 +148,6 @@ class ProductController extends BaseController
 
     //批量改价
     public function actionBatchChangePrice()
-    {
-        util::complete();
-    }
-
-    //改价
-    public function actionChangePrice()
     {
         //花材价格只能在首店修改,并同步到分店
         $shop = $this->shop;
@@ -176,41 +164,64 @@ class ProductController extends BaseController
             util::fail('没有权限操作哦');
         }
 
-        $productId = Yii::$app->request->post('productId', 0);
-        $price = Yii::$app->request->post('price', '');
-        if (is_numeric($price) && $price > 0) {
-            //判断是否有效
-            $productData = ProductClass::getProductData($productId, $this->shopId);
-            if (!$productData) {
-                util::fail("花材不存在");
+        $data = Yii::$app->request->post('data', '');
+        if (empty($data)) {
+            util::fail('没有修改');
+        }
+        $arr = json_decode($data, true);
+        if (empty($arr)) {
+            util::fail('没有修改!');
+        }
+
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+        try {
+
+            $ids = array_column($arr, 'id');
+            $ids = array_unique(array_filter($ids));
+            if (empty($ids)) {
+                util::fail('请选择花材');
             }
-            ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_CHANGE);
-        } else {
-            //自动调价
-            $productData = ProductClass::getById($productId);
-            if (!$productData) {
-                util::fail("花材不存在");
+
+            $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
+
+            foreach ($arr as $product) {
+                if (isset($product['price']) == false) {
+                    util::fail('没有价格');
+                }
+                if (isset($product['id']) == false) {
+                    util::fail('没有花材');
+                }
+                $price = $product['price'];
+                $productId = $product['id'];
+
+                $product = $productList[$productId] ?? [];
+                if (empty($product)) {
+                    util::fail('没有找到花材');
+                }
+                if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
+                    util::fail('没有权限访问');
+                }
+
+                if (empty($price)) {
+                    //恢复自动调价
+                    $price = bcadd($product->cost, $product->addPrice, 2);
+                    ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_AUTO);
+                } else {
+                    //改价
+                    ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
+                }
             }
-            $price = bcadd($productData['cost'], $productData['addPrice'], 2);
-            ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
-        }
-        util::success(['price' => $price]);
-    }
+            $transaction->commit();
+            util::complete();
 
-    //恢复自动调价
-    public function actionAutoPrice()
-    {
-        $productId = Yii::$app->request->post('productId', 0);
-        $productData = ProductClass::getById($productId);
-        if (!$productData) {
-            util::fail("花材不存在");
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            Yii::info("修改失败原因:" . $e->getMessage());
+            util::fail('修改失败');
         }
-        //2021.04.06改为用product中的成本价+加价
-        $price = bcadd($productData['cost'], $productData['addPrice'], 2);
-        ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
-        util::complete("操作成功");
-    }
 
+    }
 
     //修改加价
     public function actionChangeAddPrice()

+ 31 - 29
biz-ghs/product/classes/ProductClass.php

@@ -524,14 +524,36 @@ class ProductClass extends BaseClass
     }
 
     //修改价格 linqh 2021.1.28
-    public static function changePrice($id, $shopId, $price, $priceLabel)
+    public static function changePrice($product, $price, $priceLabel)
     {
+        $id = $product->id ?? 0;
+        $shopId = $product->shopId ?? 0;
         //2021.7.6 linqh 首店改价,同步到各个分店
-        $res = ProductClass::updateByCondition(['id' => $id, 'shopId' => $shopId], ['price' => $price, 'priceLabel' => $priceLabel]);
+        $res = ProductClass::updateByCondition(['id' => $id], ['price' => $price, 'priceLabel' => $priceLabel]);
         self::updateSyncProduct($id, $shopId, ['price' => $price, 'priceLabel' => $priceLabel]);
         return $res;
     }
 
+    //2021.7.6 修改花材同步其他分店
+    public static function updateSyncProduct($productId, $shopId, $upData)
+    {
+        $shopInfo = ShopClass::getById($shopId);
+        $productData = ProductClass::getById($productId);
+        $itemId = $productData['itemId'];
+        if ($shopInfo['default'] == 1) {
+            //首店
+            $sjId = $shopInfo['sjId'];
+            $shopAll = ShopClass::getAllShop($sjId);
+            if ($shopAll) {
+                foreach ($shopAll as $s) {
+                    if ($s['id'] != $shopId) {
+                        ProductClass::updateByCondition(['itemId' => $itemId, 'shopId' => $s['id']], $upData);
+                    }
+                }
+            }
+        }
+    }
+
     //下单时计算商品的价格  linqh 2021.1.28
     //itemInfo : [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":5,"productId":4,"bigNum":1,"smallNum":5}]
     //2021.7.26增加前端传的价格 (小程序有传,但pc端不穿)
@@ -654,17 +676,17 @@ class ProductClass extends BaseClass
         if (isset($data['stock']) && is_numeric($data['stock'])) {
             $preStock = $product->stock ?? 0;
             if ($data['stock'] != $preStock) {
-                if($data['stock'] < 0){
+                if ($data['stock'] < 0) {
                     util::fail("库存不能小于0");
                 }
                 //增加盘点记录 2021.7.27
-                $ratio = $upData['ratio']??$product->ratio;//取最新比例
-                $formNum = ProductClass::formatStock($data['stock'],$ratio);
+                $ratio = $upData['ratio'] ?? $product->ratio;//取最新比例
+                $formNum = ProductClass::formatStock($data['stock'], $ratio);
                 $ghsItemInfo = [
                     [
-                        'productId'=>$product->id,
-                        'bigNum'=>$formNum['bigNum'],
-                        'smallNum'=>$formNum['smallNum'],
+                        'productId' => $product->id,
+                        'bigNum' => $formNum['bigNum'],
+                        'smallNum' => $formNum['smallNum'],
                     ]
                 ];
                 $pdData = [];
@@ -672,7 +694,7 @@ class ProductClass extends BaseClass
                 $pdData['sjId'] = $product->sjId;
                 $pdData['shopId'] = $product->shopId;
                 $pdData['adminId'] = $data['adminId'];
-                CheckOrderClass::opOrder($pdData,false,false);
+                CheckOrderClass::opOrder($pdData, false, false);
             }
             unset($data['stock']);
         }
@@ -1035,26 +1057,6 @@ class ProductClass extends BaseClass
         }
     }
 
-    //2021.7.6 修改花材同步其他分店
-    public static function updateSyncProduct($productId, $shopId, $upData)
-    {
-        $shopInfo = ShopClass::getById($shopId);
-        $productData = ProductClass::getById($productId);
-        $itemId = $productData['itemId'];
-        if ($shopInfo['default'] == 1) {
-            //首店
-            $sjId = $shopInfo['sjId'];
-            $shopAll = ShopClass::getAllShop($sjId);
-            if ($shopAll) {
-                foreach ($shopAll as $s) {
-                    if ($s['id'] != $shopId) {
-                        ProductClass::updateByCondition(['itemId' => $itemId, 'shopId' => $s['id']], $upData);
-                    }
-                }
-            }
-        }
-    }
-
     //花材上下架
     public static function changeDelStatus($productId, $delStatus)
     {