PromotionProtectCheck.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Modules\OrangeSite\Services\CheckReport;
  3. use App\Modules\OrangeSite\Services\OrangeSitePromotionService;
  4. use App\Modules\OrangeSite\Services\OrangeSiteRechargeReportCheckContext;
  5. use Closure;
  6. use Log;
  7. class PromotionProtectCheck
  8. {
  9. /**
  10. * 判断计划保护数
  11. * @param $context OrangeSiteRechargeReportCheckContext
  12. * @param Closure $next
  13. * @return mixed
  14. */
  15. public function handle($context, Closure $next)
  16. {
  17. $channelPromotionProtectSettingNum = $context->channelCallbackConfig->eligible_count ?: 0;
  18. $userBindInfo = OrangeSitePromotionService::getUserBindInfo($context->user->id);
  19. if (OrangeSitePromotionService::judgeNoUserMatch($userBindInfo)) {
  20. Log::debug('[orangeSiteRechargeReport]当前用户没有匹配的report_user_bind_record', [
  21. 'traceInfo' => $context->traceContext->getTraceInfo()]);
  22. $context->result = [
  23. 'result' => false,
  24. 'type' => 'no_user_match',
  25. 'firstReason' => 'PromotionProtectCheck',
  26. 'content' => '无用户匹配'
  27. ];
  28. return $context;
  29. }
  30. $promotionId = $userBindInfo->promotion_id ?? '';
  31. if (!$promotionId) {
  32. \Log::debug('[orangeSiteRechargeReport]当前用户没有匹配的promotionId,不走计划保护数', [
  33. 'traceInfo' => $context->traceContext->getTraceInfo()
  34. ]);
  35. return $next($context);
  36. }
  37. $context->reportInfo['promotionId'] = $promotionId;
  38. $currentProtectNum = OrangeSitePromotionService::getCurrentPromotionProtectNum($context->user->distribution_channel_id,
  39. $promotionId);
  40. if ($currentProtectNum < $channelPromotionProtectSettingNum) {
  41. Log::debug('[orangeSiteRechargeReport]处于计划保护数之内', [
  42. 'promotionId' => $promotionId,
  43. 'currentProtectNum' => $currentProtectNum,
  44. 'channelPromotionProtectSettingNum' => $channelPromotionProtectSettingNum,
  45. 'traceInfo' => $context->traceContext->getTraceInfo()]);
  46. // !! 如果满足计划保护,那么 result 为true ,直接回传,不经过比例
  47. $context->result['result'] = true;
  48. $context->result['type'] = 'protected_eligible';
  49. $context->result['firstReason'] = 'PromotionProtectCheck';
  50. $context->result['result_ok_reason'] = 'promotionProtect';
  51. $context->result['content'] = '订单被计划保护';
  52. return $context;
  53. } else {
  54. return $next($context);
  55. }
  56. }
  57. }