From edf49e7e30ade4099a16f7e3d558b59b301d2086 Mon Sep 17 00:00:00 2001 From: cll <931958862@qq.com> Date: Sat, 26 Jul 2025 11:28:54 +0800 Subject: [PATCH] =?UTF-8?q?18mg=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 11 ----- src/config/config.default.ts | 8 ++++ src/controller/api.controller.ts | 1 + src/controller/customer.controller.ts | 1 - src/entity/customer.entity.ts | 2 +- src/service/customer.service.ts | 69 +++++++++++++++------------ src/service/order.service.ts | 5 +- 7 files changed, 52 insertions(+), 45 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 4c7f8a8..0000000 --- a/.editorconfig +++ /dev/null @@ -1,11 +0,0 @@ -# 🎨 editorconfig.org - -root = true - -[*] -charset = utf-8 -end_of_line = lf -indent_style = space -indent_size = 2 -trim_trailing_whitespace = true -insert_final_newline = true \ No newline at end of file diff --git a/src/config/config.default.ts b/src/config/config.default.ts index 41fe292..55909ad 100644 --- a/src/config/config.default.ts +++ b/src/config/config.default.ts @@ -102,4 +102,12 @@ export default { // emailPswd: '', // }, // ], + swagger: { + auth: { + name: 'authorization', + authType: 'bearer', + description: 'Bearer Auth', + addSecurityRequirements: true, + }, + }, } as MidwayConfig; diff --git a/src/controller/api.controller.ts b/src/controller/api.controller.ts index cb10a70..5cdd381 100644 --- a/src/controller/api.controller.ts +++ b/src/controller/api.controller.ts @@ -1,6 +1,7 @@ import { Inject, Controller } from '@midwayjs/core'; import { Context } from '@midwayjs/koa'; + @Controller('/') export class APIController { @Inject() diff --git a/src/controller/customer.controller.ts b/src/controller/customer.controller.ts index 69d170d..151d179 100644 --- a/src/controller/customer.controller.ts +++ b/src/controller/customer.controller.ts @@ -27,7 +27,6 @@ export class CustomerController { @Get('/list') async getCustomerList(@Query() param: QueryCustomerListDTO) { try { - console.log(param); const data = await this.customerService.getCustomerList(param); return successResponse(data); } catch (error) { diff --git a/src/entity/customer.entity.ts b/src/entity/customer.entity.ts index cee6212..0dcbd18 100644 --- a/src/entity/customer.entity.ts +++ b/src/entity/customer.entity.ts @@ -8,6 +8,6 @@ export class Customer { @Column({ unique: true }) email: string; - @Column() + @Column({ default: 0}) rate: number; } \ No newline at end of file diff --git a/src/service/customer.service.ts b/src/service/customer.service.ts index 92eefac..b039ecb 100644 --- a/src/service/customer.service.ts +++ b/src/service/customer.service.ts @@ -29,32 +29,33 @@ export class CustomerService { customerId, rate, } = param; + const whereConds: string[] = []; const havingConds: string[] = []; + // 邮箱搜索 if (email) { whereConds.push(`o.customer_email LIKE '%${email}%'`); } + + // 省份搜索 if (state) { whereConds.push( `JSON_UNQUOTE(JSON_EXTRACT(o.billing, '$.state')) = '${state}'` ); } + + // customerId 过滤 if (customerId) { - whereConds.push(` - o.customer_email = ( - SELECT email FROM customer WHERE id = ${Number(customerId)} - ) - `); - } - if (rate) { - whereConds.push(` - o.customer_email = ( - SELECT email FROM customer WHERE rate = ${Number(rate)} - ) - `); + whereConds.push(`c.id = ${Number(customerId)}`); } + // rate 过滤 + if (rate) { + whereConds.push(`c.rate = ${Number(rate)}`); + } + + // tags 过滤 if (tags) { const tagList = tags .split(',') @@ -68,25 +69,30 @@ export class CustomerService { ) `); } + + // 首次购买时间过滤 if (first_purchase_date) { havingConds.push( `DATE_FORMAT(MIN(o.date_paid), '%Y-%m') = '${first_purchase_date}'` ); } + + // 公用过滤 const baseQuery = ` ${whereConds.length ? `WHERE ${whereConds.join(' AND ')}` : ''} GROUP BY o.customer_email ${havingConds.length ? `HAVING ${havingConds.join(' AND ')}` : ''} `; - let sql = ` - select - o.customer_email as email, - MIN(date_created) as date_created, - MIN(date_paid) as first_purchase_date, - MAX(date_paid) as last_purchase_date, - COUNT(DISTINCT o.id) as orders, - SUM(total) as total, + // 主查询 + const sql = ` + SELECT + o.customer_email AS email, + MIN(o.date_created) AS date_created, + MIN(o.date_paid) AS first_purchase_date, + MAX(o.date_paid) AS last_purchase_date, + COUNT(DISTINCT o.id) AS orders, + SUM(o.total) AS total, ANY_VALUE(o.shipping) AS shipping, ANY_VALUE(o.billing) AS billing, ( @@ -94,15 +100,12 @@ export class CustomerService { FROM customer_tag ct WHERE ct.email = o.customer_email ) AS tags, - ( - 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, + c.id AS customerId, + c.rate AS rate, yoone_stats.yoone_orders, yoone_stats.yoone_total FROM \`order\` o + LEFT JOIN customer c ON o.customer_email = c.email LEFT JOIN ( SELECT oo.customer_email, @@ -115,25 +118,28 @@ export class CustomerService { ) 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' - } - limit ${pageSize} offset ${(current - 1) * pageSize} + ? `ORDER BY ${sorterKey} ${sorterValue === 'descend' ? 'DESC' : 'ASC'}` + : 'ORDER BY orders ASC, yoone_total DESC'} + LIMIT ${pageSize} OFFSET ${(current - 1) * pageSize} `; + + // 统计总数 const countSql = ` SELECT COUNT(*) AS total FROM ( SELECT o.customer_email FROM \`order\` o + LEFT JOIN customer c ON o.customer_email = c.email ${baseQuery} ) AS sub `; + const [items, countResult] = await Promise.all([ this.orderModel.query(sql), this.orderModel.query(countSql), ]); const total = countResult[0]?.total || 0; + return { items, total, @@ -142,6 +148,7 @@ export class CustomerService { }; } + async addTag(email: string, tag: string) { const isExist = await this.customerTagModel.findOneBy({ email, tag }); if (isExist) throw new Error(`${tag}已存在`); diff --git a/src/service/order.service.ts b/src/service/order.service.ts index 47bd234..44d3349 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -222,6 +222,7 @@ export class OrderService { if(!customer) { await this.customerModel.save({ email: order.customer_email, + rate: 0, }); } return await this.orderModel.save(entity); @@ -822,7 +823,8 @@ export class OrderService { SUM(CASE WHEN os.name LIKE '%yoone%' AND os.name LIKE '%6%' THEN os.quantity ELSE 0 END) AS yoone6Quantity, SUM(CASE WHEN os.name LIKE '%yoone%' AND os.name LIKE '%9%' THEN os.quantity ELSE 0 END) AS yoone9Quantity, SUM(CASE WHEN os.name LIKE '%yoone%' AND os.name LIKE '%12%' THEN os.quantity ELSE 0 END) AS yoone12Quantity, - SUM(CASE WHEN os.name LIKE '%yoone%' AND os.name LIKE '%15%' THEN os.quantity ELSE 0 END) AS yoone15Quantity + SUM(CASE WHEN os.name LIKE '%yoone%' AND os.name LIKE '%15%' THEN os.quantity ELSE 0 END) AS yoone15Quantity, + SUM(CASE WHEN os.name LIKE '%yoone%' AND os.name LIKE '%18%' THEN os.quantity ELSE 0 END) AS yoone18Quantity FROM order_sale os INNER JOIN \`order\` o ON o.id = os.orderId WHERE o.date_paid BETWEEN ? AND ? @@ -848,6 +850,7 @@ export class OrderService { yoone9Quantity: Number(totalQuantityResult.yoone9Quantity || 0), yoone12Quantity: Number(totalQuantityResult.yoone12Quantity || 0), yoone15Quantity: Number(totalQuantityResult.yoone15Quantity || 0), + yoone18Quantity: Number(totalQuantityResult.yoone18Quantity || 0), current, pageSize, };