ordersendlist.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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_state' => 30])->find();
  18. if(empty($item['mintime'])) {
  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('except_stat', $this->except_stat());
  148. Tpl::output('show_page', $model_refill_order->showpage());
  149. Tpl::showpage('refill.order.send.index');
  150. }
  151. private function extra_stats($base_cond, $timeout_type,$cur_time): array
  152. {
  153. $stat_order = function ($cond)
  154. {
  155. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  156. ->on('refill_order.order_id=vr_order.order_id')
  157. ->field('count(*) as order_count ')
  158. ->where($cond)->find();
  159. return $stat['order_count'];
  160. };
  161. $result = [];
  162. $start_day = strtotime("-5 days",$cur_time);
  163. $extra_conds = [
  164. ['between', [$cur_time - 3600, $cur_time - 1800]],
  165. ['between', [$start_day, $cur_time - 3600]]
  166. ];
  167. if (array_key_exists($timeout_type, $this->mTimeouts)) {
  168. $time_out = $this->mTimeouts[$timeout_type];
  169. $extra_conds[] = ['_multi' => true,
  170. ['egt', $start_day],
  171. ['lt', $cur_time - $time_out]];
  172. }
  173. foreach ($extra_conds as $stat_cond)
  174. {
  175. if (!empty($time_out)) {
  176. $base_cond['vr_order.add_time&vr_order.add_time'] = $stat_cond;
  177. $base_cond['refill_order.order_time&refill_order.order_time'] = $stat_cond;
  178. } else {
  179. $base_cond['refill_order.order_time'] = $stat_cond;
  180. $base_cond['vr_order.add_time'] = $stat_cond;
  181. }
  182. $result[] = $stat_order($base_cond);
  183. }
  184. return $result;
  185. }
  186. private function merchant_timeout_stats($cond)
  187. {
  188. unset($cond['refill_order.mchid']);
  189. unset($cond['vr_order.store_id']);
  190. $stats = Model('')->table('refill_order,vr_order,merchant')
  191. ->join('inner,inner')
  192. ->on('refill_order.order_id=vr_order.order_id,refill_order.mchid=merchant.mchid')
  193. ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, merchant.mchid, merchant.company_name, merchant.name')
  194. ->where($cond)
  195. ->group('refill_order.mchid')
  196. ->order('order_count desc')
  197. ->select();
  198. $special_stat = [];
  199. $order_stat = ['order_count' => 0, 'refill_amounts' => 0];
  200. if(empty($stats)) return [$special_stat, $order_stat];
  201. foreach ($stats as $stat) {
  202. $mchid = intval($stat['mchid']);
  203. $special_stat[$mchid] = [
  204. 'mchid' => $mchid, 'order_count' => $stat['order_count'], 'refill_amounts' => $stat['refill_amounts'],
  205. 'company_name' => $stat['company_name'], 'name' => $stat['name']
  206. ];
  207. $order_stat['order_count'] += $stat['order_count'];
  208. $order_stat['refill_amounts'] += $stat['refill_amounts'];
  209. }
  210. return [$special_stat, $order_stat];
  211. }
  212. private function notify_time($cur_time,$period)
  213. {
  214. $mintime_getter = function () {
  215. $mod = Model();
  216. $item = $mod->table('refill_detail')->field('min(order_time) as mintime')->where(['order_state'=>30])->find();
  217. if(empty($item['mintime'])) {
  218. return time() - 3600;
  219. }
  220. else {
  221. return intval($item['mintime']);
  222. }
  223. };
  224. $start = $mintime_getter();//strtotime("-5 days",$cur_time);
  225. // $today = strtotime(date('Y-m-d',time()));
  226. // $tomorrow = $today + 86400;
  227. $time_cond['vr_order.add_time&vr_order.add_time'] = ['_multi' => true,
  228. ['egt', $start],
  229. ['elt', $cur_time]];
  230. $time_cond['refill_order.order_time&refill_order.order_time'] = ['_multi' => true,
  231. ['egt', $start],
  232. ['elt', $cur_time]];
  233. $time_cond['refill_order.commit_time&refill_order.commit_time'] = ['_multi' => true,
  234. ['egt', $start],
  235. ['elt', $cur_time - $period]];
  236. return $time_cond;
  237. }
  238. public function monitor_notifyOp()
  239. {
  240. $model_refill_order = Model('refill_order');
  241. $base_cond['refill_order.inner_status'] = 0;
  242. $base_cond['vr_order.order_state'] = ORDER_STATE_SEND;
  243. $card_type = $_GET['card_type'];
  244. if (!empty($_GET['store_id'])) {
  245. $base_cond['vr_order.store_id'] = $_GET['store_id'];
  246. }
  247. $cur_time = time();
  248. $period = 180;
  249. if(!empty($_GET['time'])) {
  250. $period = intval($_GET['time']);
  251. }else{
  252. $_GET['time'] = $period;
  253. }
  254. $time_cond = $this->notify_time($cur_time,$period);
  255. if (!empty($_GET['mchid'])) {
  256. $base_cond['refill_order.mchid'] = $_GET['mchid'];
  257. }
  258. if (!empty($card_type))
  259. {
  260. if (in_array($card_type, ['1', '2', '4', '5', '6', '7'])) {
  261. $base_cond['refill_order.card_type'] = $card_type;
  262. }
  263. if ($card_type == 'oil') {
  264. $base_cond['refill_order.card_type'] = ['in', ['1', '2']];
  265. }
  266. if ($card_type == 'phone') {
  267. $base_cond['refill_order.card_type'] = ['in', ['4', '5', '6']];
  268. }
  269. }
  270. if (!empty($_GET['quality'])) {
  271. $base_cond['refill_order.quality'] = $_GET['quality'];
  272. }
  273. $orders_cond = array_merge($base_cond,$time_cond);
  274. if (!empty($_GET['order_query'])) {
  275. $this->updateOrderSend($orders_cond);
  276. return;
  277. }
  278. if (!empty($_GET['export'])) {
  279. $this->RefillOrderExport($orders_cond, '渠道回调超时导出', 'time_out_order');
  280. return;
  281. }
  282. $mchids = Model('')->table('refill_order,vr_order')
  283. ->join('inner')
  284. ->on('refill_order.order_id=vr_order.order_id')
  285. ->field('DISTINCT refill_order.mchid')
  286. ->where($orders_cond)
  287. ->group('refill_order.mchid')
  288. ->order('mchid asc')
  289. ->select();
  290. $mchids = array_column($mchids, 'mchid');
  291. $merchant_list = $this->merchants(['mchid' => ['in', $mchids]]);
  292. $provider_list = $this->providers();
  293. //耗时
  294. $fields = "refill_order.*,vr_order.order_state,({$cur_time} - refill_order.commit_time) as ctimeout";
  295. $order_by = "ctimeout desc";
  296. [$special_stat, $order_stat] = $this->provider_timeout_stats($orders_cond, $provider_list);
  297. if (empty($_GET['store_id'])) {
  298. $order_count = $order_stat['order_count'];
  299. }else{
  300. $order_count = $special_stat[$_GET['store_id']]['order_count'];
  301. }
  302. $order_list = $model_refill_order->getMerchantOrderList($orders_cond, 200, $order_count, $fields, $order_by);
  303. if(!empty($order_list)) {
  304. $order_list = $this->orderFormat($order_list, $merchant_list);
  305. }
  306. Tpl::output('stat', $order_stat);
  307. Tpl::output('special_stat', $special_stat);
  308. Tpl::output('order_list', $order_list);
  309. Tpl::output('merchant_list', $merchant_list);
  310. Tpl::output('provider_list', $provider_list);
  311. Tpl::output('except_stat', $this->except_stat());
  312. Tpl::output('show_page', $model_refill_order->showpage());
  313. Tpl::showpage('refill.order.monitor.notify');
  314. }
  315. private function provider_timeout_stats($cond, $provider_list)
  316. {
  317. unset($cond['refill_order.mchid']);
  318. unset($cond['vr_order.store_id']);
  319. $stats = Model('')->table('refill_order,vr_order')
  320. ->join('inner')
  321. ->on('refill_order.order_id=vr_order.order_id')
  322. ->field('count(*) as order_count, store_id, sum(refill_amount) as refill_amounts')
  323. ->where($cond)
  324. ->group('vr_order.store_id')
  325. ->order('order_count desc')
  326. ->select();
  327. $special_stat = [];
  328. $order_stat = ['order_count' => 0, 'refill_amounts' => 0];
  329. if(empty($stats)) return [$special_stat, $order_stat];
  330. foreach ($provider_list as $provider) {
  331. $providers[$provider['store_id']] = $provider;
  332. }
  333. ksort($providers);
  334. foreach ($stats as $stat) {
  335. $store_id = intval($stat['store_id']);
  336. $special_stat[$store_id] = [
  337. 'store_id' => $store_id, 'order_count' => $stat['order_count'], 'refill_amounts' => $stat['refill_amounts'],
  338. 'store_name' => $providers[$store_id]['store_name'], 'opened' =>$providers[$store_id]['opened']
  339. ];
  340. $order_stat['order_count'] += $stat['order_count'];
  341. $order_stat['refill_amounts'] += $stat['refill_amounts'];
  342. }
  343. return [$special_stat, $order_stat];
  344. }
  345. public function ajaxProviderTimeoutOp()
  346. {
  347. $base_cond['refill_order.inner_status'] = 0;
  348. $base_cond['vr_order.order_state'] = ORDER_STATE_SEND;
  349. if (!empty($card_type)) {
  350. if (in_array($card_type, ['1', '2', '4', '5', '6', '7'])) {
  351. $base_cond['refill_order.card_type'] = $card_type;
  352. }
  353. if ($card_type == 'oil') {
  354. $base_cond['refill_order.card_type'] = ['in', ['1', '2']];
  355. }
  356. if ($card_type == 'phone') {
  357. $base_cond['refill_order.card_type'] = ['in', ['4', '5', '6']];
  358. }
  359. }
  360. if (!empty($_GET['quality'])) {
  361. $base_cond['refill_order.quality'] = $_GET['quality'];
  362. }
  363. $cur_time = time();
  364. $period = 180;
  365. if(!empty($_GET['time'])) {
  366. $period = intval($_GET['time']);
  367. }
  368. $time_cond = $this->notify_time($cur_time,$period);
  369. $orders_cond = array_merge($base_cond,$time_cond);
  370. $provider_list = $this->providers();
  371. [$special_stat, $order_stat] = $this->provider_timeout_stats($orders_cond, $provider_list);
  372. echo json_encode(['special_stat' => $special_stat, 'order_stat' =>$order_stat]);
  373. }
  374. private function RefillOrderExport($cond, $method_name = '', $type='order')
  375. {
  376. $result = [];
  377. if($type == 'order') {
  378. $result = Model('refill_order')->getAllOrders($cond);
  379. }elseif($type == 'time_out_order'){
  380. $result = Model('refill_order')->getAllTimeOutOrders($cond);
  381. }
  382. $this->createExcel($result, $method_name);
  383. }
  384. private function createExcel($data = [], $method_name = '')
  385. {
  386. Language::read('export');
  387. import('libraries.excel');
  388. $excel_obj = new Excel();
  389. $excel_data = array();
  390. //设置样式
  391. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  392. //header
  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. $excel_data[0][] = array('styleid' => 's_title', 'data' => '订单状态');
  403. // $excel_data[0][] = array('styleid' => 's_title', 'data' => '扣款金额');
  404. //data
  405. foreach ((array)$data as $v) {
  406. $tmp = array();
  407. $tmp[] = array('data' => $v['mchid']);
  408. $tmp[] = array('data' => $v['mch_order']);
  409. $tmp[] = array('data' => $v['order_sn']);
  410. $tmp[] = array('data' => $v['refill_amount']);
  411. $tmp[] = array('data' => $v['card_no']);
  412. $tmp[] = array('data' => $this->scard_type($v['card_type']));
  413. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['order_time']));
  414. if (empty($v['notify_time'])) {
  415. $tmp[] = array('data' => '');
  416. } else {
  417. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['notify_time']));
  418. }
  419. $tmp[] = array('data' => $v['official_sn']);
  420. $tmp[] = array('data' => orderState($v));
  421. // $tmp[] = array('data' => $v['mch_amount']);
  422. $excel_data[] = $tmp;
  423. }
  424. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  425. $excel_obj->addArray($excel_data);
  426. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  427. $excel_obj->generateXML($method_name . date('Y-m-d-H', time()));
  428. }
  429. public function neterr_orderOp()
  430. {
  431. $model_refill_order = Model('refill_order');
  432. if (!empty($_GET['store_id'])) {
  433. $condition['vr_order.store_id'] = $_GET['store_id'];
  434. }
  435. $condition['refill_order.inner_status'] = 0;
  436. $condition['refill_order.neterr'] = 1;
  437. $condition['vr_order.order_state'] = ORDER_STATE_PAY;
  438. $cur_time = time();
  439. $start_day = strtotime("-5 days",$cur_time);
  440. $condition['vr_order.add_time&vr_order.add_time'] = ['_multi' => true,
  441. ['egt', $start_day],
  442. ['lt', $cur_time]];
  443. $condition['refill_order.order_time&refill_order.order_time'] = ['_multi' => true,
  444. ['egt', $start_day],
  445. ['lt', $cur_time]];
  446. if (!empty($_GET['order_query'])) {
  447. $this->updateOrderSend($condition);
  448. return;
  449. }
  450. if (!empty($_GET['export'])) {
  451. $this->RefillOrderExport($condition, '网络错误订单导出');
  452. return;
  453. }
  454. $merchant_list = $this->merchants();
  455. $provider_list = $this->providers();
  456. //耗时
  457. $fields = "refill_order.*,vr_order.order_state";
  458. $order_by = "refill_order.commit_time asc";
  459. [$special_stat, $order_stat] = $this->provider_timeout_stats($condition, $provider_list);
  460. $order_list = $model_refill_order->getMerchantOrderList($condition, 200, $order_stat['order_count'],$fields, $order_by);
  461. $order_list = $this->orderFormat($order_list, $merchant_list);
  462. Tpl::output('stat', $order_stat);
  463. Tpl::output('special_stat', $special_stat);
  464. Tpl::output('order_list', $order_list);
  465. Tpl::output('show_page', $model_refill_order->showpage());
  466. Tpl::showpage('refill.order.neterr.index');
  467. }
  468. public function neterr_order_manualOp()
  469. {
  470. $type = $_GET['type'];
  471. $official_sn = $_GET['official_sn'] ?? '';
  472. if ($type != 'success' && $type != 'cancel') {
  473. showMessage('手动操作类型错误');
  474. }
  475. $order_ids = $_GET['order_ids'];
  476. $model_refill_order = Model('refill_order');
  477. $condition['refill_order.order_id'] = ['in', $order_ids];
  478. $condition['refill_order.inner_status'] = 0;
  479. $condition['refill_order.neterr'] = 1;
  480. $condition['vr_order.order_state'] = ORDER_STATE_PAY;
  481. $order_list = $model_refill_order->getMerchantOrderList($condition);
  482. if (empty($order_list)) {
  483. showMessage('暂无数据');
  484. }
  485. $logic_vr_order = Logic("vr_order");
  486. $mod_vr_order = Model('vr_order');
  487. foreach ($order_list as $order) {
  488. $order_id = $order['order_id'];
  489. if ($type == 'success') {
  490. $logic_vr_order->changeOrderStateSuccess($order_id, true);
  491. if (!empty($official_sn)) {
  492. $model_refill_order->edit($order_id, ['official_sn' => $official_sn]);
  493. }
  494. } elseif ($type == 'cancel') {
  495. $order_info = $mod_vr_order->getOrderInfo(['order_id' => $order_id]);
  496. $logic_vr_order->changeOrderStateCancel($order_info, '', "充值失败", true, true);
  497. } else {
  498. continue;
  499. }
  500. if ($order['notify_time'] == 0) {
  501. $model_refill_order->edit($order_id, ['notify_state' => 1, 'notify_time' => time()]);
  502. }
  503. util::pop_queue_order($order['mchid'], $order['mch_order']);
  504. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
  505. }
  506. $this->log("手动强制处理", 1);
  507. showMessage('操作成功');
  508. }
  509. public function orderFormat($order_list, $merchant_list): array
  510. {
  511. $merchants = [];
  512. foreach ($merchant_list as $value) {
  513. $merchants[$value['mchid']] = $value;
  514. }
  515. $cur_time = time();
  516. foreach ($order_list as $order_id => $order_info) {
  517. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  518. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  519. if ($order_info['notify_time'] > 0) {
  520. $total_diff_time = $order_info['notify_time'] - $order_info['order_time'];
  521. $diff_time = $order_info['notify_time'] - $order_info['commit_time'];
  522. } else {
  523. $total_diff_time = $cur_time - $order_info['order_time'];
  524. $diff_time = $cur_time - $order_info['commit_time'];
  525. }
  526. if($order_info['mtime_out'] > 0) {
  527. $order_list[$order_id]['mtime_out_text'] = $this->elapse_time($order_info['mtime_out']);
  528. }
  529. $order_list[$order_id]['diff_time'] = $diff_time;
  530. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  531. $order_list[$order_id]['total_diff_time'] = $total_diff_time;
  532. $order_list[$order_id]['total_diff_time_text'] = $this->elapse_time($total_diff_time);
  533. $order_list[$order_id]['quality_text'] = $this->quality_format($order_info['quality'], $order_info['card_type']);
  534. if ($total_diff_time > $merchants[$order_info['mchid']]['time_out'] && $order_info['order_state'] == ORDER_STATE_SEND) {
  535. $order_list[$order_id]['time_out_state'] = 0;
  536. if (in_array($order_info['card_type'], [mtopcard\PetroChinaCard, mtopcard\SinopecCard])) {
  537. $order_list[$order_id]['time_out_state'] = 1;
  538. }
  539. if (in_array($order_info['card_type'], [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  540. if (in_array($order_info['quality'], [
  541. refill\Quality::SlowTwentyFour,
  542. refill\Quality::SlowSix,
  543. refill\Quality::SlowTwo,
  544. refill\Quality::SlowFortyEight,
  545. refill\Quality::SlowSeventyTwo])) {
  546. $order_list[$order_id]['time_out_state'] = 2;
  547. } elseif (in_array($order_info['mchid'], [10132])) {
  548. //重点机构
  549. $order_list[$order_id]['time_out_state'] = 3;
  550. } else {
  551. $order_list[$order_id]['time_out_state'] = 4;
  552. }
  553. }
  554. } else {
  555. $order_list[$order_id]['time_out_state'] = 0;
  556. }
  557. }
  558. return $order_list;
  559. }
  560. public function notify_err_orderOp()
  561. {
  562. $model_refill_order = Model('refill_order');
  563. $order_state_cancel = ORDER_STATE_CANCEL;
  564. $order_state_success = ORDER_STATE_SUCCESS;
  565. $condition['refill_order.inner_status'] = 0;
  566. $condition['refill_order.is_retrying'] = 0;
  567. $condition['vr_order.order_state'] = ['in', "{$order_state_cancel},{$order_state_success}"];
  568. $condition['refill_order.mch_notify_state'] = ['in', "0,2"];
  569. $condition['refill_order.mch_notify_times'] = ['gt', 0];
  570. if (empty($_GET['time'])) {
  571. $_GET['time'] = 1;
  572. }
  573. if (empty($_GET['notify_time'])) {
  574. $_GET['notify_time'] = 180;
  575. }
  576. $cur_time = time();
  577. $time = $_GET['time'] * 3600;
  578. $condition['refill_order.order_time'] = [['egt', ($cur_time - $time)], ['lt', $cur_time], 'and'];
  579. $condition['vr_order.add_time'] = [['egt', ($cur_time - $time)], ['lt', $cur_time], 'and'];
  580. $notify_time = $_GET['notify_time'];
  581. if($notify_time > 0) {
  582. $start_day = strtotime("-5 days",$cur_time);
  583. $condition['refill_order.notify_time'] = [['egt', $start_day], ['lt', ($cur_time - $notify_time)], 'and'];
  584. }
  585. $order_list = $model_refill_order->getMerchantOrderList($condition, 200, 1000,'refill_order.*,vr_order.order_state', '', 1000);
  586. $merchant_list = $this->merchants();
  587. $order_list = $this->orderFormat($order_list, $merchant_list);
  588. Tpl::output('order_list', $order_list);
  589. Tpl::output('show_page', $model_refill_order->showpage());
  590. Tpl::showpage('refill.order.notify.err.index');
  591. }
  592. public function notifyerr_all_notifyOp()
  593. {
  594. if (empty($_GET['time']) || empty($_GET['notify_time'])) {
  595. showMessage('日期条件错误');
  596. }
  597. $model_refill_order = Model('refill_order');
  598. $order_state_cancel = ORDER_STATE_CANCEL;
  599. $order_state_success = ORDER_STATE_SUCCESS;
  600. $condition['refill_order.inner_status'] = 0;
  601. $condition['vr_order.order_state'] = ['in', "{$order_state_cancel},{$order_state_success}"];
  602. $condition['refill_order.mch_notify_state'] = ['in', "0,2"];
  603. $condition['refill_order.is_retrying'] = 0;
  604. $time = $_GET['time'] * 3600;
  605. $condition['refill_order.order_time'] = ['gt', (time() - $time)];
  606. $notify_time = $_GET['notify_time'];
  607. $condition['refill_order.notify_time'] = ['lt', (time() - $notify_time)];
  608. $order_list = $model_refill_order->getMerchantOrderList($condition, '', 0,'refill_order.*,vr_order.order_state', 'refill_order.notify_time asc', 1000);
  609. foreach ($order_list as $order) {
  610. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order['order_id'], 'manual' => true]);
  611. }
  612. showMessage('操作成功');
  613. }
  614. private function updateOrderSend($condition)
  615. {
  616. $condition['order_state'] = ORDER_STATE_SEND;
  617. $orders = $this->getAllTimeOutOrders($condition);
  618. if (!empty($orders)) {
  619. foreach ($orders as $order) {
  620. $order_id = $order['order_id'];
  621. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  622. }
  623. }
  624. showMessage('操作成功');
  625. }
  626. private function getAllTimeOutOrders($condition): array
  627. {
  628. $len = 1000;
  629. $i = 0;
  630. $orders = [];
  631. while (true) {
  632. $start = $i * $len;
  633. $items = Model('')->table('refill_order,vr_order,merchant')
  634. ->field('refill_order.*,vr_order.order_state')
  635. ->join('inner,inner')
  636. ->on('refill_order.order_id=vr_order.order_id,refill_order.mchid=merchant.mchid')
  637. ->where($condition)
  638. ->order('refill_order.order_time desc')
  639. ->limit("{$start},{$len}")->select();
  640. $orders = array_merge($orders,$items);
  641. if (empty($items) || count($items) < $len) {
  642. break;
  643. }
  644. $i++;
  645. }
  646. return $orders;
  647. }
  648. public function order_exceptionOp()
  649. {
  650. $mod_except = Model('refill_exception');
  651. $_GET['except_state'] = $_GET['except_state'] ?? 0;
  652. if (!empty($_GET['order_sns'])) {
  653. $order_sns = rtrim($_GET['order_sns'],',');
  654. $cond['order_sn'] = ['in', $order_sns];
  655. }
  656. if(in_array($_GET['except_state'], ['0', '1'])) {
  657. $cond['except_state'] = $_GET['except_state'];
  658. }
  659. $list = $mod_except->getExceptionList($cond);
  660. $merchant_list = $this->merchants();
  661. foreach ($merchant_list as $value) {
  662. $merchants[$value['mchid']] = $value;
  663. }
  664. Tpl::output('merchant_list', $merchants);
  665. Tpl::output('list', $list);
  666. Tpl::output('except_stat', $this->except_stat());
  667. Tpl::output('show_page', $mod_except->showpage());
  668. Tpl::showpage('refill.order.exception');
  669. }
  670. public function order_exception_handledOp()
  671. {
  672. $except_ids = $_GET['except_ids'];
  673. $except_ids = explode(',', $except_ids);
  674. $cond['except_id'] = ['in',$except_ids];
  675. $cond['except_state'] = 0;
  676. $mod_except = Model('refill_exception');
  677. $excepts = $mod_except->getExceptionList($cond);
  678. if(empty($excepts)) {
  679. showMessage('操作完成');
  680. }
  681. $admininfo = $this->getAdminInfo();
  682. $update['admin_id'] = $admininfo['id'];
  683. $update['admin_name'] = $admininfo['name'];
  684. $update['oper_time'] = time();
  685. foreach ($excepts as $except) {
  686. $exc_id = $except['except_id'];
  687. $mod_except->setHandled($exc_id, $update);
  688. }
  689. showMessage('操作完成');
  690. }
  691. private function except_stat()
  692. {
  693. $mod_except = Model('refill_exception');
  694. $cond['except_state'] = 0;
  695. $except_stat = $mod_except->where($cond)->count();
  696. return intval($except_stat);
  697. }
  698. public function refill_detail_monitorOp()
  699. {
  700. $mod_detail = Model('refill_detail');
  701. $cond['order_state'] = ['neq', ORDER_STATE_HANDLED];
  702. $merchant_list = $this->merchants();
  703. foreach ($merchant_list as $value) {
  704. $merchants[$value['mchid']] = $value;
  705. }
  706. $list = $mod_detail->getRefillDetailList($cond, 30, 30, '*', 'order_time asc');
  707. foreach ($list as $key => $value) {
  708. $params = json_decode($value['params'],true);
  709. $list[$key]['company_name'] = $merchants[$value['mchid']]['company_name'];
  710. $list[$key]['params'] = $params;
  711. if($value['order_state'] == ORDER_STATE_SEND) {
  712. $list[$key]['order_state_text'] = '待收货';
  713. } elseif ($value['order_state'] == ORDER_STATE_QUEUE) {
  714. $list[$key]['order_state_text'] = '队列中';
  715. } elseif ($value['order_state'] == ORDER_STATE_HANDLED) {
  716. $list[$key]['order_state_text'] = '已处理';
  717. } else {
  718. $list[$key]['order_state_text'] = '/';
  719. }
  720. }
  721. Tpl::output('list', $list);
  722. Tpl::output('show_page', $mod_detail->showpage());
  723. Tpl::showpage('refill.detail.monitor');
  724. }
  725. }