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.

128 lines
4.5 KiB

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 Hyperf\DbConnection\Db;
  13. use Hyperf\Redis\Redis;
  14. use Hyperf\Utils\ApplicationContext;
  15. use App\CouponRebate\CouponRebateInterface;
  16. use http\Client\Curl\User;
  17. use Hyperf\Di\Annotation\Inject;
  18. use App\Model\Coupon;
  19. use App\Model\CouponUserRecType;
  20. use App\Model\CouponRec;
  21. use App\Request\CouponRebateReceiveRequest;
  22. class CouponRebateController extends BaseController
  23. {
  24. /**
  25. * @Inject
  26. * @var CouponRebateInterface
  27. */
  28. protected $CouponRebate;
  29. public function isCouponRebate()
  30. {
  31. $user_id = $this->request->input('user_id', 0);
  32. $res = $this->CouponRebate->isCouponRebate($user_id);
  33. return $this->success($res);
  34. }
  35. /**
  36. * 用户领取优惠券 2020.08.04 只领一张
  37. */
  38. public function userReceiveCoupon(CouponRebateReceiveRequest $validator)
  39. {
  40. $userId = $this->request->input("user_id", 0);
  41. $receiveType = $this->request->input("receive_type", 0);
  42. $id = $this->request->input("id", 0);
  43. $sendUserId = $this->request->input("send_user_id", 0);
  44. $phone = $this->request->input("phone", '');
  45. $now = time();
  46. // status : 0 领取成功 >0 领取失败
  47. $return = [
  48. 'status' => 1,
  49. 'data' => [],
  50. 'coupon_text' => '继续努力~'
  51. ];
  52. // mock 数据 给前端用
  53. $test = $this->request->input("test", 0);
  54. if($test){
  55. $cps = Coupon::where('id', $id)->lockForUpdate()
  56. ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts')
  57. ->first();
  58. $return['status'] = 0;
  59. $return['data'] = $cps;
  60. $return['coupon_text'] = $cps['discounts'].'元红包';
  61. return $this->success($return);
  62. }
  63. Db::transaction( function () use ($id,$receiveType,$userId,$sendUserId,$phone,&$success,&$fail,$now) {
  64. //读写锁,完全控制,性能低
  65. $cp = Coupon::where('id', $id)->lockForUpdate()
  66. ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts')
  67. ->first();
  68. $where = [
  69. 'system_coupon_user_id' => $cp->id,
  70. ];
  71. if (env('SUB_CHANNEL') == 1) {
  72. $where['receive_type'] = $receiveType;
  73. }
  74. // 查询领取类型一次能领取的数量
  75. $crt = CouponUserRecType::where($where)->first();
  76. $cr = new CouponRec;
  77. $cr->user_id = $userId;
  78. $cr->system_coupon_user_id = $cp->id;
  79. $cr->order_main_id = 0;
  80. $cr->receive_time = $now;
  81. $cr->number = $crt->one_receive_number;
  82. $cr->number_remain = $crt->one_receive_number;
  83. $cr->status = 0;
  84. $cr->update_time = $now;
  85. $cr->receive_type = $receiveType;
  86. $cr->send_user_id = $sendUserId;
  87. $cr->phone = $phone;
  88. //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券
  89. if (
  90. $cp->status != 1
  91. ||
  92. $cp->inventory <= $cp->inventory_use
  93. ||
  94. $cp->inventory < ($cp->inventory_use+$cr->number)
  95. ||
  96. $cp->start_time < $now
  97. ||
  98. $cp->end_time > $now
  99. )
  100. {
  101. $return['status'] = 2;
  102. $return['coupon_text'] = '红包已经领完~';
  103. }else{
  104. $cp->inventory_use += $cr->number;//记录已领取的数量
  105. if ( $cr->save() && $cp->save() ) {
  106. $return['status'] = 0;
  107. $return['data'] = $cps;
  108. $return['coupon_text'] = $cps['discounts'].'元红包';
  109. } else {
  110. $return['status'] = 3;
  111. $return['coupon_text'] = '下次继续努力~';
  112. }
  113. }
  114. });
  115. return $this->success($return);
  116. }
  117. }