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.

166 lines
5.2 KiB

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\CouponUserRecType;
  6. use App\Model\Coupon;
  7. use App\Model\CouponRec;
  8. use Hyperf\Utils\ApplicationContext;
  9. use App\TaskWorker\SSDBTask;
  10. use App\Constants\SsdbKeysPrefix;
  11. use App\Constants\LogLabel;
  12. use App\Commons\Log;
  13. use Exception;
  14. use App\Service\CommonService;
  15. class CouponService implements CouponServiceInterface
  16. {
  17. /**
  18. * @Inject
  19. * @var Log
  20. */
  21. protected $log;
  22. /**
  23. * @Inject
  24. * @var CommonService
  25. */
  26. protected $commonService;
  27. /**
  28. * 获取用户可领取优惠卷接口
  29. */
  30. public function getSystemCouponUserList($userId,$receiveType)
  31. {
  32. /* 优惠券活动标志 2 */
  33. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  34. $couponActivity = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
  35. $activityType = $couponActivity === false ? 0 : $couponActivity['activity'];
  36. $result = [
  37. 'active_type' => 1,
  38. 'not_receive' => [],
  39. 'jump_data' => [
  40. 'src' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType,
  41. 'src2' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType,
  42. 'share_bg' => env('OSS_IMG_HOST').'/static/img/coupon_bg.png',
  43. 'receive_bg' => env('OSS_IMG_HOST').'/static/img/coupon_share.png',
  44. 'coupons' => []
  45. ]
  46. ];
  47. $nowTime = time();
  48. $c_ids = [];
  49. $whereC = [
  50. ['end_time','>',$nowTime],
  51. ['start_time','<=',$nowTime],
  52. ['status','=',1]
  53. ];
  54. // 渠道开启,查询该渠道可以领取的优惠券ID
  55. // 渠道未开启,查询所有优惠券
  56. if (env('SUB_CHANNEL') == 1) {
  57. $c_ids = CouponUserRecType::where('receive_type', $receiveType)->where($whereC)->pluck('system_coupon_user_id');
  58. } else {
  59. $c_ids = Coupon::where($whereC)->pluck('id');
  60. }
  61. $couponReceive = CouponRec::where('user_id',$userId);
  62. // 渠道开启,查询该用户在此渠道领过的优惠券ID
  63. if (env('SUB_CHANNEL') == 1) {
  64. $couponReceive->where('receive_type', $receiveType);
  65. }
  66. $cr_ids = $couponReceive->pluck('system_coupon_user_id');
  67. // 可领取的券ID
  68. $c_ids = $c_ids->toArray();
  69. // 已经领取的券ID
  70. $cr_ids = $cr_ids->toArray();
  71. // 当前用户可领的优惠券ID
  72. $couponIds = array_diff($c_ids, $cr_ids);
  73. // 转发型优惠券
  74. $couponReceiveIds = ($couponActivity === false || $this->commonService->empty($couponActivity['forward']) )? [] : explode(',',$couponActivity['forward']);
  75. // 所有优惠券
  76. $couponIds = array_merge($couponIds,$couponReceiveIds);
  77. $whereC = [
  78. ['u.end_time','>',$nowTime],
  79. ['u.start_time','<=',$nowTime],
  80. ['u.status','=',1]
  81. ];
  82. // 查询领取型1 和 转发型2
  83. $whereActiveType = [1,2];
  84. if (env('SUB_CHANNEL') == 1) {
  85. array_push($whereC, ['type.receive_type','=', $receiveType]);
  86. }
  87. $coupons = Db::table('ims_system_coupon_user as u')
  88. ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id')
  89. ->whereIn('u.id', $couponIds)
  90. ->whereIn('u.active_type', $whereActiveType)
  91. ->where($whereC)
  92. ->whereRaw('u.inventory_use < u.inventory and u.inventory-u.inventory_use >= type.one_receive_number')
  93. ->select('u.*','type.one_receive_number')
  94. ->orderBy('u.weigh','desc')
  95. ->get();
  96. foreach ($coupons as $key => $coupon) {
  97. //拼接满减文字提示
  98. $coupon->full_amount_text = '满' . $coupon->full_amount . "可用";
  99. //判断是折扣优惠券还是满减优惠券
  100. if($coupon->discount_type == 1){
  101. $coupon->discounts_text = '¥'.$coupon->discounts;
  102. }elseif($coupon->discount_type == 2){
  103. $coupon->discounts_text = floatval($coupon->discounts)."";
  104. }
  105. //失效时间格式转换
  106. $usable_end_time = date('Y-m-d H:i:s',$coupon->usable_end_time);
  107. $coupon->usable_end_time_text = '有效期至:'.$usable_end_time;
  108. if ($coupon->usable_end_time < $nowTime || $coupon->number_remain <= 0) {
  109. $expired[] = $coupon;
  110. } else {
  111. $not_expired[] = $coupon;
  112. }
  113. }
  114. $result['active_type'] = count($result['jump_data']['coupons']) > 0 ? 2 : $result['active_type'] ;
  115. return $result;
  116. }
  117. //统计用户
  118. public function userCouponAccount()
  119. {
  120. }
  121. /**
  122. * 用户领取优惠卷
  123. */
  124. public function userReceiveCoupon()
  125. {
  126. }
  127. /**
  128. * 获取用户已经领取的优惠卷列表
  129. */
  130. public function getUserReceiveCouponList()
  131. {
  132. }
  133. /**
  134. * 获取用户当前订单可用的优惠券列表
  135. * 按分类(1订单 等优惠)分组返回
  136. */
  137. public function getUserAvailableCoupons()
  138. {
  139. }
  140. }