handler.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace task;
  3. require_once(BASE_HELPER_PATH . '/stat_helper.php');
  4. use Exception;
  5. use mtopcard;
  6. use PHPExcel;
  7. use PHPExcel_IOFactory;
  8. use statistics\stat_refill;
  9. class handler
  10. {
  11. public function refill_order_stat($condition)
  12. {
  13. try
  14. {
  15. $items = Model('')->table('refill_order,vr_order')
  16. ->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')
  17. ->join('inner')
  18. ->on('refill_order.order_id=vr_order.order_id')
  19. ->where($condition)
  20. ->group('order_state')
  21. ->select();
  22. $all = [];
  23. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  24. $sending = $success = $cancel = $data;
  25. foreach ($items as $item)
  26. {
  27. if ($item['order_state'] == ORDER_STATE_SEND) {
  28. $sending = $item;
  29. } elseif ($item['order_state'] == ORDER_STATE_SUCCESS) {
  30. $success = $item;
  31. } elseif ($item['order_state'] == ORDER_STATE_CANCEL) {
  32. $cancel = $item;
  33. }
  34. $all['order_count'] += $item['order_count'];
  35. $all['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
  36. $all['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
  37. $all['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
  38. }
  39. $result = ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  40. return [true,$result];
  41. }
  42. catch (Exception $ex)
  43. {
  44. return [false,false];
  45. }
  46. }
  47. public function refill_order_stat_title($condition)
  48. {
  49. return md5("refill_order_stat-".serialize($condition));
  50. }
  51. public function refill_order_export($condition)
  52. {
  53. $orders = Model('refill_order')->getAllOrders($condition);
  54. if (empty($orders)) {
  55. return [false, '统计数据为空'];
  56. }
  57. $merchants = [];
  58. $column_values = ['商户号', '商户名称', '商户订单号','平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额(下游)', '上游名称','上游订单号', '折扣金额(上游)'];
  59. $data_keys = ['mchid', 'mch_name', 'mch_order', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'mch_amount', 'channel_name', 'ch_trade_no', 'channel_amount'];
  60. $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
  61. foreach ($merchant_list as $value) {
  62. $merchants[$value['mchid']] = $value;
  63. }
  64. $column_key = 'A';
  65. for($index=0;$index<count($column_values);$index++){
  66. $column_keys[] = $column_key;
  67. $column_key++;
  68. }
  69. $objPHPExcel = new PHPExcel();
  70. $objPHPExcel->setActiveSheetIndex(0);
  71. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  72. foreach ($column_keys as $key => $column_key) {
  73. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(15);
  74. $cell_value = $column_key . 1;
  75. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  76. }
  77. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  78. foreach ($orders as $k => $order) {
  79. if(!empty($merchants)) {
  80. $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
  81. }
  82. $order['card_type_text'] = $card_type_texts[$order['card_type']];
  83. $order['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  84. $order['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  85. $order['order_state_text'] = orderState($order);
  86. foreach ($column_keys as $key => $column_key) {
  87. $field = $column_key . ($k + 2);
  88. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
  89. }
  90. }
  91. try {
  92. $path = BASE_ROOT_PATH . "/data/upload/task/";
  93. if (!is_dir($path)) {
  94. mkdir($path, 0755);
  95. }
  96. $filename = date('YmdHis', time()) . "-订单导出.xlsx";
  97. $file_path = $path . $filename;
  98. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  99. $objWriter->save($file_path);
  100. return [true, $filename];
  101. } catch (Exception $e) {
  102. return [false, false];
  103. }
  104. }
  105. public function refill_order_export_title($condition)
  106. {
  107. return md5("refill_order_export-".serialize($condition));
  108. }
  109. public function order_stat_reload($condition)
  110. {
  111. $refill = new stat_refill();
  112. $type = $condition['type'];
  113. $time_stamp = $condition['time_stamp'];
  114. $cid = $condition['cid'];
  115. $order_time_type = $condition['order_time_type'];
  116. if ($type == 'merchant') {
  117. $refill->merchant_stat($time_stamp, $cid, $order_time_type);
  118. } elseif ($type == 'provider') {
  119. $refill->provider_stat($time_stamp, $cid, $order_time_type);
  120. }
  121. return [true, 'success'];
  122. }
  123. public function order_stat_reload_title($condition)
  124. {
  125. return md5("order_stat_reload-".serialize($condition));
  126. }
  127. }