ledger.js 26 KB

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