refill_order.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/refill/util.php');
  3. use refill\util;
  4. class refill_orderControl extends SystemControl
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. public function indexOp()
  11. {
  12. $fShowStat = $_GET['fShowStat'];
  13. $this->refill_order($fShowStat == 1);
  14. }
  15. public function refill_order($fShowStat = false)
  16. {
  17. $model_refill_order = Model('refill_order');
  18. $condition['inner_status'] = 0;
  19. $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d H:i:s", strtotime("-1 hours"));
  20. if (!empty($_GET['order_sn'])) {
  21. $condition['refill_order.order_sn'] = $_GET['order_sn'];
  22. }
  23. if (!empty($_GET['mch_order'])) {
  24. $condition['refill_order.mch_order'] = $_GET['mch_order'];
  25. }
  26. if (!empty($_GET['ch_trade_no'])) {
  27. $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
  28. }
  29. if (!empty($_GET['card_no'])) {
  30. $condition['refill_order.card_no'] = $_GET['card_no'];
  31. }
  32. if (!empty($_GET['no_mchid'])) {
  33. $no_mchid = explode(',', $_GET['no_mchid']);
  34. $condition['refill_order.mchid'] = ['not in', $no_mchid];
  35. }
  36. if (!empty($_GET['no_amount'])) {
  37. $no_amount = explode(',', $_GET['no_amount']);
  38. $condition['refill_order.refill_amount'] = ['not in', $no_amount];
  39. }
  40. if (!empty($_GET['mchid'])) {
  41. $condition['refill_order.mchid'] = $_GET['mchid'];
  42. }
  43. if (!empty($_GET['channel_name'])) {
  44. $condition['refill_order.channel_name'] = $_GET['channel_name'];
  45. }
  46. if (!empty($_GET['store_id'])) {
  47. $condition['vr_order.store_id'] = $_GET['store_id'];
  48. }
  49. if (!empty($_GET['refill_amount'])) {
  50. $condition['refill_order.refill_amount'] = $_GET['refill_amount'];
  51. }
  52. if (!empty($_GET['quality'])) {
  53. $condition['refill_order.quality'] = $_GET['quality'];
  54. }
  55. if(!empty($_GET['official_status'])) {
  56. if($_GET['official_status'] == 1) {
  57. $condition['official_sn'] = '';
  58. } elseif ($_GET['official_status'] == 2) {
  59. $condition['official_sn'] = ['neq', ''];
  60. }
  61. }
  62. if (!empty($_GET['card_type'])) {
  63. if (in_array($_GET['card_type'], ['1', '2', '4', '5', '6', '7'])) {
  64. $condition['refill_order.card_type'] = intval($_GET['card_type']);
  65. }
  66. if ($_GET['card_type'] == 'oil') {
  67. $condition['refill_order.card_type'] = ['in', [1, 2]];
  68. }
  69. if ($_GET['card_type'] == 'phone') {
  70. $condition['refill_order.card_type'] = ['in', [4, 5, 6]];
  71. }
  72. }
  73. $fToday = false;
  74. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  75. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  76. if($end_unixtime <= 0) {
  77. $end_unixtime = time();
  78. }
  79. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  80. $condition['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  81. $condition['vr_order.add_time'] = [['egt', $start_unixtime],['lt', $end_unixtime], 'and'];
  82. } elseif ($start_unixtime > 0) {
  83. $condition['refill_order.order_time'] = ['egt', $start_unixtime];
  84. $condition['vr_order.add_time'] = ['egt', $start_unixtime];
  85. } elseif ($end_unixtime > 0) {
  86. $condition['refill_order.order_time'] = ['lt', $end_unixtime];
  87. } else {
  88. $start = time() - 3600;
  89. $_GET['query_start_time'] = date("Y-m-d H:i:s",$start);
  90. $condition['refill_order.order_time'] = ['egt', $start];
  91. $condition['vr_order.add_time'] = ['egt', $start-1];
  92. $fToday = true;
  93. }
  94. if(in_array($_GET['cardno_state'], ['0','1', '2', '4', '5'])) {
  95. $condition['refill_order.cardno_state'] = $_GET['cardno_state'];
  96. }
  97. if(in_array($_GET['is_transfer'], ['0','1'])) {
  98. $condition['refill_order.is_transfer'] = $_GET['is_transfer'];
  99. }
  100. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40']))
  101. {
  102. $condition['vr_order.order_state'] = $_GET['order_state'];
  103. if ($_GET['order_state'] == ORDER_STATE_SEND) {
  104. if ($_GET['time'] == 1) {
  105. $condition['refill_order.order_time'] = ['between', [(time() - 3600), (time() - 1800)]];
  106. }
  107. if ($_GET['time'] == 2) {
  108. $condition['refill_order.order_time'] = ['lt', (time() - 3600)];
  109. }
  110. }
  111. }
  112. if (!empty($_GET['export']) || !empty($_GET['export_stats'])) {
  113. $this->RefillOrderExport($condition);
  114. exit;
  115. }
  116. if (!empty($_GET['mch_notify'])) {
  117. $this->mch_notify($condition);
  118. exit;
  119. }
  120. if (!empty($_GET['order_query'])) {
  121. $this->updateOrderSend($condition);
  122. exit;
  123. }
  124. $merchants = [];
  125. $merchant_list = $this->merchants();
  126. foreach ($merchant_list as $value) {
  127. $merchants[$value['mchid']] = $value;
  128. }
  129. $order_list = $model_refill_order->getMerchantOrderList($condition, 200, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  130. foreach ($order_list as $order_id => $order_info) {
  131. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  132. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  133. if ($order_info['notify_time'] > 0) {
  134. $diff_time = $order_info['notify_time'] - $order_info['order_time'];
  135. } else {
  136. $diff_time = time() - $order_info['order_time'];
  137. }
  138. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  139. $order_list[$order_id]['diff_time'] = $diff_time;
  140. $order_list[$order_id]['quality_text'] = $this->quality_format($order_info['quality'],$order_info['card_type']);
  141. $order_list[$order_id]['org_quality_text'] = $this->quality_format($order_info['org_quality'],$order_info['card_type']);
  142. }
  143. $provider_list = $this->providers();
  144. if ($fShowStat) {
  145. $stat = $this->all_order_state_stat($condition);
  146. Tpl::output('stat', $stat);
  147. }
  148. $dispatcher_queue_length = refill\util::dispatcher_queue_length();
  149. Tpl::output('dispatcher_queue_length', $dispatcher_queue_length);
  150. Tpl::output('admin_info', $this->getAdminInfo());
  151. Tpl::output('ftoday', $fToday);
  152. Tpl::output('order_list', $order_list);
  153. Tpl::output('provider_list', $provider_list);
  154. Tpl::output('merchant_list', $merchant_list);
  155. Tpl::output('show_page', $model_refill_order->showpage());
  156. Tpl::showpage('refill.order.index');
  157. }
  158. private function RefillOrderExport($condition)
  159. {
  160. if(!empty($_GET['export_stats'])) {
  161. if(!empty($condition['refill_order.order_time'])) {
  162. $condition['refill_order.notify_time'] = $condition['refill_order.order_time'];
  163. unset($condition['refill_order.order_time']);
  164. unset($condition['vr_order.add_time']);
  165. }
  166. }
  167. $result = $this->getAllOrders($condition);
  168. $this->createExcel($result);
  169. }
  170. private function createExcel($data = array())
  171. {
  172. Language::read('export');
  173. import('libraries.excel');
  174. $excel_obj = new Excel();
  175. $excel_data = array();
  176. //设置样式
  177. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  178. //header
  179. $excel_data[0][] = array('styleid' => 's_title', 'data' => '商户号');
  180. $excel_data[0][] = array('styleid' => 's_title', 'data' => '客户订单号');
  181. $excel_data[0][] = array('styleid' => 's_title', 'data' => '平台单号');
  182. $excel_data[0][] = array('styleid' => 's_title', 'data' => '面额');
  183. $excel_data[0][] = array('styleid' => 's_title', 'data' => '充值卡号');
  184. $excel_data[0][] = array('styleid' => 's_title', 'data' => '充值卡类型');
  185. $excel_data[0][] = array('styleid' => 's_title', 'data' => '下单日期');
  186. $excel_data[0][] = array('styleid' => 's_title', 'data' => '完成日期');
  187. $excel_data[0][] = array('styleid' => 's_title', 'data' => '官方流水号');
  188. $excel_data[0][] = array('styleid' => 's_title', 'data' => '订单状态');
  189. $excel_data[0][] = array('styleid' => 's_title', 'data' => '扣款金额');
  190. //data
  191. foreach ((array)$data as $k => $v) {
  192. $tmp = array();
  193. $tmp[] = array('data' => $v['mchid']);
  194. $tmp[] = array('data' => $v['mch_order']);
  195. $tmp[] = array('data' => $v['order_sn']);
  196. $tmp[] = array('data' => $v['refill_amount']);
  197. $tmp[] = array('data' => $v['card_no']);
  198. $tmp[] = array('data' => $this->scard_type($v['card_type']));
  199. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['order_time']));
  200. if (empty($v['notify_time'])) {
  201. $tmp[] = array('data' => '');
  202. } else {
  203. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['notify_time']));
  204. }
  205. $tmp[] = array('data' => $v['official_sn']);
  206. $tmp[] = array('data' => orderState($v));
  207. $tmp[] = array('data' => $v['mch_amount']);
  208. $excel_data[] = $tmp;
  209. }
  210. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  211. $excel_obj->addArray($excel_data);
  212. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  213. $excel_obj->generateXML($excel_obj->charset(L('exp_od_order'), CHARSET) . date('Y-m-d-H', time()));
  214. exit;
  215. }
  216. private function all_order_state_stat($condition)
  217. {
  218. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  219. ->on('refill_order.order_id=vr_order.order_id')
  220. ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
  221. ->where($condition)
  222. ->group('order_state')
  223. ->select();
  224. $all = [];
  225. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  226. $sending = $success = $cancel = $data;
  227. foreach ($counts as $count) {
  228. if ($count['order_state'] == ORDER_STATE_SEND) {
  229. $sending = $count;
  230. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  231. $success = $count;
  232. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  233. $cancel = $count;
  234. }
  235. $all['order_count'] += $count['order_count'];
  236. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  237. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  238. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  239. }
  240. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  241. }
  242. public function merchant_dataOp()
  243. {
  244. $merchant_list = $this->merchants();
  245. $result = [];
  246. foreach ($merchant_list as $value) {
  247. $data['name'] = $value['company_name'] ?? $value['name'];
  248. $data['value'] = $value['mchid'];
  249. $data['alpha'] = $value['alpha'];
  250. $data['color'] = false;
  251. $result[] = $data;
  252. }
  253. echo json_encode($result);
  254. exit;
  255. }
  256. public function provider_dataOp()
  257. {
  258. $provider_list = $this->providers();
  259. $result = [];
  260. foreach ($provider_list as $value) {
  261. $data['name'] = $value['store_name'] ?? $value['name'];
  262. $data['value'] = $value['store_id'];
  263. $result[] = $data;
  264. }
  265. echo json_encode($result);
  266. exit;
  267. }
  268. public function refill_third_infoOp()
  269. {
  270. $order_id = $_GET['order_id'];
  271. $model_refill_order = Model('refill_order');
  272. $order_info = $model_refill_order->getOrderInfo(['order_id' => $order_id]);
  273. if (empty($order_info) || $order_info['card_type'] != mtopcard\ThirdRefillCard) {
  274. $res = ['state' => -1, 'msg' => '订单不存在或类型错误'];
  275. exit(json_encode($res));
  276. }
  277. $third_refill = Model('thrid_refill');
  278. $data = $third_refill->getThird($order_id);
  279. $data['card_info'] = $data['card_info'] ?? '';
  280. $account_type_text = ['手机号','QQ号','微信号'];
  281. $data['account_type_text'] = $account_type_text[$data['account_type']-1];
  282. $ret_state_text = ['初始化','已解析','解析失败'];
  283. $data['ret_state_text'] = $ret_state_text[$data['ret_state']];
  284. $res = ['state' => 1, 'data' => $data];
  285. exit(json_encode($res));
  286. }
  287. public function showOrdersOp()
  288. {
  289. $order_sn = $_GET['order_sn'] ?? '';
  290. $model_refill_order = Model('refill_order');
  291. $refill_info = $model_refill_order->getOrderInfo(['order_sn' => $order_sn]);
  292. if (empty($refill_info)) {
  293. $res = ['state' => -1, 'msg' => '订单不存在'];
  294. exit(json_encode($res));
  295. }
  296. $mch_order = $refill_info['mch_order'];
  297. $order_time = $refill_info['order_time'];
  298. $mchid = $refill_info['mchid'];
  299. $condition['refill_order.mch_order'] = $mch_order;
  300. $condition['refill_order.order_time'] = ['egt', $order_time];
  301. $condition['refill_order.mchid'] = $mchid;
  302. $field = 'refill_order.order_sn,refill_order.commit_time,refill_order.notify_time,refill_order.channel_name,vr_order.order_state,refill_order.err_msg';
  303. $order_list = $model_refill_order->getMerchantOrderList($condition, 1000, $field);
  304. foreach ($order_list as $order_id => $order_info) {
  305. $order_list[$order_id]['order_state_text'] = orderState($order_info);
  306. $order_list[$order_id]['notify_time_text'] = '/';
  307. $order_list[$order_id]['commit_time_text'] = date("Y-m-d H:i:s", $order_info['commit_time']);
  308. if ($order_info['notify_time'] > 0) {
  309. $diff_time = $order_info['notify_time'] - $order_info['commit_time'];
  310. $order_list[$order_id]['notify_time_text'] = date("Y-m-d H:i:s", $order_info['notify_time']);
  311. } else {
  312. if($order_info['commit_time'] == 0) {
  313. $diff_time = 0;
  314. $order_list[$order_id]['order_state_text'] = '提交失败';
  315. $order_list[$order_id]['commit_time_text'] = '/';
  316. } else {
  317. $diff_time = time() - $order_info['commit_time'];
  318. if($order_info['order_state'] != ORDER_STATE_SEND) {
  319. $diff_time = 0;
  320. $order_list[$order_id]['order_state_text'] = '提交失败';
  321. }
  322. }
  323. }
  324. if($diff_time != 0) {
  325. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  326. } else {
  327. $order_list[$order_id]['diff_time_text'] = '/';
  328. }
  329. $order_list[$order_id]['diff_time'] = $diff_time;
  330. }
  331. $mch_notify_state = ['未确认','已确认','回调失败'];
  332. $res = ['state' => 1,
  333. 'data' => $order_list,
  334. 'count' => count($order_list),
  335. 'mch_notify_times' => $refill_info['mch_notify_times'],
  336. 'mch_notify_state' => $mch_notify_state[$refill_info['mch_notify_state']]
  337. ];
  338. exit(json_encode($res));
  339. }
  340. private function mch_notify($condition)
  341. {
  342. $condition['mch_notify_state'] = 0;
  343. $condition['inner_status'] = 0;
  344. $condition['is_retrying'] = 0;
  345. $orders = $this->getAllOrders($condition);
  346. if (!empty($orders))
  347. {
  348. foreach ($orders as $order)
  349. {
  350. $order_id = $order['order_id'];
  351. if ($order['order_state'] == ORDER_STATE_SEND) {
  352. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  353. } else {
  354. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
  355. }
  356. }
  357. }
  358. showMessage('操作成功');
  359. }
  360. private function updateOrderSend($condition)
  361. {
  362. $condition['order_state'] = ORDER_STATE_SEND;
  363. $orders = $this->getAllOrders($condition);
  364. if (!empty($orders)) {
  365. foreach ($orders as $order) {
  366. $order_id = $order['order_id'];
  367. Log::record("updateOrderSend order_id={$order_id}",Log::DEBUG);
  368. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  369. }
  370. }
  371. showMessage('操作成功');
  372. }
  373. private function getAllOrders($condition): array
  374. {
  375. $len = 1000;
  376. $i = 0;
  377. $orders = [];
  378. while (true)
  379. {
  380. $start = $i * $len;
  381. $items = Model('')->table('refill_order,vr_order')
  382. ->field('refill_order.*,vr_order.order_state')
  383. ->join('inner')
  384. ->on('refill_order.order_id=vr_order.order_id')
  385. ->where($condition)
  386. ->order('refill_order.order_time desc')
  387. ->limit("{$start},{$len}")
  388. ->select();
  389. $orders = array_merge($orders,$items);
  390. if (empty($items) || count($items) < $len) {
  391. break;
  392. }
  393. $i++;
  394. }
  395. return $orders;
  396. }
  397. }