123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- //
- class OrderService extends CrudService {
- constructor(ctx) {
- super(ctx, 'order');
- this.redis = this.app.redis;
- this.redisKey = this.app.config.redisKey;
- this.model = this.ctx.model.Trade.Order;
- this.goodsModel = this.ctx.model.Shop.Goods;
- this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
- this.addressModel = this.ctx.model.User.Address;
- this.cartModel = this.ctx.model.Trade.Cart;
- }
- /**
- * 进入下单页面
- * @param {Object} body 请求参数
- * @param body.key 缓存key
- */
- async toMakeOrder({ key }) {
- key = `${this.redisKey.orderKeyPrefix}${key}`;
- let data = await this.redis.get(key);
- if (!data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求超时,请重新进入下单页');
- data = JSON.parse(data);
- console.log(data);
- let specsData = [];
- if (_.isArray(data)) {
- // 购物车来的: 1.循环校验 规格商品; 2.按店铺分组
- const { populate } = this.ctx.service.trade.cart.getRefMods();
- const carts = await this.cartModel.find({ _id: data }).populate(populate);
- for (const cart of carts) {
- const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(cart, false);
- if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
- }
- specsData = this.setCartsGoodsToPageData(carts);
- } else if (_.isObject(data)) {
- // 商品页单独买: 1.校验规格商品; 2:按店铺分组
- const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
- if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
- const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
- const list = await this.goodsSpecModel.find({ _id: data.goodsSpec }).populate(populate);
- specsData = this.setGoodsToPageData(list, data.num);
- } else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
- // 组装页面的数据
- const user = this.ctx.user;
- const customer = _.get(user, '_id');
- if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
- const pageData = {};
- // 找到默认地址
- const address = await this.addressModel.findOne({ customer, is_default: '1' });
- pageData.address = address;
- // // 商品总价,各店铺的价格明细
- specsData = this.computedShopTotal(specsData);
- const shopTotalDetail = this.computedAllTotal(specsData);
- pageData.goodsData = specsData;
- pageData.orderTotal = shopTotalDetail;
- return pageData;
- }
- /**
- * 计算商店的结算
- * @param {Array} list 按店铺分组的商品列表
- */
- computedShopTotal(list) {
- for (const i of list) {
- i.goods_total = i.goods.reduce((p, n) => p + (n.money || 0) * (n.num || 0), 0);
- i.freight_total = i.goods.reduce((p, n) => p + (n.freight || 0) * (n.num || 0), 0);
- console.log(i);
- }
- return list;
- }
- /**
- * 计算订单的结算
- * @param {Array} list 按店铺分组的商品列表
- */
- computedAllTotal(list) {
- const obj = {
- goods_total: list.reduce((p, n) => p + (n.goods_total || 0), 0),
- freight_total: list.reduce((p, n) => p + (n.freight_total || 0), 0),
- };
- return obj;
- }
- /**
- * 单商品整理数据,剩下的可以简略
- * @param {Array} list 规格数组
- * @param num 购买数量
- */
- setGoodsToPageData(list, num) {
- // 按店铺分组,精简字段
- const arr = [];
- for (const i of list) {
- const obj = {};
- obj.shop_name = _.get(i.goods, 'shop.name');
- obj.shop = _.get(i.goods, 'shop._id');
- const goods = {};
- goods.goods_id = _.get(i.goods, '_id');
- goods.goods_name = _.get(i.goods, 'name');
- goods.goodsSpec_id = _.get(i, '_id');
- goods.goodsSpec_name = _.get(i, 'name');
- goods.freight = _.get(i, 'freight');
- goods.money = _.get(i, 'sell_money');
- goods.num = num;
- goods.file = _.get(i.goods, 'file');
- obj.goods = [ goods ];
- arr.push(obj);
- }
- return arr;
- }
- /**
- *购物车数据整理
- * @param {Array} list 规格数组
- */
- setCartsGoodsToPageData(list) {
- const arr = [];
- list = _.groupBy(list, 'shop._id');
- for (const key in list) {
- const data = list[key];
- const shopData = _.get(_.head(data), 'shop');
- const obj = {};
- obj.shop = _.get(shopData, '_id');
- obj.shop_name = _.get(shopData, 'name');
- const goodsList = [];
- for (const i of data) {
- const goods = {};
- goods.cart_id = _.get(i, '_id');
- goods.goods_id = _.get(i.goods, '_id');
- goods.goods_name = _.get(i.goods, 'name');
- goods.goodsSpec_id = _.get(i.goodsSpec, '_id');
- goods.goodsSpec_name = _.get(i.goodsSpec, 'name');
- goods.freight = _.get(i.goodsSpec, 'freight');
- goods.money = _.get(i.goodsSpec, 'sell_money');
- goods.num = _.get(i, 'num');
- goods.file = _.get(i.goods, 'file', []);
- goodsList.push(goods);
- }
- obj.goods = goodsList;
- arr.push(obj);
- }
- return arr;
- }
- /**
- * 重组购物车商品
- * @param {Object} goods 商品
- * @param {Object} goodsSpec 商品规格
- * @param {Object} data 购物车数据
- */
- setCartGoodsData(data) {
- const { goods, goodsSpec, _id, num = 1 } = data;
- const obj = {};
- obj.cart_id = _id;
- obj.goods_id = _.get(goods, '_id');
- obj.goods_name = _.get(goods, 'name');
- obj.goodsSpec_id = _.get(goodsSpec, '_id');
- obj.goodsSpec_name = _.get(goodsSpec, 'name');
- obj.freight = _.get(goodsSpec, 'freight');
- obj.money = _.get(goodsSpec, 'sell_money');
- obj.num = num;
- obj.file = _.get(goods, 'file');
- return obj;
- }
- }
- module.exports = OrderService;
|