Files
medical-mall/services/admin/productService.uts

110 lines
2.9 KiB
Plaintext

import { rpcOrNull, rpcOrValue } from '@/services/analytics/rpc.uts'
export type AdminProduct = {
id: string
name: string
image: string
price: number
stock: number
sales: number
status: number
created_at: string
category_name: string
}
export type ProductPageResult = {
total: number
items: Array<AdminProduct>
}
/**
* 分页获取商品列表
*/
export async function fetchAdminProductPage(
page: number,
pageSize: number,
filters: {
name?: string,
status?: number,
categoryId?: string
}
): Promise<ProductPageResult> {
const res = await rpcOrNull('rpc_admin_product_list', {
p_page: page,
p_page_size: pageSize,
p_name: filters.name ?? null,
p_status: filters.status ?? null,
p_category_id: filters.categoryId ?? null
} as UTSJSONObject)
if (res == null) {
return { total: 0, items: [] as Array<AdminProduct> }
}
const anyTotal = (res as any).total
const anyItems = (res as any).items
return {
total: typeof anyTotal === 'number' ? anyTotal : 0,
items: Array.isArray(anyItems) ? anyItems : [] as Array<AdminProduct>
}
}
/**
* 获取商品状态汇总统计
*/
export async function fetchAdminProductCountStats() : Promise<UTSJSONObject | null> {
return await rpcOrNull('rpc_admin_product_count_stats', {} as UTSJSONObject)
}
/**
* 获取商品概况统计指标
*/
export async function fetchAdminProductStats(startTime : string, endTime : string) : Promise<UTSJSONObject | null> {
return await rpcOrNull('rpc_admin_product_stats', {
p_start_time: startTime,
p_end_time: endTime
} as UTSJSONObject)
}
/**
* 获取商品营业趋势数据
*/
export async function fetchAdminProductTrend(startTime : string, endTime : string) : Promise<Array<UTSJSONObject>> {
const res = await rpcOrValue('rpc_admin_product_trend', {
p_start_time: startTime,
p_end_time: endTime
} as UTSJSONObject)
return Array.isArray(res) ? (res as Array<UTSJSONObject>) : [] as Array<UTSJSONObject>
}
/**
* 获取商品排行
*/
export async function fetchAdminProductRanking(startTime : string, endTime : string, sortBy : string = 'sales', limit : number = 10) : Promise<Array<UTSJSONObject>> {
const res = await rpcOrValue('rpc_admin_product_ranking', {
p_start_time: startTime,
p_end_time: endTime,
p_sort_by: sortBy,
p_limit: limit
} as UTSJSONObject)
return Array.isArray(res) ? (res as Array<UTSJSONObject>) : [] as Array<UTSJSONObject>
}
/**
* 更新商品状态 (上架/下架/回收站)
* @param status 1:上架 2:下架 3:草稿 4:删除
*/
export async function updateAdminProductStatus(productId: string, status: number): Promise<boolean> {
try {
const ok = await rpcOrValue('rpc_admin_product_update_status', {
p_product_id: productId,
p_status: status
} as UTSJSONObject)
return ok === true
} catch (e: any) {
console.error('更新商品状态失败:', e)
return false
}
}