refill_order.php 18 KB

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