TSessionManagementService.php 5.7KB

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