zs 1 year ago
parent
commit
324846b6a8
1 changed files with 22 additions and 30 deletions
  1. 22 30
      src/service/Cart.service.ts

+ 22 - 30
src/service/Cart.service.ts

@@ -6,7 +6,6 @@ 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> {
@@ -23,37 +22,30 @@ export class CartService extends BaseService<modelType> {
   userModel: ReturnModelType<typeof User>;
 
   // 查询购物车全部商品
-  async self(user: string): Promise<object> {
+  async self(user: string): Promise<Array<any>> {
     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);
+    const list: any = [];
+    for (const val of data) {
+      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);
     }
-    return CartInfo;
+    return list;
   }
   // 购物车数量
   async num(user: string): Promise<number> {