add tools request libs, migrate for hyperf framework

This commit is contained in:
2025-11-10 16:48:19 +08:00
parent 5b3b0c70f2
commit 08bb2af2c3
11 changed files with 749 additions and 0 deletions
@@ -0,0 +1,75 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use Hyperf\Collection\LazyCollection;
class CompanyRequest extends HttpClient
{
public static function one(string $id): array
{
return parent::get("api/ext/companies/$id");
}
public static function find(string $name)
{
$stores = self::all();
$stores = $stores->filter(function($el) use ($name){
return strtolower($el['label']) == $name ? $el : null;
});
if($stores->isEmpty()){
throw new \InvalidArgumentException("tools 中未找到 name 为 $name 的公司信息,请确认 tools 系统中已创建该公司的记录!");
}
return $stores->first();
}
public static function all(int $page = 1, int $size = 500, $query = []): LazyCollection
{
$path = 'api/ext/companies';
$collection = new LazyCollection();
static::fetch($collection, $path, $page, $size, $query);
if($collection->isEmpty()){
throw new \InvalidArgumentException("获取的远程 tools 公司列表为空,请检查 token 配置");
}
return $collection;
}
protected static function fetch(LazyCollection &$collection, $path, $page, $size, $query)
{
$init_query = [
'page' => $page,
'size' => $size
];
$query = !empty($query) ? array_merge($init_query, $query) : $init_query;
$response = static::get($path, $query);
if($response['data']){
$collection = $collection->merge($response['data']);
}
if($response['page'] < $response['total_page']){
self::fetch($collection, $path, $page + 1, $size, $query);
}
}
public static function update(int $id, array $data): array
{
$path = "/api/ext/companies/$id";
return static::put($path, [], $data);
}
}
@@ -0,0 +1,32 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use GuzzleHttp\Client;
use Hyperf\Collection\LazyCollection;
class KpiRequest extends HttpClient
{
const BATCH_SIZE = 20;
public static function find(int $store_id, string $date): mixed
{
$path = "/api/ext/kpi";
$response = static::get($path, ['store' => $store_id, 'from' => $date, 'to' => $date]);
return isset($response['data']) && !empty($response['data']) ? $response['data']['0'] : null;
}
public static function add(array $data): array
{
$path = "/api/ext/kpi";
return static::post($path, [], $data);
}
public static function update(array $data): array
{
$path = "/api/ext/kpi";
return static::put($path, [], $data);
}
}
@@ -0,0 +1,61 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use GuzzleHttp\Client;
use Hyperf\Collection\LazyCollection;
class OrderRequest extends HttpClient
{
const BATCH_SIZE = 50;
public static function find(int $id): array
{
$path = "/api/ext/orders/$id";
return static::get($path);
}
public static function findByPlatformOrderId(int $t_store_id, string $platform_order_id): array|bool
{
$path = "/api/ext/orders";
$query = [
'store' => $t_store_id,
'platformOrderId' => $platform_order_id,
];
$response = static::get($path, $query);
if(isset($response['data']) && is_array($response['data']) && count($response['data']) == 1) {
return $response['data']['0'];
}
return false;
}
public static function all(array $query):array
{
$path = "/api/ext/orders";
return static::get($path, $query);
}
public static function add(array $data): array
{
$path = "/api/ext/orders";
return static::post($path, [], $data);
}
public static function update(array $data): array
{
$path = "/api/ext/orders";
return static::put($path, [], $data);
}
public static function queryByStorePlatformOrderIds(int $t_store_id, array $ids) : array
{
$path = "/api/ext/orders/batch-query-by-platform-order-ids";
return static::post($path, [], ['store' => $t_store_id, 'platformOrderIds' => $ids]);
}
}
@@ -0,0 +1,52 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
class ProductRequest extends HttpClient
{
const BATCH_SIZE = 50;
public static function find(int $id): array
{
$path = "/api/ext/products/$id";
return static::get($path);
}
public static function findByPlatformSkuId(int $tools_store_id, string $platformSkuId): array | null
{
$path = "/api/ext/products";
$response = static::get($path, ['store' => $tools_store_id, 'platformSkuId' => $platformSkuId]);
if(0 === $response['total'] || '0' === $response['total'] ){
return null;
}
if(1 < $response['total']){
throw new \Exception('参数有误,产品匹配到多个结果!');
}
return $response['data']['0'];
}
public static function add(array $data): array
{
$path = "/api/ext/products";
return static::post($path, [], $data);
}
public static function update(array $data): array
{
$path = "/api/ext/products/batch";
return static::put($path, [], $data);
}
public static function list(array $query) : array
{
$path = "/api/ext/products";
return static::get($path, $query);
}
}
@@ -0,0 +1,52 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use Hyperf\Collection\LazyCollection;
class RefundRequest extends HttpClient
{
const BATCH_SIZE = 50;
public static function find(int $id): array
{
$path = "/api/ext/refunds/$id";
return static::get($path);
}
public static function findByOrder(int $t_store_id, int $t_order_id): array|bool
{
$path = "/api/ext/refunds";
$query = [
'store' => $t_store_id,
'order' => $t_order_id,
];
$response = static::get($path, $query);
if(isset($response['data']) && is_array($response['data']) && count($response['data']) == 1) {
return $response['data']['0'];
}
return false;
}
public static function add(array $data): array
{
$path = "/api/ext/refunds";
return static::post($path, [], $data);
}
public static function update(array $data): array
{
$path = "/api/ext/refunds";
return static::put($path, [], $data);
}
public static function queryByStorePlatformOrderIds(int $t_store_id, array $ids) : array
{
$path = "/api/ext/refunds/batch-query-by-ref-order-ids";
return static::post($path, [], ['store' => $t_store_id, 'orderIds' => $ids]);
}
}
@@ -0,0 +1,78 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use Hyperf\Collection\LazyCollection;
class StoreRequest extends HttpClient
{
public static function one(string $id): array
{
return parent::get("api/ext/stores/$id");
}
public static function find(string $name, string $platform)
{
$stores = self::all();
$stores = $stores->filter(function($el) use ($name, $platform){
return $el['platform']['name'] == $platform && strtolower($el['label']) == $name ? $el : null;
});
if($stores->isEmpty()){
throw new \InvalidArgumentException("tools 中未找到平台为 $platform, name 为 $name 的店铺信息,请确认 tools 系统中已创建该平台和店铺的记录!");
}
return $stores->first();
}
public static function all(int $page = 1, int $size = 500, $query = []): LazyCollection
{
$path = 'api/ext/stores';
$collection = new LazyCollection();
static::fetch($collection, $path, $page, $size, $query);
if($collection->isEmpty()){
throw new \InvalidArgumentException("获取的远程 tools 商店列表为空,请检查 token 配置");
}
return $collection;
}
public static function orderNone(array $query) {
$path = 'api/ext/stores/order-none';
return static::get($path, $query);
}
protected static function fetch(LazyCollection &$collection, $path, $page, $size, $query)
{
$init_query = [
'page' => $page,
'size' => $size
];
$query = !empty($query) ? array_merge($init_query, $query) : $init_query;
$response = static::get($path, $query);
if($response['data']){
$collection = $collection->merge($response['data']);
}
if($response['page'] < $response['total_page']){
self::fetch($collection, $path, $page + 1, $size, $query);
}
}
public static function update(int $id, array $data): array
{
$path = "/api/ext/stores/$id";
return static::put($path, [], $data);
}
}
@@ -0,0 +1,86 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
class WarehouseInventoryRequest extends HttpClient
{
public static function find(int $id): array
{
$path = "/api/ext/warehouse-inventory/$id";
return static::get($path);
}
/**
* 根据仓库物品条件查询库存记录
*
* @param string $sku SKU编号
* @param string $barcode 条形码
* @param int $companyId 公司ID
* @param int $warehouseId 仓库ID
* @param string $warehouseSubId 子仓库ID
* @param int $inventoryType 库存类型,默认为1(良品库存)
* @return array|null 返回库存记录或null
*/
public static function findByWarehouseItem(
string $sku,
string $barcode,
int $companyId,
int $warehouseId,
string $warehouseSubId,
int $inventoryType = 1
): array|null {
$path = "/api/ext/warehouse-inventory";
$query = [
'sku' => $sku,
'barcode' => $barcode,
'company_id' => $companyId,
'warehouse_id' => $warehouseId,
'warehouse_sub_id' => $warehouseSubId,
'inventory_type' => $inventoryType,
];
$response = static::get($path, $query);
if (isset($response['data']) && is_array($response['data']) && count($response['data']) == 1) {
return $response['data'][0];
}
return null;
}
public static function add(array $data): array
{
$path = "/api/ext/warehouse-inventory";
return static::post($path, [], $data);
}
public static function update(int $id, array $data): array
{
$path = "/api/ext/warehouse-inventory/$id";
return static::put($path, [], $data);
}
public static function list(array $query) : array
{
$path = "/api/ext/warehouse-inventory";
return static::get($path, $query);
}
public static function remove(int $id): array
{
$path = "/api/ext/warehouse-inventory/$id";
return static::delete($path);
}
public static function updateHistory(array $data): array
{
$path = "/api/ext/warehouse-inventory-history";
return static::post($path, [], $data);
}
}