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.

141 lines
4.0 KiB

  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. class CouponService implements CouponServiceInterface
  15. {
  16. /**
  17. * @Inject
  18. * @var Log
  19. */
  20. protected $log;
  21. /**
  22. * 获取用户可领取优惠卷接口
  23. */
  24. public function getSystemCouponUserList($userId,$receiveType)
  25. {
  26. $activityType = 2;/* 优惠券活动标志 2 */
  27. $result = [
  28. 'active_type' => 1,
  29. 'not_receive' => [],
  30. 'jump_data' => [
  31. 'src' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType,
  32. 'src2' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType,
  33. 'coupons' => []
  34. ]
  35. ];
  36. $nowTime = time();
  37. $c_ids = [];
  38. // 渠道开启,查询该渠道可以领取的优惠券ID
  39. // 渠道未开启,查询所有优惠券
  40. if (env('SUB_CHANNEL') == 1) {
  41. $c_ids = CouponUserRecType::where('receive_type', $receiveType)->pluck('system_coupon_user_id');
  42. } else {
  43. $c_ids = Coupon::pluck('id');
  44. }
  45. $couponReceive = CouponRec::where('user_id',$userId);
  46. // 渠道开启,查询该用户在此渠道领过的优惠券ID
  47. if (env('SUB_CHANNEL') == 1) {
  48. $couponReceive->where('receive_type', $receiveType);
  49. }
  50. $cr_ids = $couponReceive->pluck('system_coupon_user_id');
  51. // 可领取的券ID
  52. $c_ids = $c_ids->toArray();
  53. // 已经领取的券ID
  54. $cr_ids = $cr_ids->toArray();
  55. // 当前用户可领的优惠券ID
  56. $couponIds = array_diff($c_ids, $cr_ids);
  57. // 获取领取型优惠券
  58. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  59. $couponRebateIds = $ssdb->exec('hgetall',SsdbKeysPrefix::COUPON_REBATE_FORWARD.$activityType);
  60. $couponIds = ($couponRebateIds === false || empty($couponRebateIds)) ? $couponIds : array_merge($couponIds,$couponRebateIds) ;
  61. $whereC = [
  62. ['u.end_time','>',$nowTime],
  63. ['u.start_time','<=',$nowTime],
  64. ['u.status','=',1]
  65. ];
  66. if (env('SUB_CHANNEL') == 1) {
  67. array_push($whereC, ['type.receive_type','=', $receiveType]);
  68. }
  69. $coupons = Db::table('ims_system_coupon_user as u')
  70. ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id')
  71. ->whereIn('u.id', $couponIds)
  72. ->where($whereC)
  73. ->whereRaw('u.inventory_use < u.inventory and u.inventory-u.inventory_use >= type.one_receive_number')
  74. ->select('u.*','type.one_receive_number')
  75. ->orderBy('u.weigh','desc')
  76. ->limit(4)
  77. ->get();
  78. foreach ($coupons as $k => &$v){
  79. if($v->active_type == 1){
  80. $result['not_receive'] = $v;
  81. }else if($v->active_type == 2){
  82. $result['jump_data']['coupons'][] = $v->id;
  83. }
  84. if($v->discount_type == 2){
  85. $v->discounts = floatval($v->discounts);
  86. }
  87. }
  88. $result['active_type'] = count($result['jump_data']['coupons']) > 0 ? 2 : $result['active_type'] ;
  89. return $result;
  90. }
  91. //统计用户
  92. public function userCouponAccount()
  93. {
  94. }
  95. /**
  96. * 用户领取优惠卷
  97. */
  98. public function userReceiveCoupon()
  99. {
  100. }
  101. /**
  102. * 获取用户已经领取的优惠卷列表
  103. */
  104. public function getUserReceiveCouponList()
  105. {
  106. }
  107. /**
  108. * 获取用户当前订单可用的优惠券列表
  109. * 按分类(1订单 等优惠)分组返回
  110. */
  111. public function getUserAvailableCoupons()
  112. {
  113. }
  114. }