mratio_controlex.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace refill;
  3. use Log;
  4. class mratio_controlex
  5. {
  6. private $mTimesConfig; //对应refill.ini 配置文件数据
  7. private $mInterceptConfig;
  8. private $mGrossRatios;
  9. private $mTypeRatios;
  10. private $mDetailRatios;
  11. private $mMchQTS;
  12. private $mMixedPrices;
  13. public function __construct()
  14. {
  15. $this->mTimesConfig = [];
  16. $this->mInterceptConfig = [];
  17. $this->mGrossRatios = [];
  18. $this->mTypeRatios = [];
  19. $this->mDetailRatios = [];
  20. $this->mMixedPrices = [];
  21. }
  22. public function load()
  23. {
  24. $this->load_retry();
  25. $this->load_intercept();
  26. }
  27. private function load_retry()
  28. {
  29. $isDay = functional::isDay();
  30. $mch_configs = function ($isDay) {
  31. $result = [];
  32. $i = 0;
  33. while (true) {
  34. $start = $i * 100;
  35. $items = Model()->table('merchant')->where(['mchid' => ['gt', 0], 'merchant_state' => 1])->field('mchid,retry_times_cfg')->order('mchid asc')->limit("{$start},100")->select();
  36. if (empty($items)) {
  37. break;
  38. }
  39. $i++;
  40. foreach ($items as $item) {
  41. $mchid = intval($item['mchid']);
  42. if ($mchid <= 0) continue;
  43. $retry_times_cfg = unserialize($item['retry_times_cfg']);
  44. if (empty($retry_times_cfg)) continue;
  45. $qualities = &$retry_times_cfg['qualities'];
  46. foreach ($qualities as $quality => $cfg) {
  47. if ($isDay) {
  48. $qualities[$quality]['secs'] = $cfg['day_secs'];
  49. } else {
  50. $qualities[$quality]['secs'] = $cfg['night_secs'];
  51. }
  52. }
  53. $result[$mchid] = $retry_times_cfg;
  54. }
  55. }
  56. return $result;
  57. };
  58. $this->mTimesConfig = $mch_configs($isDay);
  59. }
  60. private function load_intercept()
  61. {
  62. $mch_configs = function ()
  63. {
  64. $result = [];
  65. $i = 0;
  66. while (true)
  67. {
  68. $start = $i * 100;
  69. $items = Model()->table('merchant')->where(['mchid' => ['gt', 0], 'merchant_state' => 1])->field('mchid,intercept_cfg')->order('mchid asc')->limit("{$start},100")->select();
  70. if (empty($items)) {
  71. break;
  72. }
  73. $i++;
  74. foreach ($items as $item)
  75. {
  76. $mchid = intval($item['mchid']);
  77. if ($mchid <= 0) continue;
  78. $cfg = unserialize($item['intercept_cfg']);
  79. if (empty($cfg)) continue;
  80. if (!empty($cfg['segment']))
  81. {
  82. $segment = $cfg['segment'];
  83. $sitems = explode(',', $segment);
  84. $tmp = [];
  85. foreach ($sitems as $sitem)
  86. {
  87. $sitem = trim($sitem);
  88. if (!empty($sitem)) {
  89. $tmp[] = $sitem;
  90. }
  91. }
  92. if (!empty($tmp)) {
  93. $cfg['segment'] = implode('|', $tmp);
  94. } else {
  95. $cfg['segment'] = '';
  96. }
  97. }
  98. $result[$mchid] = $cfg;
  99. }
  100. }
  101. return $result;
  102. };
  103. $this->mInterceptConfig = $mch_configs();
  104. }
  105. public function update($gross_ratios, $detail_ratios,$type_ratios)
  106. {
  107. if (!empty($gross_ratios)) {
  108. $this->mGrossRatios = $gross_ratios;
  109. }
  110. if (!empty($type_ratios)) {
  111. $this->mTypeRatios = $type_ratios;
  112. }
  113. if (!empty($detail_ratios)) {
  114. $this->mDetailRatios = $detail_ratios;
  115. }
  116. $this->mMchQTS = [];
  117. foreach ($detail_ratios as $key => $val) {
  118. [$mchid, $card_type, $spec] = explode('-', $key);
  119. $mchid = intval($mchid);
  120. $card_type = intval($card_type);
  121. $spec = intval($spec);
  122. $this->mMchQTS[$mchid][] = [$card_type, $spec];
  123. }
  124. }
  125. public function setMixedPrice($mchid, $mixed_quality, $card_type, $spec, $prices)
  126. {
  127. $key = "{$card_type}-{$spec}";
  128. $this->mMixedPrices[$mchid][$mixed_quality][$key] = $prices;
  129. }
  130. public function total($mchid, $qualities)
  131. {
  132. if (array_key_exists($mchid, $this->mTimesConfig)) {
  133. $items = $this->mTimesConfig[$mchid]['qualities'];
  134. $times = 0;
  135. $secs = 0;
  136. foreach ($items as $quality => $val)
  137. {
  138. if (!in_array($quality, $qualities, true)) {
  139. continue;
  140. }
  141. $times += $val['times'] ?? 1;
  142. $secs += $val['secs'] ?? 180;
  143. }
  144. return [true, $times, $secs];
  145. } else {
  146. return [false, 0, 0];
  147. }
  148. }
  149. public function times($mchid, $quality)
  150. {
  151. if (array_key_exists($mchid, $this->mTimesConfig)) {
  152. $items = $this->mTimesConfig[$mchid]['qualities'] ?? [];
  153. if (array_key_exists($quality, $items)) {
  154. return $items[$quality]['times'];
  155. }
  156. }
  157. return false;
  158. }
  159. public function seconds($mchid, $quality)
  160. {
  161. if (array_key_exists($mchid, $this->mTimesConfig))
  162. {
  163. $items = $this->mTimesConfig[$mchid]['qualities'] ?? [];
  164. if (array_key_exists($quality, $items)) {
  165. return $items[$quality]['secs'];
  166. }
  167. }
  168. return false;
  169. }
  170. public function exist($mchid)
  171. {
  172. if (array_key_exists($mchid, $this->mTimesConfig)) {
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. }
  178. public function profit_formula($mchid) {
  179. return $this->mTimesConfig[$mchid]['profit_formula'] ?? 'qts';
  180. }
  181. public function lowest_ratio($mchid)
  182. {
  183. $lower_ratio = $this->mTimesConfig[$mchid]['lower_ratio'] ?? [];
  184. if (empty($lower_ratio)) {
  185. return 0.0;
  186. } else {
  187. return $lower_ratio['ratio'];
  188. }
  189. }
  190. public function profit_ratio($mchid)
  191. {
  192. $profit_ratio = $this->mTimesConfig[$mchid]['profit_ratio'] ?? 0.0;
  193. return $profit_ratio;
  194. }
  195. public function type_specs($mchid)
  196. {
  197. if(array_key_exists($mchid,$this->mMchQTS)) {
  198. return $this->mMchQTS[$mchid];
  199. }
  200. else {
  201. return [];
  202. }
  203. }
  204. //match: true 表示当前质量满足条件,可以不走溢价,false,表示可以走溢价
  205. //can_last: true,能补充,false 表示不能走溢价
  206. public function ratio_match($mchid, $org_quality, $card_type, $spec, $qualities)
  207. {
  208. if (count($qualities) <= 1) {
  209. return [true, true];
  210. }
  211. $formula = $this->profit_formula($mchid);
  212. $header = __METHOD__ . " mchid={$mchid} card_type={$card_type} spec={$spec}";
  213. Log::record("{$header} formula={$formula}", Log::DEBUG);
  214. if($formula === 'all')
  215. {
  216. $matcher = new whole_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
  217. return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
  218. }
  219. elseif($formula === 'qt') {
  220. $matcher = new type_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
  221. return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
  222. }
  223. else {
  224. $matcher = new type_spec_match($this,$this->mGrossRatios,$this->mTypeRatios,$this->mDetailRatios);
  225. return $matcher->match($mchid, $org_quality, $card_type, $spec, $qualities);
  226. }
  227. }
  228. //机构订单拦截
  229. public function need_intercept($mchid, $card_type, $card_state, $is_transfer, $card_no): bool
  230. {
  231. $start_with = function ($card_no, $segment) {
  232. $reg = "/^(?:{$segment})\d*$/";
  233. if (preg_match($reg, $card_no, $matches)) {
  234. return true;
  235. } else {
  236. return false;
  237. }
  238. };
  239. $mintercepts = $this->mInterceptConfig;
  240. if (array_key_exists($mchid, $mintercepts)) {
  241. $mintercepts = $mintercepts[$mchid];
  242. if (!empty($mintercepts['card_states']) && in_array($card_state, $mintercepts['card_states'], true)) {
  243. return true;
  244. }
  245. if (!empty($mintercepts['card_types']) && in_array($card_type, $mintercepts['card_types'], true)) {
  246. return true;
  247. }
  248. if ($mintercepts['is_transfer'] && $is_transfer) {
  249. return true;
  250. }
  251. if (!empty($mintercepts['segment']) && $start_with($card_no, $mintercepts['segment'])) {
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. }