123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- //机构指定通道分配
- namespace refill;
- use algorithm;
- use Log;
- class mchannel_item
- {
- private $mMchid;
- private $mChannelMap;
- private $mChannelTurn;
- public function __construct($mchid)
- {
- $this->mMchid = $mchid;
- $this->mChannelMap = [];
- $this->mChannelTurn = [];
- }
- public function add_channel($spec,$card_type,$quality,$chname)
- {
- $key = "{$quality}-{$card_type}-{$spec}";
- if(!array_key_exists($key,$this->mChannelMap)) {
- $this->mChannelMap[$key] = [];
- }
- $chanels = $this->mChannelMap[$key];
- if(!in_array($chname,$chanels)) {
- $this->mChannelMap[$key][] = $chname;
- }
- }
- public function add_turn($qtype,$can_other)
- {
- $this->mChannelTurn[$qtype] = $can_other;
- }
- public function find($spec,$card_type,$quality)
- {
- $key = "{$quality}-{$card_type}-{$spec}";
- if(array_key_exists($key,$this->mChannelMap)) {
- $chanels = $this->mChannelMap[$key];
- } else {
- $chanels = [];
- }
- $key = "{$quality}-{$card_type}";
- if(array_key_exists($key,$this->mChannelTurn)) {
- $can_other = $this->mChannelMap[$key];
- } else {
- $can_other = true;
- }
- return [$can_other,$chanels];
- }
- }
- class rgroup
- {
- private $mGroupID;
- private $mCtlitems;
- private $mChannelTurn;
- public function __construct($group_id)
- {
- $this->mGroupID = $group_id;
- $this->mCtlitems = [];
- $this->mChannelTurn = [];
- $this->load();
- }
- private function load()
- {
- $mod = Model('provider_group');
- $items = $mod->getGroupInfos(['group_id' => $this->mGroupID]);
- $info = [];
- foreach ($items as $item)
- {
- $info_texts = explode(',', $item['info']);
- foreach ($info_texts as $text) {
- $info[] = $text;
- }
- $quality = intval($item['quality']);
- $type = intval($item['type']);
- $can_other = intval($item['is_only']) === 0;
- if($type == 1) {
- $card_types = [1,2];
- }
- elseif($type == 2) {
- $card_types = [4,5,6];
- }
- else {
- $card_types = [];
- }
- foreach ($card_types as $card_type) {
- $this->mChannelTurn["{$quality}-{$card_type}"] = $can_other;
- }
- }
- $this->mCtlitems = $info;
- }
- public function infos()
- {
- return $this->mCtlitems;
- }
- public function channel_turns()
- {
- return $this->mChannelTurn;
- }
- }
- //通道控制
- class rgroup_ctl
- {
- private $mMch2Channel;
- public function __construct()
- {
- $this->mMch2Channel = [];
- }
- public function load($opened_names,$opened_merchants)
- {
- //加载所有的通道组
- $groups = $this->load_groups();
- $this->load_merchant($groups,$opened_names,$opened_merchants);
- }
- public function find_providers($mchid,$spec,$card_type,$quality)
- {
- if (!array_key_exists($mchid,$this->mMch2Channel)) {
- return [false,true,[]];
- }
- else {
- $item = $this->mMch2Channel[$mchid];
- [$can_other,$channels] = $item->find($spec,$card_type,$quality);
- return [true,$can_other,$channels];
- }
- }
- private function load_groups()
- {
- $mod = Model('provider_group');
- $items = $mod->getGroupList(['group_id' => ['gt' , 0]]);
- //遍历所有的group 表
- foreach ($items as $item) {
- $group_id = $item['group_id'];
- $group = new rgroup($group_id);
- $groups[$group_id] = $group;
- }
- return $groups;
- }
- private function load_merchant($groups,$opened_names,$opened_merchants)
- {
- $this->mMch2Channel = [];
- $mchitems = Model('')->table('merchant')->limit(1000)->select();
- foreach ($mchitems as $item)
- {
- $mchid = intval($item['mchid']);
- if(!algorithm::binary_search($opened_merchants,$mchid)) {
- continue;
- }
- $ids_text = $item['group_ids'];
- if (empty($ids_text)) continue;
- $gids = explode(',', $ids_text);
- if (empty($gids)) continue;
- $mchchannel = new mchannel_item($mchid);
- $has_group = false;
- foreach ($gids as $gid)
- {
- if (array_key_exists($gid, $groups))
- {
- $has_group = true;
- $group = $groups[$gid];
- $chitems = $group->infos();
- foreach ($chitems as $text)
- {
- if(empty($text)) continue;
- [$spec, $card_type, $quality, $chname] = $this->parase($text);
- if(!algorithm::binary_search($opened_names,$chname)) {
- continue;
- }
- $mchchannel->add_channel($spec, $card_type, $quality, $chname);
- }
- $turns = $group->channel_turns();
- foreach ($turns as $qtype => $can_other) {
- $mchchannel->add_turn($qtype,$can_other);
- }
- }
- }
- if ($has_group) {
- $this->mMch2Channel[$mchid] = $mchchannel;
- }
- }
- }
- private function parase($text)
- {
- $data = explode('-',$text);
- if(count($data) != 5) {
- Log::record(__METHOD__ . " group info:{$text} format error.",Log::ERR);
- }
- $quality = $data[0];
- $chname = $data[1];
- $card_type = $data[2];
- $spec = $data[3];
- return [$spec,$card_type,$quality,$chname];
- }
- }
|