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.1 KiB

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