base_tree_service.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. 'use strict';
  2. /**
  3. * 提供基础操作:
  4. * 1. 增删改查
  5. * 2. 粘贴整块
  6. * 3. 简易导入
  7. *
  8. * @author Mai
  9. * @date
  10. * @version
  11. */
  12. const Service = require('./base_service');
  13. // sql拼装器
  14. const SqlBuilder = require('../lib/sql_builder');
  15. class TreeService extends Service {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局context
  20. * @param {Object} setting - 树结构设置
  21. * e.g.: {
  22. * mid: 'tender_id', 分块id(例如tender_id, rid, list_id)
  23. * kid: 'ledger_id', 分块内的树结构id
  24. * pid: 'ledger_pid', 父节点id
  25. * order: 'order',
  26. * level: 'level',
  27. * fullPath: 'full_path',
  28. * isLeaf: 'is_leaf',
  29. * keyPre: 'revise_bills_maxLid:'
  30. * }
  31. * @return {void}
  32. */
  33. constructor(ctx, setting) {
  34. super(ctx);
  35. this.tableName = setting.tableName;
  36. this.setting = setting;
  37. // 以下字段仅可通过树结构操作改变,不可直接通过update方式从接口提交,发现时过滤
  38. this.readOnlyFields = ['id'];
  39. this.readOnlyFields.push(this.setting.mid);
  40. this.readOnlyFields.push(this.setting.kid);
  41. this.readOnlyFields.push(this.setting.pid);
  42. this.readOnlyFields.push(this.setting.order);
  43. this.readOnlyFields.push(this.setting.level);
  44. this.readOnlyFields.push(this.setting.fullPath);
  45. this.readOnlyFields.push(this.setting.isLeaf);
  46. }
  47. getCondition (condition) {
  48. const result = {};
  49. if (condition.mid) result[this.setting.mid] = condition.mid;
  50. if (condition.kid) result[this.setting.kid] = condition.kid;
  51. if (condition.pid) result[this.setting.pid] = condition.pid;
  52. if (condition[this.setting.order]) result[this.setting.order] = condition[this.setting.order];
  53. if (condition.level) result[this.setting.level] = condition.level;
  54. if (condition.fullPath) result[this.setting.fullPath] = condition.fullPath;
  55. if (condition.isLeaf) result[this.setting.isLeaf] = condition.isLeaf;
  56. return result;
  57. }
  58. /**
  59. * 获取 修订 清单数据
  60. * @param {Number} mid - masterId
  61. * @returns {Promise<void>}
  62. */
  63. async getData(mid) {
  64. return await this.db.select(this.tableName, {
  65. where: this.getCondition({mid: mid})
  66. });
  67. }
  68. /**
  69. * 获取节点数据
  70. * @param {Number} mid - masterId
  71. * @param {Number} id
  72. * @returns {Promise<void>}
  73. */
  74. async getDataByLid (mid, kid) {
  75. return await this.db.get(this.tableName, this.getCondition({
  76. mid: mid, kid: kid,
  77. }));
  78. }
  79. /**
  80. * 获取节点数据
  81. * @param id
  82. * @returns {Promise<Array>}
  83. */
  84. async getDataById(id) {
  85. if (id instanceof Array) {
  86. return await this.db.select(this.tableName, { where: {id: id} });
  87. } else {
  88. return await this.db.get(this.tableName, { id: id });
  89. }
  90. }
  91. /**
  92. * 获取最末的子节点
  93. * @param {Number} mid - masterId
  94. * @param {Number} pid - 父节点id
  95. * @return {Object}
  96. */
  97. async getLastChildData(mid, pid) {
  98. this.initSqlBuilder();
  99. this.sqlBuilder.setAndWhere(this.setting.mid, {
  100. value: mid,
  101. operate: '=',
  102. });
  103. this.sqlBuilder.setAndWhere(this.setting.pid, {
  104. value: pid,
  105. operate: '=',
  106. });
  107. this.sqlBuilder.orderBy = [['order', 'DESC']];
  108. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  109. const resultData = await this.db.queryOne(sql, sqlParam);
  110. return resultData;
  111. }
  112. /**
  113. * 根据 父节点id 和 节点排序order 获取数据
  114. *
  115. * @param {Number} mid - master id
  116. * @param {Number} pid - 父节点id
  117. * @param {Number|Array} order - 排序
  118. * @return {Object|Array} - 查询结果
  119. */
  120. async getDataByParentAndOrder(mid, pid, order) {
  121. const result = await this.db.select(this.tableName, {
  122. where: this.getCondition({mid: mid, pid: pid, order: order})
  123. });
  124. return order instanceof Array ? result : (result.length > 0 ? result[0] : null);
  125. }
  126. /**
  127. * 根据 父节点ID 和 节点排序order 获取全部后节点数据
  128. * @param {Number} mid - master id
  129. * @param {Number} pid - 父节点id
  130. * @param {Number} order - 排序
  131. * @return {Array}
  132. */
  133. async getNextsData(mid, pid, order) {
  134. this.initSqlBuilder();
  135. this.sqlBuilder.setAndWhere(this.setting.mid, {
  136. value: mid,
  137. operate: '=',
  138. });
  139. this.sqlBuilder.setAndWhere(this.setting.pid, {
  140. value: pid,
  141. operate: '=',
  142. });
  143. this.sqlBuilder.setAndWhere(this.setting.order, {
  144. value: order,
  145. operate: '>',
  146. });
  147. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  148. const data = await this.db.query(sql, sqlParam);
  149. return data;
  150. }
  151. /**
  152. * 获取最大节点id
  153. *
  154. * @param {Number} mid - master id
  155. * @return {Number}
  156. * @private
  157. */
  158. async _getMaxLid(mid) {
  159. const cacheKey = this.setting.keyPre + mid;
  160. let maxId = parseInt(await this.cache.get(cacheKey));
  161. if (!maxId) {
  162. const sql = 'SELECT Max(??) As max_id FROM ?? Where ' + this.setting.mid + ' = ?';
  163. const sqlParam = ['ledger_id', this.tableName, mid];
  164. const queryResult = await this.db.queryOne(sql, sqlParam);
  165. maxId = queryResult.max_id || 0;
  166. this.cache.set(cacheKey, maxId, 'EX', this.ctx.app.config.cacheTime);
  167. }
  168. return maxId;
  169. }
  170. /**
  171. * 缓存最大节点id
  172. *
  173. * @param {Number} mid - master id
  174. * @param {Number} maxId - 当前最大节点id
  175. * @returns {Promise<void>}
  176. * @private
  177. */
  178. _cacheMaxLid(mid, maxId) {
  179. this.cache.set(this.setting.keyPre + mid , maxId, 'EX', this.ctx.app.config.cacheTime);
  180. }
  181. /**
  182. * 更新order
  183. * @param {Number} mid - master id
  184. * @param {Number} pid - 父节点id
  185. * @param {Number} order - 开始更新的order
  186. * @param {Number} incre - 更新的增量
  187. * @returns {Promise<*>}
  188. * @private
  189. */
  190. async _updateChildrenOrder(mid, pid, order, incre = 1) {
  191. this.initSqlBuilder();
  192. this.sqlBuilder.setAndWhere(this.setting.mid, {
  193. value: mid,
  194. operate: '=',
  195. });
  196. this.sqlBuilder.setAndWhere(this.setting.order, {
  197. value: order,
  198. operate: '>=',
  199. });
  200. this.sqlBuilder.setAndWhere(this.setting.pid, {
  201. value: pid,
  202. operate: '=',
  203. });
  204. this.sqlBuilder.setUpdateData(this.setting.order, {
  205. value: Math.abs(incre),
  206. selfOperate: incre > 0 ? '+' : '-',
  207. });
  208. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  209. const data = await this.transaction.query(sql, sqlParam);
  210. return data;
  211. }
  212. /**
  213. * 新增数据(新增为selectData的后项,该方法不可单独使用)
  214. *
  215. * @param {Number} mid - master-id
  216. * @param {Object} select - 选中节点的数据
  217. * @param {Object} data - 新增节点的初始数据
  218. * @return {Object} - 新增结果
  219. * @private
  220. */
  221. async _addNodeData(mid, select, data) {
  222. if (!data) {
  223. data = {};
  224. }
  225. const maxId = await this._getMaxLid(mid);
  226. data.id = this.uuid.v4();
  227. data[this.setting.kid] = maxId + 1;
  228. data[this.setting.pid] = select[this.setting.pid];
  229. data[this.setting.mid] = mid;
  230. data[this.setting.level] = select[this.setting.level];
  231. data[this.setting.order] = select[this.setting.order] + 1;
  232. data[this.setting.fullPath] = select[this.setting.fullPath].replace('.' + select[this.setting.kid], '.' + data[this.setting.kid]);
  233. data[this.setting.isLeaf] = true;
  234. const result = await this.transaction.insert(this.tableName, data);
  235. this._cacheMaxLid(mid, maxId + 1);
  236. return result;
  237. }
  238. /**
  239. * 新增节点
  240. * @param {Number} mid - master id
  241. * @param {Number} kid - 清单节点id
  242. * @returns {Promise<void>}
  243. */
  244. async addNode(mid, kid, data) {
  245. if (!mid || !kid) return null;
  246. const select = await this.getDataByLid(mid, kid);
  247. if (!select) {
  248. throw '新增节点数据错误';
  249. }
  250. this.transaction = await this.db.beginTransaction();
  251. try {
  252. await this._updateChildrenOrder(mid, select[this.setting.pid], select[this.setting.order]+1);
  253. const newNode = await this._addNodeData(mid, select, data);
  254. if (newNode.affectedRows !== 1) {
  255. throw '新增节点数据额错误';
  256. }
  257. await this.transaction.commit();
  258. this.transaction = null;
  259. } catch (err) {
  260. await this.transaction.rollback();
  261. this.transaction = null;
  262. throw err;
  263. }
  264. const createData = await this.getDataByParentAndOrder(mid, select[this.setting.pid], [select[this.setting.order] + 1]);
  265. const updateData = await this.getNextsData(mid, select[this.setting.pid], select[this.setting.order] + 1);
  266. return {create: createData, update: updateData};
  267. }
  268. /**
  269. * 删除节点
  270. * @param {Number} mid - master id
  271. * @param {Object} deleteNode - 删除节点数据
  272. * @return {Promise<*>}
  273. * @private
  274. */
  275. async _deleteNodeData(mid, deleteNode) {
  276. this.initSqlBuilder();
  277. this.sqlBuilder.setAndWhere(this.setting.mid, {
  278. value: mid,
  279. operate: '=',
  280. });
  281. this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  282. value: this.db.escape(deleteNode[this.setting.fullPath] + '%'),
  283. operate: 'Like',
  284. });
  285. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'delete');
  286. const result = await this.transaction.query(sql, sqlParam);
  287. return result;
  288. }
  289. /**
  290. * 删除选中节点及其子节点
  291. *
  292. * @param {Number} mid - master id
  293. * @param {Number} kid - 选中节点id
  294. * @return {Array} - 被删除的数据
  295. */
  296. async deleteNode(mid, kid) {
  297. if ((mid <= 0) || (kid <= 0)) return [];
  298. const select = await this.getDataByNodeId(mid, kid);
  299. if (!select) throw '删除节点数据错误';
  300. const parent = await this.getDataByNodeId(mid, select[this.setting.pid]);
  301. // 获取将要被删除的数据
  302. const deleteData = await this.getDataByFullPath(mid, select[this.setting.fullPath] + '%');
  303. if (deleteData.length === 0) throw '删除节点数据错误';
  304. this.transaction = await this.db.beginTransaction();
  305. try {
  306. // 删除
  307. const operate = await this._deleteNodeData(mid, select);
  308. // 选中节点--父节点 只有一个子节点时,应升级is_leaf
  309. if (parent) {
  310. const count = await this.db.count(this.tableName, this.getCondition({mid: mid, pid: select[this.setting.pid]}));
  311. if (count === 1) {
  312. const updateParent = {id: parent.id };
  313. updateParent[this.setting.isLeaf] = true;
  314. await this.transaction.update(this.tableName, updateParent);
  315. }
  316. }
  317. // 选中节点--全部后节点 order--
  318. await this._updateSelectNextsOrder(select, -1);
  319. // 删除部位明细
  320. //await this.ctx.service.pos.deletePosData(this.transaction, tenderId, this._.map(deleteData, 'id'));
  321. await this.transaction.commit();
  322. } catch (err) {
  323. await this.transaction.rollback();
  324. throw err;
  325. }
  326. // 查询结果
  327. const updateData = await this.getNextsData(mid, select[this.setting.pid], select[this.setting.order] - 1);
  328. if (parent) {
  329. const updateData1 = await this.getDataByNodeId(mid, select[this.setting.pid]);
  330. if (updateData1[this.setting.isLeaf]) {
  331. updateData.push(updateData1);
  332. }
  333. }
  334. return { delete: deleteData, update: updateData };
  335. }
  336. /**
  337. * 上移节点
  338. *
  339. * @param {Number} mid - master id
  340. * @param {Number} kid - 选中节点id
  341. * @return {Array} - 发生改变的数据
  342. */
  343. async upMoveNode(mid, kid) {
  344. if (!mid || !kid) return null;
  345. const select = await this.getDataByLid(mid, kid);
  346. if (!select) {
  347. throw '上移节点数据错误';
  348. }
  349. const pre = await this.getDataByParentAndOrder(mid, select[this.setting.pid], select[this.setting.order] - 1);
  350. if (!pre) {
  351. throw '节点不可上移';
  352. }
  353. this.transaction = await this.db.beginTransaction();
  354. try {
  355. const sData = await this.transaction.update(this.tableName, { id: select.id, order: select[this.setting.order] - 1 });
  356. const pData = await this.transaction.update(this.tableName, { id: pre.id, order: pre[this.setting.order] + 1 });
  357. await this.transaction.commit();
  358. } catch (err) {
  359. await this.transaction.rollback();
  360. throw err;
  361. }
  362. const resultData = await this.getDataByParentAndOrder(mid, select[this.setting.pid], [select[this.setting.order], pre[this.setting.order]]);
  363. return { update: resultData };
  364. }
  365. /**
  366. * 下移节点
  367. *
  368. * @param {Number} mid - master id
  369. * @param {Number} kid - 选中节点id
  370. * @return {Array} - 发生改变的数据
  371. */
  372. async downMoveNode(mid, kid) {
  373. if (!mid || !kid) return null;
  374. const select = await this.getDataByLid(mid, kid);
  375. if (!select) {
  376. throw '下移节点数据错误';
  377. }
  378. const next = await this.getDataByParentAndOrder(mid, select[this.setting.pid], select[this.setting.order] + 1);
  379. if (!next) {
  380. throw '节点不可下移';
  381. }
  382. this.transaction = await this.db.beginTransaction();
  383. try {
  384. const sData = await this.transaction.update(this.tableName, { id: select.id, order: select[this.setting.order] + 1 });
  385. const pData = await this.transaction.update(this.tableName, { id: next.id, order: next[this.setting.order] - 1 });
  386. await this.transaction.commit();
  387. } catch (err) {
  388. await this.transaction.rollback();
  389. throw err;
  390. }
  391. const resultData = await this.getDataByParentAndOrder(mid, select[this.setting.pid], [select[this.setting.order], next[this.setting.order]]);
  392. return { update: resultData };
  393. }
  394. /**
  395. * 升级selectData, 同步修改所有子节点
  396. * @param {Object} selectData - 升级操作,选中节点
  397. * @return {Object}
  398. * @private
  399. */
  400. async _syncUplevelChildren(select) {
  401. this.initSqlBuilder();
  402. this.sqlBuilder.setAndWhere(this.setting.mid, {
  403. value: selectData.tender_id,
  404. operate: '=',
  405. });
  406. this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  407. value: this.db.escape(select[this.setting.fullPath] + '.%'),
  408. operate: 'like',
  409. });
  410. this.sqlBuilder.setUpdateData(this.setting.level, {
  411. value: 1,
  412. selfOperate: '-',
  413. });
  414. this.sqlBuilder.setUpdateData(this.setting.fullPath, {
  415. value: [this.setting.fullPath, this.db.escape(select[this.setting.pid] + '.'), this.db.escape('')],
  416. literal: 'Replace',
  417. });
  418. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  419. const data = await this.transaction.query(sql, sqlParam);
  420. return data;
  421. }
  422. /**
  423. * 选中节点的后兄弟节点,全部变为当前节点的子节点
  424. * @param {Object} selectData - 选中节点
  425. * @return {Object}
  426. * @private
  427. */
  428. async _syncUpLevelNexts(select) {
  429. // 查询selectData的lastChild
  430. const lastChild = await this.getLastChildData(select[this.setting.mid], select[this.setting.kid]);
  431. const nexts = await this.getNextsData(select[this.setting.mid], select[this.setting.pid], select[this.setting.order]);
  432. if (nexts && nexts.length > 0) {
  433. // 修改nextsData pid, 排序
  434. this.initSqlBuilder();
  435. this.sqlBuilder.setUpdateData(this.setting.pid, {
  436. value: select[this.setting.kid],
  437. });
  438. const orderInc = lastChild ? lastChild[this.setting.order] - select[this.setting.order] : - select[this.setting.order];
  439. this.sqlBuilder.setUpdateData(this.setting.order, {
  440. value: Math.abs(orderInc),
  441. selfOperate: orderInc > 0 ? '+' : '-',
  442. });
  443. this.sqlBuilder.setAndWhere(this.setting.mid, {
  444. value: select[this.setting.mid],
  445. operate: '=',
  446. });
  447. this.sqlBuilder.setAndWhere(this.setting.pid, {
  448. value: select[this.setting.pid],
  449. operate: '=',
  450. });
  451. this.sqlBuilder.setAndWhere(this.setting.order, {
  452. value: select[this.setting.order],
  453. operate: '>',
  454. });
  455. const [sql1, sqlParam1] = this.sqlBuilder.build(this.tableName, 'update');
  456. await this.transaction.query(sql1, sqlParam1);
  457. // 选中节点 is_leaf应为false
  458. if (select.is_leaf) {
  459. const updateData = { id: select.id, is_leaf: false };
  460. await this.transaction.update(this.tableName, updateData);
  461. }
  462. // 修改nextsData及其子节点的full_path
  463. const oldSubStr = this.db.escape(select[this.setting.pid] + '.');
  464. const newSubStr = this.db.escape(select[this.setting.kid] + '.');
  465. const sqlArr = [];
  466. sqlArr.push('Update ?? SET `full_path` = Replace(`full_path`,' + oldSubStr + ',' + newSubStr + ') Where');
  467. sqlArr.push('(`' + this.setting.mid + '` = ' + select[this.setting.mid] + ')');
  468. sqlArr.push(' And (');
  469. for (const data of nexts) {
  470. sqlArr.push('`' + this.setting.fullPath + '` Like ' + this.db.escape(data[this.setting.fullPath] + '%'));
  471. if (nexts.indexOf(data) < nexts.length - 1) {
  472. sqlArr.push(' Or ');
  473. }
  474. }
  475. sqlArr.push(')');
  476. const sql = sqlArr.join('');
  477. const resultData = await this.transaction.query(sql, [this.tableName]);
  478. return resultData;
  479. }
  480. }
  481. /**
  482. * 升级节点
  483. *
  484. * @param {Number} mid - master id
  485. * @param {Number} kid - 选中节点id
  486. * @return {Array} - 发生改变的数据
  487. */
  488. async upLevelNode(mid, kid) {
  489. if ((mid <= 0) || (kid <= 0)) return [];
  490. const select = await this.getDataByNodeId(mid, kid);
  491. if (!select) throw '升级节点数据错误';
  492. const parent = await this.getDataByNodeId(mid, select[this.setting.pid]);
  493. if (!parent) throw '升级节点数据错误';
  494. this.transaction = await this.db.beginTransaction();
  495. const newFullPath = select[this.setting.fullPath].replace(select[this.setting.pid] + '.', '');
  496. try {
  497. // 选中节点--父节点 选中节点为firstChild时,修改is_leaf
  498. if (select[this.setting.order] === 1) {
  499. await this.transaction.update(this.tableName, {
  500. id: parent.id,
  501. is_leaf: true,
  502. });
  503. }
  504. // 选中节点--父节点--全部后兄弟节点 order+1
  505. await this._updateSelectNextsOrder(parent);
  506. // 选中节点 修改pid, order, full_path, level, is_leaf, 清空计算项
  507. const updateData = { id: select.id };
  508. updateData[this.setting.pid] = parent[this.setting.pid];
  509. updateData[this.setting.order] = parent[this.setting.order] + 1;
  510. updateData[this.setting.level] = select[this.setting.level] + 1;
  511. updateData[this.setting.fullPath] = newFullPath;
  512. const nexts = await this.getNextsData(mid, parent[this.setting.kid], select[this.setting.order]);
  513. if (nexts.length > 0) {
  514. updateData.is_leaf = true;
  515. // updateData.unit_price = null;
  516. // updateData.quantity = null;
  517. // updateData.total_price = null;
  518. // updateData.deal_qty = null;
  519. // updateData.deal_tp = null;
  520. }
  521. await this.transaction.update(this.tableName, updateData);
  522. // 选中节点--全部子节点(含孙) level-1, full_path变更
  523. await this._syncUplevelChildren(select);
  524. // 选中节点--全部后兄弟节点 收编为子节点 修改pid, order, full_path
  525. await this._syncUpLevelNexts(select);
  526. await this.transaction.commit();
  527. } catch (err) {
  528. await this.transaction.rollback();
  529. throw err;
  530. }
  531. // 查询修改的数据
  532. const resultData1 = await this.getDataByFullPath(mid, newFullPath + '%');
  533. const resultData2 = await this.getNextsData(mid, parent[this.setting.pid], parent[this.setting.order] + 1);
  534. return { update: resultData1.concat(resultData2) };
  535. }
  536. /**
  537. * 降级selectData, 同步修改所有子节点
  538. * @param {Object} selectData - 选中节点
  539. * @param {Object} preData - 选中节点的前一节点(降级后为父节点)
  540. * @return {Promise<*>}
  541. * @private
  542. */
  543. async _syncDownlevelChildren(select, pre) {
  544. this.initSqlBuilder();
  545. this.sqlBuilder.setAndWhere(this.setting.mid, {
  546. value: select[this.setting.mid],
  547. operate: '=',
  548. });
  549. this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  550. value: this.db.escape(select[this.setting.fullPath] + '.%'),
  551. operate: 'like',
  552. });
  553. this.sqlBuilder.setUpdateData(this.setting.level, {
  554. value: 1,
  555. selfOperate: '+',
  556. });
  557. this.sqlBuilder.setUpdateData(this.setting.fullPath, {
  558. value: [this.setting.fullPath, this.db.escape('.' + select[this.setting.kid]), this.db.escape('.' + pre[this.setting.kid] + '.' + select[this.setting.kid])],
  559. literal: 'Replace',
  560. });
  561. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  562. const data = await this.transaction.query(sql, sqlParam);
  563. return data;
  564. }
  565. /**
  566. * 降级节点
  567. *
  568. * @param {Number} mid - master id
  569. * @param {Number} kid - 选中节点id
  570. * @return {Array} - 发生改变的数据
  571. */
  572. async downLevelNode(mid, kid) {
  573. if ((mid <= 0) || (kid <= 0)) return [];
  574. const select = await this.getDataByNodeId(mid, kid);
  575. if (!select) throw '降级节点数据错误';
  576. const pre = await this.getDataByParentAndOrder(mid, select[this.setting.pid], select[this.setting.order] - 1);
  577. if (!pre) throw '节点不可降级';
  578. const preLastChild = await this.getLastChildData(mid, pre[this.setting.kid]);
  579. this.transaction = await this.db.beginTransaction();
  580. const orgLastPath = select[this.setting.level] === 1 ? select[this.setting.kid] : '.' + select[this.setting.kid];
  581. const newLastPath = select[this.setting.level] === 1 ? pre[this.setting.kid] + '.' + select[this.setting.kid] : '.' + pre[this.setting.kid] + '.' + select[this.setting.kid];
  582. const newFullPath = select.full_path.replace(orgLastPath, newLastPath);
  583. try {
  584. // 选中节点--全部后节点 order--
  585. await this._updateSelectNextsOrder(select, -1);
  586. // 选中节点 修改pid, level, order, full_path
  587. const updateData = { id: select.id };
  588. updateData[this.setting.kid] = pre[this.setting.kid];
  589. updateData[this.setting.order] = preLastChild ? preLastChild[this.setting.order] + 1 : 1;
  590. updateData[this.setting.level] = select[this.setting.level] + 1;
  591. updateData[this.setting.fullPath] = newFullPath;
  592. await this.transaction.update(this.tableName, updateData);
  593. // 选中节点--全部子节点(含孙) level++, full_path
  594. await this._syncDownlevelChildren(select, pre);
  595. // 选中节点--前兄弟节点 is_leaf应为false, 清空计算相关字段
  596. const updateData2 = { id: pre.id };
  597. updateData2[this.setting.isLeaf] = false;
  598. // updateData2.unit_price = null;
  599. // updateData2.quantity = null;
  600. // updateData2.total_price = null;
  601. // updateData2.deal_qty = null;
  602. // updateData2.deal_tp = null;
  603. await this.transaction.update(this.tableName, updateData2);
  604. await this.transaction.commit();
  605. } catch (err) {
  606. await this.transaction.rollback();
  607. throw err;
  608. }
  609. // 查询修改的数据
  610. // 选中节点及子节点
  611. const resultData1 = await this.getDataByFullPath(mid, newFullPath + '%');
  612. // 选中节点--原前兄弟节点&全部后兄弟节点
  613. const resultData2 = await this.getNextsData(mid, pre[this.setting.pid], pre[this.setting.order]);
  614. return { update: resultData1.concat(resultData2) };
  615. }
  616. /**
  617. * 过滤data中update方式不可提交的字段
  618. * @param {Number} id - 主键key
  619. * @param {Object} data
  620. * @return {Object<{id: *}>}
  621. * @private
  622. */
  623. _filterUpdateInvalidField(id, data) {
  624. const result = {id: id};
  625. for (const prop in data) {
  626. if (this.readOnlyFields.indexOf(prop) === -1) {
  627. result[prop] = data[prop];
  628. }
  629. }
  630. return result;
  631. }
  632. /**
  633. * 提交多条数据 - 不影响计算等未提交项
  634. * @param {Number} mid - master id
  635. * @param {Array} datas - 提交数据
  636. * @return {Array} - 提交后的数据
  637. */
  638. async updateInfos(mid, datas) {
  639. if (mid <= 0) throw '数据错误';
  640. for (const data of datas) {
  641. if (mid !== data[this.setting.mid]) throw '提交数据错误';
  642. }
  643. this.transaction = await this.db.beginTransaction();
  644. try {
  645. for (const data of datas) {
  646. const updateNode = await this.getDataById(data.id);
  647. if (!updateNode || mid !== updateNode[this.setting.mid] || data.kid !== updateNode[this.setting.kid]) {
  648. throw '提交数据错误';
  649. }
  650. const updateData = this._filterUpdateInvalidField(updateNode.id, data);
  651. await this.transaction.update(this.tableName, updateData);
  652. }
  653. await this.transaction.commit();
  654. } catch (err) {
  655. await this.transaction.rollback();
  656. throw err;
  657. }
  658. const resultData = await this.getDataById(this._.map(datas, 'id'));
  659. return resultData;
  660. }
  661. }
  662. module.exports = TreeService;