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.

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