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.

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