Browse Source

修改车辆查询

zs 1 year ago
parent
commit
a9aea546b8
3 changed files with 29 additions and 6 deletions
  1. 4 4
      src/controller/car.controller.ts
  2. 2 2
      src/interface/car.interface.ts
  3. 23 0
      src/service/car.service.ts

+ 4 - 4
src/controller/car.controller.ts

@@ -21,14 +21,14 @@ export class CarController extends BaseController {
   @Get('/')
   @ApiQuery({ name: 'query' })
   @ApiResponse({ type: QVO_car })
-  async query(@Query() filter: QDTO_car, @Query('skip') skip: number, @Query('limit') limit: number) {
-    const list = await this.service.query(filter, { skip, limit });
+  async query(@Query() filter) {
+    const result = await this.service.queryList(filter);
     const data = [];
-    for (const i of list) {
+    for (const i of result[0].data) {
       const newData = new QVO_car(i);
       data.push(newData);
     }
-    const total = await this.service.count(filter);
+    const total = result[0].total;
     return { data, total };
   }
 

+ 2 - 2
src/interface/car.interface.ts

@@ -88,8 +88,8 @@ export class FVO_car {
 
 export class QDTO_car extends SearchBase {
   constructor() {
-    const like_prop = ['brand'];
-    const props = ['shop','status'];
+    const like_prop = [];
+    const props = ['shop', 'brand', 'place', 'status'];
     const mapping = [];
     super({ like_prop, props, mapping });
   }

+ 23 - 0
src/service/car.service.ts

@@ -3,9 +3,32 @@ import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService } from 'free-midway-component';
 import { Car } from '../entity/car.entity';
+import { get, isNumber } from 'lodash';
 type modelType = ReturnModelType<typeof Car>;
 @Provide()
 export class CarService extends BaseService<modelType> {
   @InjectEntityModel(Car)
   model: modelType;
+  async queryList(filter) {
+    const { skip = 0, limit, ...info } = filter;
+    const pipes: any[] = [
+      { $match: info },
+      {
+        $facet: {
+          total: [{ $count: 'count' }],
+          rows: [],
+        },
+      },
+      {
+        $project: {
+          data: '$rows',
+          total: { $arrayElemAt: ['$total.count', 0] },
+        },
+      },
+    ];
+    if (isNumber(skip) && skip > -1) pipes.push({ $skip: skip });
+    if (isNumber(limit) && limit > 0) pipes.push({ $limit: limit });
+    const result = await this.model.aggregate(pipes);
+    return result;
+  }
 }