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.

257 lines
9.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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. 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. /*
  119. * 判断用户是否已领取过优惠券
  120. * */
  121. public function isCouponRebate($user_id)
  122. {
  123. $res = Db::table('ims_system_coupon_user as u')
  124. ->leftjoin('ims_system_coupon_user_receive as r','u.id','=','r.system_coupon_user_id')
  125. ->where([
  126. ['r.user_id' ,'=', $user_id],
  127. ['r.receive_type','=', 4],
  128. ['u.active_type' ,'=', 2],
  129. ])
  130. ->select('r.id')
  131. ->first();
  132. return $res;
  133. }
  134. /*
  135. *获取活动信息
  136. */
  137. public function getActiveInfo()
  138. {
  139. $time = time();
  140. $res = Db::table('ims_system_coupon_user')
  141. ->where([
  142. ['status' ,'=', 1],
  143. ['active_type' ,'=', 2],
  144. ['start_time' ,'<=', $time],
  145. ['end_time' ,'>', $time],
  146. ])
  147. ->whereRaw('inventory > inventory_use')
  148. ->orderBy('addtime','desc')
  149. ->get();
  150. return $res;
  151. }
  152. /*
  153. * 支付成功 返券
  154. */
  155. public function couponRebate($order_id)
  156. {
  157. /* 判断优惠券类型是否为转发活动优惠券 */
  158. $coupon = Db::table('ims_system_coupon_user_receive as r')
  159. ->leftjoin('ims_system_coupon_user_use as u', 'u.user_receive_id', '=', 'r.id')
  160. ->where([
  161. ['u.order_main_id', '=', $order_id],
  162. ['r.send_user_id', '>', 0],
  163. ['r.rebate_type', '=', 1],
  164. ['r.receive_type', '=', 4],
  165. ['u.status', '=', 1],
  166. ])
  167. ->select('r.id', 'r.send_user_id', 'u.system_coupon_id')
  168. ->first();
  169. /* 如果使用的优惠券为转发活动优惠券 则给赠送者返一张优惠券*/
  170. if (isset($coupon->send_user_id) && ($coupon->send_user_id) > 0) {
  171. //判断该优惠券是否有库存
  172. // $inventory = Db::table('system_coupon_user_receive as r');
  173. // if($inventory['inventory'] <= 0){
  174. // return '库存不足';
  175. // }
  176. //判断用户是否已有该类型优惠券
  177. $exist_coupon = Db::table('ims_system_coupon_user_receive')
  178. ->where([
  179. ['system_coupon_user_id', '=', $coupon->system_coupon_id],
  180. ['user_id', '=', $coupon->send_user_id],
  181. ['receive_type', '=', 5],
  182. ['status', '=', 0],
  183. ])
  184. ->select('id')
  185. ->first();
  186. //return $exist_coupon;
  187. //开启事务
  188. try {
  189. /*
  190. * 如果已有该优惠券 则领取数量 可用数量 自增1
  191. * 否则新增一条返券记录
  192. */
  193. $nowTime = time();
  194. $res = Db::table('ims_system_coupon_user_receive')->updateOrInsert(
  195. [
  196. 'system_coupon_user_id' => $coupon->system_coupon_id,
  197. 'user_id' => $coupon->send_user_id,
  198. 'receive_type' => 5,
  199. 'status' => 0,
  200. ],
  201. [
  202. 'order_main_id' => $order_id,
  203. 'receive_time' => $nowTime,
  204. 'number' => 1,
  205. 'number_remain' => 1,
  206. 'update_time' => $nowTime,
  207. 'created_at' => $nowTime,
  208. 'updated_at' => $nowTime,
  209. ]
  210. );
  211. // if ($res) {
  212. // //首次返券更新rebate_type字段 防止重复返券
  213. // Db::table('system_coupon_user_receive')->where('id', $coupon->id)->update(['rebate_type' => 2]);
  214. // //更新库存操作
  215. // Db::table('system_coupon_user')
  216. // ->where('id', $coupon->id)
  217. // ->increment('inventory_use');
  218. // } else {
  219. // Db::rollBack();
  220. // return '事务失败';
  221. // }
  222. // 提交
  223. Db::commit();
  224. } catch (\Exception $e) {
  225. // 回滚
  226. Db::rollBack();
  227. return '返券失败';
  228. }
  229. } else {
  230. // $str1 = 'order_main_id:'.$this->order_id . ',未查询到用户领取优惠券信息['.$coupon.']';
  231. // $this->couponErrorLog($str1);
  232. }
  233. return $exist_coupon->id;
  234. }
  235. }