CouponConditionService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\frontend\modules\coupon\services;
  3. use app\frontend\modules\coupon\models\Coupon;
  4. use app\frontend\modules\coupon\models\Goods;
  5. use app\frontend\modules\coupon\models\Store;
  6. class CouponConditionService
  7. {
  8. private $latitude = null;
  9. private $longitude = null;
  10. /**
  11. * 获取优惠卷条件中的门店和商品
  12. * @param Coupon $coupon
  13. * @return array
  14. */
  15. public function getCondition (Coupon $coupon)
  16. {
  17. $this->latitude = request()->latitude;
  18. $this->longitude = request()->longitude;
  19. /**
  20. * @var $storeMo Store
  21. */
  22. $storeMo = app(Store::class);
  23. $data = [];
  24. switch ($coupon['use_type']){
  25. case 4 : $data = $this->getStore($coupon, $storeMo); break;
  26. case 9 : $data = $this->getStoreAndGoods($coupon, $storeMo); break;
  27. case 5 : $data = $this->getIndependentStore($coupon, $storeMo); break;
  28. default : break;
  29. }
  30. //判断是否为独立后台标识
  31. $data['is_shop'] = $coupon['plugin_id'] ? true : false;
  32. return $data;
  33. }
  34. /**
  35. * 优惠卷是独立门店和使用类型是5
  36. * 获取独立门店数据
  37. * @param Store $storeMo
  38. * @param Coupon $coupon
  39. * @return array
  40. */
  41. private function getIndependentStore(Coupon $coupon, Store $storeMo) : array
  42. {
  43. $data = [];
  44. if($coupon['plugin_id']){
  45. $store = $storeMo->getStoreList([$coupon['storeids']], $this->latitude, $this->longitude)->toArray();
  46. $data['store_list'] = $store['data'];
  47. }
  48. return $data;
  49. }
  50. /**
  51. * <h1>指定门店</h1>
  52. * 获取门店
  53. * @param Coupon $coupon
  54. * @param Store $storeMo
  55. * @return array
  56. */
  57. private function getStore($coupon, $storeMo) : array
  58. {
  59. $store_ids = json_decode($coupon['storeids']);
  60. $data = [];
  61. $store_list = [];
  62. if($store_ids != null){
  63. $store_list = $storeMo->getStoreList($store_ids, $this->latitude, $this->longitude);
  64. }
  65. $data['condition_type'] = 1;
  66. $data['store_list'] = $store_list;
  67. return $data;
  68. }
  69. /**
  70. * <h1>选择商品+选择门店</h1>
  71. * 获取商品和门店
  72. * @param Coupon $coupon
  73. * @param Store $storeMo
  74. * @return array
  75. */
  76. private function getStoreAndGoods($coupon, $storeMo) : array
  77. {
  78. /*门店*/
  79. $use_conditions = unserialize($coupon['use_conditions']);
  80. $store_list = [];
  81. if($use_conditions['is_all_store'] == 1){ // 全选门店
  82. $store_list = $storeMo->getStoreList([], $this->latitude, $this->longitude);
  83. }elseif ($use_conditions['store_ids'] != null){ //指定门店
  84. $store_list = $storeMo->getStoreList($use_conditions['store_ids'], $this->latitude, $this->longitude);
  85. }
  86. /*门店*/
  87. /*商品*/
  88. $goods_list = [];
  89. $goods_mo = Goods::select(['title', 'price', 'thumb', 'id'])->uniacid();
  90. if($use_conditions['is_all_good'] == 1){ // 全选商品
  91. $goods_list = $goods_mo->limit(10)->get();
  92. }elseif ($use_conditions['good_ids'] != null){ //指定商品
  93. $goods_list = $goods_mo->whereIn('id', $use_conditions['good_ids']?:[])->paginate(16,[],'goods_page');
  94. }
  95. /*商品*/
  96. $data['goods_list'] = $goods_list;
  97. $data['store_list'] = $store_list;
  98. $data['condition_type'] = 2;
  99. $data['is_all_good'] = $use_conditions['is_all_good'];
  100. $data['is_all_store'] = $use_conditions['is_all_store'];
  101. return $data;
  102. }
  103. }