refill_order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/refill/util.php');
  3. use refill\util;
  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. $this->refill_order($fShowStat == 1);
  14. }
  15. public function refill_order($fShowStat = false)
  16. {
  17. $model_refill_order = Model('refill_order');
  18. $condition['inner_status'] = 0;
  19. $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d 00:00:00");
  20. if (!empty($_GET['order_sn'])) {
  21. $condition['refill_order.order_sn'] = $_GET['order_sn'];
  22. }
  23. if (!empty($_GET['mch_order'])) {
  24. $condition['refill_order.mch_order'] = $_GET['mch_order'];
  25. }
  26. if (!empty($_GET['ch_trade_no'])) {
  27. $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
  28. }
  29. if (!empty($_GET['card_no'])) {
  30. $condition['refill_order.card_no'] = $_GET['card_no'];
  31. }
  32. if (!empty($_GET['no_mchid'])) {
  33. $no_mchid = explode(',', $_GET['no_mchid']);
  34. $condition['refill_order.mchid'] = ['not in', $no_mchid];
  35. }
  36. if (!empty($_GET['mchid'])) {
  37. $condition['refill_order.mchid'] = $_GET['mchid'];
  38. }
  39. if (!empty($_GET['channel_name'])) {
  40. $condition['refill_order.channel_name'] = $_GET['channel_name'];
  41. }
  42. if (!empty($_GET['store_id'])) {
  43. $condition['vr_order.store_id'] = $_GET['store_id'];
  44. }
  45. if (!empty($_GET['refill_amount'])) {
  46. $condition['refill_order.refill_amount'] = $_GET['refill_amount'];
  47. }
  48. if (!empty($_GET['quality'])) {
  49. $condition['refill_order.quality'] = $_GET['quality'];
  50. }
  51. if(!empty($_GET['official_status'])) {
  52. if($_GET['official_status'] == 1) {
  53. $condition['official_sn'] = '';
  54. } elseif ($_GET['official_status'] == 2) {
  55. $condition['official_sn'] = ['neq', ''];
  56. }
  57. }
  58. if (!empty($_GET['card_type'])) {
  59. if (in_array($_GET['card_type'], ['1', '2', '4', '5', '6', '7'])) {
  60. $condition['refill_order.card_type'] = intval($_GET['card_type']);
  61. }
  62. if ($_GET['card_type'] == 'oil') {
  63. $condition['refill_order.card_type'] = ['in', [1, 2]];
  64. }
  65. if ($_GET['card_type'] == 'phone') {
  66. $condition['refill_order.card_type'] = ['in', [4, 5, 6]];
  67. }
  68. }
  69. $fToday = false;
  70. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  71. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  72. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  73. $condition['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  74. } elseif ($start_unixtime > 0) {
  75. $condition['refill_order.order_time'] = ['egt', $start_unixtime];
  76. } elseif ($end_unixtime > 0) {
  77. $condition['refill_order.order_time'] = ['lt', $end_unixtime];
  78. } else {
  79. $start = strtotime(date('Y-m-d', time()));
  80. $condition['refill_order.order_time'] = ['egt', $start];
  81. $fToday = true;
  82. }
  83. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40'])) {
  84. $condition['vr_order.order_state'] = $_GET['order_state'];
  85. if ($_GET['order_state'] == ORDER_STATE_SEND) {
  86. if ($_GET['time'] == 1) {
  87. $condition['refill_order.order_time'] = ['between', [(time() - 3600), (time() - 1800)]];
  88. }
  89. if ($_GET['time'] == 2) {
  90. $condition['refill_order.order_time'] = ['lt', (time() - 3600)];
  91. }
  92. }
  93. }
  94. if (!empty($_GET['export']) || !empty($_GET['export_stats'])) {
  95. $this->RefillOrderExport($condition);
  96. }
  97. $merchants = [];
  98. $merchant_list = Model('')->table('merchant')->limit(1000)->order('name asc')->select();
  99. foreach ($merchant_list as $value) {
  100. $merchants[$value['mchid']] = $value;
  101. }
  102. $order_list = $model_refill_order->getMerchantOrderList($condition, 50, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  103. foreach ($order_list as $order_id => $order_info) {
  104. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  105. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  106. if ($order_info['notify_time'] > 0) {
  107. $diff_time = $order_info['notify_time'] - $order_info['order_time'];
  108. } else {
  109. $diff_time = time() - $order_info['order_time'];
  110. }
  111. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  112. $order_list[$order_id]['diff_time'] = $diff_time;
  113. $order_list[$order_id]['quality_text'] = $this->quality_format($order_info['quality'],$order_info['card_type']);
  114. $order_list[$order_id]['org_quality_text'] = $this->quality_format($order_info['org_quality'],$order_info['card_type']);
  115. }
  116. $provider_list = Model('')->table('refill_provider,store')
  117. ->field('refill_provider.*,store.store_name')
  118. ->join('inner')
  119. ->on('store.store_id=refill_provider.store_id')
  120. ->order('opened asc, name asc')
  121. ->limit(1000)
  122. ->select();
  123. if ($fShowStat) {
  124. $stat = $this->all_order_state_stat($condition);
  125. Tpl::output('stat', $stat);
  126. }
  127. $dispatcher_queue_length = refill\util::dispatcher_queue_length();
  128. Tpl::output('dispatcher_queue_length', $dispatcher_queue_length);
  129. Tpl::output('admin_info', $this->getAdminInfo());
  130. Tpl::output('ftoday', $fToday);
  131. Tpl::output('order_list', $order_list);
  132. Tpl::output('provider_list', $provider_list);
  133. Tpl::output('merchant_list', $merchant_list);
  134. Tpl::output('show_page', $model_refill_order->showpage());
  135. Tpl::showpage('refill.order.index');
  136. }
  137. private function RefillOrderExport($condition)
  138. {
  139. if(!empty($_GET['export_stats'])) {
  140. if(!empty($condition['refill_order.order_time'])) {
  141. $condition['refill_order.notify_time'] = $condition['refill_order.order_time'];
  142. unset($condition['refill_order.order_time']);
  143. }
  144. }
  145. $i = 0;
  146. $result = [];
  147. while (true) {
  148. $start = $i * 1000;
  149. $order_list = Model('')->table('refill_order,vr_order')->field('refill_order.*,vr_order.order_state')
  150. ->where($condition)->join('inner')->on('refill_order.order_id=vr_order.order_id')->order('refill_order.order_time desc')->limit("{$start},1000")->select();
  151. if (empty($order_list)) {
  152. break;
  153. }
  154. $i++;
  155. foreach ($order_list as $order) {
  156. $result[] = $order;
  157. }
  158. }
  159. $this->createExcel($result);
  160. }
  161. private function createExcel($data = array())
  162. {
  163. Language::read('export');
  164. import('libraries.excel');
  165. $excel_obj = new Excel();
  166. $excel_data = array();
  167. //设置样式
  168. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  169. //header
  170. $excel_data[0][] = array('styleid' => 's_title', 'data' => '商户号');
  171. $excel_data[0][] = array('styleid' => 's_title', 'data' => '客户订单号');
  172. $excel_data[0][] = array('styleid' => 's_title', 'data' => '平台单号');
  173. $excel_data[0][] = array('styleid' => 's_title', 'data' => '面额');
  174. $excel_data[0][] = array('styleid' => 's_title', 'data' => '充值卡号');
  175. $excel_data[0][] = array('styleid' => 's_title', 'data' => '充值卡类型');
  176. $excel_data[0][] = array('styleid' => 's_title', 'data' => '下单日期');
  177. $excel_data[0][] = array('styleid' => 's_title', 'data' => '完成日期');
  178. $excel_data[0][] = array('styleid' => 's_title', 'data' => '官方流水号');
  179. $excel_data[0][] = array('styleid' => 's_title', 'data' => '订单状态');
  180. $excel_data[0][] = array('styleid' => 's_title', 'data' => '扣款金额');
  181. //data
  182. foreach ((array)$data as $k => $v) {
  183. $tmp = array();
  184. $tmp[] = array('data' => $v['mchid']);
  185. $tmp[] = array('data' => $v['mch_order']);
  186. $tmp[] = array('data' => $v['order_sn']);
  187. $tmp[] = array('data' => $v['refill_amount']);
  188. $tmp[] = array('data' => $v['card_no']);
  189. $tmp[] = array('data' => $this->scard_type($v['card_type']));
  190. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['order_time']));
  191. if (empty($v['notify_time'])) {
  192. $tmp[] = array('data' => '');
  193. } else {
  194. $tmp[] = array('data' => date('Y-m-d H:i:s', $v['notify_time']));
  195. }
  196. $tmp[] = array('data' => $v['official_sn']);
  197. $tmp[] = array('data' => orderState($v));
  198. $tmp[] = array('data' => $v['mch_amount']);
  199. $excel_data[] = $tmp;
  200. }
  201. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  202. $excel_obj->addArray($excel_data);
  203. $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
  204. $excel_obj->generateXML($excel_obj->charset(L('exp_od_order'), CHARSET) . date('Y-m-d-H', time()));
  205. exit;
  206. }
  207. private function all_order_state_stat($condition)
  208. {
  209. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  210. ->on('refill_order.order_id=vr_order.order_id')
  211. ->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')
  212. ->where($condition)
  213. ->group('order_state')
  214. ->select();
  215. $all = [];
  216. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  217. $sending = $success = $cancel = $data;
  218. foreach ($counts as $count) {
  219. if ($count['order_state'] == ORDER_STATE_SEND) {
  220. $sending = $count;
  221. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  222. $success = $count;
  223. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  224. $cancel = $count;
  225. }
  226. $all['order_count'] += $count['order_count'];
  227. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  228. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  229. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  230. }
  231. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  232. }
  233. public function merchant_dataOp()
  234. {
  235. $merchant_list = Model('')->table('merchant')->limit(1000)->select();
  236. $result = [];
  237. foreach ($merchant_list as $key => $value) {
  238. $data['name'] = $value['company_name'] ?? $value['name'];
  239. $data['value'] = $value['mchid'];
  240. $result[] = $data;
  241. }
  242. echo json_encode($result);
  243. exit;
  244. }
  245. public function provider_dataOp()
  246. {
  247. $provider_list = Model('')->table('refill_provider,store')
  248. ->field('refill_provider.*,store.store_name')
  249. ->join('inner')
  250. ->on('store.store_id=refill_provider.store_id')
  251. ->order('opened asc, name asc')
  252. ->limit(1000)
  253. ->select();
  254. foreach ($provider_list as $value) {
  255. $data['name'] = $value['store_name'] ?? $value['name'];
  256. $data['value'] = $value['store_id'];
  257. $result[] = $data;
  258. }
  259. echo json_encode($result);
  260. exit;
  261. }
  262. public function refill_third_infoOp()
  263. {
  264. $order_id = $_GET['order_id'];
  265. $model_refill_order = Model('refill_order');
  266. $order_info = $model_refill_order->getOrderInfo(['order_id' => $order_id]);
  267. if (empty($order_info) || $order_info['card_type'] != mtopcard\ThirdRefillCard) {
  268. $res = ['state' => -1, 'msg' => '订单不存在或类型错误'];
  269. exit(json_encode($res));
  270. }
  271. $third_refill = Model('thrid_refill');
  272. $data = $third_refill->getThird($order_id);
  273. $data['card_info'] = $data['card_info'] ?? '';
  274. $account_type_text = ['手机号','QQ号','微信号'];
  275. $data['account_type_text'] = $account_type_text[$data['account_type']-1];
  276. $ret_state_text = ['初始化','已解析','解析失败'];
  277. $data['ret_state_text'] = $ret_state_text[$data['ret_state']];
  278. $res = ['state' => 1, 'data' => $data];
  279. exit(json_encode($res));
  280. }
  281. public function showOrdersOp()
  282. {
  283. $order_sn = $_GET['order_sn'] ?? '';
  284. $model_refill_order = Model('refill_order');
  285. $order_info = $model_refill_order->getOrderInfo(['order_sn' => $order_sn]);
  286. if (empty($order_info)) {
  287. $res = ['state' => -1, 'msg' => '订单不存在'];
  288. exit(json_encode($res));
  289. }
  290. $mch_order = $order_info['mch_order'];
  291. $order_time = $order_info['order_time'];
  292. $mchid = $order_info['mchid'];
  293. $condition['refill_order.mch_order'] = $mch_order;
  294. $condition['refill_order.order_time'] = ['egt', $order_time];
  295. $condition['refill_order.mchid'] = $mchid;
  296. $field = 'refill_order.order_sn,refill_order.commit_time,refill_order.notify_time,refill_order.channel_name,vr_order.order_state';
  297. $order_list = $model_refill_order->getMerchantOrderList($condition, 1000, $field);
  298. foreach ($order_list as $order_id => $order_info) {
  299. $order_list[$order_id]['order_state_text'] = orderState($order_info);
  300. $order_list[$order_id]['notify_time_text'] = '/';
  301. $order_list[$order_id]['commit_time_text'] = date("Y-m-d H:i:s", $order_info['commit_time']);
  302. if ($order_info['notify_time'] > 0) {
  303. $diff_time = $order_info['notify_time'] - $order_info['commit_time'];
  304. $order_list[$order_id]['notify_time_text'] = date("Y-m-d H:i:s", $order_info['notify_time']);
  305. } else {
  306. if($order_info['commit_time'] == 0) {
  307. $diff_time = 0;
  308. $order_list[$order_id]['order_state_text'] = '提交失败';
  309. $order_list[$order_id]['commit_time_text'] = '/';
  310. } else {
  311. $diff_time = time() - $order_info['commit_time'];
  312. if($order_info['order_state'] != ORDER_STATE_SEND) {
  313. $order_list[$order_id]['order_state_text'] = '提交失败';
  314. }
  315. }
  316. }
  317. if($diff_time != 0) {
  318. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  319. } else {
  320. $order_list[$order_id]['diff_time_text'] = '/';
  321. }
  322. $order_list[$order_id]['diff_time'] = $diff_time;
  323. }
  324. $res = ['state' => 1, 'data' => $order_list, 'count' => count($order_list)];
  325. exit(json_encode($res));
  326. }
  327. }