merchant_order.php 14 KB

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