hasOne('PromGoods', 'id', 'prom_id')->cache(true,10); } public function goods() { return $this->hasOne('Goods', 'goods_id', 'goods_id')->cache(true,10)->field('goods_id,cat_id,store_count,is_on_sale,prom_type,prom_id,weight')->bind([ 'cat_id' => 'cat_id','store_count'=>'store_count','is_on_sale'=>'is_on_sale','weight'=>'weight' ]); } //能加入购物车的商品都是不是虚拟商品。 public function getIsVirtualAttr($value, $data) { return 0; } public function getSpecKeyNameArrAttr($value, $data) { if ($data['spec_key_name']) { $specKeyNameArr = explode(' ', $data['spec_key_name']); return $specKeyNameArr; } else { return []; } } public function combinationCart() { return $this->hasMany('Cart', 'combination_group_id', 'id'); } public function combination() { return $this->hasOne('combination', 'combination_id', 'prom_id'); } public function combinationGoods() { return $this->hasMany('combination_goods', 'combination_id', 'prom_id')->order('is_master','desc'); } /** * 商品优惠总额 * @param $value * @param $data * @return mixed */ public function getGoodsFeeAttr($value, $data) { $goods_fee = round($data['goods_num'] * $data['member_goods_price'], 2); return $goods_fee; } /** * 商品总额 * @param $value * @param $data * @return mixed */ public function getTotalFeeAttr($value, $data) { $total_fee = round($data['goods_num'] * $data['goods_price'], 2); return $total_fee; } /** * 商品总额优惠 * @param $value * @param $data * @return mixed */ public function getCutFeeAttr($value, $data) { $cut_fee = $data['goods_num'] * ($data['goods_price'] - $data['member_goods_price']); return $cut_fee; } /** * 限购数量 * @param $value * @param $data * @return mixed */ public function getLimitNumAttr($value, $data) { $spec_goods_price = null; $goods = Goods::get($data['goods_id'], '', 20); //有规格 if ($data['spec_key']) { $spec_goods_price = SpecGoodsPrice::get(['goods_id'=>$data['goods_id'],'key' => $data['spec_key']]); if ($data['prom_type'] == 1) { $FlashSaleLogic = new FlashSaleLogic($goods, $spec_goods_price); $limitNum = $FlashSaleLogic->getUserFlashResidueGoodsNum($data['user_id']); } else if ($data['prom_type'] == 2) { $groupBuyLogic = new GroupBuyLogic($goods, $spec_goods_price); $limitNum = $groupBuyLogic->getPromotionSurplus();//团购剩余库存 }else if ($data['prom_type'] == 3) { $promoGoodsLogic = new PromGoodsLogic($goods, $spec_goods_price); $limitNum = $promoGoodsLogic->getPromoGoodsResidueGoodsNum($data['user_id']); } else { $limitNum = $spec_goods_price['store_count']; } }else{ //没有规格 if ($data['prom_type'] == 1) { $FlashSaleLogic = new FlashSaleLogic($goods, null); $limitNum = $FlashSaleLogic->getUserFlashResidueGoodsNum($data['user_id']); } else if ($data['prom_type'] == 2) { $groupBuyLogic = new GroupBuyLogic($goods, null); $limitNum = $groupBuyLogic->getPromotionSurplus();//团购剩余库存 }else if ($data['prom_type'] == 3) { $promoGoodsLogic = new PromGoodsLogic($goods, null); $limitNum = $promoGoodsLogic->getPromoGoodsResidueGoodsNum($data['user_id']); } else { $limitNum = $goods['store_count']; } } return $limitNum; } }