ExchangeCenterController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\frontend\modules\coupon\controllers;
  3. use app\common\components\ApiController;
  4. use app\common\models\Goods;
  5. use app\frontend\modules\coupon\models\Coupon;
  6. use app\frontend\modules\coupon\models\MemberCoupon;
  7. use app\frontend\modules\member\services\MemberCartService;
  8. use app\frontend\modules\memberCart\MemberCartCollection;
  9. use Carbon\Carbon;
  10. class ExchangeCenterController extends ApiController
  11. {
  12. /**
  13. * 兑换中心接口
  14. *
  15. * @return \Illuminate\Http\JsonResponse
  16. */
  17. public function index()
  18. {
  19. $pluginId = request()->input('platform_id', 0);
  20. $uid = \YunShop::app()->getMemberId();
  21. $coupons = MemberCoupon::getExchange($uid, $pluginId)->get();
  22. //过滤过期优惠券
  23. $coupons = $coupons->filter(function ($coupon) {
  24. return strtotime($coupon->time_end) > strtotime(date('Y-m-d')) || $coupon->time_end == '不限时间';
  25. });
  26. $coupons = $coupons->groupBy('coupon_id');
  27. $res = [];
  28. $list = [];
  29. foreach ($coupons as $key => $item) {
  30. if (empty($item)) {
  31. continue;
  32. }
  33. $list[$key] = $item[0]->toArray();
  34. $list[$key]['total'] = $item->count();
  35. $list[$key]['goods_id'] = array_unique($list[$key]['belongs_to_coupon']['goods_ids']);
  36. }
  37. if (!empty($list)) {
  38. $list = array_values(collect($list)->sortByDesc('get_time')->toArray());
  39. foreach ($list as &$re) {
  40. if ($pluginId == 32) {
  41. $store_goods = \Yunshop\StoreCashier\common\models\StoreGoods::select('goods_id','store_id')
  42. ->where('goods_id', $re['goods_id'][0])
  43. ->with(['store' => function ($query) {
  44. $query->select('id','store_name');
  45. }])->first();
  46. if ($store_goods) {
  47. $re['store_id'] = $store_goods->store_id;
  48. $re['store_name'] = $store_goods->store->store_name;
  49. }
  50. }
  51. if ($pluginId == 33) {
  52. $hotel_goods = \Yunshop\Hotel\common\models\HotelGoods::select('goods_id','hotel_id')
  53. ->where('goods_id', $re['goods_id'][0])
  54. ->with(['hotel' => function ($query) {
  55. $query->select('id','hotel_name');
  56. }])->first();
  57. if ($hotel_goods) {
  58. $re['hotel_id'] = $hotel_goods->hotel_id;
  59. $re['hotel_name'] = $hotel_goods->hotel->hotel_name;
  60. }
  61. }
  62. }
  63. }
  64. $res['list'] = $list;
  65. $res['navigation'][0] = [
  66. 'id' => 0,
  67. 'name' => '商城'
  68. ];
  69. if (app('plugins')->isEnabled('store-cashier')) {
  70. $res['navigation'][1] = [
  71. 'id' => 32,
  72. 'name'=>'门店'
  73. ];
  74. }
  75. if (app('plugins')->isEnabled('hotel')) {
  76. $res['navigation'][2] = [
  77. 'id' => 33,
  78. 'name' => HOTEL_NAME,
  79. ];
  80. }
  81. return $this->successJson('ok', $res);
  82. }
  83. /**
  84. * @return MemberCartCollection
  85. * @throws \app\common\exceptions\MemberNotLoginException
  86. */
  87. protected function getMemberCarts()
  88. {
  89. $data = request()->input('data');
  90. $couponCount = array_column($data,'coupon_id');
  91. //获取可以兑换的优惠券Id
  92. $memberCoupon = MemberCoupon::getExchange(\YunShop::app()->getMemberId(),0)
  93. ->whereIn('coupon_id',$couponCount)
  94. ->get()
  95. ->toArray();
  96. foreach ($memberCoupon as $key => $v) {
  97. $goodsIds[] = $v['belongs_to_coupon']['goods_ids'][0];
  98. if (strtotime($v['time_end']) < strtotime(date('Y-m-d')) && $v['time_end'] != '不限时间') {
  99. unset($memberCoupon[$key]);
  100. continue;
  101. }
  102. }
  103. $data = array_column($data,null,'coupon_id');
  104. $member_coupon_ids = [];
  105. foreach ($memberCoupon as $key => $value) {
  106. if ($data[$value['coupon_id']]) {
  107. if (count($member_coupon_ids[$value['coupon_id']]) == $data[$value['coupon_id']]['total']) {
  108. continue;
  109. }
  110. $member_coupon_ids [$value['coupon_id']][] = $value['id'];
  111. }
  112. }
  113. $member_coupon_id = array();
  114. foreach ($member_coupon_ids as $value) {
  115. foreach ($value as $v) {
  116. $member_coupon_id[] = $v;
  117. }
  118. }
  119. $member_coupon_ids = implode(',',$member_coupon_id);
  120. if (request()->input('is_exchange') == 1) {
  121. request()->offsetSet('member_coupon_ids', $member_coupon_ids);
  122. }
  123. $result = new MemberCartCollection();
  124. foreach ($data as $key => $value) {
  125. unset($value['coupon_id']);
  126. $good = Goods::with('hasManyOptions')->find($value['goods_id']);
  127. if ($good && $option = $good->hasManyOptions()->first()) { //兑换券使用商品有规格时取第一个
  128. $value['option_id'] = $option->id;
  129. }
  130. $result->push(MemberCartService::newMemberCart($value));
  131. }
  132. return $result;
  133. }
  134. /**
  135. * 验证
  136. */
  137. private function validateParam()
  138. {
  139. $this->validate([
  140. 'data' => 'required',
  141. 'data.0.goods_id' => 'required | min:1 |integer',
  142. 'data.0.total' => 'required | min:1 | integer',
  143. 'data.0.coupon_id' => 'required | min:1 |integer',
  144. ]);
  145. }
  146. /**
  147. * @return \Illuminate\Http\JsonResponse
  148. * @throws \app\common\exceptions\MemberNotLoginException
  149. */
  150. public function exchangeBuy()
  151. {
  152. $this->validateParam();
  153. $trade = $this->getMemberCarts()->getTrade();
  154. return $this->successJson('成功', $trade);
  155. }
  156. }