refill_order.php 18 KB

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