forked from yoone/API
1
0
Fork 0

客户评星

This commit is contained in:
cll 2025-07-23 17:29:00 +08:00
parent 343379519b
commit 0fc9554c7f
3 changed files with 39 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import {
Get, Get,
Inject, Inject,
Post, Post,
Put,
Query, Query,
} from '@midwayjs/core'; } from '@midwayjs/core';
import { CustomerService } from '../service/customer.service'; import { CustomerService } from '../service/customer.service';
@ -67,4 +68,16 @@ export class CustomerController {
return errorResponse(error?.message || error); 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);
}
}
} }

View File

@ -7,4 +7,7 @@ export class Customer {
@Column({ unique: true }) @Column({ unique: true })
email: string; email: string;
@Column()
rate: number;
} }

View File

@ -3,6 +3,7 @@ import { InjectEntityModel } from '@midwayjs/typeorm';
import { Order } from '../entity/order.entity'; import { Order } from '../entity/order.entity';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { CustomerTag } from '../entity/customer_tag.entity'; import { CustomerTag } from '../entity/customer_tag.entity';
import { Customer } from '../entity/customer.entity';
@Provide() @Provide()
export class CustomerService { export class CustomerService {
@ -12,6 +13,9 @@ export class CustomerService {
@InjectEntityModel(CustomerTag) @InjectEntityModel(CustomerTag)
customerTagModel: Repository<CustomerTag>; customerTagModel: Repository<CustomerTag>;
@InjectEntityModel(Customer)
customerModel: Repository<Customer>;
async getCustomerList(param: Record<string, any>) { async getCustomerList(param: Record<string, any>) {
const { const {
current = 1, current = 1,
@ -23,6 +27,7 @@ export class CustomerService {
state, state,
first_purchase_date, first_purchase_date,
customerId, customerId,
rate,
} = param; } = param;
const whereConds: string[] = []; const whereConds: string[] = [];
const havingConds: 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) { if (tags) {
const tagList = tags const tagList = tags
@ -85,6 +97,9 @@ export class CustomerService {
( (
SELECT id FROM customer c WHERE c.email = o.customer_email SELECT id FROM customer c WHERE c.email = o.customer_email
) as customerId, ) as customerId,
(
SELECT rate FROM customer c WHERE c.email = o.customer_email
) as rate,
yoone_stats.yoone_orders, yoone_stats.yoone_orders,
yoone_stats.yoone_total yoone_stats.yoone_total
FROM \`order\` o FROM \`order\` o
@ -99,10 +114,8 @@ export class CustomerService {
GROUP BY oo.customer_email GROUP BY oo.customer_email
) yoone_stats ON yoone_stats.customer_email = o.customer_email ) yoone_stats ON yoone_stats.customer_email = o.customer_email
${baseQuery} ${baseQuery}
${ ${sorterKey
sorterKey ? `ORDER BY ${sorterKey} ${sorterValue === 'descend' ? 'DESC' : 'ASC'
? `ORDER BY ${sorterKey} ${
sorterValue === 'descend' ? 'DESC' : 'ASC'
}` }`
: 'ORDER BY orders ASC, yoone_total DESC' : 'ORDER BY orders ASC, yoone_total DESC'
} }
@ -148,4 +161,8 @@ export class CustomerService {
.getRawMany(); .getRawMany();
return tags.map(t => t.tag); return tags.map(t => t.tag);
} }
async setRate(params: { id: number; rate: number }) {
return await this.customerModel.update(params.id, { rate: params.rate });
}
} }