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.

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