stanley-king 2 vuotta sitten
vanhempi
commit
716164c164
2 muutettua tiedostoa jossa 110 lisäystä ja 22 poistoa
  1. 65 22
      helper/refill/policy/mratio_controlex.php
  2. 45 0
      test/TestRefillPolicy.php

+ 65 - 22
helper/refill/policy/mratio_controlex.php

@@ -219,6 +219,24 @@ class mratio_controlex
         }
     }
 
+    //{\"10202\":{\"ratio\":0.65,\"period\":86400,\"profit_ratio\":0.002,\"profit_formula\":\"all\"}
+    //to-do for test
+//    private function lowest_ratio($mchid)
+//    {
+//        $lower_ratio = $this->mTimesConfig[$mchid]['lower_ratio'] ?? [];
+//        if (empty($lower_ratio)) {
+//            return [0.30, 86400];
+//        } else {
+//            return [$lower_ratio['ratio'], $lower_ratio['period']];
+//        }
+//    }
+//
+//    private function profit_ratio($mchid) {
+//        $profit_ratio = $this->mTimesConfig[$mchid]['profit_ratio'] ?? 0.002;
+//        return $profit_ratio;
+//    }
+
+
     private function lowest_ratio($mchid)
     {
         $lower_ratio = $this->mTimesConfig[$mchid]['lower_ratio'] ?? [];
@@ -287,11 +305,6 @@ class mratio_controlex
 
     private function pre_checker($mchid, $card_type, $spec, $header, $lowest_ratio)
     {
-        $lowest_pratio = $this->profit_ratio($mchid);
-
-        [$all_submit, $succ_count, $fail_count, $cur_ratio, $profit, $cur_pratio] = $this->detail_ratio($mchid, $card_type, $spec);
-        Log::record("{$header} $cur_pratio={$cur_pratio},lower_pratio={$lowest_pratio}", Log::DEBUG);
-
         $ts_ratios = function ($mchid,$type_specs,$all_submit)
         {
             $result = [];
@@ -311,34 +324,64 @@ class mratio_controlex
             return $result;
         };
 
-        $factor_calc = function ($item)
+        $spec_extract = function ($item)
         {
             [$card_type, $spec, $submit_ratio, $cur_ratio, $cur_pratio] = $item;
-            $factor = $submit_ratio * $cur_ratio * $cur_pratio / $spec;
-            return $factor;
+            return $spec;
+        };
+
+        $ts_sorter_spec = function ($left,$right) use($spec_extract)
+        {
+            $l = $spec_extract($left);
+            $r = $spec_extract($right);
+
+            if($l > $r)
+                return 1;
+            elseif($l < $r)
+                return -1;
+            else
+                return 0;
         };
 
-        $ts_sorter = function ($left,$right) use($factor_calc)
+        $ts_filter = function ($ts_ratios, $ratio, $lowest_ratio)
         {
-            $l = $factor_calc($left);
-            $r = $factor_calc($right);
+            $result = [];
+
+            $all = 0.00;
+            foreach ($ts_ratios as $item)
+            {
+                [$card_type, $spec, $submit_ratio, $cur_ratio, $cur_pratio] = $item;
+                $all += $submit_ratio * $cur_ratio;
 
-            if($l > $r) return -1;
-            elseif($l < $r) return 1;
-            else return 0;
+                $r = $all / ($lowest_ratio + 0.00001);
+                if($r > $ratio)
+                    break;
+                else {
+                    $result[] = "{$card_type}-{$spec}";
+                }
+            }
+            return $result;
         };
 
+        $lowest_pratio = $this->profit_ratio($mchid);
+        [$all_submit, $succ_count, $fail_count, $gross_ratio, $profit, $cur_pratio] = $this->gross_ratio($mchid);
+
+        if($cur_pratio <= $lowest_pratio) {
+            return true;
+        }
+
         $type_specs = $this->mMchQTS[$mchid];
         $ts_ratios = $ts_ratios($mchid, $type_specs, $all_submit);
-        usort($ts_ratios, $ts_sorter);
-
-        if ($cur_pratio >= $lowest_pratio)
-        {
-            if($cur_ratio >= $lowest_ratio) {
-                return false;
-            }
+        usort($ts_ratios, $ts_sorter_spec);
 
-            return false;
+        $ratio = $gross_ratio / ($lowest_ratio + 0.00001);
+        if($ratio > 1.1) {
+            return true;
+        }
+        else {
+            $meet = $ts_filter($ts_ratios,$ratio,$lowest_ratio);
+            $exist = array_key_exists("{$card_type}-{$spec}",$meet);
+            return !$exist;
         }
     }
 

+ 45 - 0
test/TestRefillPolicy.php

@@ -0,0 +1,45 @@
+<?php
+
+
+use PHPUnit\Framework\TestCase;
+use refill\mratio_controlex;
+
+define('APP_ID', 'refill_stat');
+define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
+
+require_once(BASE_ROOT_PATH . '/global.php');
+require_once(BASE_CORE_PATH . '/lrlz.php');
+require_once(BASE_ROOT_PATH . '/fooder.php');
+
+require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
+require_once(BASE_HELPER_PATH . '/queue/iqueue.php');
+require_once(BASE_HELPER_PATH . '/queue/monitor.php');
+require_once(BASE_HELPER_PATH . '/refill/util.php');
+
+class TestRefillPolicy extends TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        Base::run_util();
+    }
+
+    private function getRatios()
+    {
+        $ins = Cache::getInstance('cacheredis');
+        $content = $ins->get_org('refill_merchant_profit_ratio');
+        $counts = json_decode($content,true);
+        $gross = $counts['gross'];
+        $detail = $counts['detail'];
+
+        return [$gross,$detail];
+    }
+
+    public function testRatioCtl()
+    {
+        [$gross,$detail] = $this->getRatios();
+        $ratio_ctl = new refill\mratio_controlex();
+        $ratio_ctl->update($gross,$detail);
+        $mchid = 10202;
+        $ratio_ctl->ratio_match($mchid, 14, 4, 50, [1, 2]);
+    }
+}