refill_order.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d H:i:s", strtotime("-1 hours"));
  14. $this->refill_order($_GET, $fShowStat == 1);
  15. }
  16. public function refill_order($input, $fShowStat = false)
  17. {
  18. $time_type = $input['time_type'];
  19. if(empty($time_type) || !in_array($time_type,['order_time','notify_time'])) {
  20. $time_type = 'order_time';
  21. }
  22. $normal_cond = function ($input)
  23. {
  24. $cond['inner_status'] = 0;
  25. if (!empty($input['order_sn'])) {
  26. $cond['refill_order.order_sn'] = $input['order_sn'];
  27. }
  28. if (!empty($input['mch_order'])) {
  29. $cond['refill_order.mch_order'] = $input['mch_order'];
  30. }
  31. if (!empty($input['ch_trade_no'])) {
  32. $cond['refill_order.ch_trade_no'] = $input['ch_trade_no'];
  33. }
  34. if (!empty($input['card_no'])) {
  35. $cond['refill_order.card_no'] = $input['card_no'];
  36. }
  37. if (!empty($input['no_mchid'])) {
  38. $no_mchid = explode(',', $input['no_mchid']);
  39. $cond['refill_order.mchid'] = ['not in', $no_mchid];
  40. }
  41. if (!empty($input['no_amount'])) {
  42. $no_amount = explode(',', $input['no_amount']);
  43. $cond['refill_order.refill_amount'] = ['not in', $no_amount];
  44. }
  45. if (!empty($input['mchid'])) {
  46. $cond['refill_order.mchid'] = $input['mchid'];
  47. }
  48. if (!empty($input['channel_name'])) {
  49. $cond['refill_order.channel_name'] = $input['channel_name'];
  50. }
  51. if (!empty($input['store_id'])) {
  52. $cond['vr_order.store_id'] = $input['store_id'];
  53. }
  54. if (!empty($input['refill_amount'])) {
  55. $cond['refill_order.refill_amount'] = $input['refill_amount'];
  56. }
  57. if (!empty($input['quality'])) {
  58. $cond['refill_order.quality'] = $input['quality'];
  59. }
  60. if(!empty($input['official_status'])) {
  61. if($input['official_status'] == 1) {
  62. $cond['official_sn'] = '';
  63. } elseif ($input['official_status'] == 2) {
  64. $cond['official_sn'] = ['neq', ''];
  65. }
  66. }
  67. if (!empty($input['card_type'])) {
  68. if (in_array($input['card_type'], ['1', '2', '4', '5', '6', '7'])) {
  69. $cond['refill_order.card_type'] = intval($input['card_type']);
  70. }
  71. if ($input['card_type'] == 'oil') {
  72. $cond['refill_order.card_type'] = ['in', [1, 2]];
  73. }
  74. if ($input['card_type'] == 'phone') {
  75. $cond['refill_order.card_type'] = ['in', [4, 5, 6]];
  76. }
  77. }
  78. if(in_array($input['cardno_state'], ['0','1', '2', '4', '5'])) {
  79. $cond['refill_order.cardno_state'] = $input['cardno_state'];
  80. }
  81. if(in_array($input['is_transfer'], ['0','1'])) {
  82. $cond['refill_order.is_transfer'] = $input['is_transfer'];
  83. }
  84. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40']))
  85. {
  86. $cond['vr_order.order_state'] = $_GET['order_state'];
  87. }
  88. return $cond;
  89. };
  90. if(isset($input['export'])) {
  91. $scope['export_time'] = 'order_time';
  92. } else {
  93. $scope['export_time'] = 'notify_time';
  94. }
  95. $time_scope = function ($input,$time_type)
  96. {
  97. $start = intval(strtotime($input['query_start_time']));
  98. $end = intval(strtotime($input['query_end_time']));
  99. $today = strtotime(date('Y-m-d',time()));
  100. if($start <= 0) {
  101. $start = $today;
  102. }
  103. $cur = time();
  104. $cur = $cur - ($cur % 300) + 300;
  105. if($end <= 0) {
  106. $end = $cur;
  107. }
  108. if ($end >= $start)
  109. {
  110. if ($time_type === 'order_time') {
  111. $end_date = $today + 86400 * 5;
  112. return ['order_time' => [$start, $end], 'add_time' => [$start, $end_date > $cur ? $cur : $end_date]];
  113. } elseif ($time_type === 'notify_time') {
  114. $start_date = $start - 86400 * 5;
  115. return ['order_time' => [$start_date, $end], 'add_time' => [$start_date, $end], 'notify_time' => [$start, $end]];
  116. }
  117. }
  118. return [];
  119. };
  120. $time_cond = function ($scope)
  121. {
  122. $cond = [];
  123. [$start,$end] = $scope['order_time'];
  124. $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
  125. [$start,$end] = $scope['add_time'];
  126. $cond['vr_order.add_time'] = [['egt', $start], ['lt', $end], 'and'];
  127. if(isset($scope['notify_time'])) {
  128. [$start,$end] = $scope['notify_time'];
  129. $cond['refill_order.notify_time'] = [['egt', $start], ['lt', $end], 'and'];
  130. }
  131. return $cond;
  132. };
  133. $scope = $time_scope($input,$time_type);
  134. if(empty($scope)) {
  135. showMessage('结束日期必须大于起始日期.');
  136. }
  137. $normal = $normal_cond($input);
  138. if (!empty($input['export'])) {
  139. $this->export_execl($normal, $scope);
  140. }
  141. $tmcond = $time_cond($scope);
  142. $cond = array_merge($normal,$tmcond);
  143. if (!empty($input['mch_notify'])) {
  144. $this->mch_notify($cond);
  145. return;
  146. }
  147. if (!empty($input['order_query'])) {
  148. $this->updateOrderSend($cond);
  149. return;
  150. }
  151. $merchant_list = $this->merchants();
  152. $mod_refill = Model('refill_order');
  153. $order_list = $mod_refill->getMerchantOrderList($cond, 200, 5000,'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  154. $order_list = $this->OrderDataFormat($order_list, $merchant_list);
  155. $provider_list = $this->providers();
  156. $periodor = function ($start, $end)
  157. {
  158. $delta = $end - $start;
  159. if($delta < 0) return 300;
  160. if($delta / 86400 >= 5) {
  161. $ret = 12 * 3600;
  162. }
  163. elseif($delta / 86400 >= 1) {
  164. $ret = 2 * 3600;
  165. }
  166. elseif($delta / 3600 > 12) {
  167. $ret = 1800;
  168. }
  169. elseif($delta / 3600 > 3) {
  170. $ret = 900;
  171. }
  172. else {
  173. $ret = 300;
  174. }
  175. return $ret;
  176. };
  177. if ($fShowStat)
  178. {
  179. [$start,$end] = $scope[$time_type];
  180. $period = $periodor($start,$end);
  181. $manager = new task\manager();
  182. $task = $manager->add_task('refill_order_stat',$cond,0,$period);
  183. if($task->waiting()) {
  184. $sec = time() - $task->add_time();
  185. $comment = "已经加入后台任务,已经等待{$sec}秒";
  186. } elseif($task->processing()) {
  187. $sec = time() - $task->act_time();
  188. $comment = "正在处理中,已经执行{$sec}秒";
  189. } else {
  190. $sec = $task->fini_time() - $task->act_time();
  191. $comment = "执行完成,共耗时{$sec}秒";
  192. }
  193. Tpl::output('stat_tip', $comment);
  194. if($task->completed() && $task->success())
  195. {
  196. $stat = $task->result();
  197. Tpl::output('stat', $stat);
  198. Tpl::output('end_unixtime', $end);
  199. }
  200. }
  201. $dispatcher_queue_length = refill\util::dispatcher_queue_length();
  202. Tpl::output('dispatcher_queue_length', $dispatcher_queue_length);
  203. Tpl::output('admin_info', $this->getAdminInfo());
  204. Tpl::output('order_list', $order_list);
  205. Tpl::output('provider_list', $provider_list);
  206. Tpl::output('merchant_list', $merchant_list);
  207. Tpl::output('show_page', $mod_refill->showpage());
  208. Tpl::showpage('refill.order.index');
  209. }
  210. private function export_execl($normal_cond, $time_scope)
  211. {
  212. $title = $input['task_title'] ?? '';
  213. $manager = new task\manager();
  214. $cond = ['normal' => $normal_cond,'time_scope' => $time_scope];
  215. $task = $manager->add_task('refill_order_export',$cond,1,-1,$title);
  216. if ($task->completed() && $task->success()) {
  217. $file_name = $task->result();
  218. $file_path = UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . $file_name;
  219. header("Content-Disposition: attachment; filename={$file_name}");
  220. readfile($file_path);
  221. } else {
  222. $task_id = $task->task_id();
  223. showMessage("录入成功,任务ID:{$task_id},请稍后以相同条件再次导出,或在任务列表直接下载。","index.php?act=task&op=index&task_id={$task_id}");
  224. }
  225. }
  226. private function all_order_state_stat($condition)
  227. {
  228. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  229. ->on('refill_order.order_id=vr_order.order_id')
  230. ->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')
  231. ->where($condition)
  232. ->group('order_state')
  233. ->select();
  234. $all = [];
  235. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  236. $sending = $success = $cancel = $data;
  237. foreach ($counts as $count) {
  238. if ($count['order_state'] == ORDER_STATE_SEND) {
  239. $sending = $count;
  240. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  241. $success = $count;
  242. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  243. $cancel = $count;
  244. }
  245. $all['order_count'] += $count['order_count'];
  246. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  247. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  248. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  249. }
  250. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  251. }
  252. public function merchant_dataOp()
  253. {
  254. $merchant_list = $this->merchants();
  255. $result = [];
  256. foreach ($merchant_list as $value) {
  257. $data['name'] = $value['company_name'] ?? $value['name'];
  258. $data['value'] = $value['mchid'];
  259. $data['alpha'] = $value['alpha'];
  260. $data['color'] = false;
  261. $result[] = $data;
  262. }
  263. echo json_encode($result);
  264. }
  265. public function provider_dataOp()
  266. {
  267. $provider_list = $this->providers();
  268. $result = [];
  269. foreach ($provider_list as $value) {
  270. $data['name'] = $value['store_name'] ?? $value['name'];
  271. $data['value'] = $value['store_id'];
  272. $result[] = $data;
  273. }
  274. echo json_encode($result);
  275. }
  276. public function refill_third_infoOp()
  277. {
  278. $order_id = $_GET['order_id'];
  279. $model_refill_order = Model('refill_order');
  280. $order_info = $model_refill_order->getOrderInfo(['order_id' => $order_id]);
  281. if (empty($order_info) || $order_info['card_type'] != mtopcard\ThirdRefillCard) {
  282. $res = ['state' => -1, 'msg' => '订单不存在或类型错误'];
  283. exit(json_encode($res));
  284. }
  285. $third_refill = Model('thrid_refill');
  286. $data = $third_refill->getThird($order_id);
  287. $data['card_info'] = $data['card_info'] ?? '';
  288. $account_type_text = ['手机号','QQ号','微信号'];
  289. $data['account_type_text'] = $account_type_text[$data['account_type']-1];
  290. $ret_state_text = ['初始化','已解析','解析失败'];
  291. $data['ret_state_text'] = $ret_state_text[$data['ret_state']];
  292. $res = ['state' => 1, 'data' => $data];
  293. exit(json_encode($res));
  294. }
  295. public function showOrdersOp()
  296. {
  297. $order_sn = $_GET['order_sn'] ?? '';
  298. $model_refill_order = Model('refill_order');
  299. $refill_info = $model_refill_order->getOrderInfo(['order_sn' => $order_sn]);
  300. if (empty($refill_info)) {
  301. $res = ['state' => -1, 'msg' => '订单不存在'];
  302. exit(json_encode($res));
  303. }
  304. $mch_order = $refill_info['mch_order'];
  305. $order_time = $refill_info['order_time'];
  306. $mchid = $refill_info['mchid'];
  307. $condition['refill_order.mch_order'] = $mch_order;
  308. $condition['refill_order.order_time'] = ['egt', $order_time];
  309. $condition['refill_order.mchid'] = $mchid;
  310. $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';
  311. $order_list = $model_refill_order->getMerchantOrderList($condition, 1000,0, $field);
  312. foreach ($order_list as $order_id => $order_info)
  313. {
  314. $order_list[$order_id]['order_state_text'] = orderState($order_info);
  315. $order_list[$order_id]['notify_time_text'] = '/';
  316. $order_list[$order_id]['commit_time_text'] = date("Y-m-d H:i:s", $order_info['commit_time']);
  317. if ($order_info['notify_time'] > 0)
  318. {
  319. if($order_info['commit_time'] == 0) {
  320. $diff_time = 0;
  321. $order_list[$order_id]['order_state_text'] = '提交失败';
  322. $order_list[$order_id]['commit_time_text'] = '/';
  323. }else{
  324. $diff_time = $order_info['notify_time'] - $order_info['commit_time'];
  325. $order_list[$order_id]['notify_time_text'] = date("Y-m-d H:i:s", $order_info['notify_time']);
  326. }
  327. }
  328. elseif($order_info['commit_time'] == 0) {
  329. $diff_time = 0;
  330. $order_list[$order_id]['order_state_text'] = '提交失败';
  331. $order_list[$order_id]['commit_time_text'] = '/';
  332. }
  333. else
  334. {
  335. $diff_time = time() - $order_info['commit_time'];
  336. if($order_info['order_state'] != ORDER_STATE_SEND) {
  337. $diff_time = 0;
  338. $order_list[$order_id]['order_state_text'] = '提交失败';
  339. }
  340. }
  341. if($diff_time != 0) {
  342. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  343. } else {
  344. $order_list[$order_id]['diff_time_text'] = '/';
  345. }
  346. $order_list[$order_id]['diff_time'] = $diff_time;
  347. }
  348. $mch_notify_state = ['未确认','已确认','回调失败'];
  349. $res = ['state' => 1,
  350. 'data' => $order_list,
  351. 'count' => count($order_list),
  352. 'mch_notify_times' => $refill_info['mch_notify_times'],
  353. 'mch_notify_state' => $mch_notify_state[$refill_info['mch_notify_state']]
  354. ];
  355. exit(json_encode($res));
  356. }
  357. private function mch_notify($condition)
  358. {
  359. $condition['mch_notify_state'] = 0;
  360. $condition['inner_status'] = 0;
  361. $condition['is_retrying'] = 0;
  362. $orders = Model('refill_order')->getAllOrders($condition);
  363. if (!empty($orders))
  364. {
  365. foreach ($orders as $order)
  366. {
  367. $order_id = $order['order_id'];
  368. if ($order['order_state'] == ORDER_STATE_SEND) {
  369. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  370. } else {
  371. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
  372. }
  373. }
  374. }
  375. showMessage('操作成功');
  376. }
  377. private function updateOrderSend($condition)
  378. {
  379. $condition['order_state'] = ORDER_STATE_SEND;
  380. $orders = Model('refill_order')->getAllOrders($condition);
  381. if (!empty($orders)) {
  382. foreach ($orders as $order) {
  383. $order_id = $order['order_id'];
  384. Log::record("updateOrderSend order_id={$order_id}",Log::DEBUG);
  385. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  386. }
  387. }
  388. showMessage('操作成功');
  389. }
  390. }