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.

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