123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- namespace refill;
- use Log;
- class mratio_controlex
- {
- private $mTimesConfig; //对应refill.ini 配置文件数据
- private $mInterceptConfig;
- private $mGrossRatios;
- private $mTypeRatios;
- private $mDetailRatios;
- private $mMchQTS;
- private $mMixedPrices;
- public function __construct()
- {
- $this->mTimesConfig = [];
- $this->mInterceptConfig = [];
- $this->mGrossRatios = [];
- $this->mTypeRatios = [];
- $this->mDetailRatios = [];
- $this->mMixedPrices = [];
- }
- public function load()
- {
- $this->load_retry();
- $this->load_intercept();
- }
- 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);
- }
- private function load_intercept()
- {
- $mch_configs = function ()
- {
- $result = [];
- $i = 0;
- while (true)
- {
- $start = $i * 100;
- $items = Model()->table('merchant')->where(['mchid' => ['gt', 0], 'merchant_state' => 1])->field('mchid,intercept_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;
- $cfg = unserialize($item['intercept_cfg']);
- if (empty($cfg)) continue;
- if (!empty($cfg['segment']))
- {
- $segment = $cfg['segment'];
- $sitems = explode(',', $segment);
- $tmp = [];
- foreach ($sitems as $sitem)
- {
- $sitem = trim($sitem);
- if (!empty($sitem)) {
- $tmp[] = $sitem;
- }
- }
- if (!empty($tmp)) {
- $cfg['segment'] = implode('|', $tmp);
- } else {
- $cfg['segment'] = '';
- }
- }
- $result[$mchid] = $cfg;
- }
- }
- return $result;
- };
- $this->mInterceptConfig = $mch_configs();
- }
- 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 setMixedPrice($mchid, $mixed_quality, $card_type, $spec, $prices)
- {
- $key = "{$card_type}-{$spec}";
- $this->mMixedPrices[$mchid][$mixed_quality][$key] = $prices;
- }
- public function total($mchid, $qualities)
- {
- if (array_key_exists($mchid, $this->mTimesConfig)) {
- $items = $this->mTimesConfig[$mchid]['qualities'];
- $times = 0;
- $secs = 0;
- foreach ($items as $quality => $val)
- {
- if (!in_array($quality, $qualities, true)) {
- continue;
- }
- $times += $val['times'] ?? 1;
- $secs += $val['secs'] ?? 180;
- }
- return [true, $times, $secs];
- } else {
- return [false, 0, 0];
- }
- }
- public function times($mchid, $quality)
- {
- if (array_key_exists($mchid, $this->mTimesConfig)) {
- $items = $this->mTimesConfig[$mchid]['qualities'] ?? [];
- if (array_key_exists($quality, $items)) {
- return $items[$quality]['times'];
- }
- }
- return false;
- }
- public function seconds($mchid, $quality)
- {
- if (array_key_exists($mchid, $this->mTimesConfig))
- {
- $items = $this->mTimesConfig[$mchid]['qualities'] ?? [];
- if (array_key_exists($quality, $items)) {
- return $items[$quality]['secs'];
- }
- }
- return false;
- }
- public function exist($mchid)
- {
- if (array_key_exists($mchid, $this->mTimesConfig)) {
- return true;
- } else {
- return false;
- }
- }
- 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)
- {
- if (count($qualities) <= 1) {
- return [true, true];
- }
- $formula = $this->profit_formula($mchid);
- $header = __METHOD__ . " mchid={$mchid} card_type={$card_type} spec={$spec}";
- Log::record("{$header} formula={$formula}", Log::DEBUG);
- 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);
- }
- }
- //机构订单拦截
- public function need_intercept($mchid, $card_type, $card_state, $is_transfer, $card_no): bool
- {
- $start_with = function ($card_no, $segment) {
- $reg = "/^(?:{$segment})\d*$/";
- if (preg_match($reg, $card_no, $matches)) {
- return true;
- } else {
- return false;
- }
- };
- $mintercepts = $this->mInterceptConfig;
- if (array_key_exists($mchid, $mintercepts)) {
- $mintercepts = $mintercepts[$mchid];
- if (!empty($mintercepts['card_states']) && in_array($card_state, $mintercepts['card_states'], true)) {
- return true;
- }
- if (!empty($mintercepts['card_types']) && in_array($card_type, $mintercepts['card_types'], true)) {
- return true;
- }
- if ($mintercepts['is_transfer'] && $is_transfer) {
- return true;
- }
- if (!empty($mintercepts['segment']) && $start_with($card_no, $mintercepts['segment'])) {
- return true;
- }
- }
- return false;
- }
- }
|