|
@@ -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;
|
|
|
+ }
|
|
|
}
|