shish 1 mesiac pred
rodič
commit
64b4596a27

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

@@ -0,0 +1,61 @@
+<?php
+/**
+ * 用途:配送方式配置控制器
+ * 谁用:供货商系统管理员
+ * 解决什么问题:提供配送方式(送货、自取、跑腿、快递、物流)配置的获取和保存接口
+ */
+
+namespace ghs\controllers;
+
+use bizGhs\order\classes\ShMethodClass;
+use Yii;
+use common\components\util;
+
+class ShMethodController extends BaseController
+{
+    /**
+     * 获取指定配送方式的配置
+     * GET 参数:
+     * - style: 配送类型 (0: 送货, 1: 自取, 2: 跑腿, 3: 快递, 4: 物流)
+     */
+    public function actionGetConfig()
+    {
+        $style = Yii::$app->request->get('style', 0);
+        try {
+            $config = ShMethodClass::getConfig($this->shopId, $this->mainId, $style);
+            util::success($config);
+        } catch (\Exception $e) {
+            util::fail($e->getMessage());
+        }
+    }
+
+    /**
+     * 保存指定配送方式的配置
+     * POST 参数:
+     * - id: 配置ID(可选)
+     * - style: 配送类型 (0: 送货, 1: 自取, 2: 跑腿, 3: 快递, 4: 物流)
+     * - alias: 别名
+     * - status: 状态 (0: 禁用, 1: 启用)
+     * - sort: 排序
+     * - minAmount: 最低消费金额
+     * - minNum: 最低消费数量
+     * - unMeet: 不满条件处理方式 (0: 收运费, 1: 不能下单, 2: 收包装费)
+     * - unMeetFee: 运费/包装费金额
+     * - startKm: 起步公里数
+     * - startPrice: 起步价格
+     * - perKmPrice: 超出起步每公里加价
+     * - changeRate: 临时涨价比例 (%)
+     * - explains: 说明项列表 (二维数组,包含 explain, color, fontWeight, sort)
+     * - reduceRules: 跑腿满减规则列表 (二维数组,包含 meetNum, meetAmount, freeKm, sort)
+     */
+    public function actionSaveConfig()
+    {
+        $post = Yii::$app->request->post();
+        try {
+            ShMethodClass::saveConfig($this->shopId, $this->mainId, $post);
+            util::complete('保存成功');
+        } catch (\Exception $e) {
+            util::fail($e->getMessage());
+        }
+    }
+}

+ 15 - 0
biz-ghs/order/classes/ShExplainClass.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * 用途:配送方式说明项业务逻辑类
+ * 谁用:供货商系统
+ * 解决什么问题:处理说明项的增删改查
+ */
+
+namespace bizGhs\order\classes;
+
+use bizGhs\base\classes\BaseClass;
+
+class ShExplainClass extends BaseClass
+{
+    public static $baseFile = '\bizGhs\order\models\ShExplain';
+}

+ 173 - 0
biz-ghs/order/classes/ShMethodClass.php

@@ -0,0 +1,173 @@
+<?php
+/**
+ * 用途:配送方式配置业务逻辑类
+ * 谁用:供货商系统
+ * 解决什么问题:处理配送方式的查询、保存、默认初始化等业务逻辑
+ */
+
+namespace bizGhs\order\classes;
+
+use bizGhs\base\classes\BaseClass;
+use Yii;
+
+class ShMethodClass extends BaseClass
+{
+    public static $baseFile = '\bizGhs\order\models\ShMethod';
+
+    /**
+     * 获取指定门店和类型的配送方式配置,若不存在则进行默认初始化
+     * @param int $shopId 门店ID
+     * @param int $mainId 商户ID
+     * @param int $style 配送类型 (0: 送货, 1: 自取, 2: 跑腿, 3: 快递, 4: 物流)
+     * @return array
+     */
+    public static function getConfig($shopId, $mainId, $style)
+    {
+        $config = self::getByCondition(['shopId' => $shopId, 'style' => $style]);
+        if (empty($config)) {
+            // 默认别名
+            $aliases = [
+                0 => '送货',
+                1 => '自取',
+                2 => '跑腿',
+                3 => '快递',
+                4 => '物流'
+            ];
+            $alias = $aliases[$style] ?? '配送';
+
+            // 初始化默认配置
+            $initData = [
+                '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
+            ];
+            $config = self::add($initData, true);
+            $config = $config->toArray();
+        }
+
+        // 获取关联的说明项
+        $config['explains'] = ShExplainClass::getAllByCondition(
+            ['methodId' => $config['id']],
+            'sort ASC, id ASC'
+        );
+
+        // 如果是跑腿,获取关联的满减规则
+        if ($style == 2) {
+            $config['reduceRules'] = ShReduceRuleClass::getAllByCondition(
+                ['methodId' => $config['id']],
+                'sort ASC, id ASC'
+            );
+        } else {
+            $config['reduceRules'] = [];
+        }
+
+        return $config;
+    }
+
+    /**
+     * 保存配送方式配置,包含说明项和满减规则
+     * @param int $shopId 门店ID
+     * @param int $mainId 商户ID
+     * @param array $data 配置数据
+     * @return bool
+     * @throws \Exception
+     */
+    public static function saveConfig($shopId, $mainId, $data)
+    {
+        $id = $data['id'] ?? 0;
+        $style = $data['style'] ?? 0;
+
+        $config = null;
+        if ($id > 0) {
+            $config = self::getByCondition(['id' => $id, 'shopId' => $shopId], true);
+        }
+
+        if (empty($config)) {
+            $config = self::getByCondition(['shopId' => $shopId, 'style' => $style], true);
+        }
+
+        $saveData = [
+            'alias' => $data['alias'] ?? '',
+            'status' => isset($data['status']) ? (int)$data['status'] : 1,
+            'sort' => isset($data['sort']) ? (int)$data['sort'] : 0,
+            'minAmount' => isset($data['minAmount']) ? (float)$data['minAmount'] : 0.00,
+            'minNum' => isset($data['minNum']) ? (int)$data['minNum'] : 0,
+            'unMeet' => isset($data['unMeet']) ? (int)$data['unMeet'] : 0,
+            'unMeetFee' => isset($data['unMeetFee']) ? (float)$data['unMeetFee'] : 0.00,
+            'startKm' => isset($data['startKm']) ? (float)$data['startKm'] : 0.00,
+            'startPrice' => isset($data['startPrice']) ? (float)$data['startPrice'] : 0.00,
+            'perKmPrice' => isset($data['perKmPrice']) ? (float)$data['perKmPrice'] : 0.00,
+            'changeRate' => isset($data['changeRate']) ? (float)$data['changeRate'] : 0.00,
+        ];
+
+        $transaction = Yii::$app->db->beginTransaction();
+        try {
+            if (empty($config)) {
+                $saveData['shopId'] = $shopId;
+                $saveData['mainId'] = $mainId;
+                $saveData['style'] = $style;
+                $config = self::add($saveData, true);
+            } else {
+                self::updateByCondition(['id' => $config->id], $saveData);
+            }
+
+            $methodId = $config->id;
+
+            // 1. 保存说明项 (xhShExplain)
+            ShExplainClass::deleteByCondition(['methodId' => $methodId]);
+            if (!empty($data['explains']) && is_array($data['explains'])) {
+                $explainRows = [];
+                foreach ($data['explains'] as $index => $exp) {
+                    if (empty($exp['explain'])) {
+                        continue;
+                    }
+                    $explainRows[] = [
+                        'methodId' => $methodId,
+                        'explain' => $exp['explain'],
+                        'color' => isset($exp['color']) ? (int)$exp['color'] : 1,
+                        'fontWeight' => isset($exp['fontWeight']) ? (int)$exp['fontWeight'] : 1,
+                        'sort' => isset($exp['sort']) ? (int)$exp['sort'] : $index,
+                    ];
+                }
+                if (!empty($explainRows)) {
+                    ShExplainClass::batchAdd($explainRows);
+                }
+            }
+
+            // 2. 保存跑腿满减规则 (xhShReduceRule)
+            ShReduceRuleClass::deleteByCondition(['methodId' => $methodId]);
+            if ($style == 2 && !empty($data['reduceRules']) && is_array($data['reduceRules'])) {
+                $ruleRows = [];
+                foreach ($data['reduceRules'] as $index => $rule) {
+                    $ruleRows[] = [
+                        'methodId' => $methodId,
+                        'meetNum' => isset($rule['meetNum']) ? (int)$rule['meetNum'] : 0,
+                        'meetAmount' => isset($rule['meetAmount']) ? (float)$rule['meetAmount'] : 0.00,
+                        'freeKm' => isset($rule['freeKm']) ? (float)$rule['freeKm'] : 0.00,
+                        'sort' => isset($rule['sort']) ? (int)$rule['sort'] : $index,
+                    ];
+                }
+                if (!empty($ruleRows)) {
+                    ShReduceRuleClass::batchAdd($ruleRows);
+                }
+            }
+
+            $transaction->commit();
+            return true;
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            throw $e;
+        }
+    }
+}

+ 15 - 0
biz-ghs/order/classes/ShReduceRuleClass.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * 用途:跑腿满减规则业务逻辑类
+ * 谁用:供货商系统
+ * 解决什么问题:处理满减规则的增删改查
+ */
+
+namespace bizGhs\order\classes;
+
+use bizGhs\base\classes\BaseClass;
+
+class ShReduceRuleClass extends BaseClass
+{
+    public static $baseFile = '\bizGhs\order\models\ShReduceRule';
+}

+ 22 - 0
biz-ghs/order/models/ShExplain.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * 用途:配送方式说明项模型
+ * 谁用:供货商系统
+ * 解决什么问题:存储每种配送方式下的说明文字、颜色、加粗等配置
+ */
+
+namespace bizGhs\order\models;
+
+use bizGhs\base\models\Base;
+
+class ShExplain extends Base
+{
+    /**
+     * 返回对应的数据库表名
+     * @return string
+     */
+    public static function tableName()
+    {
+        return 'xhShExplain';
+    }
+}

+ 22 - 0
biz-ghs/order/models/ShMethod.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * 用途:配送方式配置主表模型
+ * 谁用:供货商系统
+ * 解决什么问题:存储送货、自取、跑腿、快递、物流等配送方式的基础配置数据
+ */
+
+namespace bizGhs\order\models;
+
+use bizGhs\base\models\Base;
+
+class ShMethod extends Base
+{
+    /**
+     * 返回对应的数据库表名
+     * @return string
+     */
+    public static function tableName()
+    {
+        return 'xhShMethod';
+    }
+}

+ 22 - 0
biz-ghs/order/models/ShReduceRule.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * 用途:跑腿满减规则模型
+ * 谁用:供货商系统
+ * 解决什么问题:存储跑腿配送方式下的满减/免运费规则(如:满 X 扎且满 Y 元,免 Z 公里内跑腿费)
+ */
+
+namespace bizGhs\order\models;
+
+use bizGhs\base\models\Base;
+
+class ShReduceRule extends Base
+{
+    /**
+     * 返回对应的数据库表名
+     * @return string
+     */
+    public static function tableName()
+    {
+        return 'xhShReduceRule';
+    }
+}