PlayerManagementService.php 5.3KB

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