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.

134 lines
4.5 KiB

  1. <?php
  2. namespace App\Service;
  3. use Hyperf\Di\Annotation\Inject;
  4. use Hyperf\DbConnection\Db;
  5. use App\Model\Coupon;
  6. use App\Model\CouponUserRecType;
  7. use App\Model\CouponRec;
  8. use App\Constants\LogLabel;
  9. use App\Commons\Log;
  10. use Exception;
  11. class CouponRebateService implements CouponRebateServiceInterface
  12. {
  13. /**
  14. * @Inject
  15. * @var Log
  16. */
  17. protected $log;
  18. /**
  19. * 领取优惠券
  20. */
  21. public function userReceiveCoupon($params)
  22. {
  23. $userId = $params["user_id"];
  24. $receiveType = $params["receive_type"];
  25. $id = $params["id"];
  26. $sendUserId = $params["send_user_id"];
  27. $phone = $params["phone"];
  28. $now = time();
  29. // status : 0 领取成功 >0 领取失败
  30. $result = [
  31. 'status' => 1,
  32. 'data' => [],
  33. 'coupon_text' => '继续努力~'
  34. ];
  35. // 错误日志记录
  36. $errorData = [
  37. 'coupon_id' =>$id,
  38. 'user_id' =>$userId,
  39. 'receiveType' =>$receiveType,
  40. 'sendUserId' =>$sendUserId,
  41. 'phone' =>$phone
  42. ];
  43. Db::transaction( function() use ($id,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) {
  44. try{
  45. //读写锁,完全控制,性能低
  46. $cp = Coupon::where('id', $id)->lockForUpdate()
  47. ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts')
  48. ->first();
  49. $couponReceiveType = CouponUserRecType::where('system_coupon_user_id',$cp->id)->select('one_receive_number');
  50. if (env('SUB_CHANNEL') == 1) {
  51. $couponReceiveType->where('receive_type',$receiveType);
  52. }
  53. // 查询一次能领取的数量
  54. $crt = $couponReceiveType->first();
  55. $cr = new CouponRec;
  56. $cr->user_id = $userId;
  57. $cr->system_coupon_user_id = $cp->id;
  58. $cr->order_main_id = 0;
  59. $cr->receive_time = $now;
  60. $cr->number = $crt->one_receive_number;
  61. $cr->number_remain = $crt->one_receive_number;
  62. $cr->status = 0;
  63. $cr->update_time = $now;
  64. $cr->receive_type = $receiveType;
  65. $cr->send_user_id = $sendUserId;
  66. $cr->phone = $phone;
  67. //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券
  68. if (
  69. $cp->status != 1
  70. ||
  71. $cp->inventory <= $cp->inventory_use
  72. ||
  73. $cp->inventory < ($cp->inventory_use+$cr->number)
  74. ||
  75. $cp->start_time > $now
  76. ||
  77. $cp->end_time < $now
  78. )
  79. {
  80. $result['status'] = 2;
  81. $result['coupon_text'] = '红包已经领完~';
  82. $errorData['msg'] = '优惠券已经过期或者被禁用或者已领完';
  83. $this->log->event(
  84. LogLabel::COUPON_LOG,
  85. $errorData
  86. );
  87. }else{
  88. // 判断是否领取过 存在记录则领取过
  89. $isReceive = CouponRec::select('id')
  90. ->where('system_coupon_user_id',$id)
  91. ->where('user_id',$userId)
  92. ->exists();
  93. if(!$isReceive){
  94. $cp->inventory_use += $cr->number;//记录已领取的数量
  95. if ( $cr->save() && $cp->save() ) {
  96. $result['status'] = 0;
  97. // $result['data'] = $cp;
  98. $result['coupon_text'] = $cp['discounts'].'元红包';
  99. }
  100. }else{
  101. $errorData['msg'] = '用户已经领取了此优惠券';
  102. $this->log->event(
  103. LogLabel::COUPON_LOG,
  104. $errorData
  105. );
  106. }
  107. }
  108. } catch (Exception $e){
  109. $errorData['msg'] = $e->getMessage();
  110. $this->log->event(
  111. LogLabel::COUPON_LOG,
  112. $errorData
  113. );
  114. }
  115. });
  116. return $result;
  117. }
  118. }