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.

182 lines
7.3 KiB

6 years ago
  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. * 一次可领取多张优惠券
  22. * 一个用户不可重复领取
  23. */
  24. public function userReceiveCoupon($params)
  25. {
  26. $userId = $params["user_id"];
  27. $receiveType = $params["receive_type"];
  28. $ids = $params["ids"];
  29. $sendUserId = $params["send_user_id"];
  30. $phone = $params["phone"];
  31. $now = time();
  32. $ids = is_array($ids) ? implode(',',$ids) : $ids;
  33. // status: 0领取成功 >0领取失败
  34. $result = [
  35. 'status' => 1,
  36. 'coupon_text' => '继续努力~'
  37. ];
  38. // 错误日志记录
  39. $errorData = [
  40. 'coupon_ids' =>$ids,
  41. 'user_id' =>$userId,
  42. 'receiveType' =>$receiveType,
  43. 'sendUserId' =>$sendUserId,
  44. 'phone' =>$phone
  45. ];
  46. Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) {
  47. try{
  48. //读写锁,完全控制,性能低
  49. $coupons = Coupon::whereIn('id', $ids)->lockForUpdate()
  50. ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts')
  51. ->get();
  52. foreach($coupons as $coupon){
  53. $errorData['coupon_id'] = $coupon->id;
  54. //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内可领取, 否则返回领取失败的优惠券
  55. if (
  56. $coupon->status == 1
  57. &&
  58. $coupon->inventory > $coupon->inventory_use
  59. &&
  60. $coupon->start_time <= $now
  61. &&
  62. $coupon->end_time >= $now
  63. )
  64. {
  65. // 查询一次能领取的数量
  66. $couponReceiveType = CouponUserRecType::where('system_coupon_user_id',$coupon->id)->select('one_receive_number');
  67. if (env('SUB_CHANNEL') == 1) {
  68. $couponReceiveType->where('receive_type',$receiveType);
  69. }
  70. $couponReceiveType = $couponReceiveType->first();
  71. // 优惠券可领取数量 >= 本次领取数量
  72. if($coupon->inventory - $coupon->inventory_use >= $couponReceiveType->one_receive_number){
  73. // 判断是否领取过 存在记录则领取过
  74. $isReceive = CouponRec::select('id')
  75. ->where('system_coupon_user_id',$ids)
  76. ->where('user_id',$userId)
  77. ->exists();
  78. if(!$isReceive){
  79. //记录已领取的数量
  80. $coupon->inventory_use += $couponReceiveType->number;
  81. $couponReceive = new CouponRec;
  82. $couponReceive->user_id = $userId;
  83. $couponReceive->system_coupon_user_id = $coupon->id;
  84. $couponReceive->order_main_id = 0;
  85. $couponReceive->receive_time = $now;
  86. $couponReceive->number = $couponReceiveType->one_receive_number;
  87. $couponReceive->number_remain = $couponReceiveType->one_receive_number;
  88. $couponReceive->status = 0;
  89. $couponReceive->update_time = $now;
  90. $couponReceive->receive_type = $receiveType;
  91. $couponReceive->send_user_id = $sendUserId;
  92. $couponReceive->phone = $phone;
  93. if ( $couponReceive->save() && $coupon->save() ) {
  94. $result['status'] = 0;
  95. $result['coupon_text'] = '恭喜您领取成功!';
  96. }
  97. }else{
  98. $result['status'] = 2;
  99. $result['coupon_text'] = '您已领取!赶快去下单吧~';
  100. $errorData['msg'] = '用户已经领取了优惠券';
  101. $this->log->event(
  102. LogLabel::COUPON_LOG,
  103. $errorData
  104. );
  105. }
  106. }else{
  107. $errorData['remain_receive_number'] = $coupon->inventory - $coupon->inventory_use;
  108. $errorData['one_receive_number'] = $couponReceiveType->one_receive_number;
  109. $errorData['msg'] = '优惠券剩余数量不足';
  110. $this->log->event(
  111. LogLabel::COUPON_LOG,
  112. $errorData
  113. );
  114. }
  115. }else{
  116. $errorData['msg'] = '优惠券已经过期或者被禁用或者已领完';
  117. $this->log->event(
  118. LogLabel::COUPON_LOG,
  119. $errorData
  120. );
  121. }
  122. }
  123. } catch (Exception $e){
  124. $errorData['msg'] = $e->getMessage();
  125. $this->log->event(
  126. LogLabel::COUPON_LOG,
  127. $errorData
  128. );
  129. }
  130. });
  131. return $result;
  132. }
  133. public function isCouponRebate($user_id)
  134. {
  135. $res = Db::table('ims_system_coupon_user as u')
  136. ->leftjoin('ims_system_coupon_user_receive as r','u.id','=','r.system_coupon_user_id')
  137. ->where([
  138. ['r.user_id','=',$user_id],
  139. ['r.receive_type','=',4],
  140. ['u.active_type','=',2],
  141. ])
  142. ->select('r.id')
  143. ->first();
  144. return $res;
  145. }
  146. public function getActiveInfo()
  147. {
  148. $time = time();
  149. $res = Db::table('ims_system_coupon_user')
  150. ->where([
  151. ['status','=',1],
  152. ['active_type','=',2],
  153. ['start_time','<=',$time],
  154. ['end_time','>',$time],
  155. ])
  156. ->whereRaw('inventory > inventory_use')
  157. ->orderBy('addtime','desc')
  158. ->get();
  159. return $res;
  160. }
  161. }