filing.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. }
  37. get allFilingType () {
  38. return filingType.map(x => { return x.value });
  39. }
  40. analysisFilingType(filing) {
  41. const curFilingType = filing.filter(f => {
  42. return f.tree_level === 1;
  43. });
  44. curFilingType.sort((x, y) => {
  45. return x.tree_order - y.tree_order;
  46. });
  47. return curFilingType.map(f => {
  48. return { value: f.filing_type, name: f.name }
  49. });
  50. }
  51. async getFilingType(spid) {
  52. const filing = await ctx.service.filing.getValidFiling(ctx.params.id, ctx.subProject.permission.filing_type);
  53. return this.analysisFilingType(filing);
  54. }
  55. async initFiling(spid, templateId, transaction) {
  56. const count = await this.count({ spid });
  57. if (count > 0) return;
  58. const templateFiling = await this.ctx.service.filingTemplate.getAllDataByCondition({
  59. where: { temp_id: templateId },
  60. orders: [['tree_level', 'asc']],
  61. });
  62. const insertData = [];
  63. for (const f of templateFiling) {
  64. f.newId = this.uuid.v4();
  65. const parent = f.tree_pid !== rootId ? templateFiling.find(x => { return x.id === f.tree_pid; }) : null;
  66. const newData = {
  67. id: f.newId, tree_pid : parent ? parent.newId : rootId, tree_level: f.tree_level, tree_order: f.tree_order,
  68. spid, add_user_id: this.ctx.session.sessionUser.accountId, is_fixed: f.tree_level === 1,
  69. filing_type: f.filing_type, name: f.name,
  70. };
  71. insertData.push(newData);
  72. }
  73. if (transaction) {
  74. await transaction.insert(this.tableName, insertData);
  75. } else {
  76. await this.db.insert(this.tableName, insertData);
  77. }
  78. }
  79. async getValidFiling(spid, filingType) {
  80. const condition = { spid, is_deleted: 0 };
  81. if (filingType !== 'all') condition.filing_type = filingType;
  82. if (!filingType || filingType.length === 0) return [];
  83. return await this.getAllDataByCondition({ where: condition });
  84. }
  85. async getPosterityData(id){
  86. const result = [];
  87. let cur = await this.getAllDataByCondition({ where: { tree_pid: id } });
  88. let iLevel = 1;
  89. while (cur.length > 0 && iLevel < 6) {
  90. result.push(...cur);
  91. cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
  92. iLevel += 1;
  93. }
  94. return result;
  95. }
  96. _checkFixed(data) {
  97. if (data.is_fixed) throw '固定分类,不可编辑';
  98. }
  99. async getNewName(spid, name = '新增文件类别') {
  100. const data = await this.db.query(`SELECT * FROM ${this.tableName} WHERE spid = '${spid}' AND name LIKE '${name}%'`);
  101. if (data.length === 0) return name;
  102. const _ = this._;
  103. const names = data.map(x => { return _.toInteger(x.name.replace(name, '')) });
  104. const filterNames = names.filter(x => { return x > 0 });
  105. const max = filterNames.reduce((pre, cur) => { return Math.max(pre, cur); }, 0);
  106. return max >= 0 ? name + (max + 1) : name;
  107. }
  108. async add(data) {
  109. const parent = await this.getDataById(data.tree_pid);
  110. if (!parent) throw '添加数据结构错误';
  111. if (parent.file_count > 0) throw `分类【${parent.name}】下存在文件,不可添加子分类`;
  112. const sibling = await this.getAllDataByCondition({ where: { tree_pid: parent.id }, orders: [['tree_order', 'asc']]});
  113. const preChild = data.tree_pre_id ? sibling.find(x => { return x.id === data.tree_pre_id; }) : null;
  114. const conn = await this.db.beginTransaction();
  115. try {
  116. // 获取当前用户信息
  117. const sessionUser = this.ctx.session.sessionUser;
  118. // 获取当前项目信息
  119. const sessionProject = this.ctx.session.sessionProject;
  120. const tree_order = preChild ? preChild.tree_order + 1 : (sibling.length > 0 ? sibling[sibling.length - 1].tree_order + 1 : 1);
  121. const name = await this.getNewName(parent.spid);
  122. const insertData = {
  123. id: this.uuid.v4(), spid: parent.spid, add_user_id: sessionUser.accountId,
  124. tree_pid: parent.id, tree_level: parent.tree_level + 1, tree_order,
  125. name, filing_type: parent.filing_type,
  126. };
  127. const operate = await conn.insert(this.tableName, insertData);
  128. if (operate.affectedRows === 0) throw '新增文件夹失败';
  129. const updateData = [];
  130. if (preChild) {
  131. sibling.forEach(x => {
  132. if (x.tree_order >= tree_order) updateData.push({ id: x.id, tree_order: x.tree_order + 1 });
  133. });
  134. }
  135. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  136. await conn.commit();
  137. return { create: [insertData], update: updateData };
  138. } catch (error) {
  139. await conn.rollback();
  140. throw error;
  141. }
  142. }
  143. async save(data) {
  144. const filing = await this.getDataById(data.id);
  145. this._checkFixed(filing);
  146. const result = await this.db.update(this.tableName, data);
  147. if (result.affectedRows > 0) {
  148. return data;
  149. } else {
  150. throw '更新数据失败';
  151. }
  152. }
  153. async del(data) {
  154. const filing = await this.getDataById(data.id);
  155. this._checkFixed(filing);
  156. const posterity = await this.getPosterityData(data.id);
  157. const delData = posterity.map(x => {return { id: x.id, is_deleted: 1 }; });
  158. delData.push({ id: data.id, is_deleted: 1});
  159. const sibling = await this.getAllDataByCondition({ where: { tree_pid: filing.tree_pid } });
  160. const updateData = [];
  161. sibling.forEach(x => {
  162. if (x.tree_order > filing.tree_order) updateData.push({ id: x.id, tree_order: x.tree_order - 1});
  163. });
  164. const conn = await this.db.beginTransaction();
  165. try {
  166. await conn.updateRows(this.tableName, delData);
  167. if (updateData.length > 0) conn.updateRows(this.tableName, updateData);
  168. await conn.commit();
  169. return { delete: delData.map(x => { return x.id }), update: updateData };
  170. } catch(err) {
  171. await conn.rollback();
  172. throw err;
  173. }
  174. }
  175. async move(data) {
  176. const filing = await this.getDataById(data.id);
  177. if (!filing) throw '移动的分类不存在,请刷新页面后重试';
  178. const parent = await this.getDataById(data.tree_pid);
  179. if (!parent) throw '移动后的分类不存在,请刷新页面后重试';
  180. const sibling = await this.getAllDataByCondition({ where: { tree_pid: data.tree_pid, is_deleted: 0 } });
  181. const posterity = await this.getPosterityData(filing.id);
  182. const updateData = { id: filing.id, tree_order: data.tree_order, tree_pid: data.tree_pid, tree_level: parent.tree_level + 1 };
  183. const posterityUpdateData = posterity.map(x => {
  184. return { id: x.id, tree_level: parent.tree_level + 1 - filing.tree_level + x.tree_level };
  185. });
  186. const siblingUpdateData = [];
  187. if (data.tree_pid === filing.tree_pid) {
  188. if (data.tree_order < filing.tree_order) {
  189. sibling.forEach(x => {
  190. if (x.id === filing.id) return;
  191. if (x.tree_order < data.tree_order) return;
  192. if (x.tree_order > filing.tree_order) return;
  193. siblingUpdateData.push({id: x.id, tree_order: x.tree_order + 1});
  194. });
  195. } else {
  196. sibling.forEach(x => {
  197. if (x.id === filing.id) return;
  198. if (x.tree_order < filing.tree_order) return;
  199. if (x.tree_order > data.tree_order) return;
  200. siblingUpdateData.push({id: x.id, tree_order: x.tree_order - 1});
  201. });
  202. }
  203. } else {
  204. const orgSibling = await this.getAllDataByCondition({ where: { tree_pid: filing.tree_pid, is_deleted: 0 } });
  205. orgSibling.forEach(x => {
  206. if (x.id === filing.id) return;
  207. if (x.tree_order < filing.tree_order) return;
  208. siblingUpdateData.push({id: x.id, tree_order: x.tree_order - 1});
  209. });
  210. sibling.forEach(x => {
  211. if (x.id === filing.id) return;
  212. if (x.tree_order < data.tree_order) return;
  213. siblingUpdateData.push({id: x.id, tree_order: x.tree_order + 1});
  214. })
  215. }
  216. const conn = await this.db.beginTransaction();
  217. try {
  218. await conn.update(this.tableName, updateData);
  219. if (posterityUpdateData.length > 0) await conn.updateRows(this.tableName, posterityUpdateData);
  220. if (siblingUpdateData.length > 0) await conn.updateRows(this.tableName, siblingUpdateData);
  221. await conn.commit();
  222. } catch (err) {
  223. await conn.rollback();
  224. throw err;
  225. }
  226. return { update: [updateData, ...posterityUpdateData, ...siblingUpdateData] };
  227. }
  228. async sumFileCount(spid) {
  229. const result = await this.db.queryOne(`SELECT SUM(file_count) AS file_count FROM ${this.tableName} WHERE spid = '${spid}' and is_deleted = 0`);
  230. return result.file_count;
  231. }
  232. }
  233. return Filing;
  234. };