merchant_order.php 19 KB

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