projects.service.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. const connection = require('../sql');
  2. class ProjectsService {
  3. // 查询项目分类总条数
  4. async getCategoryCount(keyword, userId) {
  5. const statement = `
  6. SELECT
  7. count(DISTINCT p.id) total
  8. FROM
  9. projects p
  10. LEFT JOIN
  11. pages pg ON p.id = pg.project_id
  12. LEFT JOIN
  13. pages_role pr ON p.id = pr.page_id AND pr.user_id = ?
  14. WHERE
  15. (p.name like COALESCE(CONCAT('%',?,'%'), p.name) OR ? IS NULL)
  16. AND
  17. p.user_id = ?
  18. OR
  19. pr.user_id = ?
  20. `;
  21. const [result] = await connection.execute(statement, [userId, keyword || null, keyword || null, userId, userId]);
  22. return result[0];
  23. }
  24. // 查询页面分类列表
  25. async getCategoryList(pageNum, pageSize, keyword, userId) {
  26. const offset = (+pageNum - 1) * pageSize + '';
  27. const statement = `
  28. SELECT
  29. p.id,
  30. p.name,
  31. p.remark,
  32. p.user_id as userId,
  33. p.user_name as userName,
  34. p.logo,
  35. p.updated_at as updatedAt,
  36. COUNT(pg.id) as count
  37. FROM
  38. projects p
  39. LEFT JOIN
  40. pages pg ON p.id = pg.project_id
  41. LEFT JOIN
  42. pages_role pr ON p.id = pr.page_id AND pr.user_id = ?
  43. WHERE
  44. (p.name like COALESCE(CONCAT('%',?,'%'), p.name) OR ? IS NULL)
  45. AND
  46. p.user_id = ?
  47. OR
  48. pr.user_id = ?
  49. GROUP BY
  50. p.id, p.name, p.user_id
  51. ORDER BY
  52. p.updated_at DESC
  53. LIMIT ?, ?
  54. `;
  55. const [result] = await connection.execute(statement, [userId, keyword || null, keyword || null, userId, userId, offset, pageSize]);
  56. return result;
  57. }
  58. // 查询项目总条数:mars-admin
  59. async getProjectCount(isTemplate, userId) {
  60. let statement = `
  61. SELECT
  62. count(id) total
  63. FROM
  64. projects
  65. WHERE
  66. 1 = 1
  67. `;
  68. if (isTemplate !== '') {
  69. statement += ` and is_template = ${isTemplate}`;
  70. }
  71. if (userId) {
  72. statement += ` and user_id = ${userId}`;
  73. }
  74. const [result] = await connection.execute(statement, []);
  75. return result[0];
  76. }
  77. // 查询项目总条数:mars-admin
  78. async getProjectList(pageNum, pageSize, isTemplate, userId) {
  79. const offset = (+pageNum - 1) * pageSize + '';
  80. let statement = `
  81. SELECT
  82. id,
  83. name,
  84. remark,
  85. logo,
  86. user_id as userId,
  87. user_name as userName,
  88. is_template as isTemplate,
  89. is_public as isPublic,
  90. breadcrumb,
  91. layout,
  92. menu_mode as menuMode,
  93. menu_theme_color as menuThemeColor,
  94. tag,
  95. footer,
  96. system_theme_color as systemThemeColor,
  97. updated_at as updatedAt,
  98. created_at as createdAt
  99. FROM
  100. projects
  101. WHERE
  102. 1 = 1
  103. `;
  104. if (isTemplate !== '') {
  105. statement += ` and is_template = ${isTemplate}`;
  106. }
  107. if (userId) {
  108. statement += ` and user_id = ${userId}`;
  109. }
  110. statement += ` LIMIT ${offset}, ${pageSize}`;
  111. const [result] = await connection.execute(statement, []);
  112. return result;
  113. }
  114. async getProjectByName(name) {
  115. const statement = `SELECT * FROM projects WHERE name = ?;`;
  116. const [result] = await connection.execute(statement, [name]);
  117. return result;
  118. }
  119. async createProject(params) {
  120. const statement = 'INSERT INTO projects (name, remark, logo, user_name, user_id, is_public) VALUES (?, ?, ?, ?, ?, ?);';
  121. const [result] = await connection.execute(statement, [
  122. params.name,
  123. params.remark,
  124. params.logo,
  125. params.userName,
  126. params.userId,
  127. params.isPublic || 1,
  128. ]);
  129. return result;
  130. }
  131. async installProject(params) {
  132. const statement =
  133. 'INSERT INTO projects (name, remark, logo, user_name, user_id, is_public, breadcrumb, layout, menu_mode, menu_theme_color, tag, footer, system_theme_color, home_page) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
  134. const [result] = await connection.execute(statement, [
  135. params.name,
  136. params.remark,
  137. params.logo,
  138. params.userName,
  139. params.userId,
  140. 2,
  141. params.breadcrumb,
  142. params.layout,
  143. params.menuMode,
  144. params.menuThemeColor,
  145. params.tag,
  146. params.footer,
  147. params.systemThemeColor,
  148. params.homePage || 'welcome',
  149. ]);
  150. return result;
  151. }
  152. async checkAuth(id) {
  153. const statement = `SELECT p.id,p.user_id userId, pr.user_id devUserId FROM projects p LEFT JOIN pages_role pr on p.id = pr.page_id WHERE p.id = ?;`;
  154. const [result] = await connection.execute(statement, [id]);
  155. return result;
  156. }
  157. async getProjectInfoById(id) {
  158. const statement =
  159. "SELECT id,name,remark,logo,user_id as userId,layout,menu_mode as menuMode,menu_theme_color as menuThemeColor,breadcrumb,tag,footer,is_public as isPublic,SUBSTRING_INDEX(user_name, '@', 1) as userName,show_header showHeader,show_tab_bar showTabBar,home_page homePage, extra_data extraData FROM projects WHERE id = ?;";
  160. const [result] = await connection.execute(statement, [id]);
  161. return result;
  162. }
  163. async deleteProject(id, userId) {
  164. const statement = 'DELETE FROM projects WHERE id = ? and user_id = ?;';
  165. const [result] = await connection.execute(statement, [id, userId]);
  166. return result;
  167. }
  168. async updateProjectInfo(params) {
  169. const statement =
  170. 'UPDATE projects SET name = ?, remark = ?, logo = ?, layout = ?, menu_mode = ?, menu_theme_color = ?, system_theme_color = ?, breadcrumb = ?, tag = ?, footer = ?, is_public = ?, show_header = ?, show_tab_bar = ?, home_page = ?, extra_data = ? WHERE id = ?;';
  171. const [result] = await connection.execute(statement, [
  172. params.name,
  173. params.remark,
  174. params.logo,
  175. params.layout,
  176. params.menuMode,
  177. params.menuThemeColor,
  178. params.systemThemeColor,
  179. params.breadcrumb ?? 1,
  180. params.tag ?? 1,
  181. params.footer ?? 0,
  182. params.isPublic ?? 2,
  183. params.showHeader ?? 1,
  184. params.showTabBar ?? 0,
  185. params.homePage || 'welcome',
  186. params.extraData || '{}',
  187. params.id,
  188. ]);
  189. return result;
  190. }
  191. // 根据项目模板id查询项目信息
  192. async getProjectByTemplateId(id) {
  193. const statement = `
  194. SELECT
  195. p.id, p.name, p.remark, p.logo, p.breadcrumb, p.layout, p.menu_mode menuMode, p.menu_theme_color menuThemeColor, p.tag, p.footer, p.system_theme_color systemThemeColor
  196. FROM
  197. projects p
  198. LEFT JOIN
  199. templates t
  200. on
  201. p.id = t.union_id
  202. WHERE
  203. t.id = ?;
  204. `;
  205. const [result] = await connection.execute(statement, [id]);
  206. return result[0];
  207. }
  208. // 设置模板以后,更新项目标识
  209. async updateProjectTemplate(id, status) {
  210. const statement = 'UPDATE projects SET is_template = ? WHERE id = ?;';
  211. const [result] = await connection.execute(statement, [status, id]);
  212. return result;
  213. }
  214. // 注销项目
  215. async deleteAllProject(userId, userName) {
  216. const statement = 'DELETE FROM projects WHERE user_id = ? and user_name = ?;';
  217. const [result] = await connection.execute(statement, [userId, userName]);
  218. return result;
  219. }
  220. }
  221. module.exports = new ProjectsService();