Quellcode durchsuchen

修改购物车接口

zs vor 1 Jahr
Ursprung
Commit
a1917cab70

+ 1 - 1
src/controller/Cart.controller.ts

@@ -31,7 +31,7 @@ export class CartController extends BaseController {
   @Validate()
   @ApiResponse({ type: CVO_Cart })
   async create(@Body() data: CDTO_Cart) {
-    const dbData = await this.service.create(data);
+    const dbData = await this.service.specialCreate(data);
     const result = new CVO_Cart(dbData);
     return result;
   }

+ 1 - 1
src/entity/Cart.entity.ts

@@ -15,5 +15,5 @@ export class Cart extends BaseModel {
   @prop({ required: false, index: false, zh: '数量' })
   num: number;
   @prop({ required: false, index: true, zh: '总金额' })
-  money: string;
+  money: number;
 }

+ 4 - 4
src/interface/Cart.interface.ts

@@ -25,7 +25,7 @@ export class FVO_Cart {
   @ApiProperty({ description: '数量' })
   'num': number = undefined;
   @ApiProperty({ description: '总金额' })
-  'money': string = undefined;
+  'money': number = undefined;
 }
 
 export class QDTO_Cart extends SearchBase {
@@ -44,7 +44,7 @@ export class QDTO_Cart extends SearchBase {
   @ApiProperty({ description: '商品规格id' })
   'spec': string = undefined;
   @ApiProperty({ description: '总金额' })
-  'money': string = undefined;
+  'money': number = undefined;
 }
 
 export class QVO_Cart extends FVO_Cart {
@@ -71,8 +71,8 @@ export class CDTO_Cart {
   @Rule(RuleType['number']().empty(''))
   'num': number = undefined;
   @ApiProperty({ description: '总金额' })
-  @Rule(RuleType['string']().empty(''))
-  'money': string = undefined;
+  @Rule(RuleType['number']().empty(''))
+  'money': number = undefined;
 }
 
 export class CVO_Cart extends FVO_Cart {

+ 48 - 21
src/service/Cart.service.ts

@@ -6,6 +6,10 @@ 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 { CDTO_Cart } from '../interface/Cart.interface';
+import { Types } from 'mongoose';
+import _ = require('lodash');
+const ObjectId = Types.ObjectId;
 type modelType = ReturnModelType<typeof Cart>;
 @Provide()
 export class CartService extends BaseService<modelType> {
@@ -21,31 +25,54 @@ export class CartService extends BaseService<modelType> {
   @InjectEntityModel(User)
   userModel: ReturnModelType<typeof User>;
 
+  // 特殊创建
+  async specialCreate(data: CDTO_Cart): Promise<object> {
+    const { user, supplier_id, goods, spec, num, money } = data;
+    let res;
+    const arr: any = await this.model
+      .findOne({ user, supplier_id, goods, spec })
+      .lean();
+    if (arr) {
+      data.num = arr.num + num;
+      data.money = arr.money + money;
+      res = await this.model.updateOne({ _id: new ObjectId(arr._id) }, data);
+    } else res = await this.model.create(data);
+    return res;
+  }
+
   // 查询购物车全部商品
   async self(user: string): Promise<Array<any>> {
     const data = await this.model.find({ user }).lean();
-    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);
+    const uni = _.uniq(data.map(i => i.supplier_id));
+    const result = [];
+    for (const val of uni) {
+      const list = data.filter(i => i.supplier_id === val);
+      result.push(list);
+    }
+    const selfList: any = [];
+    for (const as of result) {
+      const list: any = [];
+      for (const val of as) {
+        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.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);
+      }
+      selfList.push(list);
     }
-    return list;
+    return selfList;
   }
   // 购物车数量
   async num(user: string): Promise<number> {