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.

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