merchant_order.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. use task\manager;
  3. require_once(BASE_ROOT_PATH . '/mobile/control/merchantweb.php');
  4. require_once(BASE_HELPER_PATH . '/task/task_helper.php');
  5. class merchant_orderControl extends mbMerchantControl
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function listOp()
  12. {
  13. $model_vr_order = Model('refill_order');
  14. $cond['mchid'] = $this->mchid();
  15. $cond['inner_status'] = 0;
  16. if (!empty($_GET['card_type'])) {
  17. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  18. $cond['refill_order.card_type'] = $_GET['card_type'];
  19. }
  20. if($_GET['card_type'] == 'oil') {
  21. $cond['refill_order.card_type'] = ['in' , ['1' , '2']];
  22. }
  23. if($_GET['card_type'] == 'phone') {
  24. $cond['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  25. }
  26. }
  27. if (!empty($_GET['card_no'])) {
  28. $cond['refill_order.card_no'] = $_GET['card_no'];
  29. }
  30. if (!empty($_GET['refill_amount'])) {
  31. $cond['refill_order.refill_amount'] = $_GET['refill_amount'];
  32. }
  33. if (!empty($_GET['mch_order'])) {
  34. $cond['refill_order.mch_order'] = $_GET['mch_order'];
  35. }
  36. if (!empty($_GET['order_sn'])) {
  37. $cond['refill_order.order_sn'] = $_GET['order_sn'];
  38. }
  39. if (!empty($_GET['quality'])) {
  40. $cond['refill_order.quality'] = $_GET['quality'];
  41. }
  42. $cur_time = time();
  43. $end_date = strtotime(date('Y-m-d',$cur_time));
  44. $add_end = $end_date + 86400 * 5;
  45. $start = intval($_GET['start_time']);
  46. $end = intval($_GET['end_time']);
  47. if($start <= 0) {
  48. $start = $cur_time - 86400 * 2;
  49. }
  50. if($end <= 0) {
  51. $end = $cur_time;
  52. }
  53. $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
  54. $cond['vr_order.add_time'] = [['egt', $start], ['elt', $add_end], 'and'];
  55. if (in_array($_GET['order_state'], ['0', '30', '40']))
  56. {
  57. $cond['vr_order.order_state'] = $_GET['order_state'];
  58. }
  59. if ($_GET['stat']) {
  60. $stat = $this->orderStatsHour($cond);
  61. return self::outsuccess($stat);
  62. }
  63. $fields = 'refill_order.*,vr_order.order_state';
  64. $total = $this->getTotal($start, $end);
  65. $order_list = $model_vr_order->getMerchantOrderList($cond, $this->page, $total, $fields, 'refill_order.order_time desc');
  66. $order_list = $this->merchant_order_format($order_list);
  67. $result['data'] = $order_list;
  68. $result['total'] = $model_vr_order->gettotalpage();
  69. return self::outsuccess($result);
  70. }
  71. private function getTotal($start,$end)
  72. {
  73. $model_detail = Model('refill_detail');
  74. $cond['order_time'] = [['egt', $start], ['lt', $end], 'and'];
  75. $cond['mchid'] = $this->mchid();
  76. $items= $model_detail->field('count(*) as ncount')->where($cond)->select();
  77. foreach($items as $item) {
  78. $count = intval($item['ncount']);
  79. return $count;
  80. }
  81. return 0;
  82. }
  83. private function orderStatsHour($cond)
  84. {
  85. $stats = Model('')->table('refill_order,vr_order')->join('inner')
  86. ->on('refill_order.order_id=vr_order.order_id')
  87. ->field("FROM_UNIXTIME(order_time,'%d-%H') as hour, count(*) as order_count, sum(refill_amount) as refill_amounts, sum(mch_amount) as mch_amounts, order_state")
  88. ->where($cond)->group('hour, order_state')->select();
  89. $cond['refill_order.channel_name'] = '';
  90. $excp_stats = Model('')->table('refill_order,vr_order')->join('inner')
  91. ->on('refill_order.order_id=vr_order.order_id')
  92. ->field("FROM_UNIXTIME(order_time,'%d-%H') as hour, count(*) as order_count")
  93. ->where($cond)->group('hour')->select();
  94. $data = [];
  95. $except = [];
  96. $result = [];
  97. $all = [
  98. 'success_count' => 0, 'success_amounts' => 0, 'cancel_count' => 0, 'except_count' => 0
  99. ];
  100. if(!empty($excp_stats)) {
  101. foreach ($excp_stats as $value) {
  102. $except[$value['hour']] = $value['order_count'];
  103. }
  104. }
  105. if(!empty($stats)) {
  106. foreach ($stats as $stat) {
  107. $hour = $stat['hour'];
  108. if ($stat['order_state'] == ORDER_STATE_SUCCESS) {
  109. $data[$hour]['success_count'] = $stat['order_count'];
  110. $data[$hour]['success_amounts'] = $stat['refill_amounts'];
  111. } elseif ($stat['order_state'] == ORDER_STATE_CANCEL) {
  112. $data[$hour]['cancel_count'] = $stat['order_count'];
  113. }
  114. if (array_key_exists($hour, $except)) {
  115. $data[$hour]['except_count'] = $except[$hour];
  116. }
  117. }
  118. foreach ($data as $hour => $val) {
  119. $success_count = $val['success_count'] ?? 0;
  120. $success_amounts = $val['success_amounts'] ?? 0;
  121. $cancel_count = $val['cancel_count'] ?? 0;
  122. $except_count = $val['except_count'] ?? 0;
  123. $result[] = [$hour, $success_count, $success_amounts, $cancel_count, $except_count];
  124. $all['success_count'] += $success_count;
  125. $all['success_amounts'] += ncPriceFormat($success_amounts);
  126. $all['cancel_count'] += ncPriceFormat($cancel_count);
  127. $all['except_count'] += ncPriceFormat($except_count);
  128. }
  129. $result[] = ['总计', $all['success_count'], $all['success_amounts'], $all['cancel_count'], $all['except_count']];
  130. }
  131. return $result;
  132. }
  133. public function OrderStatsOp()
  134. {
  135. if(empty($_GET['time_type']) || empty($_GET['start_time']) || empty($_GET['end_time'])) {
  136. return self::outerr(errcode::ErrInputParam, "参数错误.");
  137. }
  138. $cond['inner_status'] = 0;
  139. $time_type = $_GET['time_type'];
  140. $start = $_GET['start_time'];
  141. $end = $_GET['end_time'];
  142. $start_date = strtotime(date('Y-m-d',$start));
  143. $end_date = strtotime(date('Y-m-d',$end));
  144. if ($time_type == 'order_time') {
  145. $add_end = $end_date + 86400 * 5;
  146. $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
  147. $cond['vr_order.add_time'] = [['egt', $start], ['elt', $add_end], 'and'];
  148. } elseif ($time_type == 'notify_time') {
  149. $add_begin = $start_date - 86400 * 5;
  150. $cond['refill_order.order_time'] = [['egt', $add_begin], ['lt', $end], 'and'];
  151. $cond['vr_order.add_time'] = [['egt', $add_begin], ['elt', $end], 'and'];
  152. $cond['refill_order.notify_time'] = [['egt', $start], ['elt', $end], 'and'];
  153. } else {
  154. return self::outerr(errcode::ErrInputParam, "筛选日期类型错误.");
  155. }
  156. $cond['mchid'] = $this->mchid();
  157. $stats = Model('')->table('refill_order,vr_order')->join('inner')
  158. ->on('refill_order.order_id=vr_order.order_id')
  159. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(mch_amount) as mch_amounts, order_state')
  160. ->where($cond)->group('order_state')->select();
  161. $result['count'] = $result['sendCount'] = $result['errorCount'] = $result['successCount'] = $result['refill_amounts'] = $result['mch_amounts'] = 0;
  162. foreach ($stats as $stat) {
  163. $result['count'] += $stat['order_count'];
  164. if($stat['order_state'] == ORDER_STATE_SEND) {
  165. $result['sendCount'] = $stat['order_count'];
  166. }
  167. if($stat['order_state'] == ORDER_STATE_CANCEL) {
  168. $result['errorCount'] = $stat['order_count'];
  169. }
  170. if($stat['order_state'] == ORDER_STATE_SUCCESS) {
  171. $result['successCount'] = $stat['order_count'];
  172. $result['mch_amounts'] = $stat['mch_amounts'];
  173. $result['refill_amounts'] = $stat['refill_amounts'];
  174. }
  175. }
  176. return self::outsuccess($result);
  177. }
  178. private function merchant_order_format($orders)
  179. {
  180. $data = [];
  181. foreach ($orders as $order) {
  182. if($order['notify_time'] > 0)
  183. {
  184. $order['diff_time_text'] = $this->elapse_time($order['notify_time'] - $order['order_time']);
  185. $order['diff_time'] = $order['notify_time'] - $order['order_time'];
  186. }
  187. else
  188. {
  189. $order['diff_time_text'] = $this->elapse_time(time() - $order['order_time']);
  190. $order['diff_time'] = time() - $order['order_time'];
  191. }
  192. if (isset($order['order_time'])) {
  193. $order['order_time'] = date('Y-m-d H:i:s', $order['order_time']);
  194. }
  195. if (isset($order['notify_time'])) {
  196. $order['notify_time'] = date('Y-m-d H:i:s', $order['notify_time']);
  197. }
  198. if($order['is_retrying'] == 1) {
  199. $order['order_state'] = ORDER_STATE_SEND;
  200. }
  201. if ($order['order_state'] == ORDER_STATE_NEW || $order['order_state'] == ORDER_STATE_PAY) {
  202. $order['order_state'] = ORDER_STATE_SEND;
  203. }
  204. $order['order_state_text'] = $this->_orderState($order['order_state']);
  205. $order['card_type_name'] = $this->scard_type($order['card_type']);
  206. $data[] = $order;
  207. }
  208. return $data;
  209. }
  210. /**
  211. * 取得订单状态文字输出形式
  212. *
  213. * @param int $order_state 订单数组
  214. * @return string
  215. */
  216. private function _orderState($order_state)
  217. {
  218. switch ($order_state) {
  219. case ORDER_STATE_CANCEL:
  220. $text = '已取消';
  221. break;
  222. case ORDER_STATE_NEW:
  223. $text = '新订单';
  224. break;
  225. case ORDER_STATE_SEND:
  226. $text = '充值中';
  227. break;
  228. case ORDER_STATE_PAY:
  229. $text = '支付成功';
  230. break;
  231. case ORDER_STATE_SUCCESS:
  232. $text = '充值成功';
  233. break;
  234. case 'retrying':
  235. $text = '重试中';
  236. break;
  237. default:
  238. $text = '未知状态';
  239. }
  240. return $text;
  241. }
  242. private function scard_type(int $card_type)
  243. {
  244. if ($card_type == mtopcard\PetroChinaCard) { //中石油
  245. return '中石油';
  246. } elseif ($card_type == mtopcard\SinopecCard) { //中石化
  247. return '中石化';
  248. } elseif ($card_type == mtopcard\ChinaMobileCard) { //中国移动
  249. return '中国移动';
  250. } elseif ($card_type == mtopcard\ChinaUnicomCard) { //中国联通
  251. return '中国联通';
  252. } elseif ($card_type == mtopcard\ChinaTelecomCard) { //中国电信
  253. return '中国电信';
  254. } elseif ($card_type == mtopcard\ThirdRefillCard) { //中国电信
  255. return '增值业务';
  256. } else {
  257. return 'unknown';
  258. }
  259. }
  260. private function elapse_time($seconds)
  261. {
  262. $minutes = intval($seconds / 60);
  263. $second = intval($seconds % 60);
  264. if($minutes >= 60) {
  265. $minute = $minutes % 60;
  266. $hours = intval($minutes / 60);
  267. $result = "{$minute}:{$second}";
  268. }
  269. else {
  270. if($minutes > 0){
  271. $result = "{$minutes}:{$second}";
  272. }else{
  273. $result = "{$second}";
  274. }
  275. }
  276. if(isset($hours))
  277. {
  278. $result = "{$hours}:" . $result;
  279. }
  280. return $result;
  281. }
  282. public function OrderExportOp()
  283. {
  284. $cond['mchid'] = $this->mchid();
  285. $cond['inner_status'] = 0;
  286. $fSingle = false;
  287. if (!empty($_GET['card_type'])) {
  288. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  289. $cond['refill_order.card_type'] = $_GET['card_type'];
  290. }
  291. if($_GET['card_type'] == 'oil') {
  292. $cond['refill_order.card_type'] = ['in' , ['1' , '2']];
  293. }
  294. if($_GET['card_type'] == 'phone') {
  295. $cond['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  296. }
  297. }
  298. if (!empty($_GET['card_no'])) {
  299. $fSingle = true;
  300. $cond['refill_order.card_no'] = $_GET['card_no'];
  301. }
  302. if (!empty($_GET['refill_amount'])) {
  303. $cond['refill_order.refill_amount'] = $_GET['refill_amount'];
  304. }
  305. if (!empty($_GET['mch_order'])) {
  306. $fSingle = true;
  307. $cond['refill_order.mch_order'] = $_GET['mch_order'];
  308. }
  309. if (!empty($_GET['order_sn'])) {
  310. $fSingle = true;
  311. $cond['refill_order.order_sn'] = $_GET['order_sn'];
  312. }
  313. if (!empty($_GET['quality'])) {
  314. $cond['refill_order.quality'] = $_GET['quality'];
  315. }
  316. $cur_time = time();
  317. $end_date = strtotime(date('Y-m-d',$cur_time));
  318. $add_end = $end_date + 86400 * 5;
  319. if($fSingle)
  320. {
  321. $start = intval($_GET['start_time']);
  322. $end = intval($_GET['end_time']);
  323. if($start <= 0) {
  324. $start = $cur_time - 3600;
  325. }
  326. if($end <= 0) {
  327. $end = $cur_time;
  328. }
  329. }
  330. else{
  331. $start = $cur_time - 3600;
  332. $end = $cur_time;
  333. }
  334. $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
  335. $cond['vr_order.add_time'] = [['egt', $start], ['elt', $add_end], 'and'];
  336. if (in_array($_GET['order_state'], array('0', '30', '40'))) {
  337. $cond['vr_order.order_state'] = $_GET['order_state'];
  338. if($_GET['order_state'] == 30 && $_GET['time'] == 1){
  339. $cond['refill_order.order_time'] = ['lt', (time() - 3600)];
  340. }
  341. }
  342. $order_list = $this->getAllOrders($cond);
  343. $order_list = $this->merchant_order_format($order_list);
  344. $result = $this->export_order_exec($order_list);
  345. return self::outsuccess($result);
  346. }
  347. private function getAllOrders($condition): array
  348. {
  349. $len = 1000;
  350. $i = 0;
  351. $orders = [];
  352. while (true)
  353. {
  354. $start = $i * $len;
  355. $items = Model('')->table('refill_order,vr_order')
  356. ->field('refill_order.*,vr_order.order_state')
  357. ->join('inner')
  358. ->on('refill_order.order_id=vr_order.order_id')
  359. ->where($condition)
  360. ->order('refill_order.order_time desc')
  361. ->limit("{$start},{$len}")
  362. ->select();
  363. $orders = array_merge($orders,$items);
  364. if (empty($items) || count($items) < $len) {
  365. break;
  366. }
  367. $i++;
  368. }
  369. return $orders;
  370. }
  371. private function export_order_exec($order_list)
  372. {
  373. $card_type = ['1'=>'中石油' , '2' =>'中石化' , '4' => '移动' , '5' => '联通' , '6' => '电信'];
  374. $datas = [];
  375. if(defined('COMPANY_NAME') && COMPANY_NAME === 'LZKJ_COMPANY') {
  376. $title = [
  377. ['value' => '代理商账号'],
  378. ['value' => '商品名称'],
  379. ['value' => '交易账号'],
  380. ['value' => '交易金额'],
  381. ['value' => '交易面值'],
  382. ['value' => '交易日期'],
  383. ['value' => '交易状态'],
  384. ['value' => '处理时间'],
  385. ['value' => '第三方流水']
  386. ];
  387. foreach ($order_list as $order) {
  388. $data = [];
  389. $official_sn = $order['official_sn'];
  390. $notify_time = $order['$notify_time'];
  391. if(empty($order['official_sn'])) {
  392. $official_sn = '';
  393. }
  394. if(empty($order['$notify_time'])) {
  395. $notify_time = '';
  396. }
  397. $data[] = ['value' => $order['mchid']];
  398. $data[] = ['value' => "{$card_type[$order['card_type']]}{$order['refill_amount']}元"];
  399. $data[] = ['value' => $order['card_no']];
  400. $data[] = ['value' => $order['mch_amount']];
  401. $data[] = ['value' => $order['refill_amount']];
  402. $data[] = ['value' => $order['order_time']];
  403. $data[] = ['value' => $order['order_state_text']];
  404. $data[] = ['value' => $notify_time];
  405. $data[] = ['value' => $official_sn];
  406. $datas[] = $data;
  407. }
  408. }
  409. else
  410. {
  411. $title = [
  412. ['value' => '商户号'],
  413. ['value' => '客户订单号'],
  414. ['value' => '平台单号'],
  415. ['value' => '面额'],
  416. ['value' => '充值卡号'],
  417. ['value' => '充值卡类型'],
  418. ['value' => '下单日期'],
  419. ['value' => '完成日期'],
  420. ['value' => '官方流水号'],
  421. ['value' => '订单状态'],
  422. ['value' => '扣款金额']
  423. ];
  424. foreach ($order_list as $order) {
  425. $data = [];
  426. $data[] = ['value'=>$order['mchid']];
  427. $data[] = ['value'=>$order['mch_order']];
  428. $data[] = ['value'=>$order['order_sn']];
  429. $data[] = ['value'=>$order['refill_amount']];
  430. $data[] = ['value'=>$order['card_no']];
  431. $data[] = ['value'=>$order['card_type_name']];
  432. $data[] = ['value'=>$order['order_time']];
  433. $data[] = ['value'=>$order['notify_time']];
  434. $data[] = ['value'=>$order['official_sn']];
  435. $data[] = ['value'=>$order['order_state_text']];
  436. $data[] = ['value'=>$order['mch_amount']];
  437. $datas[] = $data;
  438. }
  439. }
  440. return ['title' => $title , 'data' => $datas];
  441. }
  442. private function checkSend($start, $end)
  443. {
  444. $order_state_send = ORDER_STATE_SEND;
  445. $order_state_queue = ORDER_STATE_QUEUE;
  446. $cond['order_state'] = ['in',"{$order_state_send},{$order_state_queue}"];
  447. $cond['order_time'] = [['egt', $start], ['lt', $end], 'and'];
  448. $cond['mchid'] = $this->mchid();
  449. return Model('refill_detail')->where($cond)->select();
  450. }
  451. private function is_timestamp($timestamp) {
  452. $timestamp = intval($timestamp);
  453. if(strtotime(date('Y-m-d H:i:s',$timestamp)) === $timestamp) {
  454. return $timestamp;
  455. } else return false;
  456. }
  457. public function create_taskOp()
  458. {
  459. if(empty($_GET['time_type']) || empty($_GET['start_time']) || empty($_GET['end_time'])) {
  460. return self::outerr(errcode::ErrInputParam, "参数错误.");
  461. }
  462. if(!$this->is_timestamp($_GET['start_time']) || !$this->is_timestamp($_GET['end_time'])) {
  463. return self::outerr(errcode::ErrInputParam, "时间戳类型错误.");
  464. }
  465. $task_title_create = function ($start, $end, $time_type){
  466. $start = date('Y-m-d H:i:s', $start);
  467. $end = date('Y-m-d H:i:s', $end);
  468. if ($time_type === 'order_time') {
  469. $task_title = "订单导出: 下单时间 {$start} - {$end}";
  470. } else {
  471. $task_title = "订单导出: 回调时间 {$start} - {$end}";
  472. }
  473. return $task_title;
  474. };
  475. $time_type = $_GET['time_type'];
  476. $start = $_GET['start_time'];
  477. $end = $_GET['end_time'];
  478. if (!empty($this->checkSend($start, $end))) {
  479. return self::outerr(errcode::ErrInputParam, "还有正在充值中的订单,请稍后再试.");
  480. }
  481. $start_date = strtotime(date('Y-m-d',$start));
  482. $end_date = strtotime(date('Y-m-d',$end));
  483. $today = strtotime(date('Y-m-d',time()));
  484. $tomorrow = $today + 86400;
  485. if ($time_type === 'order_time')
  486. {
  487. $add_end = $end_date + 86400 * 5;
  488. if($add_end > $tomorrow) {
  489. $add_end = $tomorrow;
  490. }
  491. $scope = ['order_time' => [$start, $end], 'add_time' => [$start, $add_end]];
  492. } elseif ($time_type === 'notify_time') {
  493. $add_begin = $start_date - 86400 * 5;
  494. $scope = ['order_time' => [$add_begin, $end], 'add_time' => [$add_begin, $end], 'notify_time' => [$start, $end]];
  495. } else {
  496. return self::outerr(errcode::ErrInputParam, "筛选日期类型错误.");
  497. }
  498. $normal_cond['inner_status'] = 0;
  499. $normal_cond['order_state'] = ORDER_STATE_SUCCESS;
  500. $normal_cond['mchid'] = $this->mchid();
  501. $task_title = $task_title_create($start, $end, $time_type);
  502. $cond = ['normal' => $normal_cond, 'time_scope' => $scope, 'export_type' => 'merchant'];
  503. $manager = new manager();
  504. $task = $manager->add_task('refill_order_export',$cond,1,-1, $task_title, $this->mchid());
  505. $file_path = '';
  506. if ($task->completed() && $task->success()) {
  507. $file_name = $task->result();
  508. $file_path = UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . $file_name;
  509. }
  510. return self::outsuccess(['file_path' => $file_path]);
  511. }
  512. public function task_listOp()
  513. {
  514. $model = Model('task');
  515. $condition['is_show'] = 1;
  516. $condition['mchid'] = $this->mchid();
  517. $task_list = $model->getList($condition, $this->page, '*', 'add_time desc', 20);
  518. foreach ($task_list as $key => $value)
  519. {
  520. if($value['state'] == 3 && !empty($value['result']))
  521. {
  522. $task_list[$key]['file_path'] = UPLOAD_SITE_URL . '/' . ATTACH_TASK . DS . unserialize($value['result']);
  523. }else{
  524. $task_list[$key]['file_path'] = '';
  525. }
  526. }
  527. $result['data'] = $task_list;
  528. $result['total'] = $model->gettotalpage();
  529. return self::outsuccess($result);
  530. }
  531. }