filing.js 14 KB

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