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.

144 lines
4.3 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('hgetall', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
  29. $activityType = $couponActivity === false ? 0 : $couponActivity['activity'];
  30. $result = [
  31. 'active_type' => 1,
  32. 'not_receive' => [],
  33. 'jump_data' => [
  34. 'src' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType,
  35. 'src2' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType,
  36. 'share_bg' => env('OSS_IMG_HOST').'/attachment/images/png/2020/08/11/d697d43b91913f861f931649f0898815.png',
  37. 'receive_bg' => env('OSS_IMG_HOST')."/attachment/images/png/2020/08/11/9c7d7c95b5de29539fad4bc9a2fcb98d.png",
  38. 'coupons' => []
  39. ]
  40. ];
  41. $nowTime = time();
  42. $c_ids = [];
  43. // 渠道开启,查询该渠道可以领取的优惠券ID
  44. // 渠道未开启,查询所有优惠券
  45. if (env('SUB_CHANNEL') == 1) {
  46. $c_ids = CouponUserRecType::where('receive_type', $receiveType)->pluck('system_coupon_user_id');
  47. } else {
  48. $c_ids = Coupon::pluck('id');
  49. }
  50. $couponReceive = CouponRec::where('user_id',$userId);
  51. // 渠道开启,查询该用户在此渠道领过的优惠券ID
  52. if (env('SUB_CHANNEL') == 1) {
  53. $couponReceive->where('receive_type', $receiveType);
  54. }
  55. $cr_ids = $couponReceive->pluck('system_coupon_user_id');
  56. // 可领取的券ID
  57. $c_ids = $c_ids->toArray();
  58. // 已经领取的券ID
  59. $cr_ids = $cr_ids->toArray();
  60. // 当前用户可领的优惠券ID
  61. $couponIds = array_diff($c_ids, $cr_ids);
  62. // 转发型优惠券
  63. $couponReceiveIds = $couponActivity === false ? [] : explode(',',$couponActivity['forward']);
  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 && in_array($v->id,$couponReceiveIds)){
  85. $result['jump_data']['coupons'][] = $v->id;
  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. }