ordersendlist.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/refill/util.php');
  3. use refill\util;
  4. class ordersendlistControl extends SystemControl
  5. {
  6. private $mTimeouts;
  7. public function __construct()
  8. {
  9. $this->mTimeouts = [3 => 180, 4 => 300, 5 => 600, 6 => 900, 7 => 7200];
  10. parent::__construct();
  11. }
  12. private function time_cond($timeout_type,$card_type,$cur_time)
  13. {
  14. $mintime_getter = function ()
  15. {
  16. $mod = Model();
  17. $item = $mod->table('refill_detail')->field('min(order_time) as mintime')->where(['order_time' => ['egt', time() - 1 * 86400], 'order_state' => 30])->find();
  18. if(empty($item)) {
  19. return time() - 3600;
  20. }
  21. else {
  22. return intval($item['mintime']);
  23. }
  24. };
  25. $mchid_filter = function ($time_out) {
  26. $mchids = Model('')->table('merchant')->where(['time_out' => ['elt', $time_out]])->field('mchid')->select();
  27. $mchids = array_column($mchids, 'mchid');
  28. $mchids = implode(',', $mchids);
  29. return $mchids;
  30. };
  31. $period = 20;
  32. $start_day = $mintime_getter();//strtotime("-5 days",$cur_time);
  33. $time_cond = [];
  34. $mch_cond = [];
  35. if ($timeout_type > 0)
  36. {
  37. if (in_array($timeout_type, [1, 2]))
  38. {
  39. if ($timeout_type === 1) {
  40. $time_cond['refill_order.order_time'] = ['between', [$cur_time - 3600, $cur_time - 1800]];
  41. $time_cond['vr_order.add_time'] = ['between', [$cur_time - 3600, $cur_time]];
  42. }
  43. if ($timeout_type === 2) {
  44. $time_cond['refill_order.order_time'] = ['between', [$start_day, $cur_time - 3600]];
  45. $time_cond['vr_order.add_time'] = ['between', [$start_day, $cur_time]];
  46. }
  47. }
  48. elseif (in_array($timeout_type, [3, 4, 5, 6, 7])) {
  49. $time_out = $this->mTimeouts[$timeout_type];
  50. $mchids = $mchid_filter($time_out);
  51. $time_cond['vr_order.add_time&vr_order.add_time'] = ['_multi' => true, ['egt', $start_day], ['lt', $cur_time]];
  52. $time_cond['refill_order.order_time&refill_order.order_time'] = ['_multi' => true, ['egt', $start_day], ['lt', $cur_time - $time_out]];
  53. $mch_cond['refill_order.mchid'] = ['in', $mchids];
  54. $card_type = 'phone';
  55. }
  56. else {
  57. $time_cond = [];
  58. }
  59. }
  60. else
  61. {
  62. $time_cond['vr_order.add_time&vr_order.add_time'] = ['_multi' => true,
  63. ['egt', $start_day],
  64. ['lt', $cur_time]];
  65. $time_cond['refill_order.order_time&refill_order.order_time&refill_order.order_time'] = ['_multi' => true,
  66. ['egt', $start_day],
  67. ['lt', $cur_time],
  68. ['exp', "refill_order.order_time < {$cur_time} - merchant.time_out + {$period}"]];
  69. }
  70. return [$time_cond, $mch_cond, $card_type];
  71. }
  72. public function indexOp()
  73. {
  74. $model_refill_order = Model('refill_order');
  75. $base_cond['refill_order.inner_status'] = 0;
  76. $base_cond['vr_order.order_state'] = ORDER_STATE_SEND;
  77. $timeout_type = intval($_GET['time']);
  78. $card_type = $_GET['card_type'];
  79. if (!empty($_GET['store_id'])) {
  80. $base_cond['vr_order.store_id'] = $_GET['store_id'];
  81. }
  82. $cur_time = time();
  83. [$time_cond,$mch_cond,$card_type] = $this->time_cond($timeout_type,$card_type,$cur_time);
  84. if (!empty($_GET['mchid'])) {
  85. $base_cond['refill_order.mchid'] = $_GET['mchid'];
  86. } elseif(!empty($_GET['no_mchid'])) {
  87. $no_mchid = explode(',', $_GET['no_mchid']);
  88. $base_cond['refill_order.mchid'] = ['not in', $no_mchid];
  89. } elseif(!empty($mch_cond)) {
  90. $base_cond = array_merge($base_cond,$mch_cond);
  91. }
  92. if (!empty($card_type))
  93. {
  94. if (in_array($card_type, ['1', '2', '4', '5', '6', '7'])) {
  95. $base_cond['refill_order.card_type'] = $card_type;
  96. }
  97. if ($card_type == 'oil') {
  98. $base_cond['refill_order.card_type'] = ['in', ['1', '2']];
  99. }
  100. if ($card_type == 'phone') {
  101. $base_cond['refill_order.card_type'] = ['in', ['4', '5', '6']];
  102. }
  103. }
  104. if (!empty($_GET['quality'])) {
  105. $base_cond['refill_order.quality'] = $_GET['quality'];
  106. }
  107. $orders_cond = array_merge($base_cond,$time_cond);
  108. if (!empty($_GET['order_query'])) {
  109. $this->updateOrderSend($orders_cond);
  110. return;
  111. }
  112. if (!empty($_GET['export'])) {
  113. $this->RefillOrderExport($orders_cond, '商户超时监控导出', 'time_out_order');
  114. return;
  115. }
  116. // $store_ids = Model('')->table('refill_order,vr_order,merchant')
  117. // ->join('inner,inner')
  118. // ->on('refill_order.order_id=vr_order.order_id,refill_order.mchid=merchant.mchid')
  119. // ->field('DISTINCT vr_order.store_id')
  120. // ->where($orders_cond)
  121. // ->group('vr_order.store_id')
  122. // ->order('store_id asc')
  123. // ->select();
  124. // $store_ids = array_column($store_ids, 'store_id');
  125. $merchant_list = $this->merchants();
  126. // $provider_list = $this->providers(['store.store_id' => ['in', $store_ids]]);
  127. $provider_list = $this->providers();
  128. [$merchant_stat, $order_stat] = $this->merchant_timeout_stats($orders_cond);
  129. if (empty($_GET['mchid'])) {
  130. $order_count = $order_stat['order_count'];
  131. }else{
  132. $order_count = $merchant_stat[$_GET['mchid']]['order_count'];
  133. }
  134. $fields = "refill_order.*,vr_order.order_state,( {$cur_time} - refill_order.order_time - merchant.time_out ) as mtime_out";
  135. $order_by = "mtime_out desc";
  136. $order_list = $model_refill_order->getMerchantTimeOut($orders_cond, 200, $order_count, $fields, $order_by);
  137. $special_stat = $this->extra_stats($base_cond, $timeout_type,$cur_time);
  138. if(!empty($order_list)) {
  139. $order_list = $this->orderFormat($order_list, $merchant_list);
  140. }
  141. Tpl::output('stat', $order_stat);
  142. Tpl::output('count', $special_stat);
  143. Tpl::output('merchant_stat', $merchant_stat);
  144. Tpl::output('order_list', $order_list);
  145. Tpl::output('merchant_list', $merchant_list);
  146. Tpl::output('provider_list', $provider_list);
  147. Tpl::output('show_page', $model_refill_order->showpage());
  148. Tpl::showpage('refill.order.send.index');
  149. }
  150. private function extra_stats($base_cond, $timeout_type,$cur_time): array
  151. {
  152. $stat_order = function ($cond)
  153. {
  154. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  155. ->on('refill_order.order_id=vr_order.order_id')
  156. ->field('count(*) as order_count ')
  157. ->where($cond)->find();
  158. return $stat['order_count'];
  159. };
  160. $result = [];
  161. $start_day = strtotime("-5 days",$cur_time);
  162. $extra_conds = [
  163. ['between', [$cur_time - 3600, $cur_time - 1800]],
  164. ['between', [$start_day, $cur_time - 3600]]
  165. ];
  166. if (array_key_exists($timeout_type, $this->mTimeouts)) {
  167. $time_out = $this->mTimeouts[$timeout_type];
  168. $extra_conds[] = ['_multi' => true,
  169. ['egt', $start_day],
  170. ['lt', $cur_time - $time_out]];
  171. }
  172. foreach ($extra_conds as $stat_cond)
  173. {
  174. if (!empty($time_out)) {
  175. $base_cond['vr_order.add_time&vr_order.add_time'] = $stat_cond;
  176. $base_cond['refill_order.order_time&refill_order.order_time'] = $stat_cond;
  177. } else {
  178. $base_cond['refill_order.order_time'] = $stat_cond;
  179. $base_cond['vr_order.add_time'] = $stat_cond;
  180. }
  181. $result[] = $stat_order($base_cond);
  182. }
  183. return $result;
  184. }
  185. private function merchant_timeout_stats($cond)
  186. {
  187. unset($cond['refill_order.mchid']);
  188. unset($cond['vr_order.store_id']);
  189. $stats = Model('')->table('refill_order,vr_order,merchant')
  190. ->join('inner,inner')
  191. ->on('refill_order.order_id=vr_order.order_id,refill_order.mchid=merchant.mchid')
  192. ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, merchant.mchid, merchant.company_name, merchant.name')
  193. ->where($cond)
  194. ->group('refill_order.mchid')
  195. ->order('order_count desc')
  196. ->select();
  197. $special_stat = [];
  198. $order_stat = ['order_count' => 0, 'refill_amounts' => 0];
  199. if(empty($stats)) return [$special_stat, $order_stat];
  200. foreach ($stats as $stat) {
  201. $mchid = intval($stat['mchid']);
  202. $special_stat[$mchid] = [
  203. 'mchid' => $mchid, 'order_count' => $stat['order_count'], 'refill_amounts' => $stat['refill_amounts'],
  204. 'company_name' => $stat['company_name'], 'name' => $stat['name']
  205. ];
  206. $order_stat['order_count'] += $stat['order_count'];
  207. $order_stat['refill_amounts'] += $stat['refill_amounts'];
  208. }
  209. return [$special_stat, $order_stat];
  210. }
  211. private function notify_time($cur_time,$period)
  212. {
  213. $mintime_getter = function () {
  214. $mod = Model();
  215. $item = $mod->table('refill_detail')->field('min(order_time) as mintime')->where(['order_time' => ['egt',time()-1 * 86400],'order_state'=>30])->find();
  216. if(empty($item)) {
  217. return time() - 3600;
  218. }
  219. else {
  220. return intval($item['mintime']);
  221. }
  222. };
  223. $start = $mintime_getter();//strtotime("-5 days",$cur_time);
  224. // $today = strtotime(date('Y-m-d',time()));
  225. // $tomorrow = $today + 86400;
  226. $time_cond['vr_order.add_time&vr_order.add_time'] = ['_multi' => true,
  227. ['egt', $start],
  228. ['elt', $cur_time]];
  229. $time_cond['refill_order.order_time&refill_order.order_time'] = ['_multi' => true,
  230. ['egt', $start],
  231. ['elt', $cur_time]];
  232. $time_cond['refill_order.commit_time&refill_order.commit_time'] = ['_multi' => true,
  233. ['egt', $start],
  234. ['elt', $cur_time - $period]];
  235. return $time_cond;
  236. }
  237. public function monitor_notifyOp()
  238. {
  239. $model_refill_order = Model('refill_order');
  240. $base_cond['refill_order.inner_status'] = 0;
  241. $base_cond['vr_order.order_state'] = ORDER_STATE_SEND;
  242. $card_type = $_GET['card_type'];
  243. if (!empty($_GET['store_id'])) {
  244. $base_cond['vr_order.store_id'] = $_GET['store_id'];
  245. }
  246. $cur_time = time();
  247. $period = 180;
  248. if(!empty($_GET['time'])) {
  249. $period = intval($_GET['time']);
  250. }else{
  251. $_GET['time'] = $period;
  252. }
  253. $time_cond = $this->notify_time($cur_time,$period);
  254. if (!empty($_GET['mchid'])) {
  255. $base_cond['refill_order.mchid'] = $_GET['mchid'];
  256. }
  257. if (!empty($card_type))
  258. {
  259. if (in_array($card_type, ['1', '2', '4', '5', '6', '7'])) {
  260. $base_cond['refill_order.card_type'] = $card_type;
  261. }
  262. if ($card_type == 'oil') {
  263. $base_cond['refill_order.card_type'] = ['in', ['1', '2']];
  264. }
  265. if ($card_type == 'phone') {
  266. $base_cond['refill_order.card_type'] = ['in', ['4', '5', '6']];
  267. }
  268. }
  269. if (!empty($_GET['quality'])) {
  270. $base_cond['refill_order.quality'] = $_GET['quality'];
  271. }
  272. $orders_cond = array_merge($base_cond,$time_cond);
  273. if (!empty($_GET['order_query'])) {
  274. $this->updateOrderSend($orders_cond);
  275. return;
  276. }
  277. if (!empty($_GET['export'])) {
  278. $this->RefillOrderExport($orders_cond, '渠道回调超时导出', 'time_out_order');
  279. return;
  280. }
  281. $mchids = Model('')->table('refill_order,vr_order')
  282. ->join('inner')
  283. ->on('refill_order.order_id=vr_order.order_id')
  284. ->field('DISTINCT refill_order.mchid')
  285. ->where($orders_cond)
  286. ->group('refill_order.mchid')
  287. ->order('mchid asc')
  288. ->select();
  289. $mchids = array_column($mchids, 'mchid');
  290. $merchant_list = $this->merchants(['mchid' => ['in', $mchids]]);
  291. $provider_list = $this->providers();
  292. //耗时
  293. $fields = "refill_order.*,vr_order.order_state,({$cur_time} - refill_order.commit_time) as ctimeout";
  294. $order_by = "ctimeout desc";
  295. [$special_stat, $order_stat] = $this->provider_timeout_stats($orders_cond, $provider_list);
  296. if (empty($_GET['store_id'])) {
  297. $order_count = $order_stat['order_count'];
  298. }else{
  299. $order_count = $special_stat[$_GET['store_id']]['order_count'];
  300. }
  301. $order_list = $model_refill_order->getMerchantOrderList($orders_cond, 200, $order_count, $fields, $order_by);
  302. if(!empty($order_list)) {
  303. $order_list = $this->orderFormat($order_list, $merchant_list);
  304. }
  305. Tpl::output('stat', $order_stat);
  306. Tpl::output('special_stat', $special_stat);
  307. Tpl::output('order_list', $order_list);
  308. Tpl::output('merchant_list', $merchant_list);
  309. Tpl::output('provider_list', $provider_list);
  310. Tpl::output('show_page', $model_refill_order->showpage());
  311. Tpl::showpage('refill.order.monitor.notify');
  312. }
  313. private function provider_timeout_stats($cond, $provider_list)
  314. {
  315. unset($cond['refill_order.mchid']);
  316. unset($cond['vr_order.store_id']);
  317. $stats = Model('')->table('refill_order,vr_order')
  318. ->join('inner')
  319. ->on('refill_order.order_id=vr_order.order_id')
  320. ->field('count(*) as order_count, store_id, sum(refill_amount) as refill_amounts')
  321. ->where($cond)
  322. ->group('vr_order.store_id')
  323. ->order('order_count desc')
  324. ->select();
  325. $special_stat = [];
  326. $order_stat = ['order_count' => 0, 'refill_amounts' => 0];
  327. if(empty($stats)) return [$special_stat, $order_stat];
  328. foreach ($provider_list as $provider) {
  329. $providers[$provider['store_id']] = $provider;
  330. }
  331. ksort($providers);
  332. foreach ($stats as $stat) {
  333. $store_id = intval($stat['store_id']);
  334. $special_stat[$store_id] = [
  335. 'store_id' => $store_id, 'order_count' => $stat['order_count'], 'refill_amounts' => $stat['refill_amounts'],
  336. 'store_name' => $providers[$store_id]['store_name'], 'opened' =>$providers[$store_id]['opened']
  337. ];
  338. $order_stat['order_count'] += $stat['order_count'];
  339. $order_stat['refill_amounts'] += $stat['refill_amounts'];
  340. }
  341. return [$special_stat, $order_stat];
  342. }
  343. public function ajaxProviderTimeoutOp()
  344. {
  345. $base_cond['refill_order.inner_status'] = 0;
  346. $base_cond['vr_order.order_state'] = ORDER_STATE_SEND;
  347. if (!empty($card_type)) {
  348. if (in_array($card_type, ['1', '2', '4', '5', '6', '7'])) {
  349. $base_cond['refill_order.card_type'] = $card_type;
  350. }
  351. if ($card_type == 'oil') {
  352. $base_cond['refill_order.card_type'] = ['in', ['1', '2']];
  353. }
  354. if ($card_type == 'phone') {
  355. $base_cond['refill_order.card_type'] = ['in', ['4', '5', '6']];
  356. }
  357. }
  358. if (!empty($_GET['quality'])) {
  359. $base_cond['refill_order.quality'] = $_GET['quality'];
  360. }
  361. $cur_time = time();
  362. $period = 180;
  363. if(!empty($_GET['time'])) {
  364. $period = intval($_GET['time']);
  365. }
  366. $time_cond = $this->notify_time($cur_time,$period);
  367. $orders_cond = array_merge($base_cond,$time_cond);
  368. $provider_list = $this->providers();
  369. [$special_stat, $order_stat] = $this->provider_timeout_stats($orders_cond, $provider_list);
  370. echo json_encode(['special_stat' => $special_stat, 'order_stat' =>$order_stat]);
  371. }
  372. private function RefillOrderExport($cond, $method_name = '', $type='order')
  373. {
  374. $result = [];
  375. if($type == 'order') {
  376. $result = Model('refill_order')->getAllOrders($cond);
  377. }elseif($type == 'time_out_order'){
  378. $result = Model('refill_order')->getAllTimeOutOrders($cond);
  379. }
  380. $this->createExcel($result, $method_name);
  381. }
  382. private function createExcel($data = [], $method_name = '')
  383. {
  384. Language::read('export');
  385. import('libraries.excel');
  386. $excel_obj = new Excel();
  387. $excel_data = array();
  388. //设置样式
  389. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  390. //header
  391. $excel_data[0][] = array('styleid' => 's_title', 'data' => '商户号');
  392. $excel_data[0][] = array('styleid' => 's_title', 'data' => '客户订单号');
  393. $excel_data[0][] = array('styleid' => 's_title', 'data' => '平台单号');
  394. $excel_data[0][] = array('styleid' => 's_title', 'data' => '面额');
  395. $excel_data[0][] = array('styleid' => 's_title', 'data' => '充值卡号');
  396. $excel_data[0][] = array('styleid' => 's_title', 'data' => '充值卡类型');
  397. $excel_data[0][] = array('styleid' => 's_title', 'data' => '下单日期');
  398. $excel_data[0][] = array('styleid' => 's_title', 'data' => '完成日期');
  399. $excel_data[0][] = array('styleid' => 's_title', 'data' => '官方流水号');
  400. $excel_data[0][] = array('styleid' => 's_title', 'data' => '订单状态');
  401. $excel_data[0][] = array('styleid' => 's_title', 'data' => '扣款金额');
  402. //data
  403. foreach ((array)$data as $v) {
  404. $tmp = array();
  405. $tmp[] = array('data' => $v['mchid']);
  406. $tmp[] = array('data' => $v['mch_order']);
  407. $tmp[] = array('data' => $v['order_sn']);
  408. $tmp[] = array('data' => $v['refill_amount']);
  409. $tmp[] = array('data' => $v['card_no']);
  410. $tmp[] = array('data' => $this->scard_type($v['card_type']));
  411. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['order_time']));
  412. if (empty($v['notify_time'])) {
  413. $tmp[] = array('data' => '');
  414. } else {
  415. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['notify_time']));
  416. }
  417. $tmp[] = array('data' => $v['official_sn']);
  418. $tmp[] = array('data' => orderState($v));
  419. $tmp[] = array('data' => $v['mch_amount']);
  420. $excel_data[] = $tmp;
  421. }
  422. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  423. $excel_obj->addArray($excel_data);
  424. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  425. $excel_obj->generateXML($method_name . date('Y-m-d-H', time()));
  426. }
  427. public function neterr_orderOp()
  428. {
  429. $model_refill_order = Model('refill_order');
  430. if (!empty($_GET['store_id'])) {
  431. $condition['vr_order.store_id'] = $_GET['store_id'];
  432. }
  433. $condition['refill_order.inner_status'] = 0;
  434. $condition['refill_order.neterr'] = 1;
  435. $condition['vr_order.order_state'] = ORDER_STATE_PAY;
  436. $cur_time = time();
  437. $start_day = strtotime("-5 days",$cur_time);
  438. $condition['vr_order.add_time&vr_order.add_time'] = ['_multi' => true,
  439. ['egt', $start_day],
  440. ['lt', $cur_time]];
  441. $condition['refill_order.order_time&refill_order.order_time'] = ['_multi' => true,
  442. ['egt', $start_day],
  443. ['lt', $cur_time]];
  444. if (!empty($_GET['order_query'])) {
  445. $this->updateOrderSend($condition);
  446. return;
  447. }
  448. if (!empty($_GET['export'])) {
  449. $this->RefillOrderExport($condition, '网络错误订单导出');
  450. return;
  451. }
  452. $merchant_list = $this->merchants();
  453. $provider_list = $this->providers();
  454. //耗时
  455. $fields = "refill_order.*,vr_order.order_state";
  456. $order_by = "refill_order.commit_time asc";
  457. [$special_stat, $order_stat] = $this->provider_timeout_stats($condition, $provider_list);
  458. $order_list = $model_refill_order->getMerchantOrderList($condition, 200, $order_stat['order_count'],$fields, $order_by);
  459. $order_list = $this->orderFormat($order_list, $merchant_list);
  460. Tpl::output('stat', $order_stat);
  461. Tpl::output('special_stat', $special_stat);
  462. Tpl::output('order_list', $order_list);
  463. Tpl::output('show_page', $model_refill_order->showpage());
  464. Tpl::showpage('refill.order.neterr.index');
  465. }
  466. public function neterr_order_manualOp()
  467. {
  468. $type = $_GET['type'];
  469. $official_sn = $_GET['official_sn'] ?? '';
  470. if ($type != 'success' && $type != 'cancel') {
  471. showMessage('手动操作类型错误');
  472. }
  473. $order_ids = $_GET['order_ids'];
  474. $model_refill_order = Model('refill_order');
  475. $condition['refill_order.order_id'] = ['in', $order_ids];
  476. $condition['refill_order.inner_status'] = 0;
  477. $condition['refill_order.neterr'] = 1;
  478. $condition['vr_order.order_state'] = ORDER_STATE_PAY;
  479. $order_list = $model_refill_order->getMerchantOrderList($condition);
  480. if (empty($order_list)) {
  481. showMessage('暂无数据');
  482. }
  483. $logic_vr_order = Logic("vr_order");
  484. $mod_vr_order = Model('vr_order');
  485. foreach ($order_list as $order) {
  486. $order_id = $order['order_id'];
  487. if ($type == 'success') {
  488. $logic_vr_order->changeOrderStateSuccess($order_id, true);
  489. if (!empty($official_sn)) {
  490. $model_refill_order->edit($order_id, ['official_sn' => $official_sn]);
  491. }
  492. } elseif ($type == 'cancel') {
  493. $order_info = $mod_vr_order->getOrderInfo(['order_id' => $order_id]);
  494. $logic_vr_order->changeOrderStateCancel($order_info, '', "充值失败", true, true);
  495. } else {
  496. continue;
  497. }
  498. if ($order['notify_time'] == 0) {
  499. $model_refill_order->edit($order_id, ['notify_state' => 1, 'notify_time' => time()]);
  500. }
  501. util::pop_queue_order($order['mchid'], $order['mch_order']);
  502. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
  503. }
  504. $this->log("手动强制处理", 1);
  505. showMessage('操作成功');
  506. }
  507. public function orderFormat($order_list, $merchant_list): array
  508. {
  509. $merchants = [];
  510. foreach ($merchant_list as $value) {
  511. $merchants[$value['mchid']] = $value;
  512. }
  513. $cur_time = time();
  514. foreach ($order_list as $order_id => $order_info) {
  515. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  516. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  517. if ($order_info['notify_time'] > 0) {
  518. $total_diff_time = $order_info['notify_time'] - $order_info['order_time'];
  519. $diff_time = $order_info['notify_time'] - $order_info['commit_time'];
  520. } else {
  521. $total_diff_time = $cur_time - $order_info['order_time'];
  522. $diff_time = $cur_time - $order_info['commit_time'];
  523. }
  524. if($order_info['mtime_out'] > 0) {
  525. $order_list[$order_id]['mtime_out_text'] = $this->elapse_time($order_info['mtime_out']);
  526. }
  527. $order_list[$order_id]['diff_time'] = $diff_time;
  528. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  529. $order_list[$order_id]['total_diff_time'] = $total_diff_time;
  530. $order_list[$order_id]['total_diff_time_text'] = $this->elapse_time($total_diff_time);
  531. $order_list[$order_id]['quality_text'] = $this->quality_format($order_info['quality'], $order_info['card_type']);
  532. if ($total_diff_time > $merchants[$order_info['mchid']]['time_out'] && $order_info['order_state'] == ORDER_STATE_SEND) {
  533. $order_list[$order_id]['time_out_state'] = 0;
  534. if (in_array($order_info['card_type'], [mtopcard\PetroChinaCard, mtopcard\SinopecCard])) {
  535. $order_list[$order_id]['time_out_state'] = 1;
  536. }
  537. if (in_array($order_info['card_type'], [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  538. if (in_array($order_info['quality'], [
  539. refill\Quality::SlowTwentyFour,
  540. refill\Quality::SlowSix,
  541. refill\Quality::SlowTwo,
  542. refill\Quality::SlowFortyEight,
  543. refill\Quality::SlowSeventyTwo])) {
  544. $order_list[$order_id]['time_out_state'] = 2;
  545. } elseif (in_array($order_info['mchid'], [10132])) {
  546. //重点机构
  547. $order_list[$order_id]['time_out_state'] = 3;
  548. } else {
  549. $order_list[$order_id]['time_out_state'] = 4;
  550. }
  551. }
  552. } else {
  553. $order_list[$order_id]['time_out_state'] = 0;
  554. }
  555. }
  556. return $order_list;
  557. }
  558. public function notify_err_orderOp()
  559. {
  560. $model_refill_order = Model('refill_order');
  561. $order_state_cancel = ORDER_STATE_CANCEL;
  562. $order_state_success = ORDER_STATE_SUCCESS;
  563. $condition['refill_order.inner_status'] = 0;
  564. $condition['refill_order.is_retrying'] = 0;
  565. $condition['vr_order.order_state'] = ['in', "{$order_state_cancel},{$order_state_success}"];
  566. $condition['refill_order.mch_notify_state'] = ['in', "0,2"];
  567. $condition['refill_order.mch_notify_times'] = ['gt', 0];
  568. if (empty($_GET['time'])) {
  569. $_GET['time'] = 1;
  570. }
  571. if (empty($_GET['notify_time'])) {
  572. $_GET['notify_time'] = 180;
  573. }
  574. $cur_time = time();
  575. $time = $_GET['time'] * 3600;
  576. $condition['refill_order.order_time'] = [['egt', ($cur_time - $time)], ['lt', $cur_time], 'and'];
  577. $condition['vr_order.add_time'] = [['egt', ($cur_time - $time)], ['lt', $cur_time], 'and'];
  578. $notify_time = $_GET['notify_time'];
  579. if($notify_time > 0) {
  580. $start_day = strtotime("-5 days",$cur_time);
  581. $condition['refill_order.notify_time'] = [['egt', $start_day], ['lt', ($cur_time - $notify_time)], 'and'];
  582. }
  583. $order_list = $model_refill_order->getMerchantOrderList($condition, 200, 1000,'refill_order.*,vr_order.order_state', '', 1000);
  584. $merchant_list = $this->merchants();
  585. $order_list = $this->orderFormat($order_list, $merchant_list);
  586. Tpl::output('order_list', $order_list);
  587. Tpl::output('show_page', $model_refill_order->showpage());
  588. Tpl::showpage('refill.order.notify.err.index');
  589. }
  590. public function notifyerr_all_notifyOp()
  591. {
  592. if (empty($_GET['time']) || empty($_GET['notify_time'])) {
  593. showMessage('日期条件错误');
  594. }
  595. $model_refill_order = Model('refill_order');
  596. $order_state_cancel = ORDER_STATE_CANCEL;
  597. $order_state_success = ORDER_STATE_SUCCESS;
  598. $condition['refill_order.inner_status'] = 0;
  599. $condition['vr_order.order_state'] = ['in', "{$order_state_cancel},{$order_state_success}"];
  600. $condition['refill_order.mch_notify_state'] = ['in', "0,2"];
  601. $condition['refill_order.is_retrying'] = 0;
  602. $time = $_GET['time'] * 3600;
  603. $condition['refill_order.order_time'] = ['gt', (time() - $time)];
  604. $notify_time = $_GET['notify_time'];
  605. $condition['refill_order.notify_time'] = ['lt', (time() - $notify_time)];
  606. $order_list = $model_refill_order->getMerchantOrderList($condition, '', 0,'refill_order.*,vr_order.order_state', 'refill_order.notify_time asc', 1000);
  607. foreach ($order_list as $order) {
  608. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order['order_id'], 'manual' => true]);
  609. }
  610. showMessage('操作成功');
  611. }
  612. private function updateOrderSend($condition)
  613. {
  614. $condition['order_state'] = ORDER_STATE_SEND;
  615. $orders = $this->getAllTimeOutOrders($condition);
  616. if (!empty($orders)) {
  617. foreach ($orders as $order) {
  618. $order_id = $order['order_id'];
  619. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  620. }
  621. }
  622. showMessage('操作成功');
  623. }
  624. private function getAllTimeOutOrders($condition): array
  625. {
  626. $len = 1000;
  627. $i = 0;
  628. $orders = [];
  629. while (true) {
  630. $start = $i * $len;
  631. $items = Model('')->table('refill_order,vr_order,merchant')
  632. ->field('refill_order.*,vr_order.order_state')
  633. ->join('inner,inner')
  634. ->on('refill_order.order_id=vr_order.order_id,refill_order.mchid=merchant.mchid')
  635. ->where($condition)
  636. ->order('refill_order.order_time desc')
  637. ->limit("{$start},{$len}")->select();
  638. $orders = array_merge($orders,$items);
  639. if (empty($items) || count($items) < $len) {
  640. break;
  641. }
  642. $i++;
  643. }
  644. return $orders;
  645. }
  646. }