浏览代码

修改接口

zs 1 年之前
父节点
当前提交
fcc1e38f55

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

@@ -35,6 +35,12 @@ export class CartController extends BaseController {
     const result = new CVO_Cart(dbData);
     return result;
   }
+  @Post('/checkCartBuy')
+  async checkCartBuy(@Body() data: any) {
+    const result = await this.service.checkCartBuy(data);
+    return result;
+  }
+
   @Get('/')
   @ApiQuery({ name: 'query' })
   @ApiResponse({ type: QVO_Cart })

+ 2 - 2
src/entity/Order.entity.ts

@@ -5,11 +5,11 @@ import { Decimal128 } from 'mongoose';
   schemaOptions: { collection: 'Order' },
 })
 export class Order extends BaseModel {
-  @prop({ required: false, index: true, zh: '用户', ref: 'User' })
+  @prop({ required: false, index: true, zh: '用户' })
   user: string;
   @prop({ required: false, index: false, zh: '邮寄地址' })
   address: string;
-  @prop({ required: false, index: false, zh: '商品', ref: '' })
+  @prop({ required: false, index: false, zh: '商品' })
   goods: Array<any>;
   @prop({ required: false, index: false, zh: '总金额' })
   total_money: Decimal128;

+ 3 - 1
src/entity/OrderDetail.entity.ts

@@ -6,12 +6,14 @@ import { BaseModel } from 'free-midway-component';
 export class OrderDetail extends BaseModel {
   @prop({ required: false, index: true, zh: '总订单id' })
   order_id: string;
-  @prop({ required: false, index: true, zh: '用户', ref: 'User' })
+  @prop({ required: false, index: true, zh: '用户' })
   user: string;
   @prop({ required: false, index: true, zh: '商品id' })
   good: string;
   @prop({ required: false, index: true, zh: '规格id' })
   spec: string;
+  @prop({ required: false, index: true, zh: '厂商id' })
+  supplier: string;
   @prop({ required: false, index: false, zh: '收货地址' })
   address: string;
   @prop({ required: false, index: true, zh: '下单时间' })

+ 16 - 1
src/interface/OrderDetail.interface.ts

@@ -22,6 +22,8 @@ export class FVO_OrderDetail {
   'good': string = undefined;
   @ApiProperty({ description: '规格id' })
   'spec': string = undefined;
+  @ApiProperty({ description: '厂商id' })
+  'supplier': string = undefined;
   @ApiProperty({ description: '收货地址' })
   'address': string = undefined;
   @ApiProperty({ description: '下单时间' })
@@ -33,7 +35,15 @@ export class FVO_OrderDetail {
 export class QDTO_OrderDetail extends SearchBase {
   constructor() {
     const like_prop = [];
-    const props = ['order_id', 'user', 'good', 'spec', 'buy_time', 'status'];
+    const props = [
+      'order_id',
+      'user',
+      'good',
+      'spec',
+      'buy_time',
+      'status',
+      'supplier',
+    ];
     const mapping = [];
     super({ like_prop, props, mapping });
   }
@@ -45,6 +55,8 @@ export class QDTO_OrderDetail extends SearchBase {
   'good': string = undefined;
   @ApiProperty({ description: '规格id' })
   'spec': string = undefined;
+  @ApiProperty({ description: '厂商id' })
+  'supplier': string = undefined;
   @ApiProperty({ description: '下单时间' })
   'buy_time': string = undefined;
   @ApiProperty({ description: '状态' })
@@ -71,6 +83,9 @@ export class CDTO_OrderDetail {
   @ApiProperty({ description: '规格id' })
   @Rule(RuleType['string']().empty(''))
   'spec': string = undefined;
+  @ApiProperty({ description: '厂商id' })
+  @Rule(RuleType['string']().empty(''))
+  'supplier': string = undefined;
   @ApiProperty({ description: '收货地址' })
   @Rule(RuleType['string']().empty(''))
   'address': string = undefined;

+ 101 - 5
src/service/Cart.service.ts

@@ -1,14 +1,23 @@
 import { Provide } from '@midwayjs/decorator';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
-import { BaseService } from 'free-midway-component';
+import {
+  BaseService,
+  FrameworkErrorEnum,
+  ServiceError,
+} 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 { CDTO_Cart } from '../interface/Cart.interface';
+import { CDTO_Cart, QVO_Cart } from '../interface/Cart.interface';
 import { Types } from 'mongoose';
 import _ = require('lodash');
+import { Office } from '../entity/Office.entity';
+import { Order } from '../entity/Order.entity';
+import { OrderDetail } from '../entity/OrderDetail.entity';
+const assert = require('assert');
+const moment = require('moment');
 const ObjectId = Types.ObjectId;
 type modelType = ReturnModelType<typeof Cart>;
 @Provide()
@@ -25,24 +34,38 @@ export class CartService extends BaseService<modelType> {
   @InjectEntityModel(User)
   userModel: ReturnModelType<typeof User>;
 
+  @InjectEntityModel(Office)
+  offModel: ReturnModelType<typeof Office>;
+
+  @InjectEntityModel(Order)
+  orderModel: ReturnModelType<typeof Order>;
+
+  @InjectEntityModel(OrderDetail)
+  orderDetailModel: ReturnModelType<typeof OrderDetail>;
+
   // 特殊创建
   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 uni = _.uniq(data.map(i => i.supplier_id));
     const result = [];
     for (const val of uni) {
@@ -50,6 +73,7 @@ export class CartService extends BaseService<modelType> {
       result.push(list);
     }
     const selfList: any = [];
+    // 对重新按着厂商id组合的数组进行遍历 修改 id 变为具体数据
     for (const as of result) {
       const list: any = [];
       for (const val of as) {
@@ -62,15 +86,20 @@ export class CartService extends BaseService<modelType> {
           _id: res._id,
           name: res.name,
           money: res.money,
+          num: res.num,
           file: res.file,
         };
+        info.money = parseFloat(res.money) * val.num;
+        info.num = val.num;
         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 };
+        info.cart_id = val._id;
         list.push(info);
       }
-      selfList.push(list);
+      const selfInfo: any = { list };
+      const cs = await this.userModel.findById(as[0].supplier_id).lean();
+      selfInfo.supplier = { _id: cs._id, name: cs.name };
+      selfList.push(selfInfo);
     }
     return selfList;
   }
@@ -79,4 +108,71 @@ export class CartService extends BaseService<modelType> {
     const data = await this.model.count({ user }).lean();
     return data;
   }
+  // 结算
+  async checkCartBuy(data) {
+    assert(data.cartIds, '缺少选中商品信息');
+    assert(data.totalMoney, '缺少总金额');
+    const list = [];
+    const obj: any = { status: '0' };
+    // 查询选择的商品
+    for (const val of data.cartIds) {
+      const arr = await this.model.findById(val).lean();
+      if (arr) list.push(arr);
+    }
+    let order;
+    if (list.length > 0) {
+      // 创建订单数据
+      const goods = [];
+      for (const i of list) {
+        const newData = new QVO_Cart(i);
+        goods.push(newData);
+      }
+      obj.goods = goods;
+      const result = await this.userModel.findById(list[0].user).lean();
+      if (result) {
+        obj.user = result._id;
+        if (result.community) {
+          const res = await this.offModel.findById(result.community).lean();
+          obj.address = res.address;
+        } else if (result.street) {
+          const res = await this.offModel.findById(result.street).lean();
+          obj.address = res.address;
+        } else {
+          assert(result.street, '缺少街道信息或社区信息!');
+        }
+        obj.total_money = data.totalMoney;
+        obj.buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
+        order = await this.orderModel.create(obj);
+        for (const val of order.goods) {
+          // 创建订单详情数据
+          const info = {
+            order_id: order._id,
+            user: order.user,
+            good: val.goods,
+            spec: val.spec,
+            supplier: val.supplier_id,
+            address: order.address,
+            buy_time: order.buy_time,
+            status: order.status,
+          };
+          await this.orderDetailModel.create(info);
+        }
+      } else {
+        throw new ServiceError(
+          '未找到该用户',
+          FrameworkErrorEnum.NOT_FOUND_DATA
+        );
+      }
+    } else {
+      throw new ServiceError(
+        '未在购物车找到相关信息',
+        FrameworkErrorEnum.NOT_FOUND_DATA
+      );
+    }
+    // 删除购物车
+    for (const val of data.cartIds) {
+      await this.model.deleteOne({ _id: val }).lean();
+    }
+    return order;
+  }
 }