filing.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const rootId = '-1';
  10. const filingType = [
  11. { value: 1, name: '立项文件' },
  12. { value: 2, name: '招标投标、合同协议文件' },
  13. { value: 3, name: '勘察、设计文件' },
  14. { value: 4, name: '征地、拆迁、移民文件' },
  15. { value: 5, name: '项目管理文件' },
  16. { value: 6, name: '施工文件' },
  17. { value: 7, name: '信息系统开发文件' },
  18. { value: 8, name: '设备文件' },
  19. { value: 9, name: '监理文件' },
  20. { value: 10, name: '科研项目文件' },
  21. { value: 11, name: '生产技术准备、试运行文件' },
  22. { value: 12, name: '竣工验收文件' },
  23. ];
  24. module.exports = app => {
  25. class Filing extends app.BaseService {
  26. /**
  27. * 构造函数
  28. *
  29. * @param {Object} ctx - egg全局变量
  30. * @param {String} tableName - 表名
  31. * @return {void}
  32. */
  33. constructor(ctx) {
  34. super(ctx);
  35. this.tableName = 'filing';
  36. this.filingType = filingType;
  37. }
  38. get allFilingType () {
  39. return filingType.map(x => { return x.value });
  40. }
  41. async initFiling(spid, transaction) {
  42. const count = await this.count({ spid });
  43. if (count > 0) return;
  44. const insertData = [];
  45. for (const [i, f] of filingType.entries()) {
  46. insertData.push({
  47. id: this.uuid.v4(), tree_pid: -1, tree_level: 1, tree_order: i + 1,
  48. spid, add_user_id: this.ctx.session.sessionUser.accountId, is_fixed: 1,
  49. filing_type: f.value, name: f.name,
  50. });
  51. }
  52. if (transaction) {
  53. await transaction.insert(this.tableName, insertData);
  54. } else {
  55. await this.db.insert(this.tableName, insertData);
  56. }
  57. }
  58. async getValidFiling(spid, filingType) {
  59. const condition = { spid, is_deleted: 0 };
  60. if (filingType !== 'all') condition.filing_type = filingType;
  61. return await this.getAllDataByCondition({ where: condition });
  62. }
  63. async getPosterityData(id){
  64. const result = [];
  65. let cur = await this.getAllDataByCondition({ where: { tree_pid: id } });
  66. let iLevel = 1;
  67. while (cur.length > 0 && iLevel < 6) {
  68. result.push(...cur);
  69. cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
  70. iLevel += 1;
  71. }
  72. return result;
  73. }
  74. _checkFixed(data) {
  75. if (data.is_fixed) throw '固定分类,不可编辑';
  76. }
  77. async getNewName(spid, name = '新增文件类别') {
  78. const data = await this.db.query(`SELECT * FROM ${this.tableName} WHERE spid = '${spid}' AND name LIKE '${name}%'`);
  79. if (data.length === 0) return name;
  80. const _ = this._;
  81. const names = data.map(x => { return _.toInteger(x.name.replace(name, '')) });
  82. const filterNames = names.filter(x => { return x > 0 });
  83. const max = filterNames.reduce((pre, cur) => { return Math.max(pre, cur); }, 0);
  84. return max >= 0 ? name + (max + 1) : name;
  85. }
  86. async add(data) {
  87. const parent = await this.getDataById(data.tree_pid);
  88. if (!parent) throw '添加数据结构错误';
  89. if (parent.file_count > 0) throw `分类【${parent.name}】下存在文件,不可添加子分类`;
  90. const sibling = await this.getAllDataByCondition({ where: { tree_pid: parent.id }, orders: [['tree_order', 'asc']]});
  91. const preChild = data.tree_pre_id ? sibling.find(x => { x.id === data.tree_pre_id; }) : null;
  92. const conn = await this.db.beginTransaction();
  93. try {
  94. // 获取当前用户信息
  95. const sessionUser = this.ctx.session.sessionUser;
  96. // 获取当前项目信息
  97. const sessionProject = this.ctx.session.sessionProject;
  98. const tree_order = preChild ? preChild.tree_order + 1 : (sibling.length > 0 ? sibling[sibling.length - 1].tree_order + 1 : 1);
  99. const name = await this.getNewName(parent.spid);
  100. const insertData = {
  101. id: this.uuid.v4(), spid: parent.spid, add_user_id: sessionUser.accountId,
  102. tree_pid: parent.id, tree_level: parent.tree_level + 1, tree_order,
  103. name, filing_type: parent.filing_type,
  104. };
  105. const operate = await conn.insert(this.tableName, insertData);
  106. if (operate.affectedRows === 0) throw '新增文件夹失败';
  107. const updateData = [];
  108. if (preChild) {
  109. sibling.forEach(x => {
  110. if (x.tree_order >= tree_order) updateData.push({ id: x.id, tree_order: x.tree_order + 1 });
  111. });
  112. }
  113. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  114. await conn.commit();
  115. return { create: [insertData], update: updateData };
  116. } catch (error) {
  117. await conn.rollback();
  118. throw error;
  119. }
  120. }
  121. async save(data) {
  122. const filing = await this.getDataById(data.id);
  123. this._checkFixed(filing);
  124. const result = await this.db.update(this.tableName, data);
  125. if (result.affectedRows > 0) {
  126. return data;
  127. } else {
  128. throw '更新数据失败';
  129. }
  130. }
  131. async del(data) {
  132. const filing = await this.getDataById(data.id);
  133. this._checkFixed(filing);
  134. const posterity = await this.getPosterityData(data.id);
  135. const delData = posterity.map(x => {return { id: x.id, is_deleted: 1 }; });
  136. delData.push({ id: data.id, is_deleted: 1});
  137. const sibling = await this.getAllDataByCondition({ where: { tree_pid: filing.tree_pid } });
  138. const updateData = [];
  139. sibling.forEach(x => {
  140. if (x.tree_order > filing.tree_order) updateData.push({ id: x.id, tree_order: x.tree_order - 1});
  141. });
  142. const conn = await this.db.beginTransaction();
  143. try {
  144. await conn.updateRows(this.tableName, delData);
  145. if (updateData.length > 0) conn.updateRows(this.tableName, updateData);
  146. await conn.commit();
  147. return { delete: delData.map(x => { return x.id }), update: updateData };
  148. } catch(err) {
  149. await conn.rollback();
  150. throw error;
  151. }
  152. }
  153. }
  154. return Filing;
  155. };