FkStrategiesDetailsService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\common\service;
  3. // 风控策略规则管理
  4. use think\Cache;
  5. use think\Db;
  6. class FkStrategiesDetailsService
  7. {
  8. // 缓存前缀
  9. const CACHE_PREFIX = 'fk_strategies_details:';
  10. private $redis;
  11. public function __construct()
  12. {
  13. $this->redis = Cache::store('default')->handler();
  14. $this->redis->select(1);
  15. }
  16. /**
  17. * 检查频率规则
  18. * @param array $detail 规则详情
  19. * @param array $eventData 事件数据
  20. * @return array
  21. */
  22. public function checkFrequencyRule(array $detail, array $eventData)
  23. {
  24. // dump($detail['exception_validity_period']);
  25. if($detail['exception_validity_period'] > 0){
  26. $cacheKey = self::CACHE_PREFIX . 'frequency:' . $detail['rule_code'];
  27. $this->redis->hset($cacheKey, $eventData[$detail['exception_object_type']], 1);
  28. $contrast = $this->redis->hlen($cacheKey);
  29. $ttl = $this->redis->ttl($cacheKey);
  30. if($contrast === 0 || $ttl < 0) {
  31. // 设置过期时间,减少内存数据冗余
  32. $this->redis->expire($cacheKey, $detail['exception_validity_period']);
  33. }
  34. }else{
  35. $contrast = $eventData[$detail['exception_object_type']];
  36. }
  37. $result = false;
  38. // 根据逻辑类型判断
  39. switch (trim($detail['logic_type'])) {
  40. case 'gt': // 大于
  41. $result = $contrast > $detail['logic_value'];
  42. break;
  43. case 'gte': // 大于等于
  44. $result = $contrast >= $detail['logic_value'];
  45. break;
  46. case 'lt': // 小于
  47. $result = $contrast < $detail['logic_value'];
  48. break;
  49. case 'lte': // 小于等于
  50. $result = $contrast <= $detail['logic_value'];
  51. break;
  52. case 'eq': // 等于
  53. $result = $contrast == $detail['logic_value'];
  54. break;
  55. }
  56. return ['status' => $result, 'count' => $contrast];
  57. }
  58. /**
  59. * 检查统计规则
  60. * @param array $detail
  61. * @param array $eventData
  62. * @return array
  63. */
  64. public function checkStatisticsRule(array $detail, array $eventData)
  65. {
  66. $result = false;
  67. if($detail['exception_validity_period'] > 0){
  68. $cacheKey = self::CACHE_PREFIX . 'statistics:' . $detail['rule_code'].':'.$eventData[$detail['exception_object_type']];
  69. $this->redis->Incr($cacheKey, 1);
  70. $contrast = $this->redis->get($cacheKey);
  71. $ttl = $this->redis->ttl($cacheKey);
  72. if($contrast === 0 || $ttl < 0) {
  73. $this->redis->expire($cacheKey, $detail['exception_validity_period']);
  74. }
  75. }else{
  76. $contrast = $eventData[$detail['exception_object_type']];
  77. }
  78. // 根据逻辑类型判断
  79. switch ($detail['logic_type']) {
  80. case 'gt': // 大于
  81. $result = $contrast > $detail['logic_value'];
  82. break;
  83. case 'gte': // 大于等于
  84. $result = $contrast >= $detail['logic_value'];
  85. break;
  86. case 'lt': // 小于
  87. $result = $contrast < $detail['logic_value'];
  88. break;
  89. case 'lte': // 小于等于
  90. $result = $contrast <= $detail['logic_value'];
  91. break;
  92. case 'eq': // 等于
  93. $result = $contrast == $detail['logic_value'];
  94. break;
  95. }
  96. // dump($result, $contrast, ' - ', $detail['logic_type'], $detail['logic_value']);
  97. return ['status' => $result, 'count' => $contrast];
  98. }
  99. }