merchant_order.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. require_once(BASE_ROOT_PATH . '/mobile/control/merchantweb.php');
  3. class merchant_orderControl extends mbMerchantControl
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9. public function listOp()
  10. {
  11. $model_vr_order = Model('refill_order');
  12. $cond['mchid'] = $this->mchid();
  13. $cond['inner_status'] = 0;
  14. $fSingle = false;
  15. if (!empty($_GET['card_type'])) {
  16. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  17. $cond['refill_order.card_type'] = $_GET['card_type'];
  18. }
  19. if($_GET['card_type'] == 'oil') {
  20. $cond['refill_order.card_type'] = ['in' , ['1' , '2']];
  21. }
  22. if($_GET['card_type'] == 'phone') {
  23. $cond['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  24. }
  25. }
  26. if (!empty($_GET['card_no'])) {
  27. $fSingle = true;
  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. $fSingle = true;
  35. $cond['refill_order.mch_order'] = $_GET['mch_order'];
  36. }
  37. if (!empty($_GET['order_sn'])) {
  38. $fSingle = true;
  39. $cond['refill_order.order_sn'] = $_GET['order_sn'];
  40. }
  41. if (!empty($_GET['quality'])) {
  42. $cond['refill_order.quality'] = $_GET['quality'];
  43. }
  44. $start = time() - 3600;
  45. $cond['refill_order.order_time'] = ['egt', $start];
  46. if($fSingle) {
  47. $start_unixtime = intval($_GET['start_time']);
  48. $end_unixtime = intval($_GET['end_time']);
  49. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  50. $cond['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  51. } elseif ($start_unixtime > 0) {
  52. $cond['refill_order.order_time'] = ['egt', $start_unixtime];
  53. } elseif ($end_unixtime > 0) {
  54. $cond['refill_order.order_time'] = ['lt', $end_unixtime];
  55. } else {
  56. $start = time() - 3600;
  57. $cond['refill_order.order_time'] = ['egt', $start];
  58. }
  59. }
  60. if (in_array($_GET['order_state'], array('0', '30', '40'))) {
  61. $cond['vr_order.order_state'] = $_GET['order_state'];
  62. if($_GET['order_state'] == 30 && $_GET['time'] == 1){
  63. $cond['refill_order.order_time'] = ['lt', (time() - 3600)];
  64. }
  65. }
  66. $fields = 'refill_order.*,vr_order.order_state';
  67. $order_list = $model_vr_order->getMerchantOrderList($cond, $this->page, 0, $fields, 'refill_order.order_time desc');
  68. $order_list = $this->merchant_order_format($order_list);
  69. $result['data'] = $order_list;
  70. $result['total'] = $model_vr_order->gettotalpage();
  71. return self::outsuccess($result);
  72. }
  73. public function OrderStatsOp()
  74. {
  75. if(empty($_GET['time_type']) || empty($_GET['start_time']) || empty($_GET['end_time'])) {
  76. return self::outerr(errcode::ErrInputParam, "参数错误.");
  77. }
  78. $cond['inner_status'] = 0;
  79. $time_type = $_GET['time_type'];
  80. if ($time_type == 'order_time') {
  81. $cond['order_time'] = ['time', [$_GET['start_time'], $_GET['end_time']]];
  82. }
  83. if ($time_type == 'notify_time') {
  84. $cond['notify_time'] = ['time', [$_GET['start_time'], $_GET['end_time']]];
  85. }
  86. if(empty($cond)) {
  87. return self::outerr(errcode::ErrInputParam, "筛选日期类型错误.");
  88. }
  89. $cond['mchid'] = $this->mchid();
  90. $stats = Model('')->table('refill_order,vr_order')->join('inner')
  91. ->on('refill_order.order_id=vr_order.order_id')
  92. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(mch_amount) as mch_amounts, order_state')
  93. ->where($cond)->group('order_state')->select();
  94. $result['count'] = $result['sendCount'] = $result['errorCount'] = $result['successCount'] = $result['refill_amounts'] = $result['mch_amounts'] = 0;
  95. foreach ($stats as $stat) {
  96. $result['count'] += $stat['order_count'];
  97. if($stat['order_state'] == ORDER_STATE_SEND) {
  98. $result['sendCount'] = $stat['order_count'];
  99. }
  100. if($stat['order_state'] == ORDER_STATE_CANCEL) {
  101. $result['errorCount'] = $stat['order_count'];
  102. }
  103. if($stat['order_state'] == ORDER_STATE_SUCCESS) {
  104. $result['successCount'] = $stat['order_count'];
  105. $result['mch_amounts'] = $stat['mch_amounts'];
  106. $result['refill_amounts'] = $stat['refill_amounts'];
  107. }
  108. }
  109. return self::outsuccess($result);
  110. }
  111. private function merchant_order_format($orders)
  112. {
  113. $data = [];
  114. foreach ($orders as $order) {
  115. if($order['notify_time'] > 0)
  116. {
  117. $order['diff_time_text'] = $this->elapse_time($order['notify_time'] - $order['order_time']);
  118. $order['diff_time'] = $order['notify_time'] - $order['order_time'];
  119. }
  120. else
  121. {
  122. $order['diff_time_text'] = $this->elapse_time(time() - $order['order_time']);
  123. $order['diff_time'] = time() - $order['order_time'];
  124. }
  125. if (isset($order['order_time'])) {
  126. $order['order_time'] = date('Y-m-d H:i:s', $order['order_time']);
  127. }
  128. if (isset($order['notify_time'])) {
  129. $order['notify_time'] = date('Y-m-d H:i:s', $order['notify_time']);
  130. }
  131. if($order['is_retrying'] == 1) {
  132. $order['order_state'] = ORDER_STATE_SEND;
  133. }
  134. if ($order['order_state'] == ORDER_STATE_NEW || $order['order_state'] == ORDER_STATE_PAY) {
  135. $order['order_state'] = ORDER_STATE_SEND;
  136. }
  137. $order['order_state_text'] = $this->_orderState($order['order_state']);
  138. $order['card_type_name'] = $this->scard_type($order['card_type']);
  139. $data[] = $order;
  140. }
  141. return $data;
  142. }
  143. /**
  144. * 取得订单状态文字输出形式
  145. *
  146. * @param int $order_state 订单数组
  147. * @return string
  148. */
  149. private function _orderState($order_state)
  150. {
  151. switch ($order_state) {
  152. case ORDER_STATE_CANCEL:
  153. $text = '已取消';
  154. break;
  155. case ORDER_STATE_NEW:
  156. $text = '新订单';
  157. break;
  158. case ORDER_STATE_SEND:
  159. $text = '充值中';
  160. break;
  161. case ORDER_STATE_PAY:
  162. $text = '支付成功';
  163. break;
  164. case ORDER_STATE_SUCCESS:
  165. $text = '充值成功';
  166. break;
  167. case 'retrying':
  168. $text = '重试中';
  169. break;
  170. default:
  171. $text = '未知状态';
  172. }
  173. return $text;
  174. }
  175. private function scard_type(int $card_type)
  176. {
  177. if ($card_type == mtopcard\PetroChinaCard) { //中石油
  178. return '中石油';
  179. } elseif ($card_type == mtopcard\SinopecCard) { //中石化
  180. return '中石化';
  181. } elseif ($card_type == mtopcard\ChinaMobileCard) { //中国移动
  182. return '中国移动';
  183. } elseif ($card_type == mtopcard\ChinaUnicomCard) { //中国联通
  184. return '中国联通';
  185. } elseif ($card_type == mtopcard\ChinaTelecomCard) { //中国电信
  186. return '中国电信';
  187. } elseif ($card_type == mtopcard\ThirdRefillCard) { //中国电信
  188. return '增值业务';
  189. } else {
  190. return 'unknown';
  191. }
  192. }
  193. private function elapse_time($seconds)
  194. {
  195. $minutes = intval($seconds / 60);
  196. $second = intval($seconds % 60);
  197. if($minutes >= 60) {
  198. $minute = $minutes % 60;
  199. $hours = intval($minutes / 60);
  200. $result = "{$minute}:{$second}";
  201. }
  202. else {
  203. if($minutes > 0){
  204. $result = "{$minutes}:{$second}";
  205. }else{
  206. $result = "{$second}";
  207. }
  208. }
  209. if(isset($hours))
  210. {
  211. $result = "{$hours}:" . $result;
  212. }
  213. return $result;
  214. }
  215. public function OrderExportOp()
  216. {
  217. $model_vr_order = Model('refill_order');
  218. $cond['mchid'] = $this->mchid();
  219. $cond['inner_status'] = 0;
  220. $fSingle = false;
  221. if (!empty($_GET['card_type'])) {
  222. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  223. $cond['refill_order.card_type'] = $_GET['card_type'];
  224. }
  225. if($_GET['card_type'] == 'oil') {
  226. $cond['refill_order.card_type'] = ['in' , ['1' , '2']];
  227. }
  228. if($_GET['card_type'] == 'phone') {
  229. $cond['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  230. }
  231. }
  232. if (!empty($_GET['card_no'])) {
  233. $fSingle = true;
  234. $cond['refill_order.card_no'] = $_GET['card_no'];
  235. }
  236. if (!empty($_GET['refill_amount'])) {
  237. $cond['refill_order.refill_amount'] = $_GET['refill_amount'];
  238. }
  239. if (!empty($_GET['mch_order'])) {
  240. $fSingle = true;
  241. $cond['refill_order.mch_order'] = $_GET['mch_order'];
  242. }
  243. if (!empty($_GET['order_sn'])) {
  244. $fSingle = true;
  245. $cond['refill_order.order_sn'] = $_GET['order_sn'];
  246. }
  247. if (!empty($_GET['quality'])) {
  248. $cond['refill_order.quality'] = $_GET['quality'];
  249. }
  250. $start = time() - 3600;
  251. $cond['refill_order.order_time'] = ['egt', $start];
  252. if ($fSingle) {
  253. $start_unixtime = intval($_GET['start_time']);
  254. $end_unixtime = intval($_GET['end_time']);
  255. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  256. $cond['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  257. } elseif ($start_unixtime > 0) {
  258. $cond['refill_order.order_time'] = ['egt', $start_unixtime];
  259. } elseif ($end_unixtime > 0) {
  260. $cond['refill_order.order_time'] = ['lt', $end_unixtime];
  261. } else {
  262. $start = strtotime(date('Y-m-d', time()));
  263. $cond['refill_order.order_time'] = ['egt', $start];
  264. }
  265. }
  266. if (in_array($_GET['order_state'], array('0', '30', '40'))) {
  267. $cond['vr_order.order_state'] = $_GET['order_state'];
  268. if($_GET['order_state'] == 30 && $_GET['time'] == 1){
  269. $cond['refill_order.order_time'] = ['lt', (time() - 3600)];
  270. }
  271. }
  272. $order_list = $this->getAllOrders($cond);
  273. $order_list = $this->merchant_order_format($order_list);
  274. $result = $this->export_order_exec($order_list);
  275. return self::outsuccess($result);
  276. }
  277. private function getAllOrders($condition): array
  278. {
  279. $len = 1000;
  280. $i = 0;
  281. $orders = [];
  282. while (true)
  283. {
  284. $start = $i * $len;
  285. $items = Model('')->table('refill_order,vr_order')
  286. ->field('refill_order.*,vr_order.order_state')
  287. ->join('inner')
  288. ->on('refill_order.order_id=vr_order.order_id')
  289. ->where($condition)
  290. ->order('refill_order.order_time desc')
  291. ->limit("{$start},{$len}")
  292. ->select();
  293. $orders = array_merge($orders,$items);
  294. if (empty($items) || count($items) < $len) {
  295. break;
  296. }
  297. $i++;
  298. }
  299. return $orders;
  300. }
  301. private function export_order_exec($order_list)
  302. {
  303. $card_type = ['1'=>'中石油' , '2' =>'中石化' , '4' => '移动' , '5' => '联通' , '6' => '电信'];
  304. $datas = [];
  305. if(defined('COMPANY_NAME') && COMPANY_NAME === 'LZKJ_COMPANY') {
  306. $title = [
  307. ['value' => '代理商账号'],
  308. ['value' => '商品名称'],
  309. ['value' => '交易账号'],
  310. ['value' => '交易金额'],
  311. ['value' => '交易面值'],
  312. ['value' => '交易日期'],
  313. ['value' => '交易状态'],
  314. ['value' => '处理时间'],
  315. ['value' => '第三方流水']
  316. ];
  317. foreach ($order_list as $order) {
  318. $data = [];
  319. $official_sn = $order['official_sn'];
  320. $notify_time = $order['$notify_time'];
  321. if(empty($order['official_sn'])) {
  322. $official_sn = '';
  323. }
  324. if(empty($order['$notify_time'])) {
  325. $notify_time = '';
  326. }
  327. $data[] = ['value' => $order['mchid']];
  328. $data[] = ['value' => "{$card_type[$order['card_type']]}{$order['refill_amount']}元"];
  329. $data[] = ['value' => $order['card_no']];
  330. $data[] = ['value' => $order['mch_amount']];
  331. $data[] = ['value' => $order['refill_amount']];
  332. $data[] = ['value' => $order['order_time']];
  333. $data[] = ['value' => $order['order_state_text']];
  334. $data[] = ['value' => $notify_time];
  335. $data[] = ['value' => $official_sn];
  336. $datas[] = $data;
  337. }
  338. }
  339. else
  340. {
  341. $title = [
  342. ['value' => '商户号'],
  343. ['value' => '客户订单号'],
  344. ['value' => '平台单号'],
  345. ['value' => '面额'],
  346. ['value' => '充值卡号'],
  347. ['value' => '充值卡类型'],
  348. ['value' => '下单日期'],
  349. ['value' => '完成日期'],
  350. ['value' => '官方流水号'],
  351. ['value' => '订单状态'],
  352. ['value' => '扣款金额']
  353. ];
  354. foreach ($order_list as $order) {
  355. $data = [];
  356. $data[] = ['value'=>$order['mchid']];
  357. $data[] = ['value'=>$order['mch_order']];
  358. $data[] = ['value'=>$order['order_sn']];
  359. $data[] = ['value'=>$order['refill_amount']];
  360. $data[] = ['value'=>$order['card_no']];
  361. $data[] = ['value'=>$order['card_type_name']];
  362. $data[] = ['value'=>$order['order_time']];
  363. $data[] = ['value'=>$order['notify_time']];
  364. $data[] = ['value'=>$order['official_sn']];
  365. $data[] = ['value'=>$order['order_state_text']];
  366. $data[] = ['value'=>$order['mch_amount']];
  367. $datas[] = $data;
  368. }
  369. }
  370. return ['title' => $title , 'data' => $datas];
  371. }
  372. }