123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace refill;
- class ImateTester
- {
- private $mTimesConfig; //对应refill.ini 配置文件数据
- private $mGrossRatios;
- private $mTypeRatios;
- private $mDetailRatios;
- private $mMchQTS;
- public function __construct()
- {
- $this->mTimesConfig = [];
- $this->mGrossRatios = [];
- $this->mTypeRatios = [];
- $this->mDetailRatios = [];
- $this->mMchQTS = [];
- }
- public function load()
- {
- $this->load_retry();
- }
- private function load_retry()
- {
- $isDay = functional::isDay();
- $mch_configs = function ($isDay) {
- $result = [];
- $i = 0;
- while (true) {
- $start = $i * 100;
- $items = Model()->table('merchant')->where(['mchid' => ['gt', 0], 'merchant_state' => 1])->field('mchid,retry_times_cfg')->order('mchid asc')->limit("{$start},100")->select();
- if (empty($items)) {
- break;
- }
- $i++;
- foreach ($items as $item) {
- $mchid = intval($item['mchid']);
- if ($mchid <= 0) continue;
- $retry_times_cfg = unserialize($item['retry_times_cfg']);
- if (empty($retry_times_cfg)) continue;
- $qualities = &$retry_times_cfg['qualities'];
- foreach ($qualities as $quality => $cfg) {
- if ($isDay) {
- $qualities[$quality]['secs'] = $cfg['day_secs'];
- } else {
- $qualities[$quality]['secs'] = $cfg['night_secs'];
- }
- }
- $result[$mchid] = $retry_times_cfg;
- }
- }
- return $result;
- };
- $this->mTimesConfig = $mch_configs($isDay);
- }
- public function update($gross_ratios, $detail_ratios,$type_ratios)
- {
- if (!empty($gross_ratios)) {
- $this->mGrossRatios = $gross_ratios;
- }
- if (!empty($type_ratios)) {
- $this->mTypeRatios = $type_ratios;
- }
- if (!empty($detail_ratios)) {
- $this->mDetailRatios = $detail_ratios;
- }
- $this->mMchQTS = [];
- foreach ($detail_ratios as $key => $val) {
- [$mchid, $card_type, $spec] = explode('-', $key);
- $mchid = intval($mchid);
- $card_type = intval($card_type);
- $spec = intval($spec);
- $this->mMchQTS[$mchid][] = [$card_type, $spec];
- }
- }
- public function profit_formula($mchid) {
- return $this->mTimesConfig[$mchid]['profit_formula'] ?? 'qts';
- }
- public function lowest_ratio($mchid)
- {
- $lower_ratio = $this->mTimesConfig[$mchid]['lower_ratio'] ?? [];
- if (empty($lower_ratio)) {
- return 0.0;
- } else {
- return $lower_ratio['ratio'];
- }
- }
- public function profit_ratio($mchid)
- {
- $profit_ratio = $this->mTimesConfig[$mchid]['profit_ratio'] ?? 0.0;
- return $profit_ratio;
- }
- public function type_specs($mchid)
- {
- if(array_key_exists($mchid,$this->mMchQTS)) {
- return $this->mMchQTS[$mchid];
- }
- else {
- return [];
- }
- }
- //match: true 表示当前质量满足条件,可以不走溢价,false,表示可以走溢价
- //can_last: true,能补充,false 表示不能走溢价
- public function ratio_match($mchid, $org_quality, $card_type, $spec, $qualities)
- {
- $formula = $this->profit_formula($mchid);
- if($formula === 'all')
- {
- $matcher = new whole_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
- return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
- }
- elseif($formula === 'qt') {
- $matcher = new type_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
- return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
- }
- else {
- $matcher = new type_spec_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
- return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
- }
- }
- }
|