SessionManagementController.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers\Backend\DataManagement;
  3. use Illuminate\Http\Request;
  4. use App\Http\Services\Backend\DataManagement\SessionManagementService;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Services\ConstDef\GeneralConst;
  7. use App\Http\Services\CheckParamService;
  8. class SessionManagementController extends Controller
  9. {
  10. // 相關私有服務層調用器宣告
  11. private $sessionManagementSv;
  12. private $checkParamSv;
  13. public function __construct()
  14. {
  15. // 建構服務層調用器
  16. $this->sessionManagementSv = new SessionManagementService();
  17. $this->checkParamSv = new CheckParamService();
  18. // 時區調整
  19. date_default_timezone_set("Asia/Taipei");
  20. }
  21. public function index(Request $request)
  22. {
  23. // 渲染
  24. return view('admin.DataManagement.SessionManagement', [
  25. 'round' => $this->sessionManagementSv->getRounds(),
  26. 'good' => $this->sessionManagementSv->getGoods(),
  27. ]);
  28. }
  29. public function grid(Request $request)
  30. {
  31. // 取得參數
  32. $param = $_GET;
  33. if ($param == null) exit();
  34. $draw = $param["draw"]; //客戶端傳來的查詢次數,無條件回傳用以核對
  35. $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送,但 mysql 從 1 開始算
  36. $orderDir = $param["order"][0]["dir"];
  37. $start = $param["start"]; // 頁碼
  38. $length = $param["length"]; // 一頁多大
  39. $searchValue = $param["search"]["value"];
  40. //客製化搜尋欄位
  41. $lineId = $param["columns"][1]["search"]["value"];
  42. $userName = $param["columns"][2]["search"]["value"];
  43. $round = $param["columns"][3]["search"]["value"];
  44. $good = $param["columns"][4]["search"]["value"];
  45. $createDate = explode("\n", $param["columns"][5]["search"]["value"]);
  46. $createDateStart = $createDate[0] ?? null;
  47. $createDateFinal = $createDate[1] ?? null;
  48. // 驗證
  49. if ($lineId != filter_var($lineId, FILTER_SANITIZE_SPECIAL_CHARS)) $lineId = "___CANNOT_FIND_STRING___";
  50. if (!$this->checkParamSv->LenMToN($lineId, 0, 50)) $lineId = "___CANNOT_FIND_STRING___";
  51. if ($userName != filter_var($userName, FILTER_SANITIZE_SPECIAL_CHARS)) $userName = "___CANNOT_FIND_STRING___";
  52. if (!$this->checkParamSv->LenMToN($userName, 0, 100)) $userName = "___CANNOT_FIND_STRING___";
  53. if ($createDateStart == "") $createDateStart = "1900-01-01";
  54. if ($createDateFinal == "") $createDateFinal = "2050-12-31";
  55. if (!$this->checkParamSv->validateDate($createDateStart, 'Y-m-d')) $createDateStart = "1900-01-01";
  56. if (!$this->checkParamSv->validateDate($createDateFinal, 'Y-m-d')) $createDateFinal = "2050-12-31";
  57. //資料庫
  58. $recordsTotal = 0;
  59. $result = $this->sessionManagementSv->getSessions(
  60. $recordsTotal,
  61. $orderColumn,
  62. $orderDir,
  63. $start,
  64. $length,
  65. $searchValue,
  66. $lineId,
  67. $userName,
  68. $round,
  69. $good,
  70. $createDateStart,
  71. $createDateFinal
  72. );
  73. // 整理返回資料
  74. $data = array();
  75. for ($i = 0; $i < count($result); $i++) {
  76. $data[] = array(
  77. //一般資料
  78. $result[ $i ]["id"],
  79. $result[ $i ]["lineId"],
  80. $result[ $i ]["userName"],
  81. $result[ $i ]["roundName"],
  82. $result[ $i ]["lp"],
  83. $result[ $i ]["eventDate"],
  84. $result[ $i ]["currentGameGp"],
  85. $result[ $i ]["currentCheckinGp"],
  86. $result[ $i ]["currentGp"],
  87. $result[ $i ]["cdate"],
  88. );
  89. }
  90. $json = array(
  91. "draw" => $draw,
  92. "recordsTotal" => $recordsTotal,
  93. "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數,本次筆數可從陣列取得
  94. "data" => $data,
  95. );
  96. // 返回
  97. return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
  98. }
  99. public function export(Request $request)
  100. {
  101. // 取得參數
  102. $param = $_POST;
  103. if ($param == null) exit();
  104. // 驗證
  105. if ($param['lineId'] != filter_var($param['lineId'], FILTER_SANITIZE_SPECIAL_CHARS)) $param['lineId'] = "___CANNOT_FIND_STRING___";
  106. if (!$this->checkParamSv->LenMToN($param['lineId'], 0, 50)) $param['lineId'] = "___CANNOT_FIND_STRING___";
  107. if ($param['userName'] != filter_var($param['userName'], FILTER_SANITIZE_SPECIAL_CHARS)) $param['userName'] = "___CANNOT_FIND_STRING___";
  108. if (!$this->checkParamSv->LenMToN($param['userName'], 0, 100)) $param['userName'] = "___CANNOT_FIND_STRING___";
  109. if ($param['createDateStart'] == "") $param['createDateStart'] = "1900-01-01";
  110. if ($param['createDateFinal'] == "") $param['createDateFinal'] = "2050-12-31";
  111. if (!$this->checkParamSv->validateDate($param['createDateStart'], 'Y-m-d')) $param['createDateStart'] = "1900-01-01";
  112. if (!$this->checkParamSv->validateDate($param['createDateFinal'], 'Y-m-d')) $param['createDateFinal'] = "2050-12-31";
  113. // 製作
  114. $title = [
  115. 'ID',
  116. '玩家 LINE ID',
  117. '玩家名稱',
  118. '回合',
  119. '獎項 LINE POINT',
  120. '日期',
  121. '轉盤吉點',
  122. '簽到吉點',
  123. '本次吉點異動',
  124. '時間戳',
  125. ];
  126. $sessions = $this->sessionManagementSv->getExports($param);
  127. $this->sessionManagementSv->downloadExcel($title, $sessions, date("YmdHis") . '-SESSION-REPORT');
  128. }
  129. }