273 行
8.5 KiB
PHP
273 行
8.5 KiB
PHP
<?php
|
|
namespace Retail\Controller;
|
|
use Think\Controller;
|
|
|
|
/**
|
|
* 订单同步测试控制器
|
|
*/
|
|
class TestSyncController extends Controller {
|
|
|
|
/**
|
|
* 测试页面
|
|
*/
|
|
public function index() {
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* 测试获取可同步订单API
|
|
*/
|
|
public function testGetSyncableOrders() {
|
|
try {
|
|
$params = [
|
|
'start_date' => I('post.start_date', date('Y-m-d 00:00:00', strtotime('-7 days'))),
|
|
'end_date' => I('post.end_date', date('Y-m-d 23:59:59')),
|
|
'order_status' => I('post.order_status', ''),
|
|
'limit' => I('post.limit', 10)
|
|
];
|
|
|
|
// 模拟调用RiderOrderController的getSyncableOrders方法
|
|
$controller = new RiderOrderController();
|
|
$result = $controller->getSyncableOrders();
|
|
|
|
$this->ajaxReturn([
|
|
'status' => 'success',
|
|
'message' => '获取可同步订单成功',
|
|
'data' => $result,
|
|
'test_params' => $params
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$this->ajaxReturn([
|
|
'status' => 'error',
|
|
'message' => '测试失败: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试单个订单同步API
|
|
*/
|
|
public function testSyncSingleOrder() {
|
|
try {
|
|
$orderId = I('post.order_id', 0);
|
|
|
|
if (!$orderId) {
|
|
$this->ajaxReturn([
|
|
'status' => 'error',
|
|
'message' => '请提供订单ID'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 模拟调用RiderOrderController的syncOrder方法
|
|
$controller = new RiderOrderController();
|
|
$_POST['order_id'] = $orderId;
|
|
$result = $controller->syncOrder();
|
|
|
|
$this->ajaxReturn([
|
|
'status' => 'success',
|
|
'message' => '单个订单同步测试完成',
|
|
'data' => $result,
|
|
'test_order_id' => $orderId
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$this->ajaxReturn([
|
|
'status' => 'error',
|
|
'message' => '测试失败: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试批量订单同步API
|
|
*/
|
|
public function testBatchSyncOrders() {
|
|
try {
|
|
$orderIds = I('post.order_ids', []);
|
|
|
|
if (empty($orderIds)) {
|
|
$this->ajaxReturn([
|
|
'status' => 'error',
|
|
'message' => '请提供订单ID列表'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 模拟调用RiderOrderController的batchSyncOrders方法
|
|
$controller = new RiderOrderController();
|
|
$_POST['order_ids'] = $orderIds;
|
|
$result = $controller->batchSyncOrders();
|
|
|
|
$this->ajaxReturn([
|
|
'status' => 'success',
|
|
'message' => '批量订单同步测试完成',
|
|
'data' => $result,
|
|
'test_order_count' => count($orderIds)
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$this->ajaxReturn([
|
|
'status' => 'error',
|
|
'message' => '测试失败: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试同步统计API
|
|
*/
|
|
public function testSyncStatistics() {
|
|
try {
|
|
// 模拟调用RiderOrderController的getSyncStatistics方法
|
|
$controller = new RiderOrderController();
|
|
$result = $controller->getSyncStatistics();
|
|
|
|
$this->ajaxReturn([
|
|
'status' => 'success',
|
|
'message' => '同步统计测试完成',
|
|
'data' => $result
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$this->ajaxReturn([
|
|
'status' => 'error',
|
|
'message' => '测试失败: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 综合测试所有同步功能
|
|
*/
|
|
public function testAllSyncFunctions() {
|
|
$results = [];
|
|
|
|
try {
|
|
// 1. 测试获取同步统计
|
|
$results['sync_statistics'] = $this->runSyncStatisticsTest();
|
|
|
|
// 2. 测试获取可同步订单
|
|
$results['syncable_orders'] = $this->runSyncableOrdersTest();
|
|
|
|
// 3. 测试单个订单同步(如果有可同步订单)
|
|
if ($results['syncable_orders']['success'] && !empty($results['syncable_orders']['data'])) {
|
|
$firstOrderId = $results['syncable_orders']['data'][0]['bill_id'];
|
|
$results['single_sync'] = $this->runSingleSyncTest($firstOrderId);
|
|
}
|
|
|
|
// 4. 测试批量订单同步(如果有多个可同步订单)
|
|
if ($results['syncable_orders']['success'] && count($results['syncable_orders']['data']) > 1) {
|
|
$orderIds = array_slice(array_column($results['syncable_orders']['data'], 'bill_id'), 0, 3);
|
|
$results['batch_sync'] = $this->runBatchSyncTest($orderIds);
|
|
}
|
|
|
|
$this->ajaxReturn([
|
|
'status' => 'success',
|
|
'message' => '综合测试完成',
|
|
'data' => $results
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$this->ajaxReturn([
|
|
'status' => 'error',
|
|
'message' => '综合测试失败: ' . $e->getMessage(),
|
|
'partial_results' => $results
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 运行同步统计测试
|
|
*/
|
|
private function runSyncStatisticsTest() {
|
|
try {
|
|
$syncService = new \OrderSyncService();
|
|
$stats = $syncService->getSyncStatistics();
|
|
|
|
return [
|
|
'success' => true,
|
|
'data' => $stats,
|
|
'message' => '同步统计获取成功'
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'success' => false,
|
|
'error' => $e->getMessage(),
|
|
'message' => '同步统计获取失败'
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 运行可同步订单测试
|
|
*/
|
|
private function runSyncableOrdersTest() {
|
|
try {
|
|
$syncService = new \OrderSyncService();
|
|
$orders = $syncService->getSyncableOrders([
|
|
'start_date' => date('Y-m-d 00:00:00', strtotime('-7 days')),
|
|
'end_date' => date('Y-m-d 23:59:59'),
|
|
'limit' => 5
|
|
]);
|
|
|
|
return [
|
|
'success' => true,
|
|
'data' => $orders,
|
|
'message' => '可同步订单获取成功,共 ' . count($orders) . ' 个'
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'success' => false,
|
|
'error' => $e->getMessage(),
|
|
'message' => '可同步订单获取失败'
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 运行单个订单同步测试
|
|
*/
|
|
private function runSingleSyncTest($orderId) {
|
|
try {
|
|
$syncService = new \OrderSyncService();
|
|
$result = $syncService->syncSingleOrder($orderId);
|
|
|
|
return [
|
|
'success' => $result['success'],
|
|
'data' => $result,
|
|
'message' => $result['success'] ? '单个订单同步成功' : '单个订单同步失败: ' . $result['message']
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'success' => false,
|
|
'error' => $e->getMessage(),
|
|
'message' => '单个订单同步测试失败'
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 运行批量订单同步测试
|
|
*/
|
|
private function runBatchSyncTest($orderIds) {
|
|
try {
|
|
$syncService = new \OrderSyncService();
|
|
$result = $syncService->batchSyncOrders($orderIds);
|
|
|
|
return [
|
|
'success' => $result['success_count'] > 0,
|
|
'data' => $result,
|
|
'message' => "批量同步完成,成功 {$result['success_count']} 个,失败 {$result['failed_count']} 个"
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'success' => false,
|
|
'error' => $e->getMessage(),
|
|
'message' => '批量订单同步测试失败'
|
|
];
|
|
}
|
|
}
|
|
}
|
|
?>
|