|
@@ -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> {
|