filing_template.js 12 KB

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