280 行
11 KiB
PHP
280 行
11 KiB
PHP
<?php
|
||
namespace Retail\Controller;
|
||
|
||
/**
|
||
* 骑手配置管理控制器
|
||
* Class RiderConfigController
|
||
* @package Retail\Controller
|
||
*/
|
||
class RiderConfigController extends BaseController
|
||
{
|
||
/**
|
||
* 配置管理页面
|
||
*/
|
||
public function index()
|
||
{
|
||
$this->display('Rider/config');
|
||
}
|
||
|
||
/**
|
||
* 获取配置信息
|
||
*/
|
||
public function getConfig()
|
||
{
|
||
$configModel = M('RiderConfig');
|
||
$config = $configModel->where(array('merchant_id' => $this->merchant_id))->find();
|
||
|
||
if (!$config) {
|
||
// 创建默认配置
|
||
$defaultConfig = array(
|
||
'merchant_id' => $this->merchant_id,
|
||
'auto_sync_enabled' => 0, // 默认关闭自动同步
|
||
'delivery_fee_type' => 1, // 1:按距离计算 2:固定费用 3:阶梯计费
|
||
'base_fee' => 5.00, // 基础配送费
|
||
'distance_fee' => 1.50, // 每公里费用
|
||
'free_distance' => 3, // 免费配送距离(公里)
|
||
'max_distance' => 20, // 最大配送距离(公里)
|
||
'time_fee_enabled' => 0, // 是否启用时间加价
|
||
'night_fee_rate' => 0.5, // 夜间加价比例
|
||
'night_start_time' => '22:00', // 夜间开始时间
|
||
'night_end_time' => '06:00', // 夜间结束时间
|
||
'weather_fee_enabled' => 0, // 是否启用恶劣天气加价
|
||
'weather_fee_rate' => 1.0, // 恶劣天气加价
|
||
'urgent_fee_enabled' => 1, // 是否启用加急费
|
||
'urgent_fee' => 3.00, // 加急费用
|
||
'min_order_amount' => 20.00, // 最小起送金额
|
||
'packaging_fee' => 2.00, // 包装费
|
||
'created_time' => time(),
|
||
'updated_time' => time()
|
||
);
|
||
|
||
$configModel->add($defaultConfig);
|
||
$config = $defaultConfig;
|
||
}
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '获取成功',
|
||
'data' => $config
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 保存配置
|
||
*/
|
||
public function saveConfig()
|
||
{
|
||
$data = I('post.');
|
||
$configModel = M('RiderConfig');
|
||
|
||
// 验证数据
|
||
if (!isset($data['auto_sync_enabled']) || !isset($data['delivery_fee_type'])) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '参数不完整'));
|
||
}
|
||
|
||
// 数据处理
|
||
$saveData = array(
|
||
'auto_sync_enabled' => intval($data['auto_sync_enabled']),
|
||
'delivery_fee_type' => intval($data['delivery_fee_type']),
|
||
'base_fee' => floatval($data['base_fee']),
|
||
'distance_fee' => floatval($data['distance_fee']),
|
||
'free_distance' => floatval($data['free_distance']),
|
||
'max_distance' => floatval($data['max_distance']),
|
||
'time_fee_enabled' => intval($data['time_fee_enabled']),
|
||
'night_fee_rate' => floatval($data['night_fee_rate']),
|
||
'night_start_time' => $data['night_start_time'],
|
||
'night_end_time' => $data['night_end_time'],
|
||
'weather_fee_enabled' => intval($data['weather_fee_enabled']),
|
||
'weather_fee_rate' => floatval($data['weather_fee_rate']),
|
||
'urgent_fee_enabled' => intval($data['urgent_fee_enabled']),
|
||
'urgent_fee' => floatval($data['urgent_fee']),
|
||
'min_order_amount' => floatval($data['min_order_amount']),
|
||
'packaging_fee' => floatval($data['packaging_fee']),
|
||
'updated_time' => time()
|
||
);
|
||
|
||
$where = array('merchant_id' => $this->merchant_id);
|
||
$exists = $configModel->where($where)->find();
|
||
|
||
if ($exists) {
|
||
$result = $configModel->where($where)->save($saveData);
|
||
} else {
|
||
$saveData['merchant_id'] = $this->merchant_id;
|
||
$saveData['created_time'] = time();
|
||
$result = $configModel->add($saveData);
|
||
}
|
||
|
||
if ($result !== false) {
|
||
$this->ajaxReturn(array('code' => 1, 'msg' => '保存成功'));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '保存失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取配送费用计算规则
|
||
*/
|
||
public function getFeeRules()
|
||
{
|
||
$configModel = M('RiderConfig');
|
||
$config = $configModel->where(array('merchant_id' => $this->merchant_id))->find();
|
||
|
||
if (!$config) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '请先配置基础设置'));
|
||
}
|
||
|
||
$rules = array(
|
||
'delivery_fee_type' => $config['delivery_fee_type'],
|
||
'base_fee' => $config['base_fee'],
|
||
'distance_fee' => $config['distance_fee'],
|
||
'free_distance' => $config['free_distance'],
|
||
'max_distance' => $config['max_distance'],
|
||
'time_fee_enabled' => $config['time_fee_enabled'],
|
||
'night_fee_rate' => $config['night_fee_rate'],
|
||
'night_start_time' => $config['night_start_time'],
|
||
'night_end_time' => $config['night_end_time'],
|
||
'weather_fee_enabled' => $config['weather_fee_enabled'],
|
||
'weather_fee_rate' => $config['weather_fee_rate'],
|
||
'urgent_fee_enabled' => $config['urgent_fee_enabled'],
|
||
'urgent_fee' => $config['urgent_fee'],
|
||
'min_order_amount' => $config['min_order_amount'],
|
||
'packaging_fee' => $config['packaging_fee']
|
||
);
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '获取成功',
|
||
'data' => $rules
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 计算配送费用
|
||
*/
|
||
public function calculateDeliveryFee()
|
||
{
|
||
$distance = floatval(I('post.distance')); // 配送距离(公里)
|
||
$orderAmount = floatval(I('post.order_amount')); // 订单金额
|
||
$isUrgent = intval(I('post.is_urgent', 0)); // 是否加急
|
||
$isNight = intval(I('post.is_night', 0)); // 是否夜间
|
||
$isBadWeather = intval(I('post.is_bad_weather', 0)); // 是否恶劣天气
|
||
|
||
$configModel = M('RiderConfig');
|
||
$config = $configModel->where(array('merchant_id' => $this->merchant_id))->find();
|
||
|
||
if (!$config) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '请先配置基础设置'));
|
||
}
|
||
|
||
// 检查最大配送距离
|
||
if ($distance > $config['max_distance']) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 0,
|
||
'msg' => '超出最大配送距离(' . $config['max_distance'] . '公里)'
|
||
));
|
||
}
|
||
|
||
// 检查最小起送金额
|
||
if ($orderAmount < $config['min_order_amount']) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 0,
|
||
'msg' => '未达到最小起送金额(' . $config['min_order_amount'] . '元)'
|
||
));
|
||
}
|
||
|
||
$deliveryFee = 0;
|
||
$feeDetails = array();
|
||
|
||
// 基础配送费计算
|
||
switch ($config['delivery_fee_type']) {
|
||
case 1: // 按距离计算
|
||
if ($distance <= $config['free_distance']) {
|
||
$deliveryFee = $config['base_fee'];
|
||
$feeDetails[] = '基础费用: ' . $config['base_fee'] . '元';
|
||
} else {
|
||
$extraDistance = $distance - $config['free_distance'];
|
||
$distanceFee = $extraDistance * $config['distance_fee'];
|
||
$deliveryFee = $config['base_fee'] + $distanceFee;
|
||
$feeDetails[] = '基础费用: ' . $config['base_fee'] . '元';
|
||
$feeDetails[] = '距离费用: ' . $extraDistance . '公里 × ' . $config['distance_fee'] . '元 = ' . $distanceFee . '元';
|
||
}
|
||
break;
|
||
case 2: // 固定费用
|
||
$deliveryFee = $config['base_fee'];
|
||
$feeDetails[] = '固定配送费: ' . $config['base_fee'] . '元';
|
||
break;
|
||
case 3: // 阶梯计费
|
||
// 可以根据需要实现更复杂的阶梯计费逻辑
|
||
$deliveryFee = $config['base_fee'];
|
||
$feeDetails[] = '阶梯计费: ' . $config['base_fee'] . '元';
|
||
break;
|
||
}
|
||
|
||
// 包装费
|
||
$deliveryFee += $config['packaging_fee'];
|
||
$feeDetails[] = '包装费: ' . $config['packaging_fee'] . '元';
|
||
|
||
// 加急费
|
||
if ($isUrgent && $config['urgent_fee_enabled']) {
|
||
$deliveryFee += $config['urgent_fee'];
|
||
$feeDetails[] = '加急费: ' . $config['urgent_fee'] . '元';
|
||
}
|
||
|
||
// 夜间加价
|
||
if ($isNight && $config['time_fee_enabled']) {
|
||
$nightFee = $deliveryFee * $config['night_fee_rate'];
|
||
$deliveryFee += $nightFee;
|
||
$feeDetails[] = '夜间加价: ' . ($config['night_fee_rate'] * 100) . '% = ' . $nightFee . '元';
|
||
}
|
||
|
||
// 恶劣天气加价
|
||
if ($isBadWeather && $config['weather_fee_enabled']) {
|
||
$weatherFee = $config['weather_fee_rate'];
|
||
$deliveryFee += $weatherFee;
|
||
$feeDetails[] = '恶劣天气加价: ' . $weatherFee . '元';
|
||
}
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '计算成功',
|
||
'data' => array(
|
||
'delivery_fee' => round($deliveryFee, 2),
|
||
'fee_details' => $feeDetails,
|
||
'distance' => $distance,
|
||
'order_amount' => $orderAmount
|
||
)
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 测试配送费用计算
|
||
*/
|
||
public function testFeeCalculation()
|
||
{
|
||
$testCases = array(
|
||
array('distance' => 2, 'order_amount' => 50, 'is_urgent' => 0, 'is_night' => 0, 'is_bad_weather' => 0),
|
||
array('distance' => 5, 'order_amount' => 80, 'is_urgent' => 1, 'is_night' => 0, 'is_bad_weather' => 0),
|
||
array('distance' => 8, 'order_amount' => 120, 'is_urgent' => 0, 'is_night' => 1, 'is_bad_weather' => 0),
|
||
array('distance' => 10, 'order_amount' => 200, 'is_urgent' => 1, 'is_night' => 1, 'is_bad_weather' => 1),
|
||
);
|
||
|
||
$results = array();
|
||
foreach ($testCases as $case) {
|
||
$_POST = $case;
|
||
ob_start();
|
||
$this->calculateDeliveryFee();
|
||
$output = ob_get_clean();
|
||
$result = json_decode($output, true);
|
||
$results[] = array(
|
||
'case' => $case,
|
||
'result' => $result
|
||
);
|
||
}
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '测试完成',
|
||
'data' => $results
|
||
));
|
||
}
|
||
} |