EventManagementService.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace App\Http\Services\Backend\DataManagement;
  3. use App\Http\Services\ConstDef\GeneralConst;
  4. use App\Models\Web\Event;
  5. use App\Models\Web\Item;
  6. use App\Models\Web\Submit;
  7. class EventManagementService
  8. {
  9. // 相關私有 model 調用器宣告
  10. private $eventManagementDb;
  11. private $itemManagementDb;
  12. private $submitManagementDb;
  13. public function __construct()
  14. {
  15. date_default_timezone_set("Asia/Taipei");
  16. // 建構 model 調用器
  17. $this->eventManagementDb = new Event();
  18. $this->itemManagementDb = new Item();
  19. $this->submitManagementDb = new Submit();
  20. }
  21. public function getEvents(
  22. &$cnt = 0,
  23. $orderColumn,
  24. $orderDir,
  25. $start,
  26. $length,
  27. $searchValue,
  28. $name,
  29. $eventDateStart,
  30. $eventDateFinal,
  31. $archive
  32. )
  33. {
  34. // 選欄位
  35. $archiveStr = '';
  36. foreach (GeneralConst::$archiveMap as $k => $v) {
  37. $archiveStr .= ' when \'' . $k . '\' then \'' . $v . '\'';
  38. }
  39. $events = $this->eventManagementDb
  40. ->select([
  41. \DB::raw("CONCAT('<input class=\"form-check-input\" type=\"checkbox\" name=\"grp[', id, ']\" value=\"\">') as chk"),
  42. 'id',
  43. \DB::raw("CONCAT('<a href=\"eventManagement/edit/', id, '\">', name, '</a>') as name"),
  44. \DB::raw("(select count(*) from items where eid=events.id) as cnt"),
  45. 'date_begin',
  46. 'date_final',
  47. 'cdate',
  48. 'mdate',
  49. \DB::raw("(select name from users where id=events.oid) as oid"),
  50. ]);
  51. // 過濾搜尋條件
  52. $events = $events
  53. ->where('name', 'LIKE', '%' . $name . '%');
  54. if ($archive != '') $events = $events->where('archive', '=', $archive);
  55. $events = $events->where('date_begin', '>=', $eventDateStart . ' 00:00:00');
  56. $events = $events->where('date_final', '<=', $eventDateFinal . ' 23:59:59');
  57. // 取總筆數
  58. $cnt = $events->count();
  59. // 排序
  60. $events = $events
  61. ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
  62. // 彙整
  63. // 分頁
  64. $events = $events
  65. ->skip($start)->take($length);
  66. // 實際取資料
  67. $events = $events
  68. ->get()
  69. ->toArray();
  70. // 整理返回值並返回
  71. return $events;
  72. }
  73. public function getItems($eid)
  74. {
  75. $items = $this->itemManagementDb
  76. ->select([
  77. 'id',
  78. 'eid',
  79. 'name',
  80. 'is_ide',
  81. 'id_acc',
  82. \DB::raw("CONCAT('" . env('APP_URL') . "', 'main/?h=', HEX(AES_ENCRYPT(TO_BASE64(CONCAT(eid, ',', id)), '" . env('KK') . "'))) as link"),
  83. ])
  84. ->where('eid', $eid)
  85. ->get()
  86. ->toArray();
  87. // 整理返回值並返回
  88. return $items;
  89. }
  90. public function archive($grp)
  91. {
  92. $this->eventManagementDb
  93. ->whereIn('id', $grp)
  94. ->update([
  95. 'archive' => GeneralConst::ARCHIVE_YES,
  96. ]);
  97. // 整理返回值並返回
  98. return true;
  99. }
  100. public function unarchive($grp)
  101. {
  102. $this->eventManagementDb
  103. ->whereIn('id', $grp)
  104. ->update([
  105. 'archive' => GeneralConst::ARCHIVE_NO,
  106. ]);
  107. // 整理返回值並返回
  108. return true;
  109. }
  110. public function clear($grp)
  111. {
  112. // 找出所有獎項ID
  113. $items = $this->itemManagementDb->select([
  114. 'id',
  115. ])
  116. ->whereIn('eid', $grp)
  117. ->get()
  118. ->toArray();
  119. $iid = [];
  120. foreach ($items as $i) $iid[] = $i['id'];
  121. // 找出所有獎項ID下的申報資料
  122. $submits = $this->submitManagementDb->select([
  123. GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'],
  124. GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql'],
  125. GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql'],
  126. GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql'],
  127. ])
  128. ->whereIn('iid', $iid)
  129. ->get()
  130. ->toArray();
  131. foreach ($submits as $s) {
  132. if (file_exists($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'] ])) unlink($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'] ]);
  133. if (file_exists($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql'] ])) unlink($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql'] ]);
  134. if (file_exists($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql'] ])) unlink($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql'] ]);
  135. if (file_exists($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql'] ])) unlink($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql'] ]);
  136. }
  137. // 刪除
  138. $this->submitManagementDb
  139. ->whereIn('iid', $iid)
  140. ->delete();
  141. return true;
  142. }
  143. public function getEventById($id)
  144. {
  145. // 取得參數
  146. // 調用資料庫(或者其他業務邏輯)
  147. $events = $this->eventManagementDb->select([
  148. 'id',
  149. 'name',
  150. 'kv',
  151. 'date_begin',
  152. 'date_final',
  153. 'email',
  154. 'tel',
  155. 'archive',
  156. 'cdate',
  157. 'mdate',
  158. \DB::raw("(select name from users where id=events.oid) as oid"),
  159. ])
  160. ->where('id', $id)
  161. ->first()
  162. ->toArray();
  163. // 整理返回值並返回
  164. return $events;
  165. }
  166. public function insertEvent($name, $kv, $date_begin, $date_final, $email, $tel, $oid)
  167. {
  168. // 取得參數
  169. // 調用資料庫(或者其他業務邏輯)
  170. $this->eventManagementDb
  171. ->insert([
  172. 'name' => $name,
  173. 'kv' => $kv,
  174. 'date_begin' => $date_begin,
  175. 'date_final' => $date_final,
  176. 'email' => $email,
  177. 'tel' => $tel,
  178. 'cdate' => date('Y-m-d H:i:s'),
  179. 'oid' => $oid,
  180. ]);
  181. $id = \DB::getPdo()->lastInsertId();
  182. // 整理返回值並返回
  183. return $id;
  184. }
  185. public function modifyEvent($id, $name, $kv, $date_begin, $date_final, $email, $tel, $oid)
  186. {
  187. // 取得參數
  188. // 調用資料庫(或者其他業務邏輯)
  189. $res = $this->eventManagementDb
  190. ->where('id', $id)
  191. ->update([
  192. 'name' => $name,
  193. 'kv' => $kv,
  194. 'date_begin' => $date_begin,
  195. 'date_final' => $date_final,
  196. 'email' => $email,
  197. 'tel' => $tel,
  198. 'mdate' => date('Y-m-d H:i:s'),
  199. 'oid' => $oid,
  200. ]);
  201. // 整理返回值並返回
  202. return $res;
  203. }
  204. public function insertItem($eid, $name, $is_ide, $id_acc)
  205. {
  206. // 取得參數
  207. // 調用資料庫(或者其他業務邏輯)
  208. $this->itemManagementDb
  209. ->insert([
  210. 'eid' => $eid,
  211. 'name' => $name,
  212. 'is_ide' => $is_ide,
  213. 'id_acc' => $id_acc,
  214. ]);
  215. $id = \DB::getPdo()->lastInsertId();
  216. // 整理返回值並返回
  217. return $id;
  218. }
  219. public function modifyItem($id, $eid, $name)
  220. {
  221. // 取得參數
  222. // 調用資料庫(或者其他業務邏輯)
  223. $res = $this->itemManagementDb
  224. ->where('id', $id)
  225. ->where('eid', $eid)
  226. ->update([
  227. 'name' => $name
  228. ]);
  229. // 整理返回值並返回
  230. return $res;
  231. }
  232. }