refactor(services): 重构服务模块结构,按功能分类移动文件

将服务文件按功能分类移动到对应子目录,包括财务、营销、订单等模块
更新相关路由和导入路径,修复文件引用错误
归档旧版任务文档,更新README和任务统计信息
This commit is contained in:
2026-03-23 15:41:50 +08:00
parent 2b86715c09
commit e59d7c6620
156 changed files with 14658 additions and 7774 deletions

View File

@@ -0,0 +1,182 @@
import { Request, Response } from 'express';
import { AfterSalesService, AfterSalesContext } from '../../services/AfterSalesService';
export class AfterSalesController {
static async initTables(req: Request, res: Response): Promise<void> {
try {
await AfterSalesService.initTables();
res.json({ success: true, message: 'After-sales tables initialized' });
} catch (error: any) {
res.status(500).json({ success: false, error: error.message });
}
}
static async createRequest(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { orderId, type, reason, description, items, customerId, customerName, customerEmail } = req.body;
const result = await AfterSalesService.createRequest(context, {
orderId,
type,
reason,
description,
items,
customerId,
customerName,
customerEmail
});
res.status(201).json(result);
} catch (error: any) {
res.status(400).json({ success: false, error: error.message });
}
}
static async getRequests(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { status, type, orderId, page, pageSize } = req.query;
const result = await AfterSalesService.getRequests(context.tenantId, {
status: status as any,
type: type as any,
orderId: orderId as string,
page: page ? parseInt(page as string) : 1,
pageSize: pageSize ? parseInt(pageSize as string) : 20
});
res.json(result);
} catch (error: any) {
res.status(500).json({ success: false, error: error.message });
}
}
static async getRequestById(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { id } = req.params;
const request = await AfterSalesService.getRequestById(context.tenantId, id);
if (!request) {
res.status(404).json({ success: false, error: 'After-sales request not found' });
return;
}
res.json(request);
} catch (error: any) {
res.status(500).json({ success: false, error: error.message });
}
}
static async approveRequest(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { id } = req.params;
const { approved, approvedBy, rejectedReason, refundAmount } = req.body;
const result = await AfterSalesService.approveRequest(context, id, {
approved,
approvedBy,
rejectedReason,
refundAmount
});
res.json(result);
} catch (error: any) {
res.status(400).json({ success: false, error: error.message });
}
}
static async processRefund(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { refundId } = req.params;
const { transactionId, refundMethod } = req.body;
const result = await AfterSalesService.processRefund(context, refundId, {
transactionId,
refundMethod
});
res.json(result);
} catch (error: any) {
res.status(400).json({ success: false, error: error.message });
}
}
static async getRefundById(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { refundId } = req.params;
const refund = await AfterSalesService.getRefundById(context.tenantId, refundId);
if (!refund) {
res.status(404).json({ success: false, error: 'Refund not found' });
return;
}
res.json(refund);
} catch (error: any) {
res.status(500).json({ success: false, error: error.message });
}
}
static async processReturn(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { returnId } = req.params;
const { action, ...actionData } = req.body;
const result = await AfterSalesService.processReturn(context, returnId, action, actionData);
res.json(result);
} catch (error: any) {
res.status(400).json({ success: false, error: error.message });
}
}
static async getReturnById(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { returnId } = req.params;
const returnRecord = await AfterSalesService.getReturnById(context.tenantId, returnId);
if (!returnRecord) {
res.status(404).json({ success: false, error: 'Return not found' });
return;
}
res.json(returnRecord);
} catch (error: any) {
res.status(500).json({ success: false, error: error.message });
}
}
static async closeRequest(req: Request, res: Response): Promise<void> {
try {
const context = AfterSalesController.getContext(req);
const { id } = req.params;
const { reason } = req.body;
const result = await AfterSalesService.closeRequest(context, id, reason);
res.json(result);
} catch (error: any) {
res.status(400).json({ success: false, error: error.message });
}
}
private static getContext(req: Request): AfterSalesContext {
return {
tenantId: req.headers['x-tenant-id'] as string || 'default',
shopId: req.headers['x-shop-id'] as string || 'default',
taskId: req.headers['x-task-id'] as string || `task-${Date.now()}`,
traceId: req.headers['x-trace-id'] as string || `trace-${Date.now()}`,
businessType: (req.headers['x-business-type'] as 'TOC' | 'TOB') || 'TOC'
};
}
}