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.

215 lines
7.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://doc.hyperf.io
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace App\Controller;
  12. use App\Model\CouponUserRecType;
  13. use App\Model\Coupon;
  14. use App\Model\CouponRec;
  15. use Hyperf\DbConnection\Db;
  16. use Hyperf\Redis\Redis;
  17. use Hyperf\Utils\ApplicationContext;
  18. class CouponController extends BaseController
  19. {
  20. /**
  21. * 获取用户可领取优惠卷接口
  22. */
  23. public function getSystemCouponUserList(){
  24. $user_id = $this->request->input('user_id');
  25. $receive_type = $this->request->input('receive_type');
  26. $c_ids = CouponUserRecType::where('receive_type',$receive_type)->pluck('system_coupon_user_id');
  27. $nowTime = time();
  28. $cr_ids = CouponRec::where('user_id',$user_id)->pluck('system_coupon_user_id');
  29. $ids = array_merge($c_ids->toArray(),$cr_ids->toArray());
  30. $ids = collect($ids)->unique();
  31. $c = Coupon::where('start_time','<=',$nowTime)
  32. ->where('end_time','>',$nowTime)
  33. ->whereRaw('inventory_use < inventory')
  34. ->whereIn('id',$ids)
  35. ->orderBy('weigh','desc')
  36. ->limit(4)
  37. ->get();
  38. return $this->success(['not_reveive'=>$c]);
  39. }
  40. public function userCouponAccount()
  41. {
  42. $user_id = $this->request->input('user_id');
  43. $nowTime = time();
  44. $userCouponCount = DB::table('ims_system_coupon_user_receive')
  45. ->leftJoin('ims_system_coupon_user', 'ims_system_coupon_user_receive.system_coupon_user_id', '=', 'ims_system_coupon_user.id')
  46. ->count();
  47. // $userCouponCount = CouponRec::with('coupon')->where('user_id',$user_id)->where('usable_start_time','<=',$nowTime)
  48. // ->where('usable_end_time','>',$nowTime)->count();
  49. return $this->success(['total'=>$userCouponCount]);
  50. }
  51. public function userReceiveCoupon()
  52. {
  53. $userId = $this->request->input("user_id");
  54. $receiveType = $this->request->input("receive_type");
  55. $ids = $this->request->input("ids");
  56. $test = $this->request->input("test", 0);
  57. $ids = explode(',', $ids);
  58. $cps = Coupon::whereIn('id', $ids)->get();
  59. $now = time();
  60. $success = [];
  61. $fail = [];
  62. foreach ($cps as $key => $cp) {
  63. $crt = CouponUserRecType::where(
  64. [
  65. 'system_coupon_user_id' => $cp->id,
  66. 'receive_type' => $receiveType
  67. ]
  68. )->first();
  69. $cr = new CouponRec;
  70. $cr->user_id = $userId;
  71. $cr->system_coupon_user_id = $cp->id;
  72. $cr->order_main_id = 0;
  73. $cr->receive_time = $now;
  74. $cr->number = $crt->one_receive_number;
  75. $cr->number_remain = $crt->one_receive_number;
  76. $cr->status = 0;
  77. $cr->update_time = $now;
  78. $cr->receive_type = $receiveType;
  79. if ($test && ($cp->id % 2)) {
  80. $fail[] = $cp;
  81. } else {
  82. //TODO 会有超发情况
  83. //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券
  84. if ($cp->inventory<=$cp->inventory_use||$cp->inventory<=($cp->inventory_use+$cr->number)){
  85. $fail[] = $cp;
  86. }else{
  87. $cp->inventory_use += $cr->number;//记录已领取的数量
  88. if ($cr->save()&&$cp->save()) {
  89. $success[] = $cp;
  90. } else {
  91. $fail[] = $cp;
  92. }
  93. }
  94. }
  95. }
  96. return $this->success([
  97. 'success' => $success,
  98. 'fail' => $fail,
  99. ]);
  100. }
  101. /**
  102. * 获取用户已经领取的优惠卷列表
  103. */
  104. public function getUserReceiveCouponList()
  105. {
  106. $userId = $this->request->input("user_id");
  107. $nowTime = time();
  108. $couponIds = CouponRec::where('user_id',$userId)
  109. ->whereIn('status',[0,1])
  110. ->orderBy('receive_time','desc')
  111. ->get()
  112. ->pluck('system_coupon_user_id');
  113. $not_expired = [];
  114. $expired = [];
  115. $couponIds = $couponIds->toArray();
  116. $coupons = Coupon::orderByRaw('FIELD(id, '.implode(", " , $couponIds).')')->get();
  117. foreach ($coupons as $key => $coupon) {
  118. if($coupon->usable_end_time < $nowTime){
  119. $expired[] = $coupon;
  120. }else{
  121. $not_expired[] = $coupon;
  122. }
  123. }
  124. $ret = ['not_expired'=>$not_expired,'expired'=>$expired];
  125. return $this->success($ret);
  126. }
  127. /**
  128. * 获取用户当前订单可用的优惠券列表
  129. * 按分类(1订单 等优惠)分组返回
  130. */
  131. public function getUserAvailableCoupons()
  132. {
  133. // 获取参数
  134. # 订单金额
  135. $orderAmount = $this->request->input('order_amount',0);
  136. # 用户id
  137. $userId = $this->request->input('user_id',0);
  138. # 市场id
  139. $marketId = $this->request->input('market_id',0);
  140. # 类型,1全平台 2线上 3线下,20200718默认全平台
  141. $type = $this->request->input('type',1);
  142. # 店铺类型id
  143. $storetypeId = $this->request->input('storetype_id',0);
  144. $storetypeIds = explode(',', str_replace(',', ',', $storetypeId));
  145. // 获取用户优惠券
  146. $currentTime = time();
  147. $container = ApplicationContext::getContainer();
  148. $redis = $container->get(Redis::class);
  149. $couponIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
  150. $data = Db::table('ims_system_coupon_user_receive as receive')
  151. ->select([
  152. 'receive.id as receive_id',
  153. 'receive.user_id',
  154. 'receive.number_remain',
  155. 'coupon.id',
  156. 'coupon.title',
  157. 'coupon.full_amount',
  158. 'coupon.discounts',
  159. 'coupon.usable_start_time',
  160. 'coupon.usable_end_time',
  161. 'coupon.discount_type'
  162. ])
  163. ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id')
  164. ->where(['receive.user_id' => $userId])
  165. ->whereIn('receive.status', [0,1])
  166. ->where('receive.number_remain', '>', 0);
  167. if (is_array($couponIds)&&!empty($couponIds)) {
  168. $data->whereNotIn('coupon.id', $couponIds);
  169. }
  170. $data = $data->whereIn('coupon.type', [1,$type])
  171. ->where('coupon.full_amount', '<=', $orderAmount)
  172. ->where('coupon.usable_start_time', '<=', $currentTime)
  173. ->where('coupon.usable_end_time', '>=', $currentTime)
  174. ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
  175. ->where('coupon.market_id', 'in', [0,$marketId])
  176. ->whereIn('coupon.storetype_id', $storetypeIds)
  177. ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
  178. ->get();
  179. return $this->success($data);
  180. }
  181. }