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.

138 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. /**
  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. */
  49. public function userReceiveCoupon(CouponRebateReceiveRequest $validator)
  50. {
  51. $userId = $this->request->input("user_id", 0);
  52. $receiveType = $this->request->input("receive_type", 0);
  53. $ids = $this->request->input("ids", '');
  54. $sendUserId = $this->request->input("send_user_id", 0);
  55. $phone = $this->request->input("phone", '');
  56. $ids = explode(',', $ids);
  57. $now = time();
  58. // mock 数据 给前端用
  59. $test = $this->request->input("test", 0);
  60. if($test){
  61. $cps = Coupon::whereIn('id', $ids)->lockForUpdate()
  62. ->select('id','title','status','inventory','inventory_use','start_time','end_time')
  63. ->first();
  64. return $this->success([
  65. 'success' => [$cps],
  66. 'fail' => [],
  67. ]);
  68. }
  69. $success = [];
  70. $fail = [];
  71. Db::transaction( function () use ($ids,$receiveType,$userId,$sendUserId,$phone,&$success,&$fail,$now) {
  72. //读写锁,完全控制,性能低
  73. $cps = Coupon::whereIn('id', $ids)->lockForUpdate()
  74. ->select('id','title','status','inventory','inventory_use','start_time','end_time')
  75. ->get();
  76. foreach ($cps as $key => $cp) {
  77. $where = [
  78. 'system_coupon_user_id' => $cp->id,
  79. ];
  80. if (env('SUB_CHANNEL') == 1) {
  81. $where['receive_type'] = $receiveType;
  82. }
  83. $crt = CouponUserRecType::where($where)->first();
  84. $cr = new CouponRec;
  85. $cr->user_id = $userId;
  86. $cr->system_coupon_user_id = $cp->id;
  87. $cr->order_main_id = 0;
  88. $cr->receive_time = $now;
  89. $cr->number = $crt->one_receive_number;
  90. $cr->number_remain = $crt->one_receive_number;
  91. $cr->status = 0;
  92. $cr->update_time = $now;
  93. $cr->receive_type = $receiveType;
  94. $cr->send_user_id = $sendUserId;
  95. $cr->phone = $phone;
  96. //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券
  97. if (
  98. $cp->status != 1
  99. ||
  100. $cp->inventory <= $cp->inventory_use
  101. ||
  102. $cp->inventory < ($cp->inventory_use+$cr->number)
  103. ||
  104. $cp->start_time < $now
  105. ||
  106. $cp->end_time > $now
  107. )
  108. {
  109. $fail[] = $cp;
  110. }else{
  111. $cp->inventory_use += $cr->number;//记录已领取的数量
  112. if ( $cr->save() && $cp->save() ) {
  113. $success[] = $cp;
  114. } else {
  115. $fail[] = $cp;
  116. }
  117. }
  118. }
  119. });
  120. return $this->success([
  121. 'success' => $success,
  122. 'fail' => $fail,
  123. ]);
  124. }
  125. }