|
@@ -8,19 +8,37 @@ 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({ goods_id, goodsSpec_id, 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.检查商品是否可以购买
|
|
|
+ // 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 = '未找到商品';
|
|
@@ -32,7 +50,7 @@ class TradeService extends CrudService {
|
|
|
result.result = false;
|
|
|
return result;
|
|
|
}
|
|
|
- // 2.检验该规格是否可以购买
|
|
|
+ // 3.检验该规格是否可以购买
|
|
|
const goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id);
|
|
|
if (!goodsSpec) {
|
|
|
result.msg = '未找到商品的指定规格';
|