ordersendlist.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/refill/util.php');
  3. use refill\util;
  4. class ordersendlistControl extends SystemControl
  5. {
  6. private $mTimeouts;
  7. public function __construct()
  8. {
  9. $this->mTimeouts = [3 => 180, 4 => 300, 5 => 600, 6 => 900, 7 => 7200];
  10. parent::__construct();
  11. }
  12. private function time_cond($timeout_type,$card_type,$cur_time)
  13. {
  14. $mchid_filter = function ($time_out) {
  15. $mchids = Model('')->table('merchant')->where(['time_out' => ['elt', $time_out]])->field('mchid')->select();
  16. $mchids = array_column($mchids, 'mchid');
  17. $mchids = implode(',', $mchids);
  18. return $mchids;
  19. };
  20. $period = 20;
  21. $start_day = strtotime("-5 days",$cur_time);
  22. $time_cond = [];
  23. $mch_cond = [];
  24. if ($timeout_type > 0)
  25. {
  26. if (in_array($timeout_type, [1, 2]))
  27. {
  28. if ($timeout_type === 1) {
  29. $time_cond['refill_order.order_time'] = ['between', [$cur_time - 3600, $cur_time - 1800]];
  30. $time_cond['vr_order.add_time'] = ['between', [$cur_time - 3600, $cur_time - 1800]];
  31. }
  32. if ($timeout_type === 2) {
  33. $time_cond['refill_order.order_time'] = ['between', [$start_day, $cur_time - 3600]];
  34. $time_cond['vr_order.add_time'] = ['between', [$start_day, $cur_time - 3600]];
  35. }
  36. }
  37. elseif (in_array($timeout_type, [3, 4, 5, 6, 7])) {
  38. $time_out = $this->mTimeouts[$timeout_type];
  39. $mchids = $mchid_filter($time_out);
  40. $time_cond['vr_order.add_time&vr_order.add_time'] = ['_multi' => true, ['egt', $start_day], ['lt', $cur_time - $time_out]];
  41. $time_cond['refill_order.order_time&refill_order.order_time'] = ['_multi' => true, ['egt', $start_day], ['lt', $cur_time - $time_out]];
  42. $mch_cond['refill_order.mchid'] = ['in', $mchids];
  43. $card_type = 'phone';
  44. }
  45. else {
  46. $time_cond = [];
  47. }
  48. }
  49. else
  50. {
  51. $time_cond['vr_order.add_time&vr_order.add_time'] = ['_multi' => true,
  52. ['egt', $start_day],
  53. ['lt', $cur_time]];
  54. $time_cond['refill_order.order_time&refill_order.order_time&refill_order.order_time'] = ['_multi' => true,
  55. ['egt', $start_day],
  56. ['lt', $cur_time],
  57. ['exp', "refill_order.order_time < {$cur_time} - merchant.time_out + {$period}"]];
  58. }
  59. return [$time_cond, $mch_cond, $card_type];
  60. }
  61. public function indexOp()
  62. {
  63. $model_refill_order = Model('refill_order');
  64. $base_cond['refill_order.inner_status'] = 0;
  65. $base_cond['vr_order.order_state'] = ORDER_STATE_SEND;
  66. $timeout_type = intval($_GET['time']);
  67. $card_type = $_GET['card_type'];
  68. if (!empty($_GET['store_id'])) {
  69. $base_cond['vr_order.store_id'] = $_GET['store_id'];
  70. }
  71. $cur_time = time();
  72. [$time_cond,$mch_cond,$card_type] = $this->time_cond($timeout_type,$card_type,$cur_time);
  73. if (!empty($_GET['mchid'])) {
  74. $base_cond['refill_order.mchid'] = $_GET['mchid'];
  75. } elseif(!empty($_GET['no_mchid'])) {
  76. $no_mchid = explode(',', $_GET['no_mchid']);
  77. $base_cond['refill_order.mchid'] = ['not in', $no_mchid];
  78. } elseif(!empty($mch_cond)) {
  79. $base_cond = array_merge($base_cond,$mch_cond);
  80. }
  81. if (!empty($card_type))
  82. {
  83. if (in_array($card_type, ['1', '2', '4', '5', '6', '7'])) {
  84. $base_cond['refill_order.card_type'] = $card_type;
  85. }
  86. if ($card_type == 'oil') {
  87. $base_cond['refill_order.card_type'] = ['in', ['1', '2']];
  88. }
  89. if ($card_type == 'phone') {
  90. $base_cond['refill_order.card_type'] = ['in', ['4', '5', '6']];
  91. }
  92. }
  93. if (!empty($_GET['quality'])) {
  94. $base_cond['refill_order.quality'] = $_GET['quality'];
  95. }
  96. $orders_cond = array_merge($base_cond,$time_cond);
  97. if (!empty($_GET['order_query'])) {
  98. $this->updateOrderSend($orders_cond);
  99. return;
  100. }
  101. $fields = 'refill_order.*,vr_order.order_state';
  102. $order_by = 'refill_order.quality asc,refill_order.order_time asc';
  103. $order_list = $model_refill_order->getMerchantTimeOut($orders_cond, 200, $fields, $order_by);
  104. $order_stat = Model('')->table('refill_order,vr_order,merchant')
  105. ->join('inner,inner')
  106. ->on('refill_order.order_id=vr_order.order_id,refill_order.mchid=merchant.mchid')
  107. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  108. ->where($orders_cond)->find();
  109. $special_stat = $this->extra_stats($base_cond, $timeout_type,$cur_time);
  110. $merchant_list = $this->merchants();
  111. if(!empty($order_list)) {
  112. $order_list = $this->orderFormat($order_list, $merchant_list);
  113. }
  114. $provider_list = $this->providers();
  115. Tpl::output('stat', $order_stat);
  116. Tpl::output('count', $special_stat);
  117. Tpl::output('order_list', $order_list);
  118. Tpl::output('merchant_list', $merchant_list);
  119. Tpl::output('provider_list', $provider_list);
  120. Tpl::output('show_page', $model_refill_order->showpage());
  121. Tpl::showpage('refill.order.send.index');
  122. }
  123. private function extra_stats($base_cond, $timeout_type,$cur_time): array
  124. {
  125. $stat_order = function ($cond)
  126. {
  127. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  128. ->on('refill_order.order_id=vr_order.order_id')
  129. ->field('count(*) as order_count ')
  130. ->where($cond)->find();
  131. return $stat['order_count'];
  132. };
  133. $result = [];
  134. $start_day = strtotime("-5 days",$cur_time);
  135. $extra_conds = [
  136. ['between', [$cur_time - 3600, $cur_time - 1800]],
  137. [$start_day, $cur_time - 3600]
  138. ];
  139. if (array_key_exists($timeout_type, $this->mTimeouts)) {
  140. $time_out = $this->mTimeouts[$timeout_type];
  141. $extra_conds[] = ['_multi' => true,
  142. ['egt', $start_day],
  143. ['lt', $cur_time - $time_out]];
  144. }
  145. foreach ($extra_conds as $stat_cond)
  146. {
  147. if(!empty($time_out)){
  148. $base_cond['vr_order.add_time&vr_order.add_time'] = $stat_cond;
  149. $base_cond['refill_order.order_time&refill_order.order_time'] = $stat_cond;
  150. }else{
  151. $base_cond['refill_order.order_time'] = $stat_cond;
  152. $base_cond['vr_order.add_time'] = $stat_cond;
  153. }
  154. $result[] = $stat_order($base_cond);
  155. }
  156. return $result;
  157. }
  158. public function neterr_orderOp()
  159. {
  160. $model_refill_order = Model('refill_order');
  161. if (!empty($_GET['store_id'])) {
  162. $condition['vr_order.store_id'] = $_GET['store_id'];
  163. }
  164. $condition['refill_order.inner_status'] = 0;
  165. $condition['refill_order.neterr'] = 1;
  166. $condition['vr_order.order_state'] = ORDER_STATE_PAY;
  167. $order_list = $model_refill_order->getMerchantOrderList($condition, 50, 'refill_order.*,vr_order.order_state', 'refill_order.order_time asc');
  168. $merchant_list = Model('')->table('merchant')->limit(1000)->order('name asc')->select();
  169. $order_list = $this->orderFormat($order_list, $merchant_list);
  170. $provider_list = $this->providers();
  171. Tpl::output('provider_list', $provider_list);
  172. Tpl::output('order_list', $order_list);
  173. Tpl::output('show_page', $model_refill_order->showpage());
  174. Tpl::showpage('refill.order.neterr.index');
  175. }
  176. public function neterr_order_manualOp()
  177. {
  178. $type = $_GET['type'];
  179. $official_sn = $_GET['official_sn'] ?? '';
  180. if ($type != 'success' && $type != 'cancel') {
  181. showMessage('手动操作类型错误');
  182. }
  183. $order_ids = $_GET['order_ids'];
  184. $model_refill_order = Model('refill_order');
  185. $condition['refill_order.order_id'] = ['in', $order_ids];
  186. $condition['refill_order.inner_status'] = 0;
  187. $condition['refill_order.neterr'] = 1;
  188. $condition['vr_order.order_state'] = ORDER_STATE_PAY;
  189. $order_list = $model_refill_order->getMerchantOrderList($condition);
  190. if (empty($order_list)) {
  191. showMessage('暂无数据');
  192. }
  193. $logic_vr_order = Logic("vr_order");
  194. $mod_vr_order = Model('vr_order');
  195. foreach ($order_list as $order) {
  196. $order_id = $order['order_id'];
  197. if ($type == 'success') {
  198. $logic_vr_order->changeOrderStateSuccess($order_id, true);
  199. if (!empty($official_sn)) {
  200. $model_refill_order->edit($order_id, ['official_sn' => $official_sn]);
  201. }
  202. } elseif ($type == 'cancel') {
  203. $order_info = $mod_vr_order->getOrderInfo(['order_id' => $order_id]);
  204. $logic_vr_order->changeOrderStateCancel($order_info, '', "充值失败", true, true);
  205. } else {
  206. continue;
  207. }
  208. if ($order['notify_time'] == 0) {
  209. $model_refill_order->edit($order_id, ['notify_state' => 1, 'notify_time' => time()]);
  210. }
  211. util::pop_queue_order($order['mchid'], $order['mch_order']);
  212. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => true]);
  213. }
  214. showMessage('操作成功');
  215. }
  216. public function orderFormat($order_list, $merchant_list): array
  217. {
  218. $merchants = [];
  219. foreach ($merchant_list as $value) {
  220. $merchants[$value['mchid']] = $value;
  221. }
  222. foreach ($order_list as $order_id => $order_info) {
  223. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  224. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  225. if ($order_info['notify_time'] > 0) {
  226. $diff_time = $order_info['notify_time'] - $order_info['order_time'];
  227. } else {
  228. $diff_time = time() - $order_info['order_time'];
  229. }
  230. $order_list[$order_id]['diff_time'] = $diff_time;
  231. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  232. $order_list[$order_id]['quality_text'] = $this->quality_format($order_info['quality'], $order_info['card_type']);
  233. if ($diff_time > $merchants[$order_info['mchid']]['time_out'] && $order_info['order_state'] == ORDER_STATE_SEND) {
  234. $order_list[$order_id]['time_out_state'] = 0;
  235. if (in_array($order_info['card_type'], [mtopcard\PetroChinaCard, mtopcard\SinopecCard])) {
  236. $order_list[$order_id]['time_out_state'] = 1;
  237. }
  238. if (in_array($order_info['card_type'], [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  239. if (in_array($order_info['quality'], [
  240. refill\Quality::SlowTwentyFour,
  241. refill\Quality::SlowSix,
  242. refill\Quality::SlowTwo,
  243. refill\Quality::SlowFortyEight,
  244. refill\Quality::SlowSeventyTwo])) {
  245. $order_list[$order_id]['time_out_state'] = 2;
  246. } elseif (in_array($order_info['mchid'], [10132])) {
  247. //重点机构
  248. $order_list[$order_id]['time_out_state'] = 3;
  249. } else {
  250. $order_list[$order_id]['time_out_state'] = 4;
  251. }
  252. }
  253. } else {
  254. $order_list[$order_id]['time_out_state'] = 0;
  255. }
  256. }
  257. return $order_list;
  258. }
  259. public function notify_err_orderOp()
  260. {
  261. $model_refill_order = Model('refill_order');
  262. $order_state_cancel = ORDER_STATE_CANCEL;
  263. $order_state_success = ORDER_STATE_SUCCESS;
  264. $condition['refill_order.inner_status'] = 0;
  265. $condition['refill_order.is_retrying'] = 0;
  266. $condition['vr_order.order_state'] = ['in', "{$order_state_cancel},{$order_state_success}"];
  267. $condition['refill_order.mch_notify_state'] = ['in', "0,2"];
  268. $condition['refill_order.mch_notify_times'] = ['gt', 0];
  269. if (empty($_GET['time'])) {
  270. $_GET['time'] = 1;
  271. }
  272. if (empty($_GET['notify_time'])) {
  273. $_GET['notify_time'] = 180;
  274. }
  275. $cur_time = time();
  276. $time = $_GET['time'] * 3600;
  277. $condition['refill_order.order_time'] = [['gt', ($cur_time - $time)], ['lt', $cur_time], 'and'];
  278. $condition['vr_order.add_time'] = [['gt', ($cur_time - $time)], ['lt', $cur_time], 'and'];
  279. $notify_time = $_GET['notify_time'];
  280. if($notify_time > 0) {
  281. $start_day = strtotime("-5 days",$cur_time);
  282. $condition['refill_order.notify_time'] = ['lt', (time() - $notify_time)];
  283. $condition['refill_order.notify_time'] = [['gt', $start_day], ['lt', ($cur_time - $notify_time)], 'and'];
  284. }
  285. $order_list = $model_refill_order->getMerchantOrderList($condition, '', 'refill_order.*,vr_order.order_state', 'refill_order.notify_time asc', 1000);
  286. $merchant_list = Model('')->table('merchant')->limit(1000)->order('name asc')->select();
  287. $order_list = $this->orderFormat($order_list, $merchant_list);
  288. Tpl::output('order_list', $order_list);
  289. Tpl::output('show_page', $model_refill_order->showpage());
  290. Tpl::showpage('refill.order.notify.err.index');
  291. }
  292. public function notifyerr_all_notifyOp()
  293. {
  294. if (empty($_GET['time']) || empty($_GET['notify_time'])) {
  295. showMessage('日期条件错误');
  296. }
  297. $model_refill_order = Model('refill_order');
  298. $order_state_cancel = ORDER_STATE_CANCEL;
  299. $order_state_success = ORDER_STATE_SUCCESS;
  300. $condition['refill_order.inner_status'] = 0;
  301. $condition['vr_order.order_state'] = ['in', "{$order_state_cancel},{$order_state_success}"];
  302. $condition['refill_order.mch_notify_state'] = ['in', "0,2"];
  303. $condition['refill_order.is_retrying'] = 0;
  304. $time = $_GET['time'] * 3600;
  305. $condition['refill_order.order_time'] = ['gt', (time() - $time)];
  306. $notify_time = $_GET['notify_time'];
  307. $condition['refill_order.notify_time'] = ['lt', (time() - $notify_time)];
  308. $order_list = $model_refill_order->getMerchantOrderList($condition, '', 'refill_order.*,vr_order.order_state', 'refill_order.notify_time asc', 1000);
  309. foreach ($order_list as $order) {
  310. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order['order_id'], 'manual' => true]);
  311. }
  312. showMessage('操作成功');
  313. }
  314. private function updateOrderSend($condition)
  315. {
  316. $condition['order_state'] = ORDER_STATE_SEND;
  317. $orders = $this->getAllTimeOutOrders($condition);
  318. if (!empty($orders)) {
  319. foreach ($orders as $order) {
  320. $order_id = $order['order_id'];
  321. QueueClient::push("QueryRefillState", ['order_id' => $order_id]);
  322. }
  323. }
  324. showMessage('操作成功');
  325. }
  326. private function getAllTimeOutOrders($condition): array
  327. {
  328. $len = 1000;
  329. $i = 0;
  330. $orders = [];
  331. while (true) {
  332. $start = $i * $len;
  333. $items = Model('')->table('refill_order,vr_order,merchant')
  334. ->field('refill_order.*,vr_order.order_state')
  335. ->join('inner,inner')
  336. ->on('refill_order.order_id=vr_order.order_id,refill_order.mchid=merchant.mchid')
  337. ->where($condition)
  338. ->order('refill_order.order_time desc')
  339. ->limit("{$start},{$len}")->select();
  340. $orders = array_merge($orders,$items);
  341. if (empty($items) || count($items) < $len) {
  342. break;
  343. }
  344. $i++;
  345. }
  346. return $orders;
  347. }
  348. }