TPlayerManagementService.php 6.4KB

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