refill_order.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/refill/util.php');
  3. require_once(BASE_HELPER_PATH . '/task/task_helper.php');
  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. $cond['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. $cond['refill_order.order_sn'] = $_GET['order_sn'];
  22. }
  23. if (!empty($_GET['mch_order'])) {
  24. $cond['refill_order.mch_order'] = $_GET['mch_order'];
  25. }
  26. if (!empty($_GET['ch_trade_no'])) {
  27. $cond['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
  28. }
  29. if (!empty($_GET['card_no'])) {
  30. $cond['refill_order.card_no'] = $_GET['card_no'];
  31. }
  32. if (!empty($_GET['no_mchid'])) {
  33. $no_mchid = explode(',', $_GET['no_mchid']);
  34. $cond['refill_order.mchid'] = ['not in', $no_mchid];
  35. }
  36. if (!empty($_GET['no_amount'])) {
  37. $no_amount = explode(',', $_GET['no_amount']);
  38. $cond['refill_order.refill_amount'] = ['not in', $no_amount];
  39. }
  40. if (!empty($_GET['mchid'])) {
  41. $cond['refill_order.mchid'] = $_GET['mchid'];
  42. }
  43. if (!empty($_GET['channel_name'])) {
  44. $cond['refill_order.channel_name'] = $_GET['channel_name'];
  45. }
  46. if (!empty($_GET['store_id'])) {
  47. $cond['vr_order.store_id'] = $_GET['store_id'];
  48. }
  49. if (!empty($_GET['refill_amount'])) {
  50. $cond['refill_order.refill_amount'] = $_GET['refill_amount'];
  51. }
  52. if (!empty($_GET['quality'])) {
  53. $cond['refill_order.quality'] = $_GET['quality'];
  54. }
  55. if(!empty($_GET['official_status'])) {
  56. if($_GET['official_status'] == 1) {
  57. $cond['official_sn'] = '';
  58. } elseif ($_GET['official_status'] == 2) {
  59. $cond['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. $cond['refill_order.card_type'] = intval($_GET['card_type']);
  65. }
  66. if ($_GET['card_type'] == 'oil') {
  67. $cond['refill_order.card_type'] = ['in', [1, 2]];
  68. }
  69. if ($_GET['card_type'] == 'phone') {
  70. $cond['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. $cur_time = time();
  78. $cur_time = $cur_time - ($cur_time % 300) + 300;
  79. $end_unixtime = $cur_time;
  80. }
  81. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  82. $cond['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  83. $cond['vr_order.add_time'] = [['egt', $start_unixtime],['lt', $end_unixtime], 'and'];
  84. } elseif ($start_unixtime > 0) {
  85. $cond['refill_order.order_time'] = ['egt', $start_unixtime];
  86. $cond['vr_order.add_time'] = ['egt', $start_unixtime];
  87. } elseif ($end_unixtime > 0) {
  88. $cond['refill_order.order_time'] = ['lt', $end_unixtime];
  89. } else {
  90. $start = time() - 3600;
  91. $_GET['query_start_time'] = date("Y-m-d H:i:s",$start);
  92. $cond['refill_order.order_time'] = ['egt', $start];
  93. $cond['vr_order.add_time'] = ['egt', $start-1];
  94. $fToday = true;
  95. }
  96. if(in_array($_GET['cardno_state'], ['0','1', '2', '4', '5'])) {
  97. $cond['refill_order.cardno_state'] = $_GET['cardno_state'];
  98. }
  99. if(in_array($_GET['is_transfer'], ['0','1'])) {
  100. $cond['refill_order.is_transfer'] = $_GET['is_transfer'];
  101. }
  102. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40']))
  103. {
  104. $cond['vr_order.order_state'] = $_GET['order_state'];
  105. if ($_GET['order_state'] == ORDER_STATE_SEND) {
  106. if ($_GET['time'] == 1) {
  107. $cond['refill_order.order_time'] = ['between', [(time() - 3600), (time() - 1800)]];
  108. }
  109. if ($_GET['time'] == 2) {
  110. $cond['refill_order.order_time'] = ['lt', (time() - 3600)];
  111. }
  112. }
  113. }
  114. if (!empty($_GET['export']) || !empty($_GET['export_stats'])) {
  115. if (empty($_GET['query_end_time'])) {
  116. showMessage('订单导出条件必须含有结束日期.');
  117. } else {
  118. $this->RefillOrderExport($cond);
  119. }
  120. return;
  121. }
  122. if (!empty($_GET['mch_notify'])) {
  123. $this->mch_notify($cond);
  124. return;
  125. }
  126. if (!empty($_GET['order_query'])) {
  127. $this->updateOrderSend($cond);
  128. return;
  129. }
  130. $merchant_list = $this->merchants();
  131. $order_list = $model_refill_order->getMerchantOrderList($cond, 200, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  132. $order_list = $this->OrderDataFormat($order_list, $merchant_list);
  133. $provider_list = $this->providers();
  134. if ($fShowStat)
  135. {
  136. $manager = new task\manager();
  137. $task = $manager->add_task('refill_order_stat',$cond,0,600);
  138. if($task->completed() && $task->success())
  139. {
  140. $stat = $task->result();
  141. Tpl::output('stat', $stat);
  142. Tpl::output('comments', false);
  143. Tpl::output('end_unixtime', $end_unixtime);
  144. }else{
  145. Tpl::output('comments', true);
  146. }
  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($cond)
  159. {
  160. if(!empty($_GET['export_stats'])) {
  161. if(!empty($cond['refill_order.order_time'])) {
  162. $cond['refill_order.notify_time'] = $cond['refill_order.order_time'];
  163. unset($cond['refill_order.order_time']);
  164. unset($cond['vr_order.add_time']);
  165. }
  166. }
  167. $manager = new task\manager();
  168. $task = $manager->add_task('refill_order_export',$cond,1);
  169. if ($task->completed() && $task->success()) {
  170. $file_name = $task->result();
  171. $file_path = UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . $file_name;
  172. header("Content-Disposition: attachment; filename={$file_name}");
  173. readfile($file_path);
  174. } else {
  175. $task_id = $task->task_id();
  176. showMessage("录入成功,任务ID:{$task_id},请稍后以相同条件再次导出,或在任务列表直接下载。","index.php?act=task&op=index&task_id={$task_id}");
  177. }
  178. }
  179. private function all_order_state_stat($condition)
  180. {
  181. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  182. ->on('refill_order.order_id=vr_order.order_id')
  183. ->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')
  184. ->where($condition)
  185. ->group('order_state')
  186. ->select();
  187. $all = [];
  188. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  189. $sending = $success = $cancel = $data;
  190. foreach ($counts as $count) {
  191. if ($count['order_state'] == ORDER_STATE_SEND) {
  192. $sending = $count;
  193. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  194. $success = $count;
  195. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  196. $cancel = $count;
  197. }
  198. $all['order_count'] += $count['order_count'];
  199. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  200. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  201. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  202. }
  203. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  204. }
  205. public function merchant_dataOp()
  206. {
  207. $merchant_list = $this->merchants();
  208. $result = [];
  209. foreach ($merchant_list as $value) {
  210. $data['name'] = $value['company_name'] ?? $value['name'];
  211. $data['value'] = $value['mchid'];
  212. $data['alpha'] = $value['alpha'];
  213. $data['color'] = false;
  214. $result[] = $data;
  215. }
  216. echo json_encode($result);
  217. }
  218. public function provider_dataOp()
  219. {
  220. $provider_list = $this->providers();
  221. $result = [];
  222. foreach ($provider_list as $value) {
  223. $data['name'] = $value['store_name'] ?? $value['name'];
  224. $data['value'] = $value['store_id'];
  225. $result[] = $data;
  226. }
  227. echo json_encode($result);
  228. }
  229. public function refill_third_infoOp()
  230. {
  231. $order_id = $_GET['order_id'];
  232. $model_refill_order = Model('refill_order');
  233. $order_info = $model_refill_order->getOrderInfo(['order_id' => $order_id]);
  234. if (empty($order_info) || $order_info['card_type'] != mtopcard\ThirdRefillCard) {
  235. $res = ['state' => -1, 'msg' => '订单不存在或类型错误'];
  236. exit(json_encode($res));
  237. }
  238. $third_refill = Model('thrid_refill');
  239. $data = $third_refill->getThird($order_id);
  240. $data['card_info'] = $data['card_info'] ?? '';
  241. $account_type_text = ['手机号','QQ号','微信号'];
  242. $data['account_type_text'] = $account_type_text[$data['account_type']-1];
  243. $ret_state_text = ['初始化','已解析','解析失败'];
  244. $data['ret_state_text'] = $ret_state_text[$data['ret_state']];
  245. $res = ['state' => 1, 'data' => $data];
  246. exit(json_encode($res));
  247. }
  248. public function showOrdersOp()
  249. {
  250. $order_sn = $_GET['order_sn'] ?? '';
  251. $model_refill_order = Model('refill_order');
  252. $refill_info = $model_refill_order->getOrderInfo(['order_sn' => $order_sn]);
  253. if (empty($refill_info)) {
  254. $res = ['state' => -1, 'msg' => '订单不存在'];
  255. exit(json_encode($res));
  256. }
  257. $mch_order = $refill_info['mch_order'];
  258. $order_time = $refill_info['order_time'];
  259. $mchid = $refill_info['mchid'];
  260. $condition['refill_order.mch_order'] = $mch_order;
  261. $condition['refill_order.order_time'] = ['egt', $order_time];
  262. $condition['refill_order.mchid'] = $mchid;
  263. $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,vr_order.close_reason';
  264. $order_list = $model_refill_order->getMerchantOrderList($condition, 1000, $field);
  265. foreach ($order_list as $order_id => $order_info) {
  266. $order_list[$order_id]['order_state_text'] = orderState($order_info);
  267. $order_list[$order_id]['notify_time_text'] = '/';
  268. $order_list[$order_id]['commit_time_text'] = date("Y-m-d H:i:s", $order_info['commit_time']);
  269. if ($order_info['notify_time'] > 0) {
  270. if($order_info['commit_time'] == 0) {
  271. $diff_time = 0;
  272. $order_list[$order_id]['order_state_text'] = '提交失败';
  273. $order_list[$order_id]['commit_time_text'] = '/';
  274. }else{
  275. $diff_time = $order_info['notify_time'] - $order_info['commit_time'];
  276. $order_list[$order_id]['notify_time_text'] = date("Y-m-d H:i:s", $order_info['notify_time']);
  277. }
  278. } else {
  279. if($order_info['commit_time'] == 0) {
  280. $diff_time = 0;
  281. $order_list[$order_id]['order_state_text'] = '提交失败';
  282. $order_list[$order_id]['commit_time_text'] = '/';
  283. } else {
  284. $diff_time = time() - $order_info['commit_time'];
  285. if($order_info['order_state'] != ORDER_STATE_SEND) {
  286. $diff_time = 0;
  287. $order_list[$order_id]['order_state_text'] = '提交失败';
  288. }
  289. }
  290. }
  291. if($diff_time != 0) {
  292. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  293. } else {
  294. $order_list[$order_id]['diff_time_text'] = '/';
  295. }
  296. $order_list[$order_id]['diff_time'] = $diff_time;
  297. }
  298. $mch_notify_state = ['未确认','已确认','回调失败'];
  299. $res = ['state' => 1,
  300. 'data' => $order_list,
  301. 'count' => count($order_list),
  302. 'mch_notify_times' => $refill_info['mch_notify_times'],
  303. 'mch_notify_state' => $mch_notify_state[$refill_info['mch_notify_state']]
  304. ];
  305. exit(json_encode($res));
  306. }
  307. private function mch_notify($condition)
  308. {
  309. $condition['mch_notify_state'] = 0;
  310. $condition['inner_status'] = 0;
  311. $condition['is_retrying'] = 0;
  312. $orders = Model('refill_order')->getAllOrders($condition);
  313. if (!empty($orders))
  314. {
  315. foreach ($orders as $order)
  316. {
  317. $order_id = $order['order_id'];
  318. if ($order['order_state'] == ORDER_STATE_SEND) {
  319. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  320. } else {
  321. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
  322. }
  323. }
  324. }
  325. showMessage('操作成功');
  326. }
  327. private function updateOrderSend($condition)
  328. {
  329. $condition['order_state'] = ORDER_STATE_SEND;
  330. $orders = Model('refill_order')->getAllOrders($condition);
  331. if (!empty($orders)) {
  332. foreach ($orders as $order) {
  333. $order_id = $order['order_id'];
  334. Log::record("updateOrderSend order_id={$order_id}",Log::DEBUG);
  335. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  336. }
  337. }
  338. showMessage('操作成功');
  339. }
  340. }