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.

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