ordersendlist.php 29 KB

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