import { http, HttpResponse, delay } from 'msw'; import { mockCoils, type MockCoil } from '../data/aps'; // Mutable in-memory store let coils = [...mockCoils]; let nextId = Math.max(...coils.map((c) => c.id)) + 1; function paginate(list: T[], pageNo: number, pageSize: number) { const start = (pageNo - 1) * pageSize; return { list: list.slice(start, start + pageSize), total: list.length, }; } export const apsCoilHandlers = [ // Page query http.get('/admin-api/aps/coil/page', async ({ request }) => { await delay(300); const url = new URL(request.url); const pageNo = Number(url.searchParams.get('pageNo') || 1); const pageSize = Number(url.searchParams.get('pageSize') || 10); const coilId = url.searchParams.get('coilId') || ''; const alloy = url.searchParams.get('alloy') || ''; const status = url.searchParams.get('status'); let filtered = [...coils]; if (coilId) { filtered = filtered.filter((c) => c.coilId.includes(coilId)); } if (alloy) { filtered = filtered.filter((c) => c.alloy === alloy); } if (status !== null && status !== '') { filtered = filtered.filter((c) => c.status === Number(status)); } return HttpResponse.json({ code: 0, data: paginate(filtered, pageNo, pageSize), msg: '', }); }), // Get by ID http.get('/admin-api/aps/coil/get', async ({ request }) => { await delay(200); const url = new URL(request.url); const id = Number(url.searchParams.get('id')); const coil = coils.find((c) => c.id === id); if (!coil) { return HttpResponse.json({ code: 1, data: null, msg: '母卷不存在' }); } return HttpResponse.json({ code: 0, data: coil, msg: '' }); }), // Create http.post('/admin-api/aps/coil/create', async ({ request }) => { await delay(300); const body = (await request.json()) as Record; const exists = coils.find((c) => c.coilId === body.coilId); if (exists) { return HttpResponse.json({ code: 1, data: null, msg: '母卷编号已存在' }); } const newCoil: MockCoil = { id: nextId++, coilId: body.coilId as string, alloy: body.alloy as string, temper: body.temper as string, width: body.width as number, thickness: body.thickness as number, weight: body.weight as number, inDate: body.inDate as string, status: (body.status as number) ?? 0, createTime: new Date().toISOString().replace('T', ' ').slice(0, 19), }; coils.unshift(newCoil); return HttpResponse.json({ code: 0, data: newCoil.id, msg: '' }); }), // Update http.put('/admin-api/aps/coil/update', async ({ request }) => { await delay(300); const body = (await request.json()) as Record; const index = coils.findIndex((c) => c.id === body.id); if (index === -1) { return HttpResponse.json({ code: 1, data: null, msg: '母卷不存在' }); } if (body.coilId) { const dup = coils.find((c) => c.coilId === body.coilId && c.id !== body.id); if (dup) { return HttpResponse.json({ code: 1, data: null, msg: '母卷编号已存在' }); } } coils[index] = { ...coils[index], ...(body.coilId !== undefined && { coilId: body.coilId as string }), ...(body.alloy !== undefined && { alloy: body.alloy as string }), ...(body.temper !== undefined && { temper: body.temper as string }), ...(body.width !== undefined && { width: body.width as number }), ...(body.thickness !== undefined && { thickness: body.thickness as number }), ...(body.weight !== undefined && { weight: body.weight as number }), ...(body.inDate !== undefined && { inDate: body.inDate as string }), ...(body.status !== undefined && { status: body.status as number }), }; return HttpResponse.json({ code: 0, data: true, msg: '' }); }), // Delete http.delete('/admin-api/aps/coil/delete', async ({ request }) => { await delay(200); const url = new URL(request.url); const id = Number(url.searchParams.get('id')); coils = coils.filter((c) => c.id !== id); return HttpResponse.json({ code: 0, data: true, msg: '' }); }), // Import (mock) http.post('/admin-api/aps/coil/import', async () => { await delay(1000); return HttpResponse.json({ code: 0, data: { successCount: 5, failCount: 0, errors: [] }, msg: '', }); }), // Download template (mock - return empty blob) http.get('/admin-api/aps/coil/import-template', async () => { await delay(200); return new HttpResponse(new Blob(), { headers: { 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }, }); }), // Export (mock - return empty blob) http.get('/admin-api/aps/coil/export', async () => { await delay(500); return new HttpResponse(new Blob(), { headers: { 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }, }); }), ];