handler.php 5.1 KB

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