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.

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