SyslogtManagementService.php 4.4KB

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