filing_template_list.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. /**
  3. *
  4. * 2024/3/21
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const FtType = {
  10. org: 0,
  11. add: 1,
  12. };
  13. module.exports = app => {
  14. class FilingTemplateList extends app.BaseService {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局变量
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.tableName = 'filing_template_list';
  24. this.FtType = FtType;
  25. }
  26. async getOriginTemplate() {
  27. return await this.getDataByCondition({ ft_type: FtType.org });
  28. }
  29. async getAllTemplate(pid) {
  30. const sql = `SELECT * FROM ${this.tableName} WHERE ft_type = ${FtType.org} OR project_id = ? ORDER BY create_time asc`;
  31. return await this.db.query(sql, [pid]);
  32. }
  33. async getShareTemplate(pid) {
  34. const sql = `SELECT ftl.*, p.name as project_name, pa.name as user_name FROM ${this.tableName} ftl
  35. LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON ftl.user_id = pa.id
  36. LEFT JOIN ${this.ctx.service.project.tableName} p ON ftl.project_id = p.id
  37. WHERE ftl.is_share = 1 and ftl.project_id <> ?`;
  38. return await this.db.query(sql, [pid]);
  39. }
  40. /**
  41. * 保存/新增数据
  42. *
  43. * @param {Object} name - 模板名称
  44. * @param {String} id - 存在则为保存,反之新增
  45. * @return {boolean} - 操作结果
  46. */
  47. async save(name, is_share, id = '') {
  48. if (id) {
  49. const updateData = { id };
  50. if (name !== undefined) updateData.name = name;
  51. if (is_share !== undefined) updateData.is_share = is_share;
  52. const result = await this.defaultUpdate(updateData);
  53. return [result.affectedRows > 0, id];
  54. } else {
  55. const conn = await this.db.beginTransaction();
  56. try {
  57. const newTemplate = {
  58. id: this.uuid.v4(), project_id: this.ctx.session.sessionProject.id,
  59. user_id: this.ctx.session.sessionUser.accountId,
  60. name: name || '新增XXX模板库', ft_type: FtType.add,
  61. };
  62. await conn.insert(this.tableName, newTemplate);
  63. await this.ctx.service.filingTemplate.initTemplate(conn, newTemplate);
  64. await conn.commit();
  65. return [true, newTemplate.id];
  66. } catch (err) {
  67. this.ctx.log(err);
  68. await conn.rollback();
  69. return [false, ''];
  70. }
  71. }
  72. }
  73. /**
  74. * 拷贝模板
  75. *
  76. * @param {string} shareId - 拷贝模板的id
  77. * @return {Boolean} - 操作结果
  78. */
  79. async copy(shareId) {
  80. const shareTemplate = await this.getDataById(shareId);
  81. if (!shareTemplate.is_share) throw '模板已取消共享';
  82. const shareTemplateData = await this.ctx.service.filingTemplate.getData(shareId);
  83. const conn = await this.db.beginTransaction();
  84. try {
  85. const newTemplate = {
  86. id: this.uuid.v4(), project_id: this.ctx.session.sessionProject.id,
  87. user_id: this.ctx.session.sessionUser.accountId,
  88. name: shareTemplate.name || '拷贝XXX模板库', ft_type: FtType.add,
  89. };
  90. await conn.insert(this.tableName, newTemplate);
  91. await this.ctx.service.filingTemplate.initTemplate(conn, newTemplate, shareTemplateData);
  92. await conn.commit();
  93. return [true, newTemplate.id];
  94. } catch (err) {
  95. this.ctx.log(err);
  96. await conn.rollback();
  97. return [false, err.stack ? '拷贝模板数据错误' : ''];
  98. }
  99. }
  100. /**
  101. * 删除模板
  102. *
  103. * @param {string} id - 删除的id
  104. * @return {Boolean} - 返回删除的结果
  105. */
  106. async delete(id) {
  107. const conn = await this.db.beginTransaction();
  108. try {
  109. await conn.delete(this.tableName, { id });
  110. await conn.delete(this.ctx.service.filingTemplate.tableName, { temp_id: id });
  111. await conn.commit();
  112. return true;
  113. } catch (error) {
  114. this.ctx.log(error);
  115. await conn.rollback();
  116. return false;
  117. }
  118. }
  119. async reset(id) {
  120. const template = await this.getDataById(id);
  121. const conn = await this.db.beginTransaction();
  122. try {
  123. await conn.delete(this.ctx.service.filingTemplate.tableName, { temp_id: id });
  124. await this.ctx.service.filingTemplate.initTemplate(conn, template);
  125. await conn.commit();
  126. } catch(err) {
  127. await conn.rollback();
  128. throw err;
  129. }
  130. }
  131. }
  132. return FilingTemplateList;
  133. };