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]; } }