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.

139 lines
5.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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.category_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 getListByUser($userId,$type,$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. ]);
  75. /**
  76. * $type unused 未使用 used 已使用 expired 已失效
  77. */
  78. switch ($type){
  79. case 'unused':
  80. $builder = $builder->where([
  81. ['lanzu_coupon.usable_end_time' ,'>', time()],
  82. ['lanzu_coupon_receive.number_remain' ,'>', 0]
  83. ])
  84. ->whereIn('lanzu_coupon_receive.status',[0,1]);
  85. break;
  86. case 'used':
  87. $builder = $builder->whereIn('lanzu_coupon_receive.status',[1,2]);
  88. break;
  89. case 'expired':
  90. $builder = $builder->where(function ($query) {
  91. $query->where('lanzu_coupon.usable_end_time', '<', time())
  92. ->orWhere('lanzu_coupon.status', '<>', 1);
  93. });
  94. break;
  95. }
  96. $builder = $builder->select(
  97. 'lanzu_coupon.title',
  98. 'lanzu_coupon.discounts',
  99. 'lanzu_coupon.full_amount',
  100. 'lanzu_coupon.discount_type',
  101. 'lanzu_coupon.introduce',
  102. 'lanzu_coupon.usable_start_time',
  103. 'lanzu_coupon.usable_end_time',
  104. 'lanzu_coupon_receive.number',
  105. 'lanzu_coupon_receive.number_remain'
  106. )
  107. ->orderBy('lanzu_coupon.weigh','desc');
  108. $paginate = $builder->paginate($pagesize);
  109. $couponList = $paginate->toArray();
  110. foreach ($couponList['data'] as $key => &$coupon) {
  111. //拼接满减文字提示
  112. $coupon['full_amount_text'] = '满' . $coupon['full_amount'] . "可用";
  113. //判断是折扣优惠券还是满减优惠券
  114. if($coupon['discount_type'] == 1){
  115. $coupon['discounts_text'] = '¥'.$coupon['discounts'];
  116. }elseif($coupon['discount_type'] == 2){
  117. $coupon['discounts_text'] = floatval($coupon['discounts'])."";
  118. }
  119. //拼接时间文字提示
  120. $coupon['time_text'] = date("Y-m-d H:i:s",$coupon['usable_start_time']). ' - ' .date("Y-m-d H:i:s",$coupon['usable_end_time']);
  121. }
  122. return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']];
  123. }
  124. }