|
|
@@ -0,0 +1,195 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * 用途:花店配送方式配置业务逻辑类
|
|
|
+ * 谁用:花店系统 hdApp 配送方式设置页
|
|
|
+ * 解决什么问题:读写 xhPsMethod / xhPsExplain / xhPsReduceRule,与供货商 xhSh* 分离
|
|
|
+ */
|
|
|
+
|
|
|
+namespace bizHd\order\classes;
|
|
|
+
|
|
|
+use bizHd\base\classes\BaseClass;
|
|
|
+use common\components\dict;
|
|
|
+use Yii;
|
|
|
+
|
|
|
+class PsMethodClass extends BaseClass
|
|
|
+{
|
|
|
+ public static $baseFile = '\bizHd\order\models\PsMethod';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有配送方式的排序和别名信息
|
|
|
+ * @param int $mainId 商户ID
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function getSorts($mainId)
|
|
|
+ {
|
|
|
+ $list = [];
|
|
|
+ $aliases = [
|
|
|
+ 0 => '送货',
|
|
|
+ 1 => '自取',
|
|
|
+ 2 => '跑腿',
|
|
|
+ 3 => '物流',
|
|
|
+ 4 => '快递'
|
|
|
+ ];
|
|
|
+ for ($style = 0; $style <= 4; $style++) {
|
|
|
+ $config = self::getByCondition(['mainId' => $mainId, 'style' => $style]);
|
|
|
+ $sort = 0;
|
|
|
+ $alias = $aliases[$style];
|
|
|
+ $status = 1;
|
|
|
+ if (!empty($config)) {
|
|
|
+ $sort = isset($config['sort']) ? (int)$config['sort'] : 0;
|
|
|
+ $alias = !empty($config['name']) ? $config['name'] : $aliases[$style];
|
|
|
+ $status = isset($config['status']) ? (int)$config['status'] : 1;
|
|
|
+ }
|
|
|
+ $list[] = [
|
|
|
+ 'style' => $style,
|
|
|
+ 'name' => $alias,
|
|
|
+ 'sort' => $sort,
|
|
|
+ 'status' => $status
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定配送方式的配置(不存在则按字典默认初始化)
|
|
|
+ * @param int $shopId 门店ID
|
|
|
+ * @param int $mainId 商户ID
|
|
|
+ * @param int $style 配送类型 0-4
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function getConfig($shopId, $mainId, $style)
|
|
|
+ {
|
|
|
+ // 复杂分支/关键逻辑:按 mainId + style 查 xhPsMethod,无记录则写入默认配置
|
|
|
+ $config = self::getByCondition(['mainId' => $mainId, 'style' => $style]);
|
|
|
+ if (empty($config)) {
|
|
|
+ $defaultConfig = dict::getDict('shMethod', $style);
|
|
|
+
|
|
|
+ $initData = array_merge([
|
|
|
+ 'shopId' => $shopId,
|
|
|
+ 'mainId' => $mainId,
|
|
|
+ 'style' => $style,
|
|
|
+ ], $defaultConfig);
|
|
|
+
|
|
|
+ $config = self::add($initData, true);
|
|
|
+ $config = $config->toArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关联说明项 xhPsExplain
|
|
|
+ $config['explains'] = PsExplainClass::getAllByCondition(
|
|
|
+ ['methodId' => $config['id']],
|
|
|
+ 'sort ASC, id ASC'
|
|
|
+ );
|
|
|
+
|
|
|
+ // 跑腿 style=2 时加载满减规则 xhPsReduceRule
|
|
|
+ if ($style == 2) {
|
|
|
+ $config['reduceRules'] = PsReduceRuleClass::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, 'mainId' => $mainId], true);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($config)) {
|
|
|
+ $config = self::getByCondition(['mainId' => $mainId, 'style' => $style], true);
|
|
|
+ }
|
|
|
+
|
|
|
+ $saveData = [
|
|
|
+ 'name' => $data['name'] ?? '',
|
|
|
+ '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;
|
|
|
+ $defaultConfig = dict::getDict('shMethod', $style);
|
|
|
+ if (!empty($defaultConfig['originName'])) {
|
|
|
+ $saveData['originName'] = $defaultConfig['originName'];
|
|
|
+ }
|
|
|
+ $config = self::add($saveData, true);
|
|
|
+ } else {
|
|
|
+ self::updateByCondition(['id' => $config->id], $saveData);
|
|
|
+ }
|
|
|
+
|
|
|
+ $methodId = $config->id;
|
|
|
+
|
|
|
+ // 1. 保存说明项 xhPsExplain
|
|
|
+ PsExplainClass::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)) {
|
|
|
+ PsExplainClass::batchAdd($explainRows);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 保存跑腿满减规则 xhPsReduceRule
|
|
|
+ PsReduceRuleClass::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)) {
|
|
|
+ PsReduceRuleClass::batchAdd($ruleRows);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $transaction->commit();
|
|
|
+ return true;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $transaction->rollBack();
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|