menu.service.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const connection = require('../sql');
  2. class MenuService {
  3. async list(name, status, project_id) {
  4. const statement = `
  5. SELECT
  6. id,
  7. project_id as projectId,
  8. name,
  9. parent_id as parentId,
  10. type,
  11. icon,
  12. path,
  13. page_id as pageId,
  14. sort_num as sortNum,
  15. status,
  16. code,
  17. user_id as userId,
  18. user_name as userName,
  19. updated_at as updatedAt,
  20. created_at as createdAt
  21. FROM
  22. menu
  23. WHERE
  24. (name LIKE COALESCE(CONCAT('%',?,'%'), name) OR ? IS NULL)
  25. AND
  26. (status = COALESCE(?, status) OR ? IS NULL)
  27. AND project_id = ?;
  28. `;
  29. const [result] = await connection.execute(statement, [
  30. name || null,
  31. name || null,
  32. status >= 0 ? status : null,
  33. status >= 0 ? status : null,
  34. project_id,
  35. ]);
  36. return result;
  37. }
  38. async create(params) {
  39. const statement =
  40. 'INSERT INTO menu (project_id, name, parent_id, type, code, icon, path, page_id, sort_num, status, user_name, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
  41. const [result] = await connection.execute(statement, [
  42. params.projectId,
  43. params.name,
  44. params.parentId || null,
  45. params.type || 1,
  46. params.code || '',
  47. params.icon || '',
  48. params.path || '',
  49. params.pageId || 0,
  50. params.sortNum || 1,
  51. params.status || 1,
  52. params.userName,
  53. params.userId,
  54. ]);
  55. return result;
  56. }
  57. async getMenuInfoById(id) {
  58. const statement = `
  59. SELECT
  60. id,
  61. project_id as projectId,
  62. name,
  63. parent_id as parentId,
  64. type,
  65. icon,
  66. path,
  67. page_id as pageId,
  68. sort_num as sortNum,
  69. status,
  70. code,
  71. user_id as userId,
  72. user_name as userName
  73. FROM
  74. menu
  75. WHERE id = ?;`;
  76. const [result] = await connection.execute(statement, [id]);
  77. return result;
  78. }
  79. async deleteMenuById(id, projectId) {
  80. const statement = `DELETE FROM menu WHERE id = ? || parent_id = ? and project_id = ?;`;
  81. const [result] = await connection.execute(statement, [id, id, projectId]);
  82. return result;
  83. }
  84. // 根据页面ID删除
  85. async deleteMenuByProjectId(projectId) {
  86. const statement = 'DELETE FROM menu WHERE project_id = ?;';
  87. const [result] = await connection.execute(statement, [projectId]);
  88. return result;
  89. }
  90. async update(params) {
  91. const statement =
  92. 'UPDATE menu SET name = ?, parent_id = ?, type = ?, code = ?, icon = ?, path = ?, page_id = ?, sort_num = ?, status = ? WHERE id = ?;';
  93. const [result] = await connection.execute(statement, [
  94. params.name,
  95. params.parentId || null,
  96. params.type || 1,
  97. params.code || '',
  98. params.icon || '',
  99. params.path || '',
  100. params.pageId || 0,
  101. params.sortNum || 1,
  102. params.status || 1,
  103. params.id,
  104. ]);
  105. return result;
  106. }
  107. // 根据项目模板id查询项目信息
  108. async getMenuByTemplateId(id) {
  109. const statement = `
  110. SELECT
  111. id, name, parent_id parentId, type, icon, path, page_id pageId, sort_num sortNum, status, code
  112. FROM
  113. menu
  114. WHERE
  115. project_id = ?;
  116. `;
  117. const [result] = await connection.execute(statement, [id]);
  118. return result;
  119. }
  120. // 注销用户所有菜单
  121. async deleteAllMenu(userId) {
  122. const statement = 'DELETE FROM menu WHERE user_id = ?;';
  123. const [result] = await connection.execute(statement, [userId]);
  124. return result;
  125. }
  126. }
  127. module.exports = new MenuService();