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.

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