filing.js 13 KB

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