filing_template.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict';
  2. /**
  3. *
  4. * 2024/3/21
  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. const maxFilingType = 12;
  25. module.exports = app => {
  26. class FilingTemplate extends app.BaseService {
  27. /**
  28. * 构造函数
  29. *
  30. * @param {Object} ctx - egg全局变量
  31. * @return {void}
  32. */
  33. constructor(ctx) {
  34. super(ctx);
  35. this.tableName = 'filing_template';
  36. }
  37. async initTemplate(transaction, template) {
  38. const insertData = [];
  39. for (const [i, f] of filingType.entries()) {
  40. insertData.push({
  41. id: this.uuid.v4(), temp_id: template.id, tree_pid: -1, tree_level: 1, tree_order: i + 1,
  42. add_user_id: this.ctx.session.sessionUser.accountId, is_fixed: true,
  43. name: f.name, filing_type: f.value,
  44. });
  45. }
  46. if (transaction) {
  47. await transaction.insert(this.tableName, insertData);
  48. } else {
  49. await this.db.insert(this.tableName, insertData);
  50. }
  51. }
  52. async getData(templateId) {
  53. return await this.getAllDataByCondition({
  54. where: { temp_id: templateId }
  55. })
  56. }
  57. async getPosterityData(templateId, id){
  58. const result = [];
  59. let cur = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: id } });
  60. let iLevel = 1;
  61. while (cur.length > 0 && iLevel < 6) {
  62. result.push(...cur);
  63. cur = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: cur.map(x => { return x.id })} });
  64. iLevel += 1;
  65. }
  66. return result;
  67. }
  68. async getNewName(templateId, name = '新增文件类别') {
  69. const data = await this.db.query(`SELECT * FROM ${this.tableName} WHERE temp_id = '${templateId}' AND name LIKE '${name}%'`);
  70. if (data.length === 0) return name;
  71. const _ = this._;
  72. const names = data.map(x => { return _.toInteger(x.name.replace(name, '')) });
  73. const filterNames = names.filter(x => { return x > 0 });
  74. const max = filterNames.reduce((pre, cur) => { return Math.max(pre, cur); }, 0);
  75. return max >= 0 ? name + (max + 1) : name;
  76. }
  77. async getNewFilingType(templateId) {
  78. const max = await this.db.queryOne(`SELECT filing_type FROM ${this.tableName} WHERE temp_id = '${templateId}' ORDER BY filing_type DESC`);
  79. return max && max.filing_type ? max.filing_type + 1 : maxFilingType + 1;
  80. }
  81. async add(templateId, data) {
  82. const parent = await this.getDataById(data.tree_pid);
  83. const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: parent ? parent.id : rootId }, orders: [['tree_order', 'asc']]});
  84. const preChild = data.tree_pre_id ? sibling.find(x => { return x.id === data.tree_pre_id; }) : null;
  85. const filing_type = parent ? parent.filing_type : await this.getNewFilingType(templateId);
  86. const conn = await this.db.beginTransaction();
  87. try {
  88. // 获取当前用户信息
  89. const sessionUser = this.ctx.session.sessionUser;
  90. // 获取当前项目信息
  91. const sessionProject = this.ctx.session.sessionProject;
  92. const tree_order = preChild ? preChild.tree_order + 1 : (sibling.length > 0 ? sibling[sibling.length - 1].tree_order + 1 : 1);
  93. const name = await this.getNewName(templateId);
  94. const insertData = {
  95. id: this.uuid.v4(), temp_id: templateId, add_user_id: sessionUser.accountId,
  96. tree_pid: parent ? parent.id : rootId, tree_level: parent ? parent.tree_level + 1 : 1, tree_order,
  97. name, filing_type,
  98. };
  99. const operate = await conn.insert(this.tableName, insertData);
  100. if (operate.affectedRows === 0) throw '新增文件夹失败';
  101. const updateData = [];
  102. if (preChild) {
  103. sibling.forEach(x => {
  104. if (x.tree_order >= tree_order) updateData.push({ id: x.id, tree_order: x.tree_order + 1 });
  105. });
  106. }
  107. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  108. await conn.commit();
  109. return { create: [insertData], update: updateData };
  110. } catch (error) {
  111. await conn.rollback();
  112. throw error;
  113. }
  114. }
  115. async save(data) {
  116. const filing = await this.getDataById(data.id);
  117. if (!filing) throw '分类不存在,请刷新页面后重试';
  118. const result = await this.db.update(this.tableName, { id: data.id, name: data.name });
  119. if (result.affectedRows > 0) {
  120. return data;
  121. } else {
  122. throw '更新数据失败';
  123. }
  124. }
  125. async del(templateId, data) {
  126. const filing = await this.getDataById(data.id);
  127. if (!filing) throw '分类不存在,请刷新页面后重试';
  128. const posterity = await this.getPosterityData(templateId, data.id);
  129. const delData = posterity.map(x => {return x.id; });
  130. delData.push(data.id);
  131. const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: filing.tree_pid } });
  132. const updateData = [];
  133. sibling.forEach(x => {
  134. if (x.tree_order > filing.tree_order) updateData.push({ id: x.id, tree_order: x.tree_order - 1});
  135. });
  136. const conn = await this.db.beginTransaction();
  137. try {
  138. await conn.delete(this.tableName, { id: delData });
  139. if (updateData.length > 0) conn.updateRows(this.tableName, updateData);
  140. await conn.commit();
  141. return { delete: delData, update: updateData };
  142. } catch(err) {
  143. await conn.rollback();
  144. throw err;
  145. }
  146. }
  147. async move(templateId, data) {
  148. const filing = await this.getDataById(data.id);
  149. if (!filing) throw '移动的分类不存在,请刷新页面后重试';
  150. const parent = await this.getDataById(data.tree_pid);
  151. const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: data.tree_pid } });
  152. const filing_type = parent ? parent.filing_type : await this.getNewFilingType(templateId);
  153. const posterity = await this.getPosterityData(templateId, filing.id);
  154. const result = [];
  155. const updateData = { id: filing.id, tree_order: data.tree_order, tree_pid: data.tree_pid, tree_level: parent ? parent.tree_level + 1 : 1, filing_type };
  156. result.push(updateData);
  157. const posterityUpdateData = posterity.map(x => { return { id: x.id, filing_type, tree_level: updateData.tree_level - filing.tree_level + x.tree_level }});
  158. result.push(posterityUpdateData);
  159. const siblingUpdateData = [];
  160. if (data.tree_pid === filing.tree_pid) {
  161. if (data.tree_order < filing.tree_order) {
  162. sibling.forEach(x => {
  163. if (x.id === filing.id) return;
  164. if (x.tree_order < data.tree_order) return;
  165. if (x.tree_order > filing.tree_order) return;
  166. siblingUpdateData.push({id: x.id, tree_order: x.tree_order + 1});
  167. });
  168. } else {
  169. sibling.forEach(x => {
  170. if (x.id === filing.id) return;
  171. if (x.tree_order < filing.tree_order) return;
  172. if (x.tree_order > data.tree_order) return;
  173. siblingUpdateData.push({id: x.id, tree_order: x.tree_order - 1});
  174. });
  175. }
  176. } else {
  177. const orgSibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: filing.tree_pid } });
  178. orgSibling.forEach(x => {
  179. if (x.id === filing.id) return;
  180. if (x.tree_order < filing.tree_order) return;
  181. siblingUpdateData.push({id: x.id, tree_order: x.tree_order - 1});
  182. });
  183. sibling.forEach(x => {
  184. if (x.id === filing.id) return;
  185. if (x.tree_order < data.tree_order) return;
  186. siblingUpdateData.push({id: x.id, tree_order: x.tree_order + 1});
  187. })
  188. }
  189. result.push(...siblingUpdateData);
  190. const conn = await this.db.beginTransaction();
  191. try {
  192. await conn.update(this.tableName, updateData);
  193. if (posterityUpdateData.length > 0) await conn.updateRows(this.tableName, posterityUpdateData);
  194. if (siblingUpdateData.length > 0) await conn.updateRows(this.tableName, siblingUpdateData);
  195. await conn.commit();
  196. return { update: result };
  197. } catch(err) {
  198. await conn.rollback();
  199. throw err;
  200. }
  201. }
  202. }
  203. return FilingTemplate;
  204. };