filing_template.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. getDefaultTemplateData(templateId) {
  38. const result = [];
  39. for (const [i, f] of filingType.entries()) {
  40. result.push({
  41. id: this.uuid.v4(), temp_id: templateId, 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. return result;
  47. }
  48. getCopyTemplateData(templateId, templateData) {
  49. templateData.sort((x, y) => { return x.tree_level - y.tree_level; });
  50. const insertData = [];
  51. for (const d of templateData) {
  52. if (d.id === undefined || d.tree_pid === undefined || d.tree_order === undefined || d.tree_level === undefined || d.name === undefined || d.is_fixed === undefined || d.filing_type === undefined) {
  53. throw '数据格式有误';
  54. }
  55. const parent = insertData.find(x => { return x.org_id === d.tree_pid });
  56. insertData.push({
  57. id: this.uuid.v4(), temp_id: templateId, add_user_id: this.ctx.session.sessionUser.accountId,
  58. tree_pid: parent ? parent.id : rootId, tree_level: parent ? parent.tree_level + 1 : 1, tree_order: d.tree_order,
  59. name: d.name, tips: d.tips, filing_type: d.filing_type, is_fixed: parent ? d.is_fixed : 1, org_id: d.id,
  60. });
  61. }
  62. insertData.forEach(x => { delete x.org_id; });
  63. return insertData;
  64. }
  65. async initTemplate(transaction, template, templateData) {
  66. const insertData = templateData ? this.getCopyTemplateData(template.id, templateData) : this.getDefaultTemplateData(template.id);
  67. if (transaction) {
  68. await transaction.insert(this.tableName, insertData);
  69. } else {
  70. await this.db.insert(this.tableName, insertData);
  71. }
  72. }
  73. async getData(templateId) {
  74. return await this.getAllDataByCondition({
  75. where: { temp_id: templateId }
  76. })
  77. }
  78. async getPosterityData(templateId, id){
  79. const result = [];
  80. let cur = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: id } });
  81. let iLevel = 1;
  82. while (cur.length > 0 && iLevel < 6) {
  83. result.push(...cur);
  84. cur = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: cur.map(x => { return x.id })} });
  85. iLevel += 1;
  86. }
  87. return result;
  88. }
  89. async getNewName(templateId, name = '新增文件类别') {
  90. const data = await this.db.query(`SELECT * FROM ${this.tableName} WHERE temp_id = '${templateId}' AND name LIKE '${name}%'`);
  91. if (data.length === 0) return name;
  92. const _ = this._;
  93. const names = data.map(x => { return _.toInteger(x.name.replace(name, '')) });
  94. const filterNames = names.filter(x => { return x > 0 });
  95. const max = filterNames.reduce((pre, cur) => { return Math.max(pre, cur); }, 0);
  96. return max >= 0 ? name + (max + 1) : name;
  97. }
  98. async getNewFilingType(templateId) {
  99. const max = await this.db.queryOne(`SELECT filing_type FROM ${this.tableName} WHERE temp_id = '${templateId}' ORDER BY filing_type DESC`);
  100. return max && max.filing_type ? max.filing_type + 1 : maxFilingType + 1;
  101. }
  102. async add(templateId, data) {
  103. const parent = await this.getDataById(data.tree_pid);
  104. const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: parent ? parent.id : rootId }, orders: [['tree_order', 'asc']]});
  105. const preChild = data.tree_pre_id ? sibling.find(x => { return x.id === data.tree_pre_id; }) : null;
  106. const filing_type = parent ? parent.filing_type : await this.getNewFilingType(templateId);
  107. const conn = await this.db.beginTransaction();
  108. try {
  109. // 获取当前用户信息
  110. const sessionUser = this.ctx.session.sessionUser;
  111. // 获取当前项目信息
  112. const sessionProject = this.ctx.session.sessionProject;
  113. const tree_order = preChild ? preChild.tree_order + 1 : (sibling.length > 0 ? sibling[sibling.length - 1].tree_order + 1 : 1);
  114. const name = await this.getNewName(templateId);
  115. const insertData = {
  116. id: this.uuid.v4(), temp_id: templateId, add_user_id: sessionUser.accountId,
  117. tree_pid: parent ? parent.id : rootId, tree_level: parent ? parent.tree_level + 1 : 1, tree_order,
  118. name, filing_type
  119. };
  120. insertData.is_fixed = insertData.tree_level === 1 ? 1 : (preChild ? preChild.is_fixed : 0);
  121. const operate = await conn.insert(this.tableName, insertData);
  122. if (operate.affectedRows === 0) throw '新增文件夹失败';
  123. const updateData = [];
  124. if (preChild) {
  125. sibling.forEach(x => {
  126. if (x.tree_order >= tree_order) updateData.push({ id: x.id, tree_order: x.tree_order + 1 });
  127. });
  128. }
  129. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  130. await conn.commit();
  131. return { create: [insertData], update: updateData };
  132. } catch (error) {
  133. await conn.rollback();
  134. throw error;
  135. }
  136. }
  137. async save(data) {
  138. const filing = await this.getDataById(data.id);
  139. if (!filing) throw '分类不存在,请刷新页面后重试';
  140. const result = await this.db.update(this.tableName, { id: data.id, name: data.name });
  141. if (result.affectedRows > 0) {
  142. return data;
  143. } else {
  144. throw '更新数据失败';
  145. }
  146. }
  147. async del(templateId, data) {
  148. const filing = await this.getDataById(data.id);
  149. if (!filing) throw '分类不存在,请刷新页面后重试';
  150. const posterity = await this.getPosterityData(templateId, data.id);
  151. const delData = posterity.map(x => {return x.id; });
  152. delData.push(data.id);
  153. const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: filing.tree_pid } });
  154. const updateData = [];
  155. sibling.forEach(x => {
  156. if (x.tree_order > filing.tree_order) updateData.push({ id: x.id, tree_order: x.tree_order - 1});
  157. });
  158. const conn = await this.db.beginTransaction();
  159. try {
  160. await conn.delete(this.tableName, { id: delData });
  161. if (updateData.length > 0) conn.updateRows(this.tableName, updateData);
  162. await conn.commit();
  163. return { delete: delData, update: updateData };
  164. } catch(err) {
  165. await conn.rollback();
  166. throw err;
  167. }
  168. }
  169. async move(templateId, data) {
  170. const filing = await this.getDataById(data.id);
  171. if (!filing) throw '移动的分类不存在,请刷新页面后重试';
  172. const parent = await this.getDataById(data.tree_pid);
  173. const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: data.tree_pid } });
  174. const filing_type = parent ? parent.filing_type : await this.getNewFilingType(templateId);
  175. const posterity = await this.getPosterityData(templateId, filing.id);
  176. const result = [];
  177. 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 };
  178. result.push(updateData);
  179. const posterityUpdateData = posterity.map(x => { return { id: x.id, filing_type, tree_level: updateData.tree_level - filing.tree_level + x.tree_level }});
  180. result.push(posterityUpdateData);
  181. const siblingUpdateData = [];
  182. if (data.tree_pid === filing.tree_pid) {
  183. if (data.tree_order < filing.tree_order) {
  184. sibling.forEach(x => {
  185. if (x.id === filing.id) return;
  186. if (x.tree_order < data.tree_order) return;
  187. if (x.tree_order > filing.tree_order) return;
  188. siblingUpdateData.push({id: x.id, tree_order: x.tree_order + 1});
  189. });
  190. } else {
  191. sibling.forEach(x => {
  192. if (x.id === filing.id) return;
  193. if (x.tree_order < filing.tree_order) return;
  194. if (x.tree_order > data.tree_order) return;
  195. siblingUpdateData.push({id: x.id, tree_order: x.tree_order - 1});
  196. });
  197. }
  198. } else {
  199. const orgSibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: filing.tree_pid } });
  200. orgSibling.forEach(x => {
  201. if (x.id === filing.id) return;
  202. if (x.tree_order < filing.tree_order) return;
  203. siblingUpdateData.push({id: x.id, tree_order: x.tree_order - 1});
  204. });
  205. sibling.forEach(x => {
  206. if (x.id === filing.id) return;
  207. if (x.tree_order < data.tree_order) return;
  208. siblingUpdateData.push({id: x.id, tree_order: x.tree_order + 1});
  209. })
  210. }
  211. result.push(...siblingUpdateData);
  212. const conn = await this.db.beginTransaction();
  213. try {
  214. await conn.update(this.tableName, updateData);
  215. if (posterityUpdateData.length > 0) await conn.updateRows(this.tableName, posterityUpdateData);
  216. if (siblingUpdateData.length > 0) await conn.updateRows(this.tableName, siblingUpdateData);
  217. await conn.commit();
  218. return { update: result };
  219. } catch(err) {
  220. await conn.rollback();
  221. throw err;
  222. }
  223. }
  224. async import(templateId, data) {
  225. if (!data || data.length === 0) throw '导入数据不存在';
  226. const insertData = [];
  227. for (const d of data) {
  228. if (d.id === undefined || d.tree_pid === undefined || d.tree_order === undefined || d.tree_level === undefined || d.name === undefined || d.is_fixed === undefined || d.filing_type === undefined) {
  229. throw '导入数据格式有误';
  230. }
  231. const parent = insertData.find(x => { return x.org_id === d.tree_pid });
  232. insertData.push({
  233. id: this.uuid.v4(), temp_id: templateId, add_user_id: this.ctx.session.sessionUser.accountId,
  234. tree_pid: parent ? parent.id : rootId, tree_level: parent ? parent.tree_level + 1 : 1, tree_order: d.tree_order,
  235. name: d.name, tips: d.tips, filing_type: d.filing_type, is_fixed: parent ? d.is_fixed : 1, org_id: d.id,
  236. });
  237. }
  238. insertData.forEach(x => { delete x.org_id; });
  239. const conn = await this.db.beginTransaction();
  240. try {
  241. await conn.delete(this.tableName, { temp_id: templateId });
  242. await conn.insert(this.tableName, insertData);
  243. await conn.commit();
  244. } catch(err) {
  245. await conn.rollback();
  246. throw '导入数据格式有误';
  247. }
  248. return await this.getData(templateId)
  249. }
  250. async multiUpdate(templateId, data) {
  251. if (!data || data.length === 0) throw '提交数据格式错误';
  252. const sourceData = await this.getData(templateId);
  253. const validFields = ['id', 'is_fixed', 'name', 'filing_type', 'tree_order', 'tips'];
  254. const updateData = [];
  255. for (const d of data) {
  256. if (!d.id) throw '提交数据格式错误';
  257. const sd = sourceData.find(x => { return x.id === d.id; });
  258. if (!sd) throw '提交数据格式错误';
  259. const nd = {};
  260. for (const prop in d) {
  261. if (validFields.indexOf(prop) < 0) continue;
  262. nd[prop] = d[prop];
  263. }
  264. updateData.push(nd);
  265. }
  266. await this.db.updateRows(this.tableName, updateData);
  267. return await this.getData(templateId);
  268. }
  269. }
  270. return FilingTemplate;
  271. };