Files
makemd/dashboard/src/services/suppliersDataSource.ts

105 lines
4.1 KiB
TypeScript
Raw Normal View History

/**
* [MOCK-SUPPLIERS] Suppliers模块DataSource
*/
export interface Supplier {
id: string;
name: string;
contact: string;
contactName?: string;
email: string;
phone: string;
address: string;
category: string;
rating: number;
status: 'active' | 'inactive' | 'pending';
paymentTerms: string;
leadTime: number;
minOrder?: number;
minOrderQty: number;
notes?: string;
createdAt: string;
}
export interface SupplierProduct {
id: string;
supplierId?: string;
sku: string;
name: string;
productName?: string;
price: number;
costPrice?: number;
stock: number;
moq: number;
leadTime: number;
quality?: 'A' | 'B' | 'C';
}
export interface ISuppliersDataSource {
fetchSuppliers(): Promise<Supplier[]>;
fetchSupplierDetail(id: string): Promise<Supplier>;
saveSupplier(data: Partial<Supplier>): Promise<Supplier>;
deleteSupplier(id: string): Promise<void>;
fetchSupplierProducts(supplierId: string): Promise<SupplierProduct[]>;
}
class MockSuppliersDataSource implements ISuppliersDataSource {
async fetchSuppliers(): Promise<Supplier[]> {
return [
{ id: 'sup_001', name: 'Shenzhen Tech Co.', contact: 'Li Wei', email: 'liwei@szttech.com', phone: '+86-138-0000-0001', address: 'Shenzhen, China', category: 'Electronics', rating: 4.8, status: 'active', paymentTerms: '30% deposit, 70% before shipping', leadTime: 15, minOrderQty: 100, createdAt: '2025-01-15' },
{ id: 'sup_002', name: 'Guangzhou Accessories Ltd.', contact: 'Wang Ming', email: 'wangming@gzacc.com', phone: '+86-138-0000-0002', address: 'Guangzhou, China', category: 'Accessories', rating: 4.5, status: 'active', paymentTerms: 'Net 30', leadTime: 10, minOrderQty: 50, createdAt: '2025-02-20' },
{ id: 'sup_003', name: 'Dongguan Manufacturing', contact: 'Zhang Hua', email: 'zhanghua@dgmfg.com', phone: '+86-138-0000-0003', address: 'Dongguan, China', category: 'Electronics', rating: 4.2, status: 'active', paymentTerms: '50% deposit, 50% on delivery', leadTime: 20, minOrderQty: 200, createdAt: '2025-03-10' },
];
}
async fetchSupplierDetail(id: string): Promise<Supplier> {
const suppliers = await this.fetchSuppliers();
return suppliers.find(s => s.id === id) || suppliers[0];
}
async saveSupplier(data: Partial<Supplier>): Promise<Supplier> {
return { ...data, id: data.id || `sup_${Date.now()}`, createdAt: new Date().toISOString() } as Supplier;
}
async deleteSupplier(id: string): Promise<void> {}
async fetchSupplierProducts(supplierId: string): Promise<SupplierProduct[]> {
return [
{ id: 'sp_001', supplierId, sku: 'SKU-001', name: 'Wireless Headphones', productName: 'Wireless Headphones', price: 12.50, costPrice: 12.50, stock: 1000, moq: 100, leadTime: 15, quality: 'A' },
{ id: 'sp_002', supplierId, sku: 'SKU-002', name: 'USB-C Cable', productName: 'USB-C Cable', price: 1.80, costPrice: 1.80, stock: 5000, moq: 500, leadTime: 10, quality: 'A' },
{ id: 'sp_003', supplierId, sku: 'SKU-003', name: 'Phone Case', productName: 'Phone Case', price: 2.50, costPrice: 2.50, stock: 2000, moq: 200, leadTime: 12, quality: 'B' },
];
}
}
class ApiSuppliersDataSource implements ISuppliersDataSource {
private baseUrl = '/api/suppliers';
async fetchSuppliers(): Promise<Supplier[]> {
const res = await fetch(this.baseUrl);
return res.json();
}
async fetchSupplierDetail(id: string): Promise<Supplier> {
const res = await fetch(`${this.baseUrl}/${id}`);
return res.json();
}
async saveSupplier(data: Partial<Supplier>): Promise<Supplier> {
const res = await fetch(this.baseUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) });
return res.json();
}
async deleteSupplier(id: string): Promise<void> {
await fetch(`${this.baseUrl}/${id}`, { method: 'DELETE' });
}
async fetchSupplierProducts(supplierId: string): Promise<SupplierProduct[]> {
const res = await fetch(`${this.baseUrl}/${supplierId}/products`);
return res.json();
}
}
const useMock = process.env.REACT_APP_USE_MOCK === 'true';
export const suppliersDataSource: ISuppliersDataSource = useMock ? new MockSuppliersDataSource() : new ApiSuppliersDataSource();