RoundManagementService.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Http\Services\Backend\DataManagement;
  3. use App\Http\Services\ConstDef\GeneralConst;
  4. use App\Models\Web\Round;
  5. use App\Models\Web\Syslog;
  6. class RoundManagementService
  7. {
  8. // 相關私有 model 調用器宣告
  9. private $roundManagementDb;
  10. private $syslogManagementDb;
  11. public function __construct()
  12. {
  13. date_default_timezone_set("Asia/Taipei");
  14. // 建構 model 調用器
  15. $this->roundManagementDb = new Round();
  16. $this->syslogManagementDb = new Syslog();
  17. }
  18. public function getRounds(
  19. &$cnt = 0,
  20. $orderColumn,
  21. $orderDir,
  22. $start,
  23. $length,
  24. $searchValue
  25. )
  26. {
  27. // 選欄位
  28. $activeStr = '';
  29. foreach (GeneralConst::$activeMap as $k => $v) {
  30. $activeStr .= ' when \'' . $k . '\' then \'' . $v . '\'';
  31. }
  32. $rounds = $this->roundManagementDb
  33. ->select([
  34. 'id',
  35. \DB::raw("CONCAT('<a href=\"roundManagement/edit/', id, '\">', roundName, '</a>') as roundName"),
  36. 'dateBegin',
  37. 'dateFinal',
  38. 'redeemExtra',
  39. \DB::raw("(case active $activeStr end) as active"),
  40. 'cdate',
  41. 'mdate',
  42. \DB::raw("(select name from users where id=rounds.oid) as oid"),
  43. ]);
  44. // 過濾搜尋條件
  45. // 取總筆數
  46. $cnt = $rounds->count();
  47. // 排序
  48. $rounds = $rounds
  49. ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
  50. // 彙整
  51. // 分頁
  52. $rounds = $rounds
  53. ->skip($start)->take($length);
  54. // 實際取資料
  55. $rounds = $rounds
  56. ->get()
  57. ->toArray();
  58. // 整理返回值並返回
  59. return $rounds;
  60. }
  61. public function getRoundById($id)
  62. {
  63. // 取得參數
  64. // 調用資料庫(或者其他業務邏輯)
  65. $rounds = $this->roundManagementDb->select([
  66. 'id',
  67. 'roundName',
  68. 'dateBegin',
  69. 'dateFinal',
  70. 'redeemExtra',
  71. 'active',
  72. 'cdate',
  73. 'mdate',
  74. \DB::raw("(select name from users where id=rounds.oid) as oid"),
  75. ])
  76. ->where('id', $id)
  77. ->first()
  78. ->toArray();
  79. // 整理返回值並返回
  80. return $rounds;
  81. }
  82. public function insertRound($roundName, $dateBegin, $dateFinal, $redeemExtra, $active, $oid)
  83. {
  84. // 取得參數
  85. $data = [
  86. 'roundName' => $roundName,
  87. 'dateBegin' => $dateBegin,
  88. 'dateFinal' => $dateFinal,
  89. 'redeemExtra' => $redeemExtra,
  90. 'active' => $active,
  91. 'cdate' => date('Y-m-d H:i:s'),
  92. 'mdate' => date('Y-m-d H:i:s'),
  93. 'oid' => $oid,
  94. ];
  95. // 調用資料庫(或者其他業務邏輯)
  96. $this->roundManagementDb
  97. ->insert($data);
  98. $id = \DB::getPdo()->lastInsertId();
  99. // syslog
  100. $this->syslogManagementDb
  101. ->insert([
  102. 'type' => GeneralConst::LOG_ADMIN,
  103. 'func' => __FUNCTION__,
  104. 'k' => $oid,
  105. 'memoIn' => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
  106. 'memoOut' => json_encode(['id' => $id], JSON_UNESCAPED_UNICODE),
  107. 'cdate' => date("Y-m-d H:i:s"),
  108. ]);
  109. // 整理返回值並返回
  110. return $id;
  111. }
  112. public function modifyRound($id, $roundName, $dateBegin, $dateFinal, $redeemExtra, $active, $oid)
  113. {
  114. // 取得參數
  115. $data = [
  116. 'roundName' => $roundName,
  117. 'dateBegin' => $dateBegin,
  118. 'dateFinal' => $dateFinal,
  119. 'redeemExtra' => $redeemExtra,
  120. 'active' => $active,
  121. 'mdate' => date('Y-m-d H:i:s'),
  122. 'oid' => $oid,
  123. ];
  124. // 調用資料庫(或者其他業務邏輯)
  125. $res = $this->roundManagementDb
  126. ->where('id', $id)
  127. ->update($data);
  128. $rc = \DB::select("SELECT ROW_COUNT() AS rc;");
  129. $rc = $rc[0]->rc;
  130. // syslog
  131. $this->syslogManagementDb
  132. ->insert([
  133. 'type' => GeneralConst::LOG_ADMIN,
  134. 'func' => __FUNCTION__,
  135. 'k' => $oid,
  136. 'memoIn' => json_encode(['id' => $id, 'data' => $data], JSON_UNESCAPED_UNICODE),
  137. 'memoOut' => json_encode(['rc' => $rc], JSON_UNESCAPED_UNICODE),
  138. 'cdate' => date("Y-m-d H:i:s"),
  139. ]);
  140. // 整理返回值並返回
  141. return $res;
  142. }
  143. }