'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 TradeService extends CrudService { constructor(ctx) { super(ctx, 'trade'); this.redis = this.app.redis; this.redisKey = this.app.config.redisKey; this.redisTimeout = this.app.config.redisTimeout; this.shopModel = this.ctx.model.Shop.Shop; this.goodsModel = this.ctx.model.Shop.Goods; this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec; this.cartModel = this.ctx.model.Trade.Cart; } /** * 检查选中的购物车是否符合购买的条件 * @param {Object} body 请求体 * @param body.cartIds 选中的购物车id列表 * @param {Boolean} makeCache 生成缓存 */ async checkCartBuy({ cartIds }, makeCache = true) { const list = await this.cartModel.find({ _id: cartIds }); let result = { result: true }; for (const i of list) { const res = await this.checkCanBuy(i, false); if (res.result !== true) { result = res; break; } } if (makeCache) { const key = await this.makeOrderKey(cartIds); result.key = key; } return result; } /** * 检测是否可以购买该物品 * @param {Object} param 查询条件 * @param param.shop 商店id * @param param.goods 商品id * @param param.goodsSpec 商品规格id * @param param.num 购买数量 * @param param.type 订单类型: 0:常规单 1:团购单,需要按团购价计算 * @param param.group 团id * @param makeCache 生成缓存 */ async checkCanBuy({ shop, goods, goodsSpec, num, type = '0', group }, makeCache = true) { assert(shop, '缺少店铺信息'); assert(goods, '缺少商品信息'); assert(goodsSpec, '缺少商品规格信息'); assert(num, '缺少购买数量'); const result = { result: true }; // 1.检查商店是否正常运行 const shopData = await this.shopModel.findById(shop); const shopRes = this.checkShop(shopData); if (shopRes.result !== true) return shopRes; // 2.检查商品是否可以购买 const goodsData = await this.goodsModel.findById(goods); const goodsRes = this.checkGoods(goodsData); if (shopRes.result !== true) return goodsRes; // 3.检验该规格是否可以购买 const goodsSpecData = await this.goodsSpecModel.findById(goodsSpec); const gsRes = this.checkGoodsSpec(goodsSpecData, num); if (gsRes.result !== true) return gsRes; // #region 团购部分 // 4.检查是否是团购单 if (type === '1' && group) { // 为团购单,但是没有团id,团长开团.不需要检查 // 为团购单,且有团id: 团员参团 const groupRes = await this.ctx.service.group.group.checkGroupCanJoin({ id: group }); if (groupRes.result !== true) return groupRes; } // #endregion if (makeCache) { const key = await this.makeOrderKey({ shop, goods, goodsSpec, num, type, group }); result.key = key; } return result; } /** * 检查商店是否满足条件 * @param {Object} shopData 店铺数据 */ checkShop(shopData) { const result = { result: true }; if (!shopData) { result.msg = '未找到店铺'; result.result = false; return result; } if (shopData.status !== '1') { result.msg = '店铺不处于营业状态'; result.result = false; return result; } return result; } /** * 检查商品是否满足条件 * @param {Object} goodsData 商品数据 */ checkGoods(goodsData) { const result = { result: true }; if (!goodsData) { result.msg = '未找到商品'; result.result = false; return result; } if (goodsData.status === '0') { result.msg = '该商品已下架'; result.result = false; return result; } return result; } /** * 检查商品的规格是否满足购买条件 * @param {Object} goodsSpecData 商品规格的数据 * @param {Number} num 购买数量 */ checkGoodsSpec(goodsSpecData, num) { const result = { result: true }; if (!goodsSpecData) { result.msg = '未找到商品的指定规格'; result.result = false; return result; } if (goodsSpecData.status !== '0') { result.msg = '该规格的商品已下架'; result.result = false; return result; } if (goodsSpecData.num < num) { result.msg = '库存量不足'; result.result = false; return result; } return result; } // 随机字符串产生函数 createNonceStr() { return Math.random().toString(36).substr(2, 15); } // 生成key async makeOrderKey(data) { const str = this.createNonceStr(); const key = `${this.redisKey.orderKeyPrefix}${str}`; const value = JSON.stringify(data); await this.redis.set(key, value, 'EX', this.redisTimeout); return str; } } module.exports = TradeService;