250 lines
7.5 KiB
TypeScript
250 lines
7.5 KiB
TypeScript
import { Controller, Get, Post, Inject, Query, Body, Put, Del, Param } from '@midwayjs/core';
|
||
import { successResponse, errorResponse, ApiResponse } from '../utils/response.util';
|
||
import { CustomerService } from '../service/customer.service';
|
||
import { CustomerQueryParamsDTO, CreateCustomerDTO, UpdateCustomerDTO, GetCustomerDTO, BatchCreateCustomerDTO, BatchUpdateCustomerDTO, BatchDeleteCustomerDTO, SyncCustomersDTO } from '../dto/customer.dto';
|
||
import { ApiProperty } from '@midwayjs/swagger';
|
||
import { ApiOkResponse } from '@midwayjs/swagger';
|
||
import { UnifiedPaginationDTO } from '../dto/api.dto';
|
||
|
||
export class CustomerTagDTO {
|
||
@ApiProperty({ description: '客户邮箱' })
|
||
email: string;
|
||
|
||
@ApiProperty({ description: '标签名称' })
|
||
tag: string;
|
||
}
|
||
|
||
@Controller('/customer')
|
||
export class CustomerController {
|
||
@Inject()
|
||
customerService: CustomerService;
|
||
|
||
@ApiOkResponse({ type: ApiResponse<UnifiedPaginationDTO<GetCustomerDTO>> })
|
||
@Get('/list')
|
||
async getCustomerList(@Query() query: CustomerQueryParamsDTO) {
|
||
try {
|
||
const result = await this.customerService.getCustomerList(query)
|
||
return successResponse(result);
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
|
||
@ApiOkResponse({ type: Object })
|
||
@Get('/statistic/list')
|
||
async getCustomerStatisticList(@Query() query: CustomerQueryParamsDTO) {
|
||
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);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 同步客户数据
|
||
* 从指定站点获取客户数据并保存到本地数据库
|
||
* 支持通过where和orderBy参数筛选和排序要同步的客户数据
|
||
*/
|
||
@ApiOkResponse({ type: Object })
|
||
@Post('/sync')
|
||
async syncCustomers(@Body() body: SyncCustomersDTO) {
|
||
try {
|
||
const { siteId, params = {} } = body;
|
||
|
||
// 调用service层的同步方法,所有业务逻辑都在service中处理
|
||
const syncResult = await this.customerService.syncCustomersFromSite(siteId, params);
|
||
|
||
return successResponse(syncResult);
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
|
||
// ====================== 单个客户CRUD操作 ======================
|
||
|
||
/**
|
||
* 创建单个客户
|
||
* 使用CreateCustomerDTO进行数据验证
|
||
*/
|
||
@ApiOkResponse({ type: GetCustomerDTO })
|
||
@Post('/')
|
||
async createCustomer(@Body() body: CreateCustomerDTO) {
|
||
try {
|
||
// 调用service层的upsertCustomer方法
|
||
const result = await this.customerService.upsertCustomer({...body, origin_id: String(body.origin_id)});
|
||
return successResponse(result.customer);
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 根据ID获取单个客户
|
||
* 返回GetCustomerDTO格式的客户信息
|
||
*/
|
||
@ApiOkResponse({ type: GetCustomerDTO })
|
||
@Get('/:id')
|
||
async getCustomerById(@Param('id') id: number) {
|
||
try {
|
||
const customer = await this.customerService.customerModel.findOne({ where: { id } });
|
||
if (!customer) {
|
||
return errorResponse('客户不存在');
|
||
}
|
||
return successResponse(customer);
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新单个客户
|
||
* 使用UpdateCustomerDTO进行数据验证
|
||
*/
|
||
@ApiOkResponse({ type: GetCustomerDTO })
|
||
@Put('/:id')
|
||
async updateCustomer(@Param('id') id: number, @Body() body: UpdateCustomerDTO) {
|
||
try {
|
||
const customer = await this.customerService.updateCustomer(id, body);
|
||
if (!customer) {
|
||
return errorResponse('客户不存在');
|
||
}
|
||
return successResponse(customer);
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除单个客户
|
||
* 根据客户ID删除客户记录
|
||
*/
|
||
@ApiOkResponse({ type: Object })
|
||
@Del('/:id')
|
||
async deleteCustomer(@Param('id') id: number) {
|
||
try {
|
||
// 先检查客户是否存在
|
||
const customer = await this.customerService.customerModel.findOne({ where: { id } });
|
||
if (!customer) {
|
||
return errorResponse('客户不存在');
|
||
}
|
||
// 删除客户
|
||
await this.customerService.customerModel.delete(id);
|
||
return successResponse({ message: '删除成功' });
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
|
||
// ====================== 批量客户操作 ======================
|
||
|
||
/**
|
||
* 批量创建客户
|
||
* 使用BatchCreateCustomerDTO进行数据验证
|
||
*/
|
||
@ApiOkResponse({ type: Object })
|
||
@Post('/batch')
|
||
async batchCreateCustomers(@Body() body: BatchCreateCustomerDTO) {
|
||
try {
|
||
const result = await this.customerService.upsertManyCustomers(body.customers.map(c => ({ ...c, origin_id: String(c.origin_id) })));
|
||
return successResponse(result);
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量更新客户
|
||
* 使用BatchUpdateCustomerDTO进行数据验证
|
||
* 每个客户可以有独立的更新字段,支持统一化修改或分别更新
|
||
*/
|
||
@ApiOkResponse({ type: Object })
|
||
@Put('/batch')
|
||
async batchUpdateCustomers(@Body() body: BatchUpdateCustomerDTO) {
|
||
try {
|
||
const { customers } = body;
|
||
|
||
// 调用service层的批量更新方法
|
||
const result = await this.customerService.batchUpdateCustomers(customers);
|
||
|
||
return successResponse({
|
||
total: result.total,
|
||
updated: result.updated,
|
||
processed: result.processed,
|
||
errors: result.errors,
|
||
message: `成功更新${result.updated}个客户,共处理${result.processed}个`
|
||
});
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量删除客户
|
||
* 使用BatchDeleteCustomerDTO进行数据验证
|
||
*/
|
||
@ApiOkResponse({ type: Object })
|
||
@Del('/batch')
|
||
async batchDeleteCustomers(@Body() body: BatchDeleteCustomerDTO) {
|
||
try {
|
||
const { ids } = body;
|
||
|
||
// 调用service层的批量删除方法
|
||
const result = await this.customerService.batchDeleteCustomers(ids);
|
||
|
||
return successResponse({
|
||
total: result.total,
|
||
updated: result.updated,
|
||
processed: result.processed,
|
||
errors: result.errors,
|
||
message: `成功删除${result.updated}个客户,共处理${result.processed}个`
|
||
});
|
||
} catch (error) {
|
||
return errorResponse(error.message);
|
||
}
|
||
}
|
||
} |