mgroup.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. //机构指定通道分配
  3. namespace refill;
  4. use algorithm;
  5. use Log;
  6. class mchannel_item
  7. {
  8. private $mMchid;
  9. private $mChannelMap;
  10. private $mChannelTurn;
  11. public function __construct($mchid)
  12. {
  13. $this->mMchid = $mchid;
  14. $this->mChannelMap = [];
  15. $this->mChannelTurn = [];
  16. }
  17. public function add_channel($spec,$card_type,$quality,$chname)
  18. {
  19. $key = "{$quality}-{$card_type}-{$spec}";
  20. if(!array_key_exists($key,$this->mChannelMap)) {
  21. $this->mChannelMap[$key] = [];
  22. }
  23. $chanels = $this->mChannelMap[$key];
  24. if(!in_array($chname,$chanels)) {
  25. $this->mChannelMap[$key][] = $chname;
  26. }
  27. }
  28. public function add_turn($qtype,$can_other)
  29. {
  30. $this->mChannelTurn[$qtype] = $can_other;
  31. }
  32. public function find($spec,$card_type,$quality)
  33. {
  34. $key = "{$quality}-{$card_type}-{$spec}";
  35. if(array_key_exists($key,$this->mChannelMap)) {
  36. $chanels = $this->mChannelMap[$key];
  37. } else {
  38. $chanels = [];
  39. }
  40. $key = "{$quality}-{$card_type}";
  41. if(array_key_exists($key,$this->mChannelTurn)) {
  42. $can_other = $this->mChannelMap[$key];
  43. } else {
  44. $can_other = true;
  45. }
  46. return [$can_other,$chanels];
  47. }
  48. }
  49. class rgroup
  50. {
  51. private $mGroupID;
  52. private $mCtlitems;
  53. private $mChannelTurn;
  54. public function __construct($group_id)
  55. {
  56. $this->mGroupID = $group_id;
  57. $this->mCtlitems = [];
  58. $this->mChannelTurn = [];
  59. $this->load();
  60. }
  61. private function load()
  62. {
  63. $mod = Model('provider_group');
  64. $items = $mod->getGroupInfos(['group_id' => $this->mGroupID]);
  65. $info = [];
  66. foreach ($items as $item)
  67. {
  68. $info_texts = explode(',', $item['info']);
  69. foreach ($info_texts as $text) {
  70. $info[] = $text;
  71. }
  72. $quality = intval($item['quality']);
  73. $type = intval($item['type']);
  74. $can_other = intval($item['is_only']) === 0;
  75. if($type == 1) {
  76. $card_types = [1,2];
  77. }
  78. elseif($type == 2) {
  79. $card_types = [4,5,6];
  80. }
  81. else {
  82. $card_types = [];
  83. }
  84. foreach ($card_types as $card_type) {
  85. $this->mChannelTurn["{$quality}-{$card_type}"] = $can_other;
  86. }
  87. }
  88. $this->mCtlitems = $info;
  89. }
  90. public function infos()
  91. {
  92. return $this->mCtlitems;
  93. }
  94. public function channel_turns()
  95. {
  96. return $this->mChannelTurn;
  97. }
  98. }
  99. //通道控制
  100. class rgroup_ctl
  101. {
  102. private $mMch2Channel;
  103. public function __construct()
  104. {
  105. $this->mMch2Channel = [];
  106. }
  107. public function load($opened_names,$opened_merchants)
  108. {
  109. //加载所有的通道组
  110. $groups = $this->load_groups();
  111. $this->load_merchant($groups,$opened_names,$opened_merchants);
  112. }
  113. public function find_providers($mchid,$spec,$card_type,$quality)
  114. {
  115. if (!array_key_exists($mchid,$this->mMch2Channel)) {
  116. return [false,true,[]];
  117. }
  118. else {
  119. $item = $this->mMch2Channel[$mchid];
  120. [$can_other,$channels] = $item->find($spec,$card_type,$quality);
  121. return [true,$can_other,$channels];
  122. }
  123. }
  124. private function load_groups()
  125. {
  126. $mod = Model('provider_group');
  127. $items = $mod->getGroupList(['group_id' => ['gt' , 0]]);
  128. //遍历所有的group 表
  129. foreach ($items as $item) {
  130. $group_id = $item['group_id'];
  131. $group = new rgroup($group_id);
  132. $groups[$group_id] = $group;
  133. }
  134. return $groups;
  135. }
  136. private function load_merchant($groups,$opened_names,$opened_merchants)
  137. {
  138. $this->mMch2Channel = [];
  139. $mchitems = Model('')->table('merchant')->limit(1000)->select();
  140. foreach ($mchitems as $item)
  141. {
  142. $mchid = intval($item['mchid']);
  143. if(!algorithm::binary_search($opened_merchants,$mchid)) {
  144. continue;
  145. }
  146. $ids_text = $item['group_ids'];
  147. if (empty($ids_text)) continue;
  148. $gids = explode(',', $ids_text);
  149. if (empty($gids)) continue;
  150. $mchchannel = new mchannel_item($mchid);
  151. $has_group = false;
  152. foreach ($gids as $gid)
  153. {
  154. if (array_key_exists($gid, $groups))
  155. {
  156. $has_group = true;
  157. $group = $groups[$gid];
  158. $chitems = $group->infos();
  159. foreach ($chitems as $text)
  160. {
  161. if(empty($text)) continue;
  162. [$spec, $card_type, $quality, $chname] = $this->parase($text);
  163. if(!algorithm::binary_search($opened_names,$chname)) {
  164. continue;
  165. }
  166. $mchchannel->add_channel($spec, $card_type, $quality, $chname);
  167. }
  168. $turns = $group->channel_turns();
  169. foreach ($turns as $qtype => $can_other) {
  170. $mchchannel->add_turn($qtype,$can_other);
  171. }
  172. }
  173. }
  174. if ($has_group) {
  175. $this->mMch2Channel[$mchid] = $mchchannel;
  176. }
  177. }
  178. }
  179. private function parase($text)
  180. {
  181. $data = explode('-',$text);
  182. if(count($data) != 5) {
  183. Log::record(__METHOD__ . " group info:{$text} format error.",Log::ERR);
  184. }
  185. $quality = $data[0];
  186. $chname = $data[1];
  187. $card_type = $data[2];
  188. $spec = $data[3];
  189. return [$spec,$card_type,$quality,$chname];
  190. }
  191. }