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.

143 lines
4.1 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
  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. class CouponController extends BaseController
  17. {
  18. /**
  19. * 获取用户可领取优惠卷接口
  20. */
  21. public function getSystemCouponUserList(){
  22. $user_id = $this->request->input('user_id');
  23. $receive_type = $this->request->input('receive_type');
  24. $c_ids = CouponUserRecType::where('receive_type',$receive_type)->pluck('system_coupon_user_id');
  25. $nowTime = time();
  26. $cr_ids = CouponRec::where('user_id',$user_id)->pluck('system_coupon_user_id');
  27. $ids = array_merge($c_ids->toArray(),$cr_ids->toArray());
  28. $ids = collect($ids)->unique();
  29. $c = Coupon::where('start_time','<=',$nowTime)
  30. ->where('end_time','>',$nowTime)
  31. ->whereRaw('inventory_use < inventory')
  32. ->whereIn('id',$ids)
  33. ->orderBy('weigh','desc')
  34. ->limit(4)
  35. ->get();
  36. return $this->success(['not_reveive'=>$c]);
  37. }
  38. public function userCouponAccount()
  39. {
  40. $user_id = $this->request->input('user_id');
  41. $nowTime = time();
  42. $userCouponCount = DB::table('ims_system_coupon_user_receive')
  43. ->leftJoin('ims_system_coupon_user', 'ims_system_coupon_user_receive.system_coupon_user_id', '=', 'ims_system_coupon_user.id')
  44. ->count();
  45. // $userCouponCount = CouponRec::with('coupon')->where('user_id',$user_id)->where('usable_start_time','<=',$nowTime)
  46. // ->where('usable_end_time','>',$nowTime)->count();
  47. return $this->success(['total'=>$userCouponCount]);
  48. }
  49. /**
  50. * 获取用户当前订单可用的优惠券列表
  51. * 按分类(1订单 等优惠)分组返回
  52. */
  53. public function getUserAvailableCoupons()
  54. {
  55. $userId = $this->request->input("user_id");
  56. $receiveType = $this->request->input("receive_type");
  57. $ids = $this->request->input("ids");
  58. $type = $this->request->input("test",0);
  59. $ids = is_array($ids)?implode(',',$ids):$ids;
  60. $cps = Coupon::whereIn('id', $ids)->get();
  61. $now = time();
  62. $success = [];
  63. $fail = [];
  64. foreach ($cps as $key => $cp) {
  65. $crt = CouponUserRecType::where('system_coupon_user_id',$cp->id)->first();
  66. //TODO 会有超发情况
  67. $cr = new CouponRec;
  68. $cr->user_id = $userId;
  69. $cr->system_coupon_user_id = $cp->id;
  70. $cr->order_main_id = 0;
  71. $cr->receive_time = $now;
  72. $cr->number = $crt->one_receive_number;
  73. $cr->nnumber_remain = $crt->one_receive_number;
  74. $cr->status = 0;
  75. $cr->update_time = $now;
  76. $cr->receive_type = $receiveType;
  77. if($cr->save()){
  78. $success[] = $cp;
  79. }else{
  80. $fail[] = $cp;
  81. }
  82. }
  83. return $this->success([
  84. 'success'=>$success,
  85. 'fail'=>$fail,
  86. ]);
  87. }
  88. /**
  89. * 获取用户已经领取的优惠卷列表
  90. */
  91. public function getUserReceiveCouponList()
  92. {
  93. $userId = $this->request->input("user_id");
  94. $nowTime = time();
  95. $couponIds = CouponRec::where('user_id',$userId)
  96. ->whereIn('status',[0,1])
  97. ->orderBy('receive_time','desc')
  98. ->get()
  99. ->pluck('system_coupon_user_id');
  100. $not_expired = [];
  101. $expired = [];
  102. $couponIds = $couponIds->toArray();
  103. $coupons = Coupon::orderByRaw('FIELD(id, '.implode(", " , $couponIds).')')->get();
  104. foreach ($coupons as $key => $coupon) {
  105. if($coupon->usable_end_time < $nowTime){
  106. $expired[] = $coupon;
  107. }else{
  108. $not_expired[] = $coupon;
  109. }
  110. }
  111. $ret = ['not_expired'=>$not_expired,'expired'=>$expired];
  112. return $this->success($ret);
  113. }
  114. }