ai_analysis/server.php

84 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwd2QiOiI2ODI5N2FjYzUwZDczYWQzZmNmMTNlM2Y4MWYxMWQyZSIsImlzcyI6InRwb2ludC5hZ3JpbWVkaWEuY24iLCJhdWQiOiJ0cG9pbnQuYWdyaW1lZGlhLmNuIiwiaWF0IjoxNzgwODk3Mjk3LCJuYmYiOjE3ODA4OTcyOTcsImV4cCI6MTc4MzQwMjg5NywianRpIjp7ImlkIjo0LCJ0eXBlIjoiYWRtaW4ifX0.G2yB38bYkf0b37kpAbQViDuXY4QHeRjm99WNXjSh4L0';
$server = Server::builder()
->setServerInfo('Merchant MCP', '1.0.0')
->addTool(
handler: function () use ($token): array {
$client = new Client([
'timeout' => 10,
]);
$response = $client->get(
'https://tpoint.agrimedia.cn/adminapi/offline/list',
[
'headers' => [
'accept' => 'application/json',
'authori-zation' => 'Bearer ' . $token,
],
'query' => [
'status' => -1,
'field_key' => 'all',
'page' => 1,
'limit' => 100,
'time' => '2026/06/23-2026/06/23',
],
]
);
$data = json_decode(
(string) $response->getBody(),
true
);
$orders = $data['data']['list'] ?? [];
$activeMerchants = [];
$successOrderCount = 0;
$totalOrderAmount = 0.0;
$totalRealPay = 0.0;
foreach ($orders as $order) {
$merchantId = (int) ($order['merchant_id'] ?? 0);
if ($merchantId > 0) {
$activeMerchants[$merchantId] = true;
}
/*
* 这里暂时按 status = 1 作为成功订单。
* 后面再确认你项目的真实状态含义。
*/
if ((int) ($order['status'] ?? -1) === 1) {
$successOrderCount++;
}
$totalOrderAmount += (float) ($order['order_price'] ?? 0);
$totalRealPay += (float) ($order['real_pay'] ?? 0);
}
return [
'orderCount' => count($orders),
'activeMerchantCount' => count($activeMerchants),
'successOrderCount' => $successOrderCount,
'totalOrderAmount' => round($totalOrderAmount, 2),
'totalRealPay' => round($totalRealPay, 2),
];
},
name: 'get_order_summary',
description: '获取订单总数、活跃商户数、成功订单数和金额统计'
)
->build();
$transport = new StdioTransport();
$server->run($transport);