mgroup.php 5.2 KB

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