'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} data 查询条件 * @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 param.inviter 邀请用户id 返现部分 * @param makeCache 生成缓存 */ async checkCanBuy(data, makeCache = true) { if (!_.isArray(data)) data = [ data ]; let result = { result: true }; for (const i of data) { const { shop, goods, goodsSpec, num } = i; if (!shop) { result.result = false; result.msg = '缺少店铺信息'; break; } if (!goods) { result.result = false; result.msg = '缺少商品信息'; break; } if (!goodsSpec) { result.result = false; result.msg = '缺少商品规格信息'; break; } if (!num) { result.result = false; result.msg = '缺少购买数量'; break; } // 1.检查商店是否正常运行 const shopData = await this.shopModel.findById(shop); const shopRes = this.checkShop(shopData); if (shopRes.result !== true) { result = shopRes; break; } // 2.检查商品是否可以购买 const goodsData = await this.goodsModel.findById(goods); const goodsRes = this.checkGoods(goodsData); if (goodsRes.result !== true) { result = goodsRes; break; } // 3.检验该规格是否可以购买 const goodsSpecData = await this.goodsSpecModel.findById(goodsSpec); const gsRes = this.checkGoodsSpec(goodsSpecData, num); if (gsRes.result !== true) { result = gsRes; break; } } // #endregion if (result.result && makeCache) { const key = await this.makeOrderKey(data); 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;