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.

222 lines
7.0 KiB

5 years ago
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. use Hyperf\Redis\Redis;
  16. class CouponService implements CouponServiceInterface
  17. {
  18. /**
  19. * @Inject
  20. * @var Log
  21. */
  22. protected $log;
  23. /**
  24. * @Inject
  25. * @var CommonService
  26. */
  27. protected $commonService;
  28. /**
  29. * 获取用户可领取优惠卷接口
  30. */
  31. public function getSystemCouponUserList($userId,$receiveType)
  32. {
  33. /* 优惠券活动标志 2 */
  34. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  35. $couponActivity = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
  36. $activityType = $couponActivity === false ? 0 : $couponActivity['activity'];
  37. $result = [
  38. 'active_type' => 1,
  39. 'not_receive' => [],
  40. 'jump_data' => [
  41. 'src' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType,
  42. 'src2' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType,
  43. 'share_bg' => env('OSS_IMG_HOST').'/static/img/coupon_share.png',
  44. 'receive_bg' => env('OSS_IMG_HOST').'/static/img/coupon_bg.png',
  45. 'coupons' => []
  46. ]
  47. ];
  48. $nowTime = time();
  49. $c_ids = [];
  50. $whereC = [
  51. ['end_time','>',$nowTime],
  52. ['start_time','<=',$nowTime],
  53. ['status','=',1]
  54. ];
  55. // 渠道开启,查询该渠道可以领取的优惠券ID
  56. // 渠道未开启,查询所有优惠券
  57. if (env('SUB_CHANNEL') == 1) {
  58. $c_ids = CouponUserRecType::where('receive_type', $receiveType)->where($whereC)->pluck('system_coupon_user_id');
  59. } else {
  60. $c_ids = Coupon::where($whereC)->pluck('id');
  61. }
  62. $couponReceive = CouponRec::where('user_id',$userId);
  63. // 渠道开启,查询该用户在此渠道领过的优惠券ID
  64. if (env('SUB_CHANNEL') == 1) {
  65. $couponReceive->where('receive_type', $receiveType);
  66. }
  67. $cr_ids = $couponReceive->pluck('system_coupon_user_id');
  68. // 可领取的券ID
  69. $c_ids = $c_ids->toArray();
  70. // 已经领取的券ID
  71. $cr_ids = $cr_ids->toArray();
  72. // 当前用户可领的优惠券ID
  73. $couponIds = array_diff($c_ids, $cr_ids);
  74. // 转发型优惠券
  75. $couponReceiveIds = ($couponActivity === false || $this->commonService->empty($couponActivity['forward']) )? [] : explode(',',$couponActivity['forward']);
  76. // 所有优惠券
  77. $couponIds = array_merge($couponIds,$couponReceiveIds);
  78. $whereC = [
  79. ['u.end_time','>',$nowTime],
  80. ['u.start_time','<=',$nowTime],
  81. ['u.status','=',1]
  82. ];
  83. // 查询领取型1 和 转发型2
  84. $whereActiveType = [1,2];
  85. if (env('SUB_CHANNEL') == 1) {
  86. array_push($whereC, ['type.receive_type','=', $receiveType]);
  87. }
  88. $coupons = Db::table('ims_system_coupon_user as u')
  89. ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id')
  90. ->whereIn('u.id', $couponIds)
  91. ->whereIn('u.active_type', $whereActiveType)
  92. ->where($whereC)
  93. ->whereRaw('u.inventory_use < u.inventory and u.inventory-u.inventory_use >= type.one_receive_number')
  94. ->select('u.*','type.one_receive_number')
  95. ->orderBy('u.weigh','desc')
  96. ->get();
  97. foreach ($coupons as $k => &$v){
  98. if($v->active_type == 1 && count($result['not_receive']) < 4){
  99. $result['not_receive'][] = $v;
  100. }else if($v->active_type == 2 && in_array($v->id,$couponReceiveIds)){
  101. $result['jump_data']['coupons'][] = $v->id;
  102. }
  103. if($v->discount_type == 2){
  104. $v->discounts = floatval($v->discounts);
  105. }
  106. }
  107. $result['active_type'] = count($result['jump_data']['coupons']) > 0 ? 2 : $result['active_type'] ;
  108. return $result;
  109. }
  110. //统计用户
  111. public function userCouponAccount()
  112. {
  113. }
  114. /**
  115. * 用户领取优惠卷
  116. */
  117. public function userReceiveCoupon()
  118. {
  119. }
  120. /**
  121. * 获取用户已经领取的优惠卷列表
  122. */
  123. public function getUserReceiveCouponList()
  124. {
  125. }
  126. /**
  127. * 获取用户当前订单可用的优惠券列表
  128. * 按分类(1订单 等优惠)分组返回
  129. */
  130. public function getUserAvailableCoupons()
  131. {
  132. }
  133. /**
  134. * @inheritDoc
  135. */
  136. public function getOrderCanUseCoupons($orderAmount, $marketId, $userId, $fields=[], $type = 1, $storeTypeIds = [0])
  137. {
  138. // 用户今日使用过的优惠券
  139. $redis = ApplicationContext::getContainer()->get(Redis::class);
  140. $couponTodayUsedIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
  141. $currentTime = time();
  142. $builder = Db::table('ims_system_coupon_user_receive as receive')
  143. ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id', 'inner');
  144. if (is_array($fields)&&!empty($fields)) {
  145. $builder->select($fields);
  146. }
  147. if (is_array($couponTodayUsedIds)&&!empty($couponTodayUsedIds)) {
  148. $builder->whereNotIn('coupon.id', $couponTodayUsedIds);
  149. }
  150. return $builder->where(['receive.user_id' => $userId])
  151. ->whereIn('receive.status', [0,1])
  152. ->where('receive.number_remain', '>', 0)
  153. ->whereIn('coupon.type', [1,$type])
  154. ->where('coupon.full_amount', '<=', $orderAmount)
  155. ->where('coupon.usable_start_time', '<=', $currentTime)
  156. ->where('coupon.usable_end_time', '>=', $currentTime)
  157. ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
  158. ->where('coupon.market_id', 'in', [0, $marketId])
  159. ->whereIn('coupon.storetype_id', $storeTypeIds)
  160. ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
  161. ->get()
  162. ->toArray();
  163. }
  164. /**
  165. * 缓存优惠券今日使用情况
  166. * @param $userId
  167. * @param $couponId
  168. * @param $couponRecId
  169. * @return bool
  170. */
  171. function cacheTodayCouponUsed($userId, $couponId, $couponRecId)
  172. {
  173. $redis = ApplicationContext::getContainer()->get(Redis::class);
  174. $setRes = $redis->sAdd(
  175. 'coupon_'.date('Ymd').'_used_'.$userId,
  176. $couponId
  177. );
  178. $expireRes = $redis->expire(
  179. 'coupon_'.date('Ymd').'_used_'.$userId,
  180. strtotime(date('Y-m-d').' 23:59:59')-time()
  181. );
  182. return $setRes&&$expireRes;
  183. }
  184. }