zs преди 1 година
родител
ревизия
5e9d4dbbaf
променени са 4 файла, в които са добавени 80 реда и са изтрити 11 реда
  1. 10 0
      src/controller/Cart.controller.ts
  2. 7 5
      src/entity/Cart.entity.ts
  3. 11 6
      src/interface/Cart.interface.ts
  4. 52 0
      src/service/Cart.service.ts

+ 10 - 0
src/controller/Cart.controller.ts

@@ -52,6 +52,16 @@ export class CartController extends BaseController {
     const total = await this.service.count(filter);
     return { data, total };
   }
+  @Get('/self')
+  async self(@Query('user') user: string) {
+    const list = await this.service.self(user);
+    return list;
+  }
+  @Get('/num')
+  async num(@Query('user') user: string) {
+    const result = await this.service.num(user);
+    return result;
+  }
 
   @Get('/:id')
   @ApiResponse({ type: FVO_Cart })

+ 7 - 5
src/entity/Cart.entity.ts

@@ -4,14 +4,16 @@ import { BaseModel } from 'free-midway-component';
   schemaOptions: { collection: 'Cart' },
 })
 export class Cart extends BaseModel {
-  @prop({ required: false, index: true, zh: '用户', ref: 'User' })
+  @prop({ required: false, index: true, zh: '用户' })
   user: string;
-  @prop({ required: false, index: true, zh: '商品id', ref: 'Good' })
+  @prop({ required: false, index: true, zh: '商品id' })
   goods: string;
-  @prop({ required: false, index: true, zh: '商品规格id', ref: 'Spec' })
+  @prop({ required: false, index: true, zh: '供应商' })
+  supplier_id: string;
+  @prop({ required: false, index: true, zh: '商品规格id' })
   spec: string;
-  @prop({ required: false, index: true, zh: '数量' })
-  num: string;
+  @prop({ required: false, index: false, zh: '数量' })
+  num: number;
   @prop({ required: false, index: true, zh: '总金额' })
   money: string;
 }

+ 11 - 6
src/interface/Cart.interface.ts

@@ -18,10 +18,12 @@ export class FVO_Cart {
   'user': string = undefined;
   @ApiProperty({ description: '商品id' })
   'goods': string = undefined;
+  @ApiProperty({ description: '供应商' })
+  'supplier_id': string = undefined;
   @ApiProperty({ description: '商品规格id' })
   'spec': string = undefined;
   @ApiProperty({ description: '数量' })
-  'num': string = undefined;
+  'num': number = undefined;
   @ApiProperty({ description: '总金额' })
   'money': string = undefined;
 }
@@ -29,7 +31,7 @@ export class FVO_Cart {
 export class QDTO_Cart extends SearchBase {
   constructor() {
     const like_prop = [];
-    const props = ['user', 'goods', 'spec', 'num', 'money'];
+    const props = ['user', 'goods', 'supplier_id', 'spec', 'money'];
     const mapping = [];
     super({ like_prop, props, mapping });
   }
@@ -37,10 +39,10 @@ export class QDTO_Cart extends SearchBase {
   'user': string = undefined;
   @ApiProperty({ description: '商品id' })
   'goods': string = undefined;
+  @ApiProperty({ description: '供应商' })
+  'supplier_id': string = undefined;
   @ApiProperty({ description: '商品规格id' })
   'spec': string = undefined;
-  @ApiProperty({ description: '数量' })
-  'num': string = undefined;
   @ApiProperty({ description: '总金额' })
   'money': string = undefined;
 }
@@ -59,12 +61,15 @@ export class CDTO_Cart {
   @ApiProperty({ description: '商品id' })
   @Rule(RuleType['string']().empty(''))
   'goods': string = undefined;
+  @ApiProperty({ description: '供应商' })
+  @Rule(RuleType['string']().empty(''))
+  'supplier_id': string = undefined;
   @ApiProperty({ description: '商品规格id' })
   @Rule(RuleType['string']().empty(''))
   'spec': string = undefined;
   @ApiProperty({ description: '数量' })
-  @Rule(RuleType['string']().empty(''))
-  'num': string = undefined;
+  @Rule(RuleType['number']().empty(''))
+  'num': number = undefined;
   @ApiProperty({ description: '总金额' })
   @Rule(RuleType['string']().empty(''))
   'money': string = undefined;

+ 52 - 0
src/service/Cart.service.ts

@@ -3,9 +3,61 @@ import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService } from 'free-midway-component';
 import { Cart } from '../entity/Cart.entity';
+import { Good } from '../entity/Good.entity';
+import { Specs } from '../entity/Specs.entity';
+import { User } from '../entity/User.entity';
+import _ = require('lodash');
 type modelType = ReturnModelType<typeof Cart>;
 @Provide()
 export class CartService extends BaseService<modelType> {
   @InjectEntityModel(Cart)
   model: modelType;
+
+  @InjectEntityModel(Good)
+  goodModel: ReturnModelType<typeof Good>;
+
+  @InjectEntityModel(Specs)
+  specModel: ReturnModelType<typeof Specs>;
+
+  @InjectEntityModel(User)
+  userModel: ReturnModelType<typeof User>;
+
+  // 查询购物车全部商品
+  async self(user: string): Promise<object> {
+    const data = await this.model.find({ user }).lean();
+    const supplier = data.map(i => i.supplier_id);
+    const result = _.uniq(supplier);
+    const CartInfo: any = {};
+    for (const as of result) {
+      const list: any = [];
+      const arr = await this.model.aggregate([{ $match: { supplier_id: as } }]);
+      for (const val of arr) {
+        let res;
+        const info: any = {};
+        res = await this.goodModel.findById(val.goods).lean();
+        info.goods = { _id: res._id, name: res.name, file: res.file };
+        res = await this.specModel.findById(val.spec).lean();
+        info.money = parseFloat(res.money) * val.num;
+        info.num = val.num;
+        info.specs = {
+          _id: res._id,
+          name: res.name,
+          money: res.money,
+          file: res.file,
+        };
+        res = await this.userModel.findById(val.user).lean();
+        info.user = { _id: res._id, name: res.name };
+        res = await this.userModel.findById(val.supplier_id).lean();
+        info.supplier = { _id: res._id, name: res.name };
+        list.push(info);
+      }
+      [].push.apply(CartInfo, list);
+    }
+    return CartInfo;
+  }
+  // 购物车数量
+  async num(user: string): Promise<number> {
+    const data = await this.model.count({ user }).lean();
+    return data;
+  }
 }