filing.js 9.6 KB

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