refill_order.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. $task = $manager->add_task('refill_order_stat',$cond,0,$period);
  191. if($task->waiting()) {
  192. $sec = time() - $task->add_time();
  193. $comment = "已经加入后台任务,已经等待{$sec}秒";
  194. } elseif($task->processing()) {
  195. $sec = time() - $task->act_time();
  196. $comment = "正在处理中,已经执行{$sec}秒";
  197. } else {
  198. $sec = $task->fini_time() - $task->act_time();
  199. $comment = "执行完成,共耗时{$sec}秒";
  200. }
  201. Tpl::output('stat_tip', $comment);
  202. if($task->completed() && $task->success())
  203. {
  204. $stat = $task->result();
  205. Tpl::output('stat', $stat);
  206. Tpl::output('end_unixtime', $end);
  207. }
  208. }
  209. $dispatcher_queue_length = refill\util::dispatcher_queue_length();
  210. Tpl::output('dispatcher_queue_length', $dispatcher_queue_length);
  211. Tpl::output('admin_info', $this->getAdminInfo());
  212. Tpl::output('order_list', $order_list);
  213. Tpl::output('provider_list', $provider_list);
  214. Tpl::output('merchant_list', $merchants);
  215. $exmechs = function ($merchants) {
  216. $result = [];
  217. foreach ($merchants as $value) {
  218. $data['name'] = $value['company_name'] ?? $value['name'];
  219. $data['value'] = $value['mchid'];
  220. $data['alpha'] = $value['alpha'];
  221. $data['color'] = false;
  222. $result[] = $data;
  223. }
  224. return base64_encode(json_encode($result));
  225. };
  226. Tpl::output('exmechs', $exmechs($merchants));
  227. Tpl::output('show_page', $mod_refill->showpage());
  228. Tpl::showpage('refill.order.index');
  229. }
  230. private function export_execl($normal_cond, $time_scope, $input)
  231. {
  232. $title = $input['task_title'] ?? '';
  233. $manager = new task\manager();
  234. $cond = ['normal' => $normal_cond,'time_scope' => $time_scope,'export_type'=> $input['export_type']];
  235. $task = $manager->add_task('refill_order_export',$cond,1,-1,$title);
  236. if ($task->completed() && $task->success()) {
  237. $file_name = $task->result();
  238. $file_path = UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . $file_name;
  239. header("Content-Disposition: attachment; filename={$file_name}");
  240. readfile($file_path);
  241. } else {
  242. $task_id = $task->task_id();
  243. showMessage("录入成功,任务ID:{$task_id},请稍后以相同条件再次导出,或在任务列表直接下载。","index.php?act=task&op=index&task_id={$task_id}");
  244. }
  245. }
  246. private function all_order_state_stat($condition)
  247. {
  248. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  249. ->on('refill_order.order_id=vr_order.order_id')
  250. ->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')
  251. ->where($condition)
  252. ->group('order_state')
  253. ->select();
  254. $all = [];
  255. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  256. $sending = $success = $cancel = $data;
  257. foreach ($counts as $count) {
  258. if ($count['order_state'] == ORDER_STATE_SEND) {
  259. $sending = $count;
  260. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  261. $success = $count;
  262. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  263. $cancel = $count;
  264. }
  265. $all['order_count'] += $count['order_count'];
  266. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  267. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  268. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  269. }
  270. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  271. }
  272. public function merchant_dataOp()
  273. {
  274. $merchant_list = $this->merchants();
  275. $result = [];
  276. foreach ($merchant_list as $value) {
  277. $data['name'] = $value['company_name'] ?? $value['name'];
  278. $data['value'] = $value['mchid'];
  279. $data['alpha'] = $value['alpha'];
  280. $data['color'] = false;
  281. $result[] = $data;
  282. }
  283. echo json_encode($result);
  284. }
  285. public function provider_dataOp()
  286. {
  287. $provider_list = $this->providers();
  288. $result = [];
  289. foreach ($provider_list as $value) {
  290. $data['name'] = $value['store_name'] ?? $value['name'];
  291. $data['value'] = $value['store_id'];
  292. $result[] = $data;
  293. }
  294. echo json_encode($result);
  295. }
  296. public function refill_third_infoOp()
  297. {
  298. $order_id = $_GET['order_id'];
  299. $model_refill_order = Model('refill_order');
  300. $order_info = $model_refill_order->getOrderInfo(['order_id' => $order_id]);
  301. if (empty($order_info) || $order_info['card_type'] != mtopcard\ThirdRefillCard) {
  302. $res = ['state' => -1, 'msg' => '订单不存在或类型错误'];
  303. exit(json_encode($res));
  304. }
  305. $third_refill = Model('thrid_refill');
  306. $data = $third_refill->getThird($order_id);
  307. $data['card_info'] = $data['card_info'] ?? '';
  308. $account_type_text = ['手机号','QQ号','微信号'];
  309. $data['account_type_text'] = $account_type_text[$data['account_type']-1];
  310. $ret_state_text = ['初始化','已解析','解析失败'];
  311. $data['ret_state_text'] = $ret_state_text[$data['ret_state']];
  312. $res = ['state' => 1, 'data' => $data];
  313. exit(json_encode($res));
  314. }
  315. public function showOrdersOp()
  316. {
  317. $order_sn = $_GET['order_sn'] ?? '';
  318. $model_refill_order = Model('refill_order');
  319. $refill_info = $model_refill_order->getOrderInfo(['order_sn' => $order_sn]);
  320. if (empty($refill_info)) {
  321. $res = ['state' => -1, 'msg' => '订单不存在'];
  322. exit(json_encode($res));
  323. }
  324. $mch_order = $refill_info['mch_order'];
  325. $order_time = $refill_info['order_time'];
  326. $mchid = $refill_info['mchid'];
  327. $condition['refill_order.mch_order'] = $mch_order;
  328. $condition['refill_order.order_time'] = ['egt', $order_time];
  329. $condition['refill_order.mchid'] = $mchid;
  330. $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';
  331. $order_list = $model_refill_order->getMerchantOrderList($condition, 1000,0, $field);
  332. foreach ($order_list as $order_id => $order_info)
  333. {
  334. $order_list[$order_id]['order_state_text'] = orderState($order_info);
  335. $order_list[$order_id]['notify_time_text'] = '/';
  336. $order_list[$order_id]['commit_time_text'] = date("Y-m-d H:i:s", $order_info['commit_time']);
  337. if ($order_info['notify_time'] > 0)
  338. {
  339. if($order_info['commit_time'] == 0) {
  340. $diff_time = 0;
  341. $order_list[$order_id]['order_state_text'] = '提交失败';
  342. $order_list[$order_id]['commit_time_text'] = '/';
  343. }else{
  344. $diff_time = $order_info['notify_time'] - $order_info['commit_time'];
  345. $order_list[$order_id]['notify_time_text'] = date("Y-m-d H:i:s", $order_info['notify_time']);
  346. }
  347. }
  348. elseif($order_info['commit_time'] == 0) {
  349. $diff_time = 0;
  350. $order_list[$order_id]['order_state_text'] = '提交失败';
  351. $order_list[$order_id]['commit_time_text'] = '/';
  352. }
  353. else
  354. {
  355. $diff_time = time() - $order_info['commit_time'];
  356. if($order_info['order_state'] != ORDER_STATE_SEND) {
  357. $diff_time = 0;
  358. $order_list[$order_id]['order_state_text'] = '提交失败';
  359. }
  360. }
  361. if($diff_time != 0) {
  362. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  363. } else {
  364. $order_list[$order_id]['diff_time_text'] = '/';
  365. }
  366. $order_list[$order_id]['diff_time'] = $diff_time;
  367. }
  368. $mch_notify_state = ['未确认','已确认','回调失败'];
  369. $res = ['state' => 1,
  370. 'data' => $order_list,
  371. 'count' => count($order_list),
  372. 'mch_notify_times' => $refill_info['mch_notify_times'],
  373. 'mch_notify_state' => $mch_notify_state[$refill_info['mch_notify_state']]
  374. ];
  375. exit(json_encode($res));
  376. }
  377. private function mch_notify($condition)
  378. {
  379. $condition['mch_notify_state'] = 0;
  380. $condition['inner_status'] = 0;
  381. $condition['is_retrying'] = 0;
  382. $orders = Model('refill_order')->getAllOrders($condition);
  383. if (!empty($orders))
  384. {
  385. foreach ($orders as $order)
  386. {
  387. $order_id = $order['order_id'];
  388. if ($order['order_state'] == ORDER_STATE_SEND) {
  389. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  390. } else {
  391. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
  392. }
  393. }
  394. }
  395. showMessage('操作成功');
  396. }
  397. private function updateOrderSend($condition)
  398. {
  399. $condition['order_state'] = ORDER_STATE_SEND;
  400. $orders = Model('refill_order')->getAllOrders($condition);
  401. if (!empty($orders)) {
  402. foreach ($orders as $order) {
  403. $order_id = $order['order_id'];
  404. Log::record("updateOrderSend order_id={$order_id}",Log::DEBUG);
  405. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  406. }
  407. }
  408. showMessage('操作成功');
  409. }
  410. }