filing.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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,
  89. create_uid: this.ctx.session.sessionUser.accountId, is_fixed: f.is_fixed,
  90. filing_type: f.filing_type, name: f.name, tips: f.tips, upload_tips: f.upload_tips, file_company: f.file_company,
  91. };
  92. insertData.push(newData);
  93. }
  94. if (transaction) {
  95. await transaction.insert(this.tableName, insertData);
  96. } else {
  97. await this.db.insert(this.tableName, insertData);
  98. }
  99. }
  100. _buildInitializedFiling(spid, sourceData, operatorUid) {
  101. const source = (sourceData || []).slice().sort((x, y) => {
  102. const levelDiff = Number(x.tree_level) - Number(y.tree_level);
  103. return levelDiff || Number(x.tree_order) - Number(y.tree_order);
  104. });
  105. const sourceNodeMap = {};
  106. const insertData = [];
  107. for (const node of source) {
  108. const sourceId = String(node.id || '');
  109. if (!sourceId || sourceNodeMap[sourceId]) throw '来源项目目录数据错误';
  110. const sourceParentId = String(node.tree_pid);
  111. const isRoot = sourceParentId === rootId;
  112. const parent = isRoot ? null : sourceNodeMap[sourceParentId];
  113. if (!isRoot && !parent) throw '来源项目目录结构不完整';
  114. const newNode = {
  115. id: this.uuid.v4(),
  116. spid,
  117. tree_pid: parent ? parent.id : rootId,
  118. tree_level: parent ? Number(parent.tree_level) + 1 : 1,
  119. tree_order: Number(node.tree_order),
  120. name: String(node.name || ''),
  121. filing_type: Number(node.filing_type),
  122. add_user_id: Number(operatorUid),
  123. create_uid: Number(operatorUid),
  124. is_fixed: Number(node.is_fixed) === 1 ? 1 : 0,
  125. is_deleted: 0,
  126. file_count: 0,
  127. tips: node.tips || '',
  128. upload_tips: node.upload_tips || '',
  129. file_company: node.file_company || '',
  130. is_rela: Number(node.is_rela) === 1 ? 1 : 0,
  131. };
  132. if (!newNode.name || !Number.isInteger(newNode.filing_type) || newNode.filing_type <= 0 ||
  133. !Number.isInteger(newNode.tree_order) || newNode.tree_order <= 0) {
  134. throw '来源项目目录数据错误';
  135. }
  136. sourceNodeMap[sourceId] = newNode;
  137. insertData.push(newNode);
  138. }
  139. return insertData;
  140. }
  141. /**
  142. * 首次初始化项目资料目录。
  143. *
  144. * 空白模式创建一个可继续编辑的根目录;模板和项目模式只复制目录字段,
  145. * 不复制文件与目录授权用户配置。
  146. *
  147. * @param {String} spid 目标子项目ID
  148. * @param {String} initType 初始化方式(blank/template/project)
  149. * @param {Array} sourceData 来源模板或项目的有效目录
  150. * @param {Number} operatorUid 初始化用户ID
  151. * @param {Object|null} templateData 选用的系统模板
  152. * @param {Object|null} transaction 模板校验与初始化共用的事务
  153. * @return {Object} 初始化结果
  154. */
  155. async initializeDirectory(spid, initType, sourceData, operatorUid, templateData = null, transaction = null) {
  156. let directorySource = sourceData || [];
  157. if (initType === 'blank') {
  158. directorySource = [{
  159. id: 'blank-root', tree_pid: rootId, tree_level: 1, tree_order: 1,
  160. name: '新建文件夹', filing_type: maxFilingType + 1, is_fixed: 1,
  161. tips: '', upload_tips: '', file_company: '', is_rela: 0,
  162. }];
  163. } else if (initType !== 'project' && initType !== 'template') {
  164. throw '初始化方式错误';
  165. }
  166. if (initType === 'template' && (!templateData || !templateData.id)) throw '系统模板数据错误';
  167. if (directorySource.length === 0) throw '来源数据没有可复制的资料目录';
  168. const insertData = this._buildInitializedFiling(spid, directorySource, operatorUid);
  169. const conn = transaction || await this.db.beginTransaction();
  170. try {
  171. await conn.query('SELECT id FROM ?? WHERE id = ? FOR UPDATE', [
  172. this.ctx.service.subProject.tableName, spid,
  173. ]);
  174. const activeCount = await conn.count(this.tableName, { spid, is_deleted: 0 });
  175. if (activeCount > 0) throw '当前项目已经存在资料目录,无需重复初始化';
  176. await conn.insert(this.tableName, insertData);
  177. if (initType === 'template') {
  178. await conn.update(this.ctx.service.subProject.tableName, {
  179. id: spid,
  180. filing_template_id: templateData.id,
  181. filing_template_name: templateData.name,
  182. });
  183. }
  184. if (!transaction) await conn.commit();
  185. return { count: insertData.length };
  186. } catch (err) {
  187. if (!transaction) await conn.rollback();
  188. throw err;
  189. }
  190. }
  191. _filterValidFiling(filing, filingType) {
  192. const validFiling = filing.filter(x => { return filingType.indexOf(x.filing_type) > -1;});
  193. const checkParent = function(child) {
  194. let parent = validFiling.find(x => { return x.id === child.tree_pid; });
  195. if (!parent) {
  196. parent = filing.find(x => { return x.id === child.tree_pid; });
  197. validFiling.push(parent);
  198. }
  199. if (parent.tree_level > 1) checkParent(parent);
  200. };
  201. for (const vf of validFiling) {
  202. if (vf.tree_level > 1 && vf.is_fixed) checkParent(vf);
  203. }
  204. return validFiling;
  205. }
  206. async getValidFiling(spid, filingType) {
  207. if (!filingType || filingType.length === 0) return [];
  208. const result = await this.getAllDataByCondition({ where: { spid, is_deleted: 0 } });
  209. if (filingType === 'all') return result;
  210. return this._filterValidFiling(result, filingType);
  211. }
  212. async getPosterityData(id){
  213. const result = [];
  214. let cur = await this.getAllDataByCondition({ where: { tree_pid: id } });
  215. let iLevel = 1;
  216. while (cur.length > 0 && iLevel < 6) {
  217. result.push(...cur);
  218. cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
  219. iLevel += 1;
  220. }
  221. return result;
  222. }
  223. _checkFixed(data) {
  224. if (data.is_fixed) throw '固定分类,不可编辑';
  225. }
  226. async getNewName(spid, name = '新增文件类别') {
  227. const data = await this.db.query(`SELECT * FROM ${this.tableName} WHERE spid = '${spid}' AND name LIKE '${name}%'`);
  228. if (data.length === 0) return name;
  229. const _ = this._;
  230. const names = data.map(x => { return _.toInteger(x.name.replace(name, '')) });
  231. const filterNames = names.filter(x => { return x > 0 });
  232. const max = filterNames.reduce((pre, cur) => { return Math.max(pre, cur); }, 0);
  233. return max >= 0 ? name + (max + 1) : name;
  234. }
  235. async getNewFilingType(spid) {
  236. const max = await this.db.queryOne(`SELECT filing_type FROM ${this.tableName} WHERE spid = '${spid}' ORDER BY filing_type DESC`);
  237. return max && max.filing_type ? max.filing_type + 1 : maxFilingType + 1;
  238. }
  239. async add(data) {
  240. const parent = await this.getDataById(data.tree_pid);
  241. // 允许管理员添加顶层
  242. // if (!parent) throw '添加数据结构错误';
  243. if (parent && parent.file_count > 0) throw `分类【${parent.name}】下存在文件,不可添加子分类`;
  244. const sibling = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, tree_pid: parent ? parent.id : rootId, is_deleted: 0 }, orders: [['tree_order', 'asc']]});
  245. const preChild = data.tree_pre_id ? sibling.find(x => { return x.id === data.tree_pre_id; }) : null;
  246. const filing_type = parent ? parent.filing_type : await this.getNewFilingType(this.ctx.subProject.id);
  247. const conn = await this.db.beginTransaction();
  248. try {
  249. // 获取当前用户信息
  250. const sessionUser = this.ctx.session.sessionUser;
  251. // 获取当前项目信息
  252. const sessionProject = this.ctx.session.sessionProject;
  253. const tree_order = preChild ? preChild.tree_order + 1 : (sibling.length > 0 ? sibling[sibling.length - 1].tree_order + 1 : 1);
  254. const name = data.name === undefined ? await this.getNewName(this.ctx.subProject.id) : data.name;
  255. const insertData = {
  256. id: this.uuid.v4(), spid: this.ctx.subProject.id, add_user_id: sessionUser.accountId,
  257. create_uid: sessionUser.accountId,
  258. tree_pid: parent ? parent.id : rootId, tree_level: parent ? parent.tree_level + 1 : 1, tree_order,
  259. name, filing_type: filing_type, is_fixed: parent ? 0 : 1, file_count: 0, is_deleted: 0
  260. };
  261. const operate = await conn.insert(this.tableName, insertData);
  262. if (operate.affectedRows === 0) throw '新增文件夹失败';
  263. const updateData = [];
  264. if (preChild) {
  265. sibling.forEach(x => {
  266. if (x.tree_order >= tree_order) updateData.push({ id: x.id, tree_order: x.tree_order + 1 });
  267. });
  268. }
  269. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  270. await conn.commit();
  271. return { create: [insertData], update: updateData };
  272. } catch (error) {
  273. await conn.rollback();
  274. throw error;
  275. }
  276. }
  277. async save(data) {
  278. const filing = await this.getDataById(data.id);
  279. // this._checkFixed(filing);
  280. const result = await this.db.update(this.tableName, data);
  281. if (result.affectedRows > 0) {
  282. return data;
  283. } else {
  284. throw '更新数据失败';
  285. }
  286. }
  287. async del(data) {
  288. const filing = await this.getDataById(data.id);
  289. // this._checkFixed(filing);
  290. const posterity = await this.getPosterityData(data.id);
  291. const delData = posterity.map(x => {return { id: x.id, is_deleted: 1 }; });
  292. delData.push({ id: data.id, is_deleted: 1});
  293. const sibling = await this.getAllDataByCondition({ where: { tree_pid: filing.tree_pid, spid: delData.spid, is_deleted: 0 } });
  294. const updateData = [];
  295. sibling.forEach(x => {
  296. if (x.tree_order > filing.tree_order) updateData.push({ id: x.id, tree_order: x.tree_order - 1});
  297. });
  298. const conn = await this.db.beginTransaction();
  299. try {
  300. await conn.updateRows(this.tableName, delData);
  301. if (updateData.length > 0) conn.updateRows(this.tableName, updateData);
  302. await conn.update(this.ctx.service.file.tableName, { is_deleted: 1}, { where: {filing_id: delData.map(x => { return x.id; })} });
  303. await this.ctx.service.subProjectFilingPermission.removeFilingNodes(
  304. filing.spid, delData.map(x => x.id), conn
  305. );
  306. await conn.commit();
  307. return { delete: delData.map(x => { return x.id }), update: updateData };
  308. } catch(err) {
  309. await conn.rollback();
  310. throw err;
  311. }
  312. }
  313. async move(data) {
  314. const filing = await this.getDataById(data.id);
  315. if (!filing || filing.is_deleted) throw '移动的分类不存在,请刷新页面后重试';
  316. if (this.ctx.subProject && filing.spid !== this.ctx.subProject.id) throw '移动的分类不属于当前项目';
  317. if (Number(filing.is_fixed)) throw '固定分类不可移动';
  318. if (data.tree_pid === undefined || data.tree_pid === null || data.tree_pid === '') throw '请选择移动后的目录';
  319. const treePid = String(data.tree_pid);
  320. const treeOrder = Number(data.tree_order);
  321. if (!Number.isInteger(treeOrder) || treeOrder < 0) throw '移动后的目录顺序错误';
  322. const posterity = await this.getPosterityData(filing.id);
  323. const filingWithFiles = [filing, ...posterity].find(x => !x.is_deleted && Number(x.file_count) > 0);
  324. if (filingWithFiles) throw `分类【${filingWithFiles.name}】下存在文件,不可移动目录`;
  325. if (treePid === String(filing.id) || posterity.find(x => String(x.id) === treePid)) {
  326. throw '不能将目录移动到自身或其子目录下';
  327. }
  328. const parent = treePid === rootId ? null : await this.getDataById(treePid);
  329. if (treePid !== rootId && (!parent || parent.is_deleted || parent.spid !== filing.spid)) {
  330. throw '移动后的分类不存在,请刷新页面后重试';
  331. }
  332. if (parent && String(filing.tree_pid) !== treePid && Number(parent.file_count) > 0) {
  333. throw `分类【${parent.name}】下存在文件,不可添加子分类`;
  334. }
  335. const sibling = await this.getAllDataByCondition({ where: { spid: filing.spid, tree_pid: treePid, is_deleted: 0 } });
  336. const updateData = { id: filing.id, tree_order: treeOrder, tree_pid: treePid, tree_level: (parent ? parent.tree_level : 0) + 1 };
  337. if (data.name !== undefined) {
  338. const name = String(data.name).trim();
  339. if (!name) throw '目录名称不能为空';
  340. if (name.length > 100) throw '目录名称不能超过100个字符';
  341. updateData.name = name;
  342. }
  343. if (data.is_fixed !== undefined) {
  344. const isFixed = Number(data.is_fixed);
  345. if (isFixed !== 0 && isFixed !== 1) throw '固定目录状态错误';
  346. updateData.is_fixed = isFixed;
  347. }
  348. const posterityUpdateData = posterity.map(x => {
  349. return { id: x.id, tree_level: (parent ? parent.tree_level : 0) + 1 - filing.tree_level + x.tree_level };
  350. });
  351. const siblingUpdateData = [];
  352. if (treePid === String(filing.tree_pid)) {
  353. if (treeOrder < filing.tree_order) {
  354. sibling.forEach(x => {
  355. if (x.id === filing.id) return;
  356. if (x.tree_order < treeOrder) return;
  357. if (x.tree_order > filing.tree_order) return;
  358. siblingUpdateData.push({ id: x.id, tree_order: x.tree_order + 1 });
  359. });
  360. } else {
  361. sibling.forEach(x => {
  362. if (x.id === filing.id) return;
  363. if (x.tree_order < filing.tree_order) return;
  364. if (x.tree_order > treeOrder) return;
  365. siblingUpdateData.push({ id: x.id, tree_order: x.tree_order - 1 });
  366. });
  367. }
  368. } else {
  369. const orgSibling = await this.getAllDataByCondition({ where: { spid: filing.spid, tree_pid: filing.tree_pid, is_deleted: 0 } });
  370. orgSibling.forEach(x => {
  371. if (x.id === filing.id) return;
  372. if (x.tree_order < filing.tree_order) return;
  373. siblingUpdateData.push({ id: x.id, tree_order: x.tree_order - 1 });
  374. });
  375. sibling.forEach(x => {
  376. if (x.id === filing.id) return;
  377. if (x.tree_order < treeOrder) return;
  378. siblingUpdateData.push({ id: x.id, tree_order: x.tree_order + 1 });
  379. });
  380. }
  381. const conn = await this.db.beginTransaction();
  382. try {
  383. await conn.update(this.tableName, updateData);
  384. if (posterityUpdateData.length > 0) await conn.updateRows(this.tableName, posterityUpdateData);
  385. if (siblingUpdateData.length > 0) await conn.updateRows(this.tableName, siblingUpdateData);
  386. await conn.commit();
  387. } catch (err) {
  388. await conn.rollback();
  389. throw err;
  390. }
  391. return { update: [updateData, ...posterityUpdateData, ...siblingUpdateData] };
  392. }
  393. async editDirectory(data) {
  394. if (!data || !data.id) throw '请选择需要编辑的目录';
  395. const filing = await this.getDataById(data.id);
  396. if (!filing || filing.is_deleted) throw '编辑的分类不存在,请刷新页面后重试';
  397. if (this.ctx.subProject && filing.spid !== this.ctx.subProject.id) throw '编辑的分类不属于当前项目';
  398. const name = String(data.name || '').trim();
  399. if (!name) throw '目录名称不能为空';
  400. if (name.length > 100) throw '目录名称不能超过100个字符';
  401. if (data.tree_pid === undefined && data.is_fixed === undefined) {
  402. if (typeof data.name !== 'string') throw '目录名称格式错误';
  403. const result = await this.db.update(this.tableName, { name }, {
  404. where: { id: filing.id, spid: filing.spid, is_deleted: 0 },
  405. });
  406. if (result.affectedRows === 0) throw '更新目录失败';
  407. return { update: [{ id: filing.id, name }] };
  408. }
  409. const isFixed = Number(data.is_fixed);
  410. if (isFixed !== 0 && isFixed !== 1) throw '固定目录状态错误';
  411. if (data.tree_pid === undefined || data.tree_pid === null || data.tree_pid === '') throw '请选择目录';
  412. const treePid = String(data.tree_pid);
  413. if (treePid === String(filing.tree_pid)) {
  414. const updateData = { id: filing.id, name, is_fixed: isFixed };
  415. const result = await this.db.update(this.tableName, updateData);
  416. if (result.affectedRows === 0) throw '更新目录失败';
  417. return { update: [updateData] };
  418. }
  419. const parent = treePid === rootId ? null : await this.getDataById(treePid);
  420. if (treePid !== rootId && (!parent || parent.is_deleted || parent.spid !== filing.spid)) {
  421. throw '移动后的分类不存在,请刷新页面后重试';
  422. }
  423. const targetChildren = await this.getAllDataByCondition({
  424. where: { spid: filing.spid, tree_pid: treePid, is_deleted: 0 },
  425. orders: [['tree_order', 'asc']],
  426. });
  427. const lastChild = targetChildren.length > 0 ? targetChildren[targetChildren.length - 1] : null;
  428. const treeOrder = lastChild ? Number(lastChild.tree_order) + 1 : 1;
  429. return await this.move({
  430. id: filing.id, tree_pid: treePid, tree_order: treeOrder,
  431. name, is_fixed: isFixed,
  432. });
  433. }
  434. async multiUpdate(spid, data) {
  435. if (!data || data.length === 0) throw '提交数据格式错误';
  436. const sourceData = await this.getAllDataByCondition({ where: { spid } });
  437. const validFields = ['id', 'is_fixed', 'name', 'filing_type', 'tree_order', 'tips', 'upload_tips', 'file_company'];
  438. const updateData = [];
  439. for (const d of data) {
  440. if (!d.id) throw '提交数据格式错误';
  441. const sd = sourceData.find(x => { return x.id === d.id; });
  442. if (!sd) throw '提交数据格式错误';
  443. const nd = {};
  444. for (const prop in d) {
  445. if (validFields.indexOf(prop) < 0) continue;
  446. nd[prop] = d[prop];
  447. }
  448. updateData.push(nd);
  449. }
  450. await this.db.updateRows(this.tableName, updateData);
  451. return await this.getAllDataByCondition({ where: { spid } });
  452. }
  453. async sumFileCount(spid) {
  454. const result = await this.db.queryOne(`SELECT SUM(file_count) AS file_count FROM ${this.tableName} WHERE spid = '${spid}' and is_deleted = 0`);
  455. return result.file_count;
  456. }
  457. async saveNames(spid, items) {
  458. if (!Array.isArray(items) || items.length === 0) throw '请选择需要替换名称的分类';
  459. const ids = new Set();
  460. const updateData = items.map(item => {
  461. if (!item || typeof item.id !== 'string' || !item.id || ids.has(item.id)) throw '分类数据格式错误';
  462. ids.add(item.id);
  463. if (typeof item.name !== 'string' || typeof item.old_name !== 'string') throw '分类名称格式错误';
  464. const name = item.name.trim();
  465. if (!name) throw '目录名称不能为空';
  466. if (name.length > 100) throw '目录名称不能超过100个字符';
  467. return { id: item.id, name, old_name: item.old_name };
  468. });
  469. const conn = await this.db.beginTransaction();
  470. try {
  471. const result = [];
  472. // 按 ID 更新,保持多批次重叠操作的加锁顺序一致。
  473. const sortedData = [...updateData].sort((a, b) => a.id.localeCompare(b.id));
  474. for (const item of sortedData) {
  475. const filing = await conn.get(this.tableName, { id: item.id, spid, is_deleted: 0 });
  476. if (!filing) throw '部分分类不存在,请刷新页面后重新粘贴';
  477. if (Number(filing.is_fixed)) throw '固定分类不可修改名称';
  478. if (filing.name !== item.old_name) throw '分类名称已发生变化,请刷新页面后重新粘贴';
  479. if (item.name === item.old_name) continue;
  480. const operation = await conn.update(this.tableName, { name: item.name }, {
  481. where: { id: item.id, spid, is_deleted: 0, is_fixed: 0, name: item.old_name },
  482. });
  483. if (operation.affectedRows !== 1) throw '分类信息已发生变化,请刷新页面后重新粘贴';
  484. result.push({ id: item.id, name: item.name });
  485. }
  486. await conn.commit();
  487. return { update: result };
  488. } catch (err) {
  489. await conn.rollback();
  490. throw err;
  491. }
  492. }
  493. async getDirectoryFilesForUpdate(spid, filingIds, transaction) {
  494. const sql = 'SELECT id FROM ?? WHERE spid = ? AND filing_id IN (?) AND is_deleted = 0 LIMIT 1 FOR UPDATE';
  495. const sqlParam = [this.ctx.service.file.tableName, spid, filingIds];
  496. return await transaction.query(sql, sqlParam);
  497. }
  498. async deleteEmptyDirectories(spid, filingIds, updateData, transaction) {
  499. await transaction.update(this.tableName, { is_deleted: 1 }, {
  500. where: { spid, id: filingIds, is_deleted: 0 },
  501. });
  502. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  503. await this.ctx.service.subProjectFilingPermission.removeFilingNodes(spid, filingIds, transaction);
  504. }
  505. }
  506. return Filing;
  507. };