1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- '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.shopModel = this.ctx.model.Shop.Shop;
- this.goodsModel = this.ctx.model.Shop.Goods;
- this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
- }
- /**
- * 检测是否可以购买该物品
- * @param {Object} param 查询条件
- * @param param.shop_id 商店id
- * @param param.goods_id 商品id
- * @param param.goodsSpec_id 商品规格id
- * @param param.num 购买数量
- */
- async checkCanBuy({ shop_id, goods_id, goodsSpec_id, num }) {
- assert(shop_id, '缺少店铺信息');
- assert(goods_id, '缺少商品信息');
- assert(goodsSpec_id, '缺少商品规格信息');
- assert(num, '缺少购买数量');
- const result = { result: true };
- // 1.检查商店是否正常运行
- const shop = await this.shopModel.findById(shop_id);
- if (!shop) {
- result.msg = '未找到店铺';
- result.result = false;
- return result;
- }
- if (shop.status !== '1') {
- result.msg = '店铺不处于营业状态';
- result.result = false;
- return result;
- }
- // 2.检查商品是否可以购买
- const goods = await this.goodsModel.findById(goods_id);
- if (!goods) {
- result.msg = '未找到商品';
- result.result = false;
- return result;
- }
- if (goods.status === '0') {
- result.msg = '该商品已下架';
- result.result = false;
- return result;
- }
- // 3.检验该规格是否可以购买
- const goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id);
- if (!goodsSpec) {
- result.msg = '未找到商品的指定规格';
- result.result = false;
- return result;
- }
- if (goods.status !== '0') {
- result.msg = '该规格的商品已下架';
- result.result = false;
- return result;
- }
- if (goods.num < num) {
- result.msg = '库存量不足';
- result.result = false;
- return result;
- }
- return result;
- }
- }
- module.exports = TradeService;
|