InfoManagementService.php 5.0KB

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