publish.service.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const connection = require('../sql');
  2. class PublishService {
  3. async listCount(env, userName, createdAt, updatedAt, pageId) {
  4. const statement = `
  5. SELECT
  6. count(id)
  7. FROM
  8. pages_publish
  9. WHERE
  10. (env = COALESCE(?, env) OR ? IS NULL)
  11. AND (user_name = COALESCE(?, user_name) OR ? IS NULL)
  12. AND (created_at >= ? OR ? IS NULL)
  13. AND (updated_at <= ? OR ? IS NULL)
  14. AND (page_id = COALESCE(?, page_id) OR ? IS NULL)
  15. `;
  16. const [result] = await connection.execute(statement, [
  17. env || null,
  18. env || null,
  19. userName || null,
  20. userName || null,
  21. createdAt || null,
  22. createdAt || null,
  23. updatedAt || null,
  24. updatedAt || null,
  25. pageId || null,
  26. pageId || null,
  27. ]);
  28. return result.length;
  29. }
  30. async list(params) {
  31. const offset = (+params.pageNum - 1) * params.pageSize + '';
  32. const limit = params.pageSize;
  33. const statement = `
  34. SELECT
  35. id,
  36. page_id as pageId,
  37. page_name as pageName,
  38. user_id as userId,
  39. user_name as userName,
  40. env,
  41. created_at as createdAt,
  42. updated_at as updatedAt
  43. FROM
  44. pages_publish
  45. WHERE
  46. (env = COALESCE(?, env) OR ? IS NULL)
  47. AND (user_name = COALESCE(?, user_name) OR ? IS NULL)
  48. AND (created_at >= ? OR ? IS NULL)
  49. AND (updated_at <= ? OR ? IS NULL)
  50. AND (page_id = COALESCE(?, page_id) OR ? IS NULL)
  51. ORDER BY id DESC
  52. LIMIT ${offset},${limit};
  53. `;
  54. const [result] = await connection.execute(statement, [
  55. params.env || null,
  56. params.env || null,
  57. params.userName || null,
  58. params.userName || null,
  59. params.start || null,
  60. params.start || null,
  61. params.end || null,
  62. params.end || null,
  63. params.pageId || null,
  64. params.pageId || null,
  65. ]);
  66. return result;
  67. }
  68. async getDetail(id, userId) {
  69. const statement =
  70. 'SELECT id,page_data pageData,page_id pageId,user_id userId,updated_at updatedAt FROM pages_publish WHERE id = ? and user_id = ?;';
  71. const [result] = await connection.execute(statement, [id, userId]);
  72. return result[0];
  73. }
  74. async createPublish(page_id, page_name, page_data, user_name, user_id, env) {
  75. const statement = 'INSERT INTO pages_publish (page_id, page_name, page_data, user_name, user_id, env) VALUES (?, ?, ?, ?, ?, ?);';
  76. const [result] = await connection.execute(statement, [page_id, page_name, page_data, user_name, user_id, env]);
  77. return result;
  78. }
  79. // 注销用户所有菜单
  80. async deleteAllPublish(userId) {
  81. const statement = 'DELETE FROM pages_publish WHERE user_id = ?;';
  82. const [result] = await connection.execute(statement, [userId]);
  83. return result;
  84. }
  85. }
  86. module.exports = new PublishService();