shish hace 1 mes
padre
commit
1a83f97835

+ 14 - 0
app-ghs/controllers/ShMethodController.php

@@ -29,6 +29,20 @@ class ShMethodController extends BaseController
         }
     }
 
+    /**
+     * 获取门店所有的5种配送方式配置
+     * 如果数据库里没有,则取默认配置进行初始化
+     */
+    public function actionGetAllConfig()
+    {
+        try {
+            $configs = ShMethodClass::getAllConfigs($this->shopId, $this->mainId);
+            util::success($configs);
+        } catch (\Exception $e) {
+            util::fail($e->getMessage());
+        }
+    }
+
     /**
      * 获取所有配送方式的排序和别名信息
      */

+ 88 - 31
biz-ghs/order/classes/ShMethodClass.php

@@ -8,6 +8,7 @@
 namespace bizGhs\order\classes;
 
 use bizGhs\base\classes\BaseClass;
+use common\components\dict;
 use Yii;
 
 class ShMethodClass extends BaseClass
@@ -53,40 +54,23 @@ class ShMethodClass extends BaseClass
      * 获取指定门店和类型的配送方式配置,若不存在则进行默认初始化
      * @param int $shopId 门店ID
      * @param int $mainId 商户ID
-     * @param int $style 配送类型 (0: 送货, 1: 自取, 2: 跑腿, 3: 快递, 4: 物流)
+     * @param int $style 配送类型 (0: 送货, 1: 自取, 2: 跑腿, 3: 物流, 4: 快递)
      * @return array
      */
     public static function getConfig($shopId, $mainId, $style)
     {
-        $config = self::getByCondition(['shopId' => $shopId, 'style' => $style]);
+        // 复杂分支/关键逻辑:优化。后端的查询全部由 shopId 改为 mainId,只根据 mainId 和 style 进行查询。
+        $config = self::getByCondition(['mainId' => $mainId, 'style' => $style]);
         if (empty($config)) {
-            // 默认别名
-            $aliases = [
-                0 => '送货',
-                1 => '自取',
-                2 => '跑腿',
-                3 => '物流',
-                4 => '快递'
-            ];
-            $alias = $aliases[$style] ?? '配送';
-
-            // 初始化默认配置
-            $initData = [
-                'shopId' => $shopId,
+            // 从字典配置中读取对应类型的默认配置
+            $defaultConfig = dict::getDict('shMethod', $style);
+            
+            $initData = array_merge([
+                'shopId' => $shopId, // shopId 仅作为保存预留字段存入
                 'mainId' => $mainId,
                 'style' => $style,
-                'alias' => $alias,
-                'status' => 1,
-                'sort' => 0,
-                'minAmount' => 0.00,
-                'minNum' => 0,
-                'unMeet' => 0,
-                'unMeetFee' => 0.00,
-                'startKm' => 0.00,
-                'startPrice' => 0.00,
-                'perKmPrice' => 0.00,
-                'changeRate' => 0.00
-            ];
+            ], $defaultConfig);
+
             $config = self::add($initData, true);
             $config = $config->toArray();
         }
@@ -97,7 +81,7 @@ class ShMethodClass extends BaseClass
             'sort ASC, id ASC'
         );
 
-        // 如果是跑腿,获取关联的满减规则
+        // 如果 is_跑腿 (style为2),获取关联的满减规则
         if ($style == 2) {
             $config['reduceRules'] = ShReduceRuleClass::getAllByCondition(
                 ['methodId' => $config['id']],
@@ -110,6 +94,77 @@ class ShMethodClass extends BaseClass
         return $config;
     }
 
+    /**
+     * 一次性获取商户所有的5种配送方式配置,减少数据库查询交互。
+     * 如果数据库里没有对应配置,则自动采用 dict.php 里的默认参数进行填充并完成入库。
+     * @param int $shopId 门店ID
+     * @param int $mainId 商户ID
+     * @return array
+     */
+    public static function getAllConfigs($shopId, $mainId)
+    {
+        // 1. 一次性从数据库中查询当前商户的所有配送方式配置
+        $configsMap = self::getAllByCondition(['mainId' => $mainId], null, '*', 'style', true);
+
+        // 2. 补齐缺失的配送方式,并做初始化入库
+        for ($style = 0; $style <= 4; $style++) {
+            if (!isset($configsMap[$style])) {
+                $defaultConfig = dict::getDict('shMethod', $style);
+                $initData = array_merge([
+                    'shopId' => $shopId, // shopId 仅作为保存预留字段存入
+                    'mainId' => $mainId,
+                    'style' => $style,
+                ], $defaultConfig);
+
+                $configObj = self::add($initData, true);
+                $configsMap[$style] = $configObj;
+            }
+        }
+
+        // 将 ActiveRecord 统一转换为 array,并提取 methodIds 用于批量子表查询
+        $configsArray = [];
+        $methodIds = [];
+        foreach ($configsMap as $style => $configObj) {
+            $configArr = $configObj instanceof \yii\db\ActiveRecord ? $configObj->toArray() : $configObj;
+            $configsArray[$style] = $configArr;
+            $methodIds[] = (int)$configArr['id'];
+        }
+
+        // 3. 批量查询所有的说明项 (xhShExplain) - 仅 1 次查询
+        $allExplains = [];
+        if (!empty($methodIds)) {
+            $allExplains = ShExplainClass::getAllByCondition(['methodId' => ['in', $methodIds]], 'sort ASC, id ASC');
+        }
+        $explainsMap = [];
+        foreach ($allExplains as $exp) {
+            $explainsMap[(int)$exp['methodId']][] = $exp;
+        }
+
+        // 4. 批量查询所有的跑腿满减规则 (xhShReduceRule) - 仅 1 次查询
+        $allReduceRules = [];
+        if (!empty($methodIds)) {
+            $allReduceRules = ShReduceRuleClass::getAllByCondition(['methodId' => ['in', $methodIds]], 'sort ASC, id ASC');
+        }
+        $reduceRulesMap = [];
+        foreach ($allReduceRules as $rule) {
+            $reduceRulesMap[(int)$rule['methodId']][] = $rule;
+        }
+
+        // 5. 将批量查出的说明项和规则,合并填充到 5 个配送方式中
+        $result = [];
+        for ($style = 0; $style <= 4; $style++) {
+            $config = $configsArray[$style];
+            $methodId = (int)$config['id'];
+            
+            $config['explains'] = $explainsMap[$methodId] ?? [];
+            $config['reduceRules'] = ($style == 2) ? ($reduceRulesMap[$methodId] ?? []) : [];
+            
+            $result[] = $config;
+        }
+
+        return $result;
+    }
+
     /**
      * 保存配送方式配置,包含说明项和满减规则
      * @param int $shopId 门店ID
@@ -125,11 +180,13 @@ class ShMethodClass extends BaseClass
 
         $config = null;
         if ($id > 0) {
-            $config = self::getByCondition(['id' => $id, 'shopId' => $shopId], true);
+            // 复杂分支/关键逻辑:优化。后端的查询全部由 shopId 改为 mainId。
+            $config = self::getByCondition(['id' => $id, 'mainId' => $mainId], true);
         }
 
         if (empty($config)) {
-            $config = self::getByCondition(['shopId' => $shopId, 'style' => $style], true);
+            // 复杂分支/关键逻辑:优化。后端的查询全部由 shopId 改为 mainId。
+            $config = self::getByCondition(['mainId' => $mainId, 'style' => $style], true);
         }
 
         $saveData = [
@@ -149,7 +206,7 @@ class ShMethodClass extends BaseClass
         $transaction = Yii::$app->db->beginTransaction();
         try {
             if (empty($config)) {
-                $saveData['shopId'] = $shopId;
+                $saveData['shopId'] = $shopId; // shopId 作为保存预留字段存入
                 $saveData['mainId'] = $mainId;
                 $saveData['style'] = $style;
                 $config = self::add($saveData, true);

+ 68 - 0
common/components/dict.php

@@ -8,6 +8,74 @@ class dict
 {
 
     public static $data = [
+        // 配送方式默认配置
+        'shMethod' => [
+            0 => [
+                'alias' => '送货',
+                'status' => 1,
+                'sort' => 0,
+                'minAmount' => 0.00,
+                'minNum' => 0,
+                'unMeet' => 0,
+                'unMeetFee' => 0.00,
+                'startKm' => 0.00,
+                'startPrice' => 0.00,
+                'perKmPrice' => 0.00,
+                'changeRate' => 0.00
+            ],
+            1 => [
+                'alias' => '自取',
+                'status' => 1,
+                'sort' => 0,
+                'minAmount' => 0.00,
+                'minNum' => 0,
+                'unMeet' => 0,
+                'unMeetFee' => 0.00,
+                'startKm' => 0.00,
+                'startPrice' => 0.00,
+                'perKmPrice' => 0.00,
+                'changeRate' => 0.00
+            ],
+            2 => [
+                'alias' => '跑腿',
+                'status' => 1,
+                'sort' => 0,
+                'minAmount' => 0.00,
+                'minNum' => 0,
+                'unMeet' => 0,
+                'unMeetFee' => 0.00,
+                'startKm' => 0.00,
+                'startPrice' => 0.00,
+                'perKmPrice' => 0.00,
+                'changeRate' => 0.00
+            ],
+            3 => [
+                'alias' => '物流',
+                'status' => 1,
+                'sort' => 0,
+                'minAmount' => 0.00,
+                'minNum' => 0,
+                'unMeet' => 0,
+                'unMeetFee' => 0.00,
+                'startKm' => 0.00,
+                'startPrice' => 0.00,
+                'perKmPrice' => 0.00,
+                'changeRate' => 0.00
+            ],
+            4 => [
+                'alias' => '快递',
+                'status' => 1,
+                'sort' => 0,
+                'minAmount' => 0.00,
+                'minNum' => 0,
+                'unMeet' => 0,
+                'unMeetFee' => 0.00,
+                'startKm' => 0.00,
+                'startPrice' => 0.00,
+                'perKmPrice' => 0.00,
+                'changeRate' => 0.00
+            ],
+        ],
         //APP通知打开关闭状态 1打开 0关闭
         'appNotice' => 1,
         //微信通知打开关闭状态 1打开 0关闭