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 }); + } }