SyslogManagementService.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Services\Backend\DataManagement;
  3. use App\Http\Services\ConstDef\GeneralConst;
  4. use App\Models\Web\Syslog;
  5. use PhpOffice\PhpSpreadsheet\IOFactory;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use Illuminate\Support\Facades\DB;
  8. class SyslogManagementService
  9. {
  10. // 相關私有 model 調用器宣告
  11. private $syslogManagementDb;
  12. public function __construct()
  13. {
  14. // 建構 model 調用器
  15. $this->syslogManagementDb = new Syslog();
  16. ini_set("memory_limit","8192M");
  17. }
  18. public function getSyslogs(
  19. &$cnt = 0,
  20. $orderColumn,
  21. $orderDir,
  22. $start,
  23. $length,
  24. $searchValue,
  25. $k,
  26. $type,
  27. $createDateStart,
  28. $createDateFinal
  29. )
  30. {
  31. $syslogs = $this->syslogManagementDb
  32. ->select([
  33. 'id',
  34. 'type',
  35. 'func',
  36. 'k',
  37. 'memoIn',
  38. 'memoOut',
  39. 'cdate',
  40. ]);
  41. // 過濾搜尋條件
  42. if ($k != '') $syslogs = $syslogs->where('k', '=', $k);
  43. if ($type != '') $syslogs = $syslogs->where('type', '=', $type);
  44. $syslogs = $syslogs->where('cdate', '>=', $createDateStart . ' 00:00:00');
  45. $syslogs = $syslogs->where('cdate', '<=', $createDateFinal . ' 23:59:59');
  46. // 取總筆數
  47. $cnt = $syslogs->count();
  48. // 排序
  49. $syslogs = $syslogs
  50. ->orderByRaw((int)$orderColumn . ' ' . 'DESC');
  51. // 彙整
  52. // 分頁
  53. $syslogs = $syslogs
  54. ->skip($start)->take($length);
  55. // 實際取資料
  56. $syslogs = $syslogs
  57. ->get()
  58. ->toArray();
  59. // 整理返回值並返回
  60. return $syslogs;
  61. }
  62. public function getExports($param)
  63. {
  64. $syslogs = $this->syslogManagementDb
  65. ->select([
  66. 'id',
  67. 'type',
  68. 'func',
  69. 'k',
  70. 'memoIn',
  71. 'memoOut',
  72. 'cdate',
  73. ]);
  74. // 過濾搜尋條件
  75. if ($param["k"] != '') $syslogs = $syslogs->where('k', 'like', '%' . $param["k"] . '%');
  76. if ($param["type"] != '') $syslogs = $syslogs->where('type', 'like', '%' . $param["type"] . '%');
  77. $syslogs = $syslogs->where('cdate', '>=', $param["createDateStart"] . ' 00:00:00');
  78. $syslogs = $syslogs->where('cdate', '<=', $param["createDateFinal"] . ' 23:59:59');
  79. // 實際取資料
  80. $syslogs = $syslogs
  81. ->get()
  82. ->toArray();
  83. // 整理返回值並返回
  84. return $syslogs;
  85. }
  86. public function downloadExcel($titles = [], $datas = [], $fileName = 'simple')
  87. {
  88. $spreadsheet = new Spreadsheet();
  89. ini_set('memory_limit', '1024M');
  90. ini_set("max_execution_time", "600");
  91. $spreadsheet->getActiveSheet()
  92. ->fromArray(
  93. $titles, // The data to set
  94. null, // Array values with this value will not be set
  95. 'A1' // Top left coordinate of the worksheet range where we want to set these values (default is A1)
  96. );
  97. $spreadsheet->getActiveSheet()
  98. ->fromArray(
  99. $datas, // The data to set
  100. null, // Array values with this value will not be set
  101. 'A2' // Top left coordinate of the worksheet range where we want to set these values (default is A1)
  102. );
  103. // Redirect output to a client’s web browser (Xlsx)
  104. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  105. header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
  106. header('Cache-Control: max-age=0');
  107. // If you're serving to IE 9, then the following may be needed
  108. header('Cache-Control: max-age=1');
  109. // If you're serving to IE over SSL, then the following may be needed
  110. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  111. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  112. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  113. header('Pragma: public'); // HTTP/1.0
  114. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  115. $writer->save('php://output');
  116. exit;
  117. }
  118. }