You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
7.4 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Model\ShopCar;
  4. use App\Model\v3\Coupon;
  5. use App\Model\v3\CouponRec;
  6. use App\Service\v3\Interfaces\CouponRecServiceInterface;
  7. use Hyperf\DbConnection\Db;
  8. use Hyperf\Redis\Redis;
  9. use Hyperf\Utils\ApplicationContext;
  10. class CouponRecService implements CouponRecServiceInterface
  11. {
  12. public function do()
  13. {
  14. // TODO: Implement do() method.
  15. }
  16. public function check()
  17. {
  18. // TODO: Implement check() method.
  19. }
  20. public function undo()
  21. {
  22. // TODO: Implement undo() method.
  23. }
  24. /**
  25. * 获取当前订单可使用的优惠券
  26. * @param $totalAmount
  27. * @param $userId
  28. * @param $marketId
  29. * @param $type
  30. * @param $storeTypeIds
  31. * @return array
  32. */
  33. public function allForOrderOlAvailable($totalAmount, $userId, $marketId, $type, $storeTypeIds = [])
  34. {
  35. // 用户今日使用过的优惠券
  36. $redis = ApplicationContext::getContainer()->get(Redis::class);
  37. $couponTodayUsedIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
  38. $currentTime = time();
  39. $builder = Db::table('lanzu_coupon_receive as receive')
  40. ->join('lanzu_coupon as coupon', 'coupon.id', '=', 'receive.coupon_id', 'inner');
  41. if (is_array($couponTodayUsedIds)&&!empty($couponTodayUsedIds)) {
  42. $builder->whereNotIn('coupon.id', $couponTodayUsedIds);
  43. }
  44. foreach ($storeTypeIds as $key => &$item) {
  45. $item = (string)$item;
  46. }
  47. if (!empty($storeTypeIds)) {
  48. $builder->whereJsonContains('coupon.storetype_ids', $storeTypeIds);
  49. }
  50. $builder->where(['receive.user_id' => $userId])
  51. ->whereIn('receive.status', [0,1])
  52. ->where('receive.number_remain', '>', 0)
  53. ->whereIn('coupon.type', [1,$type])
  54. ->where('coupon.full_amount', '<=', $totalAmount)
  55. ->where('coupon.usable_start_time', '<=', $currentTime)
  56. ->where('coupon.usable_end_time', '>=', $currentTime)
  57. ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'));
  58. if ($marketId) {
  59. $builder->whereJsonContains('coupon.market_ids', [(string)$marketId]);
  60. }
  61. return $builder->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
  62. ->get()
  63. ->toArray();
  64. }
  65. /**
  66. * 用户未使用优惠券列表
  67. */
  68. public function getUnusedListByUser($userId,$page = 1,$pagesize = 5)
  69. {
  70. //查询可用优惠券
  71. $builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id')
  72. ->where([
  73. ['lanzu_coupon_receive.user_id' ,'=', $userId],
  74. ['lanzu_coupon.usable_end_time' ,'>', time()],
  75. ['lanzu_coupon_receive.number_remain' ,'>', 0]
  76. ]);
  77. $builder = $builder->whereIn('lanzu_coupon_receive.status',[0,1])
  78. ->select(
  79. 'lanzu_coupon.title',
  80. 'lanzu_coupon.discounts',
  81. 'lanzu_coupon.full_amount',
  82. 'lanzu_coupon.discount_type',
  83. 'lanzu_coupon.introduce',
  84. 'lanzu_coupon.usable_end_time',
  85. 'lanzu_coupon_receive.number',
  86. 'lanzu_coupon_receive.number_remain'
  87. )
  88. ->orderBy('lanzu_coupon.weigh','desc');
  89. $paginate = $builder->paginate($pagesize);
  90. $couponList = $paginate->toArray();
  91. foreach ($couponList['data'] as $key => &$coupon) {
  92. //拼接满减文字提示
  93. $coupon['full_amount_text'] = '满' . $coupon['full_amount'] . "可用";
  94. //判断是折扣优惠券还是满减优惠券
  95. if($coupon['discount_type'] == 1){
  96. $coupon['discounts_text'] = '¥'.$coupon['discounts'];
  97. }elseif($coupon['discount_type'] == 2){
  98. $coupon['discounts_text'] = floatval($coupon['discounts'])."";
  99. }
  100. }
  101. return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']];
  102. }
  103. /**
  104. * 用户已使用
  105. */
  106. public function getUsedListByUser($userId,$page = 1,$pagesize = 5)
  107. {
  108. //查询已使用优惠券
  109. $builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id')
  110. ->where('lanzu_coupon_receive.user_id' ,$userId)
  111. ->whereIn('lanzu_coupon_receive.status',[1,2])
  112. ->select(
  113. 'lanzu_coupon.title',
  114. 'lanzu_coupon.discounts',
  115. 'lanzu_coupon.full_amount',
  116. 'lanzu_coupon.discount_type',
  117. 'lanzu_coupon.introduce',
  118. 'lanzu_coupon.usable_end_time',
  119. 'lanzu_coupon_receive.number',
  120. 'lanzu_coupon_receive.number_remain'
  121. )
  122. ->orderBy('lanzu_coupon_receive.updated_at','desc');
  123. $paginate = $builder->paginate($pagesize);
  124. $couponList = $paginate->toArray();
  125. foreach ($couponList['data'] as $key => &$coupon) {
  126. //拼接满减文字提示
  127. $coupon['full_amount_text'] = '满' . $coupon['full_amount'] . "可用";
  128. //判断是折扣优惠券还是满减优惠券
  129. if($coupon['discount_type'] == 1){
  130. $coupon['discounts_text'] = '¥'.$coupon['discounts'];
  131. }elseif($coupon['discount_type'] == 2){
  132. $coupon['discounts_text'] = floatval($coupon['discounts'])."";
  133. }
  134. }
  135. return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']];
  136. }
  137. /**
  138. * 用户已失效
  139. */
  140. public function getExpiredListByUser($userId,$page = 1,$pagesize = 5)
  141. {
  142. //查询失效优惠券
  143. $builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id')
  144. ->where('lanzu_coupon_receive.user_id',$userId)
  145. ->where(function ($query) {
  146. $query->where('lanzu_coupon.usable_end_time', '<', time())
  147. ->orWhere('lanzu_coupon.status', '<>', 1);
  148. })
  149. ->select(
  150. 'lanzu_coupon.title',
  151. 'lanzu_coupon.discounts',
  152. 'lanzu_coupon.full_amount',
  153. 'lanzu_coupon.discount_type',
  154. 'lanzu_coupon.introduce',
  155. 'lanzu_coupon.usable_end_time',
  156. 'lanzu_coupon_receive.number',
  157. 'lanzu_coupon_receive.number_remain'
  158. )
  159. ->orderBy('lanzu_coupon.weigh','desc');
  160. $paginate = $builder->paginate($pagesize);
  161. $couponList = $paginate->toArray();
  162. foreach ($couponList['data'] as $key => &$coupon) {
  163. //拼接满减文字提示
  164. $coupon['full_amount_text'] = '满' . $coupon['full_amount'] . "可用";
  165. //判断是折扣优惠券还是满减优惠券
  166. if($coupon['discount_type'] == 1){
  167. $coupon['discounts_text'] = '¥'.$coupon['discounts'];
  168. }elseif($coupon['discount_type'] == 2){
  169. $coupon['discounts_text'] = floatval($coupon['discounts'])."";
  170. }
  171. }
  172. return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']];
  173. }
  174. }