123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- const connection = require('../sql');
- class MenuService {
- async list(name, status, project_id) {
- const statement = `
- SELECT
- id,
- project_id as projectId,
- name,
- parent_id as parentId,
- type,
- icon,
- path,
- page_id as pageId,
- sort_num as sortNum,
- status,
- code,
- user_id as userId,
- user_name as userName,
- updated_at as updatedAt,
- created_at as createdAt
- FROM
- menu
- WHERE
- (name LIKE COALESCE(CONCAT('%',?,'%'), name) OR ? IS NULL)
- AND
- (status = COALESCE(?, status) OR ? IS NULL)
- AND project_id = ?;
- `;
- const [result] = await connection.execute(statement, [
- name || null,
- name || null,
- status >= 0 ? status : null,
- status >= 0 ? status : null,
- project_id,
- ]);
- return result;
- }
- async create(params) {
- const statement =
- 'INSERT INTO menu (project_id, name, parent_id, type, code, icon, path, page_id, sort_num, status, user_name, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
- const [result] = await connection.execute(statement, [
- params.projectId,
- params.name,
- params.parentId || null,
- params.type || 1,
- params.code || '',
- params.icon || '',
- params.path || '',
- params.pageId || 0,
- params.sortNum || 1,
- params.status || 1,
- params.userName,
- params.userId,
- ]);
- return result;
- }
- async getMenuInfoById(id) {
- const statement = `
- SELECT
- id,
- project_id as projectId,
- name,
- parent_id as parentId,
- type,
- icon,
- path,
- page_id as pageId,
- sort_num as sortNum,
- status,
- code,
- user_id as userId,
- user_name as userName
- FROM
- menu
- WHERE id = ?;`;
- const [result] = await connection.execute(statement, [id]);
- return result;
- }
- async deleteMenuById(id, projectId) {
- const statement = `DELETE FROM menu WHERE id = ? || parent_id = ? and project_id = ?;`;
- const [result] = await connection.execute(statement, [id, id, projectId]);
- return result;
- }
- // 根据页面ID删除
- async deleteMenuByProjectId(projectId) {
- const statement = 'DELETE FROM menu WHERE project_id = ?;';
- const [result] = await connection.execute(statement, [projectId]);
- return result;
- }
- async update(params) {
- const statement =
- 'UPDATE menu SET name = ?, parent_id = ?, type = ?, code = ?, icon = ?, path = ?, page_id = ?, sort_num = ?, status = ? WHERE id = ?;';
- const [result] = await connection.execute(statement, [
- params.name,
- params.parentId || null,
- params.type || 1,
- params.code || '',
- params.icon || '',
- params.path || '',
- params.pageId || 0,
- params.sortNum || 1,
- params.status || 1,
- params.id,
- ]);
- return result;
- }
- // 根据项目模板id查询项目信息
- async getMenuByTemplateId(id) {
- const statement = `
- SELECT
- id, name, parent_id parentId, type, icon, path, page_id pageId, sort_num sortNum, status, code
- FROM
- menu
- WHERE
- project_id = ?;
- `;
- const [result] = await connection.execute(statement, [id]);
- return result;
- }
- // 注销用户所有菜单
- async deleteAllMenu(userId) {
- const statement = 'DELETE FROM menu WHERE user_id = ?;';
- const [result] = await connection.execute(statement, [userId]);
- return result;
- }
- }
- module.exports = new MenuService();
|