import { Controller, Get, Post, Inject, Query, Body } from '@midwayjs/core'; import { successResponse, errorResponse } from '../utils/response.util'; import { CustomerService } from '../service/customer.service'; import { QueryCustomerListDTO, CustomerTagDTO } from '../dto/customer.dto'; import { ApiOkResponse } from '@midwayjs/swagger'; import { UnifiedSearchParamsDTO } from '../dto/site-api.dto'; @Controller('/customer') export class CustomerController { @Inject() customerService: CustomerService; @ApiOkResponse({ type: Object }) @Get('/getcustomerlist') async getCustomerList(@Query() query: QueryCustomerListDTO) { try { const result = await this.customerService.getCustomerList(query) return successResponse(result); } catch (error) { return errorResponse(error.message); } } @ApiOkResponse({ type: Object }) @Get('/getcustomerstatisticlist') async getCustomerStatisticList(@Query() query: QueryCustomerListDTO) { try { const result = await this.customerService.getCustomerStatisticList(query as any); return successResponse(result); } catch (error) { return errorResponse(error.message); } } @ApiOkResponse({ type: Object }) @Post('/addtag') async addTag(@Body() body: CustomerTagDTO) { try { const result = await this.customerService.addTag(body.email, body.tag); return successResponse(result); } catch (error) { return errorResponse(error.message); } } @ApiOkResponse({ type: Object }) @Post('/deltag') async delTag(@Body() body: CustomerTagDTO) { try { const result = await this.customerService.delTag(body.email, body.tag); return successResponse(result); } catch (error) { return errorResponse(error.message); } } @ApiOkResponse({ type: Object }) @Get('/gettags') async getTags() { try { const result = await this.customerService.getTags(); return successResponse(result); } catch (error) { return errorResponse(error.message); } } @ApiOkResponse({ type: Object }) @Post('/setrate') async setRate(@Body() body: { id: number; rate: number }) { try { const result = await this.customerService.setRate({ id: body.id, rate: body.rate }); return successResponse(result); } catch (error) { return errorResponse(error.message); } } /** * 同步客户数据 * 从指定站点获取客户数据并保存到本地数据库 * 业务逻辑已移到service层,controller只负责参数传递和响应 */ @ApiOkResponse({ type: Object }) @Post('/sync') async syncCustomers(@Body() body: { siteId: number; params?: UnifiedSearchParamsDTO }) { try { const { siteId, params = {} } = body; // 调用service层的同步方法,所有业务逻辑都在service中处理 const syncResult = await this.customerService.syncCustomersFromSite(siteId, params); return successResponse(syncResult); } catch (error) { return errorResponse(error.message); } } }