CouponUseScope.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/28
  6. * Time: 下午3:00
  7. */
  8. namespace app\frontend\modules\coupon\services\models\UseScope;
  9. use app\common\models\goods\GoodsCoupon;
  10. use app\common\modules\orderGoods\OrderGoodsCollection;
  11. use app\frontend\modules\coupon\services\models\Coupon;
  12. use app\frontend\modules\orderGoods\models\PreOrderGoodsCollection;
  13. abstract class CouponUseScope
  14. {
  15. protected $orderGoods;
  16. public function valid()
  17. {
  18. if($this->getOrderGoodsOfUsedCoupon()->isNotEmpty()){
  19. //todo 此处有bug ,如果调用处提前结束了判断条件,会导致 orderGoodsGroup属性获取失败
  20. $this->setOrderGoodsCollection();
  21. return true;
  22. }
  23. return false;
  24. }
  25. /**
  26. * (缓存)订单中使用了该优惠券的商品组
  27. * @return Collection
  28. */
  29. protected function getOrderGoodsOfUsedCoupon(){
  30. if(isset($this->orderGoods)){
  31. return $this->orderGoods;
  32. }
  33. $this->orderGoods = $this->_getOrderGoodsOfUsedCoupon();
  34. //todo 新加商品不能使用优惠券功能
  35. if (!$this->orderGoods->isEmpty()) {
  36. $goodsIds = $this->orderGoods->pluck('goods_id')->all();
  37. $goodsCoupon = GoodsCoupon::select('goods_id','no_use')->whereIn('goods_id',$goodsIds)->get()->toArray();
  38. $goodsCoupon = array_column($goodsCoupon,null,'goods_id');
  39. $this->orderGoods = $this->orderGoods->filter(function ($goods) use ($goodsCoupon){
  40. if(!empty($goodsCoupon[$goods->goods_id]['no_use'])){
  41. return false;
  42. }
  43. return true;
  44. });
  45. }
  46. return $this->orderGoods;
  47. }
  48. /**
  49. * @var Coupon
  50. */
  51. protected $coupon;
  52. /**
  53. * @var PreOrderGoodsCollection
  54. */
  55. protected $orderGoodsGroup;
  56. public function __construct(Coupon $coupon)
  57. {
  58. $this->coupon = $coupon;
  59. }
  60. public function getOrderGoodsInScope(){
  61. return $this->orderGoodsGroup;
  62. }
  63. /**
  64. * 将订单商品装入 订单商品组对象
  65. */
  66. protected function setOrderGoodsCollection()
  67. {
  68. //dd($this->getOrderGoodsOfUsedCoupon());
  69. $this->orderGoodsGroup = new OrderGoodsCollection($this->getOrderGoodsOfUsedCoupon());
  70. }
  71. abstract protected function _getOrderGoodsOfUsedCoupon();
  72. }