ImateTester.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace refill;
  3. class ImateTester
  4. {
  5. private $mTimesConfig; //对应refill.ini 配置文件数据
  6. private $mGrossRatios;
  7. private $mTypeRatios;
  8. private $mDetailRatios;
  9. private $mMchQTS;
  10. public function __construct()
  11. {
  12. $this->mTimesConfig = [];
  13. $this->mGrossRatios = [];
  14. $this->mTypeRatios = [];
  15. $this->mDetailRatios = [];
  16. $this->mMchQTS = [];
  17. }
  18. public function load()
  19. {
  20. $this->load_retry();
  21. }
  22. private function load_retry()
  23. {
  24. $isDay = functional::isDay();
  25. $mch_configs = function ($isDay) {
  26. $result = [];
  27. $i = 0;
  28. while (true) {
  29. $start = $i * 100;
  30. $items = Model()->table('merchant')->where(['mchid' => ['gt', 0], 'merchant_state' => 1])->field('mchid,retry_times_cfg')->order('mchid asc')->limit("{$start},100")->select();
  31. if (empty($items)) {
  32. break;
  33. }
  34. $i++;
  35. foreach ($items as $item) {
  36. $mchid = intval($item['mchid']);
  37. if ($mchid <= 0) continue;
  38. $retry_times_cfg = unserialize($item['retry_times_cfg']);
  39. if (empty($retry_times_cfg)) continue;
  40. $qualities = &$retry_times_cfg['qualities'];
  41. foreach ($qualities as $quality => $cfg) {
  42. if ($isDay) {
  43. $qualities[$quality]['secs'] = $cfg['day_secs'];
  44. } else {
  45. $qualities[$quality]['secs'] = $cfg['night_secs'];
  46. }
  47. }
  48. $result[$mchid] = $retry_times_cfg;
  49. }
  50. }
  51. return $result;
  52. };
  53. $this->mTimesConfig = $mch_configs($isDay);
  54. }
  55. public function update($gross_ratios, $detail_ratios,$type_ratios)
  56. {
  57. if (!empty($gross_ratios)) {
  58. $this->mGrossRatios = $gross_ratios;
  59. }
  60. if (!empty($type_ratios)) {
  61. $this->mTypeRatios = $type_ratios;
  62. }
  63. if (!empty($detail_ratios)) {
  64. $this->mDetailRatios = $detail_ratios;
  65. }
  66. $this->mMchQTS = [];
  67. foreach ($detail_ratios as $key => $val) {
  68. [$mchid, $card_type, $spec] = explode('-', $key);
  69. $mchid = intval($mchid);
  70. $card_type = intval($card_type);
  71. $spec = intval($spec);
  72. $this->mMchQTS[$mchid][] = [$card_type, $spec];
  73. }
  74. }
  75. public function profit_formula($mchid) {
  76. return $this->mTimesConfig[$mchid]['profit_formula'] ?? 'qts';
  77. }
  78. public function lowest_ratio($mchid)
  79. {
  80. $lower_ratio = $this->mTimesConfig[$mchid]['lower_ratio'] ?? [];
  81. if (empty($lower_ratio)) {
  82. return 0.0;
  83. } else {
  84. return $lower_ratio['ratio'];
  85. }
  86. }
  87. public function profit_ratio($mchid)
  88. {
  89. $profit_ratio = $this->mTimesConfig[$mchid]['profit_ratio'] ?? 0.0;
  90. return $profit_ratio;
  91. }
  92. public function type_specs($mchid)
  93. {
  94. if(array_key_exists($mchid,$this->mMchQTS)) {
  95. return $this->mMchQTS[$mchid];
  96. }
  97. else {
  98. return [];
  99. }
  100. }
  101. //match: true 表示当前质量满足条件,可以不走溢价,false,表示可以走溢价
  102. //can_last: true,能补充,false 表示不能走溢价
  103. public function ratio_match($mchid, $org_quality, $card_type, $spec, $qualities)
  104. {
  105. $formula = $this->profit_formula($mchid);
  106. if($formula === 'all')
  107. {
  108. $matcher = new whole_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
  109. return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
  110. }
  111. elseif($formula === 'qt') {
  112. $matcher = new type_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
  113. return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
  114. }
  115. else {
  116. $matcher = new type_spec_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
  117. return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
  118. }
  119. }
  120. }