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.

222 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
  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. /**
  52. * 用户领取优惠卷
  53. */
  54. public function userReceiveCoupon()
  55. {
  56. $userId = $this->request->input("user_id");
  57. $receiveType = $this->request->input("receive_type");
  58. $ids = $this->request->input("ids");
  59. $test = $this->request->input("test", 0);
  60. $ids = explode(',', $ids);
  61. $cps = Coupon::whereIn('id', $ids)->get();
  62. $now = time();
  63. $success = [];
  64. $fail = [];
  65. foreach ($cps as $key => $cp) {
  66. $crt = CouponUserRecType::where(
  67. [
  68. 'system_coupon_user_id' => $cp->id,
  69. 'receive_type' => $receiveType
  70. ]
  71. )->first();
  72. //TODO 会有超发情况
  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) {
  84. $fail[] = $cp;
  85. } else {
  86. if ($cr->save()) {
  87. $success[] = $cp;
  88. } else {
  89. $fail[] = $cp;
  90. }
  91. }
  92. }
  93. return $this->success([
  94. 'success' => $success,
  95. 'fail' => $fail,
  96. ]);
  97. }
  98. /**
  99. * 获取用户已经领取的优惠卷列表
  100. */
  101. public function getUserReceiveCouponList()
  102. {
  103. $userId = $this->request->input("user_id");
  104. $nowTime = time();
  105. $couponIds = CouponRec::where('user_id',$userId)
  106. ->whereIn('status',[0,1])
  107. ->orderBy('receive_time','desc')
  108. ->get()
  109. ->pluck('system_coupon_user_id');
  110. $not_expired = [];
  111. $expired = [];
  112. $couponIds = $couponIds->toArray();
  113. $coupons = Coupon::orderByRaw('FIELD(id, '.implode(", " , $couponIds).')')->get();
  114. foreach ($coupons as $key => $coupon) {
  115. if($coupon->usable_end_time < $nowTime){
  116. $expired[] = $coupon;
  117. }else{
  118. $not_expired[] = $coupon;
  119. }
  120. }
  121. $ret = ['not_expired'=>$not_expired,'expired'=>$expired];
  122. return $this->success($ret);
  123. }
  124. /**
  125. * 获取用户当前订单可用的优惠券列表
  126. * 按分类(1订单 等优惠)分组返回
  127. */
  128. public function getUserAvailableCoupons()
  129. {
  130. // 获取参数
  131. # 订单金额
  132. $orderAmount = $this->request->input('order_amount',0);
  133. # 用户id
  134. $userId = $this->request->input('user_id',0);
  135. # 市场id
  136. $marketId = $this->request->input('market_id',0);
  137. # 类型,1全平台 2线上 3线下,20200718默认全平台
  138. $type = $this->request->input('type',1);
  139. # 店铺类型id
  140. $storetypeId = $this->request->input('storetype_id',0);
  141. // 获取用户优惠券
  142. $currentTime = time();
  143. $container = ApplicationContext::getContainer();
  144. $redis = $container->get(Redis::class);
  145. $couponDayUsed = $redis->hGetAll('coupon_day_used');
  146. $couponRecIds = array_values($couponDayUsed);
  147. $data = Db::table('ims_system_coupon_user_receive as receive')
  148. ->select([
  149. 'receive.id as receive_id',
  150. 'receive.user_id',
  151. 'receive.number_remain',
  152. 'coupon.id',
  153. 'coupon.title',
  154. 'coupon.full_amount',
  155. 'coupon.discounts',
  156. 'coupon.usable_start_time',
  157. 'coupon.usable_end_time',
  158. 'coupon.discount_type'
  159. ])
  160. ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id')
  161. ->whereNotIn('receive.id', $couponRecIds)
  162. ->where(['receive.user_id' => $userId])
  163. ->where(['receive.user_id' => $userId])
  164. ->whereIn('coupon.type', [1,$type])
  165. ->whereIn('receive.status', [0,1])
  166. ->where('receive.number_remain', '>', 0)
  167. ->where('coupon.full_amount', '<=', $orderAmount)
  168. ->where('coupon.usable_start_time', '<=', $currentTime)
  169. ->where('coupon.usable_end_time', '>=', $currentTime)
  170. ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
  171. ->where('coupon.market_id', 'in', [0,$marketId])
  172. ->where(function ($query) use ($storetypeId) {
  173. $query->whereOr(
  174. [
  175. [
  176. ['coupon.type', 'in', [1,2]],
  177. ['coupon.storetype_id', '=', 0]
  178. ],
  179. [
  180. ['coupon.type', 'in', [1,3]],
  181. ['coupon.storetype_id', '=', $storetypeId]
  182. ]
  183. ]
  184. );
  185. })
  186. ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
  187. ->get();
  188. return $this->success($data);
  189. }
  190. }