refill_order.php 18 KB

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