From 0fc9554c7faf9d473f96e8d4c52029d8ecec06f5 Mon Sep 17 00:00:00 2001 From: cll <931958862@qq.com> Date: Wed, 23 Jul 2025 17:29:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E8=AF=84=E6=98=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/customer.controller.ts | 13 ++++++++++++ src/entity/customer.entity.ts | 3 +++ src/service/customer.service.ts | 29 +++++++++++++++++++++------ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/controller/customer.controller.ts b/src/controller/customer.controller.ts index 2c2b2de..69d170d 100644 --- a/src/controller/customer.controller.ts +++ b/src/controller/customer.controller.ts @@ -6,6 +6,7 @@ import { Get, Inject, Post, + Put, Query, } from '@midwayjs/core'; import { CustomerService } from '../service/customer.service'; @@ -67,4 +68,16 @@ export class CustomerController { return errorResponse(error?.message || error); } } + + + @ApiOkResponse({ type: BooleanRes }) + @Put('/rate') + async setRate(@Body() params: { id: number; rate: number }) { + try { + await this.customerService.setRate(params); + return successResponse(true); + } catch (error) { + return errorResponse(error?.message || error); + } + } } diff --git a/src/entity/customer.entity.ts b/src/entity/customer.entity.ts index a018a79..cee6212 100644 --- a/src/entity/customer.entity.ts +++ b/src/entity/customer.entity.ts @@ -7,4 +7,7 @@ export class Customer { @Column({ unique: true }) email: string; + + @Column() + rate: number; } \ No newline at end of file diff --git a/src/service/customer.service.ts b/src/service/customer.service.ts index 8c74ef8..92eefac 100644 --- a/src/service/customer.service.ts +++ b/src/service/customer.service.ts @@ -3,6 +3,7 @@ import { InjectEntityModel } from '@midwayjs/typeorm'; import { Order } from '../entity/order.entity'; import { Repository } from 'typeorm'; import { CustomerTag } from '../entity/customer_tag.entity'; +import { Customer } from '../entity/customer.entity'; @Provide() export class CustomerService { @@ -12,6 +13,9 @@ export class CustomerService { @InjectEntityModel(CustomerTag) customerTagModel: Repository; + @InjectEntityModel(Customer) + customerModel: Repository; + async getCustomerList(param: Record) { const { current = 1, @@ -23,6 +27,7 @@ export class CustomerService { state, first_purchase_date, customerId, + rate, } = param; const whereConds: string[] = []; const havingConds: string[] = []; @@ -42,6 +47,13 @@ export class CustomerService { ) `); } + if (rate) { + whereConds.push(` + o.customer_email = ( + SELECT email FROM customer WHERE rate = ${Number(rate)} + ) + `); + } if (tags) { const tagList = tags @@ -85,6 +97,9 @@ export class CustomerService { ( SELECT id FROM customer c WHERE c.email = o.customer_email ) as customerId, + ( + SELECT rate FROM customer c WHERE c.email = o.customer_email + ) as rate, yoone_stats.yoone_orders, yoone_stats.yoone_total FROM \`order\` o @@ -99,12 +114,10 @@ export class CustomerService { GROUP BY oo.customer_email ) yoone_stats ON yoone_stats.customer_email = o.customer_email ${baseQuery} - ${ - sorterKey - ? `ORDER BY ${sorterKey} ${ - sorterValue === 'descend' ? 'DESC' : 'ASC' - }` - : 'ORDER BY orders ASC, yoone_total DESC' + ${sorterKey + ? `ORDER BY ${sorterKey} ${sorterValue === 'descend' ? 'DESC' : 'ASC' + }` + : 'ORDER BY orders ASC, yoone_total DESC' } limit ${pageSize} offset ${(current - 1) * pageSize} `; @@ -148,4 +161,8 @@ export class CustomerService { .getRawMany(); return tags.map(t => t.tag); } + + async setRate(params: { id: number; rate: number }) { + return await this.customerModel.update(params.id, { rate: params.rate }); + } }