|
|
@@ -344,7 +344,7 @@ class PsMethodClass extends BaseClass
|
|
|
return false;
|
|
|
}
|
|
|
$current = date('Y_m_d');
|
|
|
- $key = 'hd_ps_custom_today_has_get_pack_cost_' . (int)$sendType . '_' . $current . '_' . $customId;
|
|
|
+ $key = 'hdCustom_hasGetPackCost:' . (int)$sendType . '_' . $current . ':' . $customId;
|
|
|
$has = Yii::$app->redis->executeCommand('GET', [$key]);
|
|
|
return (!empty($has) && $has == 1);
|
|
|
}
|
|
|
@@ -358,7 +358,7 @@ class PsMethodClass extends BaseClass
|
|
|
return false;
|
|
|
}
|
|
|
$current = date('Y_m_d');
|
|
|
- $key = 'hd_ps_custom_today_skip_pack_cost_' . (int)$sendType . '_' . $current . '_' . $customId;
|
|
|
+ $key = 'hdCustom_skipPackCost:' . (int)$sendType . '_' . $current . ':' . $customId;
|
|
|
$has = Yii::$app->redis->executeCommand('GET', [$key]);
|
|
|
return (!empty($has) && $has == 1);
|
|
|
}
|
|
|
@@ -380,7 +380,7 @@ class PsMethodClass extends BaseClass
|
|
|
return false;
|
|
|
}
|
|
|
$current = date('Y_m_d');
|
|
|
- $key = 'hd_ps_custom_today_has_get_send_cost_' . (int)$sendType . '_' . $current . '_' . $customId;
|
|
|
+ $key = 'hdCustom_hasGetSendCost:' . (int)$sendType . '_' . $current . ':' . $customId;
|
|
|
$has = Yii::$app->redis->executeCommand('GET', [$key]);
|
|
|
return (!empty($has) && $has == 1);
|
|
|
}
|
|
|
@@ -394,7 +394,7 @@ class PsMethodClass extends BaseClass
|
|
|
return false;
|
|
|
}
|
|
|
$current = date('Y_m_d');
|
|
|
- $key = 'hd_ps_custom_today_skip_send_cost_' . (int)$sendType . '_' . $current . '_' . $customId;
|
|
|
+ $key = 'hdCustom_skipSendCost:' . (int)$sendType . '_' . $current . ':' . $customId;
|
|
|
$has = Yii::$app->redis->executeCommand('GET', [$key]);
|
|
|
return (!empty($has) && $has == 1);
|
|
|
}
|
|
|
@@ -429,20 +429,20 @@ class PsMethodClass extends BaseClass
|
|
|
|
|
|
if (!self::hasSendFeeDecisionToday($customId, $style)) {
|
|
|
if ((float)$unMeetSendCost > 0) {
|
|
|
- $sendKey = 'hd_ps_custom_today_has_get_send_cost_' . $style . '_' . $current . '_' . $customId;
|
|
|
+ $sendKey = 'hdCustom_hasGetSendCost:' . $style . '_' . $current . ':' . $customId;
|
|
|
Yii::$app->redis->executeCommand('SETEX', [$sendKey, $ttl, '1']);
|
|
|
} else {
|
|
|
- $skipKey = 'hd_ps_custom_today_skip_send_cost_' . $style . '_' . $current . '_' . $customId;
|
|
|
+ $skipKey = 'hdCustom_skipSendCost:' . $style . '_' . $current . ':' . $customId;
|
|
|
Yii::$app->redis->executeCommand('SETEX', [$skipKey, $ttl, '1']);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!self::hasPackFeeDecisionToday($customId, $style)) {
|
|
|
if ((float)$packingFee > 0) {
|
|
|
- $packKey = 'hd_ps_custom_today_has_get_pack_cost_' . $style . '_' . $current . '_' . $customId;
|
|
|
+ $packKey = 'hdCustom_hasGetPackCost:' . $style . '_' . $current . ':' . $customId;
|
|
|
Yii::$app->redis->executeCommand('SETEX', [$packKey, $ttl, '1']);
|
|
|
} else {
|
|
|
- $skipKey = 'hd_ps_custom_today_skip_pack_cost_' . $style . '_' . $current . '_' . $customId;
|
|
|
+ $skipKey = 'hdCustom_skipPackCost:' . $style . '_' . $current . ':' . $customId;
|
|
|
Yii::$app->redis->executeCommand('SETEX', [$skipKey, $ttl, '1']);
|
|
|
}
|
|
|
}
|
|
|
@@ -462,15 +462,208 @@ class PsMethodClass extends BaseClass
|
|
|
$packingFee = (float)(is_object($order) ? ($order->packingFee ?? 0) : ($order['packingFee'] ?? 0));
|
|
|
$unMeetSendCost = 0;
|
|
|
if (!empty($orderSn)) {
|
|
|
- $unMeetSendKey = 'hd_ps_order_unmeet_send_' . $orderSn;
|
|
|
+ $unMeetSendKey = 'hd_order_unmeet_send:' . $orderSn;
|
|
|
$cached = Yii::$app->redis->executeCommand('GET', [$unMeetSendKey]);
|
|
|
if (!empty($cached)) {
|
|
|
$unMeetSendCost = (float)$cached;
|
|
|
}
|
|
|
}
|
|
|
+ // 无附加包装费/附加运费时不写入(避免满足门槛的订单误记 skip)
|
|
|
+ if ($packingFee <= 0 && $unMeetSendCost <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
self::recordDailyUnMeetFeeState($customId, $sendType, $packingFee, $unMeetSendCost);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 合并 hd + custom 为 calcFee / applyCustomHomeRule 使用的客户信息(与商城 getAllMethod 一致)
|
|
|
+ * @param mixed $hd 花店关系 xhHd
|
|
|
+ * @param mixed $custom 客户 xhCustom
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function buildCustomInfo($hd = null, $custom = null)
|
|
|
+ {
|
|
|
+ $customInfo = [];
|
|
|
+ if (!empty($hd)) {
|
|
|
+ if (is_array($hd)) {
|
|
|
+ $customInfo = $hd;
|
|
|
+ } elseif (is_object($hd) && method_exists($hd, 'toArray')) {
|
|
|
+ $customInfo = $hd->toArray();
|
|
|
+ } elseif (is_object($hd) && isset($hd->attributes)) {
|
|
|
+ $customInfo = $hd->attributes;
|
|
|
+ } else {
|
|
|
+ $customInfo = (array)$hd;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!empty($custom)) {
|
|
|
+ if (is_array($custom)) {
|
|
|
+ $customArr = $custom;
|
|
|
+ } elseif (is_object($custom) && method_exists($custom, 'toArray')) {
|
|
|
+ $customArr = $custom->toArray();
|
|
|
+ } elseif (is_object($custom) && isset($custom->attributes)) {
|
|
|
+ $customArr = $custom->attributes;
|
|
|
+ } else {
|
|
|
+ $customArr = (array)$custom;
|
|
|
+ }
|
|
|
+ $customInfo = array_merge($customInfo, $customArr);
|
|
|
+ }
|
|
|
+ return $customInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对配送方式列表应用 style=0 客户单独送货上门规则
|
|
|
+ * @param array $configs getAllConfigs 结果
|
|
|
+ * @param array $customInfo buildCustomInfo 结果
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function applyCustomHomeRuleToConfigs(array $configs, array $customInfo)
|
|
|
+ {
|
|
|
+ if (empty($customInfo)) {
|
|
|
+ return $configs;
|
|
|
+ }
|
|
|
+ foreach ($configs as &$config) {
|
|
|
+ if ((int)($config['style'] ?? 0) === 0) {
|
|
|
+ $config = self::applyCustomHomeRule($config, $customInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ unset($config);
|
|
|
+ return $configs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 标记当日附加费是否已决策(供商城 getAllMethod 下发锁定态)
|
|
|
+ * @param array $configs 配送方式列表
|
|
|
+ * @param int $customId 花店客户 id
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function applyDailyFeeLockFlags(array $configs, $customId)
|
|
|
+ {
|
|
|
+ $customId = (int)$customId;
|
|
|
+ if ($customId <= 0) {
|
|
|
+ return $configs;
|
|
|
+ }
|
|
|
+ foreach ($configs as &$config) {
|
|
|
+ $style = (int)($config['style'] ?? 0);
|
|
|
+ $unMeet = (int)($config['unMeet'] ?? 0);
|
|
|
+ $hasSendDecision = self::hasSendFeeDecisionToday($customId, $style);
|
|
|
+ $hasPackDecision = self::hasPackFeeDecisionToday($customId, $style);
|
|
|
+ $config['todaySendFeeLocked'] = $hasSendDecision ? 1 : 0;
|
|
|
+ $config['todayPackFeeLocked'] = $hasPackDecision ? 1 : 0;
|
|
|
+ if ($unMeet === 0 && $hasSendDecision) {
|
|
|
+ $config['minAmount'] = 0;
|
|
|
+ $config['minNum'] = 0;
|
|
|
+ $config['unMeetFee'] = 0;
|
|
|
+ } elseif ($unMeet === 2 && $hasPackDecision) {
|
|
|
+ $config['minAmount'] = 0;
|
|
|
+ $config['minNum'] = 0;
|
|
|
+ $config['unMeetFee'] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ unset($config);
|
|
|
+ return $configs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商城混合结算:统一入口调用 calcFee(预览与下单共用)
|
|
|
+ */
|
|
|
+ public static function calcFeeForMallContext($shopId, $mainId, $sendType, $itemTotalAmount, $bigNum, $customId = 0, $customInfo = [])
|
|
|
+ {
|
|
|
+ return self::calcFee($shopId, $mainId, $sendType, $itemTotalAmount, $bigNum, $customId, is_array($customInfo) ? $customInfo : []);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商城结算页预览未达门槛附加运费/包装费(与 create-mix-order 内 calcFee 同源)
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function previewMixUnMeetFee($shopId, $mainId, $sendType, $itemTotalAmount, $bigNum, $customId = 0, $customInfo = [])
|
|
|
+ {
|
|
|
+ $costs = self::calcFeeForMallContext($shopId, $mainId, $sendType, $itemTotalAmount, $bigNum, $customId, $customInfo);
|
|
|
+ $style = (int)$sendType;
|
|
|
+ $config = self::getConfig($shopId, $mainId, $style);
|
|
|
+ if (empty($config)) {
|
|
|
+ return [
|
|
|
+ 'sendCost' => 0,
|
|
|
+ 'packCost' => 0,
|
|
|
+ 'unMeetSendCost' => 0,
|
|
|
+ 'packingFee' => 0,
|
|
|
+ 'belowMinimum' => false,
|
|
|
+ 'canOrder' => true,
|
|
|
+ 'tip' => '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $config = self::mergeCustomHomeRule($config, $style, $customInfo);
|
|
|
+ $belowMinimum = self::isBelowMinimum($itemTotalAmount, $bigNum, $config);
|
|
|
+ $tip = '';
|
|
|
+ $canOrder = true;
|
|
|
+
|
|
|
+ if ($belowMinimum) {
|
|
|
+ $unMeet = (int)($config['unMeet'] ?? 0);
|
|
|
+ $unMeetFee = (float)($config['unMeetFee'] ?? 0);
|
|
|
+ $conditionText = self::buildUnMeetConditionText($config);
|
|
|
+ if ($unMeet === 1) {
|
|
|
+ $canOrder = false;
|
|
|
+ $tip = $conditionText !== '' ? "订单不满{$conditionText},不能下单" : '未达到最低消费,不能下单';
|
|
|
+ } elseif (($unMeet === 0 || $unMeet === 2) && $unMeetFee > 0) {
|
|
|
+ $locked = ($unMeet === 0 && self::hasSendFeeDecisionToday($customId, $style))
|
|
|
+ || ($unMeet === 2 && self::hasPackFeeDecisionToday($customId, $style));
|
|
|
+ if (!$locked) {
|
|
|
+ $feeLabel = $unMeet === 2 ? '包装费' : '运费';
|
|
|
+ if ($conditionText !== '') {
|
|
|
+ $tip = "订单不满{$conditionText},加{$feeLabel}{$unMeetFee}元";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $checkErr = self::checkLimit(
|
|
|
+ $shopId,
|
|
|
+ $mainId,
|
|
|
+ $sendType,
|
|
|
+ $itemTotalAmount,
|
|
|
+ $bigNum,
|
|
|
+ $costs['packCost'],
|
|
|
+ $costs['sendCost'],
|
|
|
+ $customId,
|
|
|
+ $customInfo
|
|
|
+ );
|
|
|
+ if ($checkErr !== '') {
|
|
|
+ $canOrder = false;
|
|
|
+ $tip = $checkErr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'sendCost' => (float)$costs['sendCost'],
|
|
|
+ 'packCost' => (float)$costs['packCost'],
|
|
|
+ 'unMeetSendCost' => (float)$costs['sendCost'],
|
|
|
+ 'packingFee' => (float)$costs['packCost'],
|
|
|
+ 'belowMinimum' => $belowMinimum,
|
|
|
+ 'canOrder' => $canOrder,
|
|
|
+ 'tip' => $tip,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 混合结算页:一次预览各购买方式未达门槛附加费(不含 style=2 跑腿距离运费)
|
|
|
+ * @return array 键为 style 字符串,如 ['0' => [...], '1' => [...]]
|
|
|
+ */
|
|
|
+ public static function previewMixUnMeetFeeBatch($shopId, $mainId, $itemTotalAmount, $bigNum, $customId = 0, $hd = null, $custom = null)
|
|
|
+ {
|
|
|
+ $customInfo = self::buildCustomInfo($hd, $custom);
|
|
|
+ $fees = [];
|
|
|
+ for ($style = 0; $style <= 4; $style++) {
|
|
|
+ $fees[(string)$style] = self::previewMixUnMeetFee(
|
|
|
+ $shopId,
|
|
|
+ $mainId,
|
|
|
+ $style,
|
|
|
+ $itemTotalAmount,
|
|
|
+ $bigNum,
|
|
|
+ (int)$customId,
|
|
|
+ $customInfo
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return $fees;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 计算未达最低消费时应写入订单的附加运费/包装费
|
|
|
* @return array ['sendCost' => 附加运费, 'packCost' => 附加包装费]
|