Menu.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\DB;
  6. use Kalnoy\Nestedset\NodeTrait;
  7. class Menu extends Model
  8. {
  9. use NodeTrait;
  10. protected $table = 'tb_menu';
  11. protected $primaryKey = 'id';
  12. public $keyType = 'int';
  13. public $incrementing = true;
  14. public $timestamps = true;
  15. public string $cacheName_1 = 'menu-config';
  16. public string $cacheName_2 = 'menu-front';
  17. public int $cacheTime = 31536000;
  18. const CREATED_AT = 'created_at';
  19. const UPDATED_AT = 'updated_at';
  20. const DELETED_AT = null;
  21. protected $guarded = [];
  22. public function getLftName(): string
  23. {
  24. return 'lft';
  25. }
  26. public function getRgtName(): string
  27. {
  28. return 'rgt';
  29. }
  30. public function getParentIdName(): string
  31. {
  32. return 'parent_id';
  33. }
  34. /**
  35. * 메뉴 DB 조회
  36. */
  37. public function data(): ?object
  38. {
  39. if (!$ret = Cache::get($this->cacheName_1)) {
  40. $list = DB::select("CALL SP_MENU_LIST();");
  41. $rows = count($list);
  42. $total = $this->count();
  43. $ret = (object)[
  44. 'total' => $total,
  45. 'rows' => $rows,
  46. 'list' => $list
  47. ];
  48. Cache::put($this->cacheName_1, $ret, $this->cacheTime);
  49. }
  50. return $ret;
  51. }
  52. /**
  53. * 메뉴 저장
  54. */
  55. public function register(array $insertData): int
  56. {
  57. $insertData = [
  58. $insertData['menu_id'],
  59. $insertData['name'],
  60. $insertData['link'],
  61. $insertData['target'],
  62. $insertData['desktop'],
  63. $insertData['mobile'],
  64. $insertData['custom'],
  65. $insertData['icon'],
  66. $insertData['depth']
  67. ];
  68. $menuID = DB::selectOne("CALL SP_MENU_SAVE(?, ?, ?, ?, ?, ?, ?, ?, ?);", $insertData)->id;
  69. $this->reload();
  70. return $menuID;
  71. }
  72. /**
  73. * 메뉴 수정
  74. */
  75. public function updater(array $updateData): void
  76. {
  77. DB::select("CALL SP_MENU_UPDATE(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", [
  78. $updateData['menu_id'],
  79. $updateData['parent_id'],
  80. $updateData['name'],
  81. $updateData['link'],
  82. $updateData['target'],
  83. $updateData['desktop'],
  84. $updateData['mobile'],
  85. $updateData['custom'],
  86. $updateData['icon'],
  87. $updateData['depth']
  88. ]);
  89. $this->reload();
  90. }
  91. /**
  92. * 메뉴 삭제
  93. */
  94. public function remove(int $menuID): void
  95. {
  96. DB::select("CALL SP_MENU_DELETE(?);", [$menuID]);
  97. $this->reload();
  98. }
  99. /**
  100. * 메뉴 캐시 생성
  101. */
  102. private function reload(): void
  103. {
  104. Cache::forget($this->cacheName_1);
  105. Cache::forget($this->cacheName_2);
  106. $this->data();
  107. }
  108. /**
  109. * 메뉴 한 단계 위로
  110. */
  111. public function orderUp(int $menuID): void
  112. {
  113. $this->findCategory($menuID)->up();
  114. $this->reload();
  115. }
  116. /**
  117. * 메뉴 한 단계 아래로
  118. */
  119. public function orderDown(int $menuID): void
  120. {
  121. $this->findCategory($menuID)->down();
  122. $this->reload();
  123. }
  124. /**
  125. * 메뉴 조회
  126. */
  127. public function findCategory(int $menuID): Menu
  128. {
  129. return (new Menu)->newQuery()->findOrNew($menuID);
  130. }
  131. }