contract_tree.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const contractConst = require('../const/contract');
  7. const rootId = -1;
  8. module.exports = app => {
  9. class ContractTree extends app.BaseBillsService {
  10. /**
  11. * 构造函数
  12. *
  13. * @param {Object} ctx - egg全局变量
  14. * @return {void}
  15. */
  16. constructor(ctx) {
  17. const setting = {
  18. spid: 'spid',
  19. mid: 'tid',
  20. type: 'contract_type',
  21. kid: 'contract_id',
  22. pid: 'contract_pid',
  23. order: 'order',
  24. level: 'level',
  25. isLeaf: 'is_leaf',
  26. fullPath: 'full_path',
  27. keyPre: 'contract_maxLid:', // 换个名称,防止缓存导致旧数据出问题
  28. uuid: true,
  29. };
  30. super(ctx, setting, 'pos');
  31. this.setting = setting;
  32. this.depart = 0;
  33. this.tableName = 'contract_tree';
  34. }
  35. _getStringOptions(options) {
  36. const optionStr = [];
  37. for (const key in options) {
  38. if (options.hasOwnProperty(key)) {
  39. optionStr.push(options[key]);
  40. }
  41. }
  42. return optionStr.join('&&');
  43. }
  44. /**
  45. * 获取最大节点id
  46. *
  47. * @param {Number} mid - master id
  48. * @return {Number}
  49. * @private
  50. */
  51. async _getMaxLid(options) {
  52. const cacheKey = this.setting.keyPre + this._getStringOptions(options);
  53. let maxId = parseInt(await this.cache.get(cacheKey)) || 0;
  54. if (!maxId) {
  55. const sql = 'SELECT Max(??) As max_id FROM ?? Where ' + this.ctx.helper._getOptionsSql(options);
  56. const sqlParam = [this.setting.kid, this.tableName];
  57. const queryResult = await this.db.queryOne(sql, sqlParam);
  58. if (maxId < queryResult.max_id || 0) {
  59. maxId = queryResult.max_id || 0;
  60. }
  61. const sql1 = 'SELECT Max(??) As max_id FROM ?? Where ' + this.ctx.helper._getOptionsSql(options);
  62. const sqlParam1 = [this.setting.kid, this.ctx.service.contract.tableName];
  63. const queryResult1 = await this.db.queryOne(sql1, sqlParam1);
  64. if (maxId < queryResult1.max_id || 0) {
  65. maxId = queryResult1.max_id || 0;
  66. }
  67. this.cache.set(cacheKey, maxId, 'EX', this.ctx.app.config.cacheTime);
  68. }
  69. return maxId;
  70. }
  71. _cacheMaxLid(options, maxId) {
  72. const cacheKey = this.setting.keyPre + this._getStringOptions(options);
  73. this.cache.set(cacheKey, maxId, 'EX', this.ctx.app.config.cacheTime);
  74. }
  75. /**
  76. * 更新order
  77. * @param {Number} mid - master id
  78. * @param {Number} pid - 父节点id
  79. * @param {Number} order - 开始更新的order
  80. * @param {Number} incre - 更新的增量
  81. * @returns {Promise<*>}
  82. * @private
  83. */
  84. async _updateChildrenOrder(options, pid, order, incre = 1, transaction = null) {
  85. const optionSql = this.ctx.helper._getOptionsSql(options);
  86. const sql = 'UPDATE ?? SET `' + this.setting.order + '` = `' + this.setting.order + '` ' + (incre > 0 ? '+' : '-') + Math.abs(incre) + ' WHERE ' + optionSql + ' AND `' + this.setting.order + '` >= ? AND ' + this.setting.pid + ' = ?';
  87. const sqlParam = [this.tableName, order, pid];
  88. const data = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  89. const sql1 = 'UPDATE ?? SET `' + this.setting.order + '` = `' + this.setting.order + '` ' + (incre > 0 ? '+' : '-') + Math.abs(incre) + ' WHERE ' + optionSql + ' AND `' + this.setting.order + '` >= ? AND ' + this.setting.pid + ' = ?';
  90. const sqlParam1 = [this.ctx.service.contract.tableName, order, pid];
  91. transaction ? await transaction.query(sql1, sqlParam1) : await this.db.query(sql1, sqlParam1);
  92. return data;
  93. }
  94. _getOptionsSql(options) {
  95. const optionSql = [];
  96. for (const key in options) {
  97. if (options.hasOwnProperty(key)) {
  98. optionSql.push(key + ' = ' + this.db.escape(options[key]));
  99. }
  100. }
  101. return optionSql.join(' AND ');
  102. }
  103. async insertTree(options, subInfo) {
  104. const hadTree = await this.getDataByCondition(options);
  105. if (!hadTree) {
  106. if (options.tid && !subInfo.spid) {
  107. throw '该标段未绑定项目';
  108. }
  109. const subProj = options.spid ? subInfo : await this.ctx.service.subProject.getDataById(subInfo.spid);
  110. if (subProj.std_id === 0) {
  111. throw '该项目未绑定概预算标准';
  112. }
  113. const stdInfo = await this.ctx.service.budgetStd.getDataById(subProj.std_id);
  114. if (!stdInfo) {
  115. throw '概预算标准不存在';
  116. } else if (options.spid && !stdInfo.ht_project_template_id) {
  117. throw '概预算标准未绑定项目合同模板';
  118. } else if (options.tid && !stdInfo.ht_tender_template_id) {
  119. throw '概预算标准未绑定标段合同模板';
  120. }
  121. const ht_template_id = options.spid ? stdInfo.ht_project_template_id.split(',')[0] : stdInfo.ht_tender_template_id.split(',')[0];
  122. const ht_template_datas = await this.ctx.service.tenderNodeTemplate.getData(ht_template_id);
  123. if (!ht_template_datas.length) throw '模板数据有误';
  124. const expensesDatas = [];
  125. const incomeDatas = [];
  126. for (const t of ht_template_datas) {
  127. const insertData = {
  128. spid: options.spid || null,
  129. tid: options.tid || null,
  130. contract_id: t.template_id,
  131. contract_pid: t.pid,
  132. level: t.level,
  133. order: t.order,
  134. full_path: t.full_path,
  135. is_leaf: t.is_leaf,
  136. code: t.code,
  137. name: t.name,
  138. unit: t.unit,
  139. };
  140. const expensesData = this.ctx.helper._.cloneDeep(insertData);
  141. expensesData.id = this.uuid.v4();
  142. expensesData.contract_type = contractConst.type.expenses;
  143. expensesDatas.push(expensesData);
  144. const incomeData = this.ctx.helper._.cloneDeep(insertData);
  145. incomeData.id = this.uuid.v4();
  146. incomeData.contract_type = contractConst.type.income;
  147. incomeDatas.push(incomeData);
  148. }
  149. await this.db.insert(this.tableName, [...expensesDatas, ...incomeDatas]);
  150. }
  151. }
  152. /**
  153. * 提交数据 - 响应计算(增量方式计算)
  154. * @param {Number} tenderId
  155. * @param {Object} data
  156. * @return {Promise<*>}
  157. */
  158. async updateCalc(options, data) {
  159. const helper = this.ctx.helper;
  160. if (!data) {
  161. throw '提交数据错误';
  162. }
  163. const datas = data instanceof Array ? data : [data];
  164. const ids = [];
  165. for (const row of datas) {
  166. ids.push(row.id);
  167. }
  168. const transaction = await this.db.beginTransaction();
  169. try {
  170. const updateDatas = [];
  171. for (const row of datas) {
  172. const updateNode = await this.getDataById(row.id);
  173. if (!updateNode) {
  174. throw '提交数据错误';
  175. }
  176. const updateData = this._filterUpdateInvalidField(updateNode.id, row);
  177. // 如非子节点,需要更新底下所有已选清单的分部分项等数据
  178. updateDatas.push(updateData);
  179. }
  180. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  181. await transaction.commit();
  182. } catch (err) {
  183. await transaction.rollback();
  184. throw err;
  185. }
  186. return { update: await this.getDataById(ids) };
  187. }
  188. async getDataByKid(options, kid) {
  189. const condition = { ...options };
  190. condition[this.setting.kid] = kid;
  191. return await this.getDataByCondition(condition) || await this.ctx.service.contract.getDataByCondition(condition);
  192. }
  193. async getDataByKidAndCount(options, kid, count) {
  194. if (kid <= 0) return [];
  195. const select = await this.getDataByKid(options, kid);
  196. if (!select) throw '数据错误';
  197. if (count > 1) {
  198. const selects = await this.getNextsData(options, select[this.setting.pid], select[this.setting.order] - 1);
  199. if (selects.length < count) throw '数据错误';
  200. return selects.slice(0, count);
  201. } else {
  202. return [select];
  203. }
  204. }
  205. /**
  206. * 根据 父节点id 和 节点排序order 获取数据
  207. *
  208. * @param {Number} mid - master id
  209. * @param {Number} pid - 父节点id
  210. * @param {Number|Array} order - 排序
  211. * @return {Object|Array} - 查询结果
  212. */
  213. async getDataByParentAndOrder(options, pid, order) {
  214. const condition = { ...options };
  215. condition[this.setting.pid] = pid;
  216. condition[this.setting.order] = order;
  217. const result = await this.db.select(this.tableName, {
  218. where: condition,
  219. });
  220. const result1 = await this.db.select(this.ctx.service.contract.tableName, {
  221. where: condition,
  222. });
  223. // data和data1合并且按order排序
  224. const resultData = result.concat(result1).sort((a, b) => a.order - b.order);
  225. return order instanceof Array ? resultData : (resultData.length > 0 ? resultData[0] : null);
  226. }
  227. async addNodeBatch(options, kid, count = 1) {
  228. if (!options[this.setting.type]) throw '参数有误';
  229. const select = kid ? await this.getDataByKid(options, kid) : null;
  230. if (kid && !select) throw '新增节点数据错误';
  231. const transaction = await this.db.beginTransaction();
  232. try {
  233. // 判断select的父节点是否是变更新增的,如果是则修改自己的表就行了,否则修改2个ledger,changeLedger表
  234. if (select) await this._updateChildrenOrder(options, select[this.setting.pid], select[this.setting.order] + 1, count, transaction);
  235. const newDatas = [];
  236. const maxId = await this._getMaxLid(options);
  237. for (let i = 1; i < count + 1; i++) {
  238. const newData = [];
  239. if (this.setting.uuid) newData.id = this.uuid.v4();
  240. newData[this.setting.kid] = maxId + i;
  241. newData[this.setting.pid] = select ? select[this.setting.pid] : rootId;
  242. newData[this.setting.spid] = options.spid || null;
  243. newData[this.setting.type] = options[this.setting.type];
  244. newData[this.setting.mid] = options.tid || null;
  245. newData[this.setting.level] = select ? select[this.setting.level] : 1;
  246. newData[this.setting.order] = select ? select[this.setting.order] + i : i;
  247. newData[this.setting.fullPath] = newData[this.setting.level] > 1
  248. ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid])
  249. : newData[this.setting.kid] + '';
  250. newData[this.setting.isLeaf] = true;
  251. newDatas.push(newData);
  252. }
  253. const insertResult = await transaction.insert(this.tableName, newDatas);
  254. this._cacheMaxLid(options, maxId + count);
  255. if (insertResult.affectedRows !== count) throw '新增节点数据错误';
  256. await transaction.commit();
  257. } catch (err) {
  258. await transaction.rollback();
  259. throw err;
  260. }
  261. if (select) {
  262. let createData = await this.getChildBetween(options, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1);
  263. let updateData = await this.getNextsData(options, select[this.setting.pid], select[this.setting.order] + count);
  264. return { create: createData, update: updateData };
  265. } else {
  266. const createData = await this.getChildBetween(options, -1, 0, count + 1);
  267. return { create: createData };
  268. }
  269. }
  270. async addChildNode(options, kid, count = 1) {
  271. if (!options[this.setting.type]) throw '参数有误';
  272. const select = kid ? await this.getDataByKid(options, kid) : null;
  273. if (!select) throw '新增子节点数据错误';
  274. if (select && select.c_code) throw '合同无法新增子节点';
  275. const transaction = await this.db.beginTransaction();
  276. try {
  277. // 判断select的父节点是否是变更新增的,如果是则修改自己的表就行了,否则修改2个ledger,changeLedger表
  278. // if (select) await this._updateChildrenOrder(options, select[this.setting.pid], select[this.setting.order] + 1, count, transaction);
  279. const maxOrder = await this.ctx.service.contract.getMaxOrder(options, select[this.setting.kid], transaction);
  280. const newDatas = [];
  281. const maxId = await this._getMaxLid(options);
  282. for (let i = 1; i < count + 1; i++) {
  283. const newData = [];
  284. if (this.setting.uuid) newData.id = this.uuid.v4();
  285. newData[this.setting.kid] = maxId + i;
  286. newData[this.setting.pid] = select[this.setting.kid];
  287. newData[this.setting.spid] = options.spid || null;
  288. newData[this.setting.type] = options[this.setting.type];
  289. newData[this.setting.mid] = options.tid || null;
  290. newData[this.setting.level] = select[this.setting.level] + 1;
  291. newData[this.setting.order] = maxOrder - 1 + i;
  292. newData[this.setting.fullPath] = select[this.setting.fullPath] + '-' + newData[this.setting.kid];
  293. newData[this.setting.isLeaf] = true;
  294. newDatas.push(newData);
  295. }
  296. const insertResult = await transaction.insert(this.tableName, newDatas);
  297. this._cacheMaxLid(options, maxId + count);
  298. if (insertResult.affectedRows !== count) throw '新增子节点数据错误';
  299. if (select[this.setting.isLeaf]) {
  300. select.is_leaf = 0;
  301. await transaction.update(this.tableName, { id: select.id, is_leaf: 0 });
  302. }
  303. await transaction.commit();
  304. } catch (err) {
  305. await transaction.rollback();
  306. throw err;
  307. }
  308. let createData = await this.getLastChildData(options, select[this.setting.kid]);
  309. let updateData = select;
  310. return { create: [createData], update: [updateData] };
  311. }
  312. /**
  313. * tenderId标段中, 删除选中节点及其子节点
  314. *
  315. * @param {Number} tenderId - 标段id
  316. * @param {Number} selectId - 选中节点id
  317. * @return {Array} - 被删除的数据
  318. */
  319. async deleteNode(options, kid) {
  320. if (kid <= 0) return [];
  321. const select = await this.getDataByKid(options, kid);
  322. if (!select) throw '删除节点数据错误';
  323. const parent = await this.getDataByKid(options, select[this.setting.pid]);
  324. // 获取将要被删除的数据
  325. const deleteData = await this.getDataByFullPath(options, select[this.setting.fullPath] + '-%');
  326. deleteData.unshift(select);
  327. if (deleteData.length === 0) throw '删除节点数据错误';
  328. const transaction = await this.db.beginTransaction();
  329. try {
  330. // 删除
  331. if (select.c_code) {
  332. if (select.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) throw '当前合同无权删除';
  333. const contractPays = await this.ctx.service.contractPay.getDataByCondition({ cid: select.id });
  334. if (contractPays) throw '还存在合同支付项,无法删除';
  335. await transaction.delete(this.ctx.service.contract.tableName, { id: select.id });
  336. const attList = await this.ctx.service.contractAtt.getAllDataByCondition({ where: { cid: select.id } });
  337. await this.ctx.helper.delFiles(attList);
  338. await transaction.delete(this.ctx.service.contractAtt.tableName, { cid: select.id });
  339. } else {
  340. await transaction.delete(this.tableName, { id: select.id });
  341. const delOptions = this._.cloneDeep(options);
  342. delOptions.contract_id = this._.map(deleteData, 'contract_id');
  343. await transaction.delete(this.ctx.service.contractTreeAudit.tableName, delOptions);
  344. const contracts = this.ctx.helper._.filter(deleteData, function (item) {
  345. return item.c_code;
  346. });
  347. if (contracts.length > 0) {
  348. const contractUids = this.ctx.helper._.uniq(this.ctx.helper._.map(contracts, 'uid'));
  349. if (contractUids.length > 1 || !(contractUids[0] === this.ctx.session.sessionUser.accountId || this.ctx.session.sessionUser.is_admin)) throw '存在合同你无权删除';
  350. const contractPays = await transaction.select(this.ctx.service.contractPay.tableName, { where: { cid: this.ctx.helper._.map(contracts, 'id') } });
  351. if (contractPays.length > 0) throw '还存在合同支付项,无法删除';
  352. const attList = await this.ctx.service.contractAtt.getAllDataByCondition({ where: { cid: this.ctx.helper._.map(contracts, 'id') } });
  353. await this.ctx.helper.delFiles(attList);
  354. await transaction.delete(this.ctx.service.contractAtt.tableName, { cid: this.ctx.helper._.map(contracts, 'id') });
  355. }
  356. const operate = await this._deletePosterity(options, select, transaction);
  357. }
  358. // 选中节点--父节点 只有一个子节点时,应升级isLeaf
  359. if (parent) {
  360. const condition = { ...options };
  361. condition[this.setting.pid] = select[this.setting.pid];
  362. const count = await this.db.count(this.tableName, condition);
  363. const count1 = await this.db.count(this.ctx.service.contract.tableName, condition);
  364. const sum = count + count1;
  365. if (sum === 1) {
  366. const updateParent = {id: parent.id };
  367. updateParent[this.setting.isLeaf] = true;
  368. await transaction.update(this.tableName, updateParent);
  369. }
  370. }
  371. // 选中节点--全部后节点 order--
  372. await this._updateChildrenOrder(options, select[this.setting.pid], select[this.setting.order] + 1, -1, transaction);
  373. await transaction.commit();
  374. } catch (err) {
  375. await transaction.rollback();
  376. throw err;
  377. }
  378. // 查询结果
  379. const updateData = await this.getNextsData(options, select[this.setting.pid], select[this.setting.order] - 1);
  380. if (parent) {
  381. const updateData1 = await this.getDataByKid(options, select[this.setting.pid]);
  382. if (updateData1[this.setting.isLeaf]) {
  383. updateData.push(updateData1);
  384. }
  385. }
  386. return { delete: deleteData, update: updateData };
  387. }
  388. async deleteNodes(options, kid, count) {
  389. const _ = this.ctx.helper._;
  390. if ((kid <= 0) || (count <= 0)) return [];
  391. const selects = await this.getDataByKidAndCount(options, kid, count);
  392. const first = selects[0];
  393. const parent = await this.getDataByKid(options, first[this.setting.pid]);
  394. const condition = { ...options };
  395. condition[this.setting.pid] = parent[this.setting.kid];
  396. const childCount1 = parent ? await this.count(condition) : -1;
  397. const childCount2 = parent ? await this.db.count(this.ctx.service.contract.tableName, condition) : -1;
  398. const childCount = childCount1 + childCount2;
  399. let deleteData = [];
  400. for (const s of selects) {
  401. deleteData = deleteData.concat(await this.getDataByFullPath(options, s[this.setting.fullPath] + '-%'));
  402. deleteData.push(s);
  403. }
  404. const transaction = await this.db.beginTransaction();
  405. try {
  406. // 删除
  407. for (const s of selects) {
  408. if (s.c_code) {
  409. if (s.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) throw '存在合同你无权删除';
  410. const contractPays = await this.ctx.service.contractPay.getDataByCondition({ cid: s.id });
  411. if (contractPays) throw '部分合同还存在合同支付项,无法删除';
  412. await transaction.delete(this.ctx.service.contract.tableName, { id: s.id });
  413. const attList = await this.ctx.service.contractAtt.getAllDataByCondition({ where: { cid: s.id } });
  414. await this.ctx.helper.delFiles(attList);
  415. await transaction.delete(this.ctx.service.contractAtt.tableName, { cid: s.id });
  416. } else {
  417. await transaction.delete(this.tableName, { id: s.id });
  418. const contracts = _.filter(deleteData, function (item) {
  419. return item.c_code && _.includes(s.full_path, item.full_path);
  420. });
  421. if (contracts.length > 0) {
  422. const contractUids = _.uniq(_.map(contracts, 'uid'));
  423. if (contractUids.length > 1 || !(contractUids[0] === this.ctx.session.sessionUser.accountId || this.ctx.session.sessionUser.is_admin)) throw '存在合同你无权删除';
  424. const contractPays = await transaction.select(this.ctx.service.contractPay.tableName, { where: { cid: _.map(contracts, 'id') } });
  425. if (contractPays.length > 0) throw '还存在合同支付项,无法删除';
  426. const attList = await this.ctx.service.contractAtt.getAllDataByCondition({ where: { cid: _.map(contracts, 'id') } });
  427. await this.ctx.helper.delFiles(attList);
  428. await transaction.delete(this.ctx.service.contractAtt.tableName, { cid: _.map(contracts, 'id') });
  429. }
  430. }
  431. const operate = await this._deletePosterity(options, s, transaction);
  432. }
  433. // 选中节点--父节点 只有一个子节点时,应升级isLeaf
  434. if (parent && childCount === count) {
  435. const updateParent = {id: parent.id };
  436. updateParent[this.setting.isLeaf] = true;
  437. await transaction.update(this.tableName, updateParent);
  438. }
  439. // 选中节点--全部后节点 order--
  440. await this._updateChildrenOrder(options, first[this.setting.pid], first[this.setting.order] + count, -count, transaction);
  441. const delOptions = this._.cloneDeep(options);
  442. delOptions.contract_id = this._.map(deleteData, 'contract_id');
  443. await transaction.delete(this.ctx.service.contractTreeAudit.tableName, delOptions);
  444. await transaction.commit();
  445. } catch (err) {
  446. await transaction.rollback();
  447. throw err;
  448. }
  449. const updateData = await this.getNextsData(options, first[this.setting.pid], first[this.setting.order] - 1);
  450. if (parent && childCount === count) {
  451. const updateData1 = await this.getDataByKid(options, parent[this.setting.kid]);
  452. updateData.push(updateData1);
  453. }
  454. return { delete: deleteData, update: updateData };
  455. }
  456. async delete(options, kid, count = null) {
  457. if (count && count > 1) {
  458. return await this.deleteNodes(options, kid, count);
  459. } else {
  460. return await this.deleteNode(options, kid);
  461. }
  462. }
  463. /**
  464. * 上移节点
  465. *
  466. * @param {Number} mid - master id
  467. * @param {Number} kid - 选中节点id
  468. * @return {Array} - 发生改变的数据
  469. */
  470. async upMoveNode(options, kid, count) {
  471. if (!count) count = 1;
  472. if (!kid || (kid <= 0)) return null;
  473. const selects = await this.getDataByKidAndCount(options, kid, count);
  474. if (selects.length !== count) throw '上移节点数据错误';
  475. const first = selects[0];
  476. const pre = await this.getDataByParentAndOrder(options, first[this.setting.pid], first[this.setting.order] - 1);
  477. if (!pre) throw '节点不可上移';
  478. const order = [];
  479. const transaction = await this.db.beginTransaction();
  480. try {
  481. for (const s of selects) {
  482. const sData = { id: s.id };
  483. sData[this.setting.order] = s[this.setting.order] - 1;
  484. !s.c_code ? await transaction.update(this.tableName, sData) : await transaction.update(this.ctx.service.contract.tableName, sData);
  485. order.push(s[this.setting.order] - 1);
  486. }
  487. const pData = { id: pre.id };
  488. pData[this.setting.order] = pre[this.setting.order] + count;
  489. !pre.c_code ? await transaction.update(this.tableName, pData) : await transaction.update(this.ctx.service.contract.tableName, pData);
  490. order.push(pre[this.setting.order] + count);
  491. await transaction.commit();
  492. } catch (err) {
  493. await transaction.rollback();
  494. throw err;
  495. }
  496. const resultData = await this.getDataByParentAndOrder(options, first[this.setting.pid], order);
  497. return { update: resultData };
  498. }
  499. /**
  500. * 下移节点
  501. *
  502. * @param {Number} mid - master id
  503. * @param {Number} kid - 选中节点id
  504. * @return {Array} - 发生改变的数据
  505. */
  506. async downMoveNode(options, kid, count) {
  507. if (!count) count = 1;
  508. if (!kid || (kid <= 0)) return null;
  509. const selects = await this.getDataByKidAndCount(options, kid, count);
  510. if (selects.length !== count) {
  511. throw '下移节点数据错误';
  512. }
  513. const last = selects[count - 1];
  514. const next = await this.getDataByParentAndOrder(options, last[this.setting.pid], last[this.setting.order] + 1);
  515. if (!next) {
  516. throw '节点不可下移';
  517. }
  518. const order = [];
  519. const transaction = await this.db.beginTransaction();
  520. try {
  521. for (const s of selects) {
  522. const sData = { id: s.id };
  523. sData[this.setting.order] = s[this.setting.order] + 1;
  524. !s.c_code ? await transaction.update(this.tableName, sData) : await transaction.update(this.ctx.service.contract.tableName, sData);
  525. order.push(s[this.setting.order] + 1);
  526. }
  527. const nData = { id: next.id };
  528. nData[this.setting.order] = next[this.setting.order] - count;
  529. !next.c_code ? await transaction.update(this.tableName, nData) : await transaction.update(this.ctx.service.contract.tableName, nData);
  530. order.push(next[this.setting.order] - count);
  531. await transaction.commit();
  532. } catch (err) {
  533. await transaction.rollback();
  534. throw err;
  535. }
  536. const resultData = await this.getDataByParentAndOrder(options, last[this.setting.pid], order);
  537. return { update: resultData };
  538. }
  539. /**
  540. * 升级节点
  541. *
  542. * @param {Number} tenderId - 标段id
  543. * @param {Number} selectId - 选中节点id
  544. * @return {Array} - 发生改变的数据
  545. */
  546. async upLevelNode(options, kid, count) {
  547. if (!count) count = 1;
  548. const selects = await this.getDataByKidAndCount(options, kid, count);
  549. if (selects.length !== count) throw '升级节点数据错误';
  550. if (this._.findIndex(selects, function (item) {
  551. return item.c_code;
  552. }) !== -1) {
  553. throw '存在合同节点不可升级';
  554. }
  555. const first = selects[0], last = selects[count - 1];
  556. const parent = await this.getDataByKid(options, first[this.setting.pid]);
  557. if (!parent) throw '升级节点数据错误';
  558. const newPath = [];
  559. const transaction = await this.db.beginTransaction();
  560. try {
  561. // 选中节点--父节点 选中节点为firstChild时,修改isLeaf
  562. if (first[this.setting.order] === 1) {
  563. const updateParentData = { id: parent.id };
  564. updateParentData[this.setting.isLeaf] = true;
  565. await transaction.update(this.tableName, updateParentData);
  566. }
  567. // 选中节点--父节点--全部后兄弟节点 order+1
  568. await this._updateChildrenOrder(options, parent[this.setting.pid], parent[this.setting.order] + 1, count, transaction);
  569. for (const [i, s] of selects.entries()) {
  570. // 选中节点 修改pid, order, fullPath, level, isLeaf, 清空计算项
  571. const updateData = { id: s.id };
  572. updateData[this.setting.pid] = parent[this.setting.pid];
  573. updateData[this.setting.order] = parent[this.setting.order] + i + 1;
  574. updateData[this.setting.level] = s[this.setting.level] - 1;
  575. updateData[this.setting.fullPath] = s[this.setting.fullPath].replace(`-${s[this.setting.pid]}-`, '-');
  576. newPath.push(updateData[this.setting.fullPath]);
  577. if (s[this.setting.isLeaf] && s.id === last.id) {
  578. const nexts = await this.getNextsData(options, parent[this.setting.kid], last[this.setting.order]);
  579. if (nexts.length > 0) {
  580. updateData[this.setting.isLeaf] = false;
  581. }
  582. }
  583. await transaction.update(this.tableName, updateData);
  584. // 选中节点--全部子节点(含孙) level-1, fullPath变更
  585. await this._syncUplevelChildren(options, s, transaction);
  586. }
  587. // 选中节点--全部后兄弟节点 收编为子节点 修改pid, order, fullPath
  588. await this._syncUpLevelNexts(options, last, transaction);
  589. await transaction.commit();
  590. } catch (err) {
  591. await transaction.rollback();
  592. throw err;
  593. }
  594. // 查询修改的数据
  595. let updateData = await this.getNextsData(options, parent[this.setting.pid], parent[this.setting.order] - 1);
  596. for (const path of newPath) {
  597. const children = await this.getDataByFullPath(options, path + '-%');
  598. updateData = updateData.concat(children);
  599. }
  600. return { update: updateData };
  601. }
  602. /**
  603. * 降级节点
  604. *
  605. * @param {Number} tenderId - 标段id
  606. * @param {Number} selectId - 选中节点id
  607. * @return {Array} - 发生改变的数据
  608. */
  609. async downLevelNode(options, kid, count) {
  610. if (!count) count = 1;
  611. const selects = await this.getDataByKidAndCount(options, kid, count);
  612. if (!selects) throw '降级节点数据错误';
  613. // if (this._.findIndex(selects, function (item) {
  614. // return item.c_code;
  615. // }) !== -1) {
  616. // throw '存在合同节点不可降级';
  617. // }
  618. const first = selects[0], last = selects[count - 1];
  619. const pre = await this.getDataByParentAndOrder(options, first[this.setting.pid], first[this.setting.order] - 1);
  620. if (!pre) throw '节点不可降级';
  621. const preLastChild = await this.getLastChildData(options, pre[this.setting.kid]);
  622. const newPath = [];
  623. const transaction = await this.db.beginTransaction();
  624. try {
  625. // 选中节点--全部后节点 order--
  626. await this._updateChildrenOrder(options, first[this.setting.pid], last[this.setting.order] + 1, -count, transaction);
  627. for (const [i, s] of selects.entries()) {
  628. // 选中节点 修改pid, level, order, fullPath
  629. const updateData = { id: s.id };
  630. updateData[this.setting.pid] = pre[this.setting.kid];
  631. updateData[this.setting.order] = preLastChild ? preLastChild[this.setting.order] + i + 1 : i + 1;
  632. updateData[this.setting.level] = s[this.setting.level] + 1;
  633. if (s[this.setting.level] === 1) {
  634. updateData[this.setting.fullPath] = pre[this.setting.kid] + '-' + s[this.setting.kid];
  635. } else {
  636. const index = s[this.setting.fullPath].lastIndexOf(s[this.setting.kid]);
  637. updateData[this.setting.fullPath] = s[this.setting.fullPath].substring(0, index-1) + '-' + pre[this.setting.kid] + '-' + s[this.setting.kid];
  638. }
  639. newPath.push(updateData[this.setting.fullPath]);
  640. s.c_code ? await transaction.update(this.ctx.service.contract.tableName, updateData) : await transaction.update(this.tableName, updateData);
  641. // 选中节点--全部子节点(含孙) level++, fullPath
  642. await this._syncDownlevelChildren(options, s, updateData[this.setting.fullPath], transaction);
  643. }
  644. // 选中节点--前兄弟节点 isLeaf应为false, 清空计算相关字段
  645. const updateData2 = { id: pre.id };
  646. updateData2[this.setting.isLeaf] = false;
  647. await transaction.update(this.tableName, updateData2);
  648. await transaction.commit();
  649. } catch (err) {
  650. await transaction.rollback();
  651. throw err;
  652. }
  653. // 查询修改的数据
  654. let updateData = await this.getNextsData(options, pre[this.setting.pid], pre[this.setting.order] - 1);
  655. // 选中节点及子节点
  656. for (const p of newPath) {
  657. updateData = updateData.concat(await this.getDataByFullPath(options, p + '-%'));
  658. }
  659. const contractSelects = selects.filter(x => x.c_code);
  660. const treeSelects = selects.filter(x => !x.c_code);
  661. updateData = updateData.concat(await this.getDataById(treeSelects.map(x => { return x.id; })));
  662. updateData = updateData.concat(await this.ctx.service.contract.getDataById(contractSelects.map(x => { return x.id; })));
  663. // 选中节点--原前兄弟节点&全部后兄弟节点
  664. return { update: updateData };
  665. }
  666. async pasteBlockData(options, kid, pasteData, defaultData) {
  667. if (!options[this.setting.type]) throw '参数有误';
  668. if (!pasteData || pasteData.length <= 0) throw '复制数据错误';
  669. for (const pd of pasteData) {
  670. if (!pd || pd.length <= 0) throw '复制数据错误';
  671. pd.sort(function (x, y) {
  672. return x.level - y.level
  673. });
  674. if (pd[0].contract_pid !== pasteData[0][0].contract_pid) throw '复制数据错误:仅可操作同层节点';
  675. }
  676. const selectData = await this.getDataByKid(options, kid);
  677. if (!selectData) throw '粘贴数据错误';
  678. const newParentPath = selectData.full_path.replace(selectData.contract_id, '');
  679. const pasteBillsData = [];
  680. let maxId = await this._getMaxLid(options);
  681. for (const [i, pd] of pasteData.entries()) {
  682. for (const d of pd) {
  683. d.children = pd.filter(function (x) {
  684. return x.contract_pid === d.contract_id;
  685. });
  686. }
  687. const pbd = [];
  688. for (const [j, d] of pd.entries()) {
  689. const newBills = {
  690. id: this.uuid.v4(),
  691. spid: options.spid || null,
  692. tid: options.tid || null,
  693. contract_type: options.contract_type,
  694. contract_id: maxId + j + 1,
  695. contract_pid: j === 0 ? selectData.contract_pid : d.contract_pid,
  696. level: d.level + selectData.level - pd[0].level,
  697. order: j === 0 ? selectData.order + i + 1 : d.order,
  698. is_leaf: d.is_leaf,
  699. code: d.code,
  700. name: d.name,
  701. remark: d.remark,
  702. };
  703. for (const c of d.children) {
  704. c.contract_pid = newBills.contract_id;
  705. }
  706. pbd.push(newBills);
  707. }
  708. for (const d of pbd) {
  709. const parent = pbd.find(function (x) {
  710. return x.contract_id === d.contract_pid;
  711. });
  712. d.full_path = parent
  713. ? parent.full_path + '-' + d.contract_id
  714. : newParentPath + d.contract_id;
  715. if (defaultData) this.ctx.helper._.assignIn(pbd, defaultData);
  716. pasteBillsData.push(d);
  717. }
  718. maxId = maxId + pbd.length;
  719. }
  720. const transaction = await this.db.beginTransaction();
  721. try {
  722. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  723. await this._updateChildrenOrder(options, selectData.ledger_pid, selectData.order + 1, pasteData.length, transaction);
  724. // 数据库创建新增节点数据
  725. if (pasteBillsData.length > 0) {
  726. const newData = await transaction.insert(this.tableName, pasteBillsData);
  727. }
  728. this._cacheMaxLid(options, maxId);
  729. await transaction.commit();
  730. } catch (err) {
  731. await transaction.rollback();
  732. throw err;
  733. }
  734. // 查询应返回的结果
  735. const updateData = await this.getNextsData(options, selectData.contract_pid, selectData.order + pasteData.length);
  736. return {
  737. ledger: { create: pasteBillsData, update: updateData },
  738. };
  739. }
  740. /**
  741. * 删除节点
  742. * @param {Number} tenderId - 标段id
  743. * @param {Object} deleteData - 删除节点数据
  744. * @return {Promise<*>}
  745. * @private
  746. */
  747. async _deletePosterity(options, node, transaction = null) {
  748. const sql = 'DELETE FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  749. const sqlParam = [this.tableName, node[this.setting.fullPath] + '-%'];
  750. const result = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam)
  751. const sql1 = 'DELETE FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  752. const sqlParam1 = [this.ctx.service.contract.tableName, node[this.setting.fullPath] + '-%'];
  753. const result1 = transaction ? await transaction.query(sql1, sqlParam1) : await this.db.query(sql1, sqlParam1)
  754. return result;
  755. }
  756. /**
  757. * 根据fullPath获取数据 fullPath Like ‘1.2.3%’(传参fullPath = '1.2.3%')
  758. * @param {Number} tenderId - 标段id
  759. * @param {String} fullPath - 路径
  760. * @return {Promise<void>}
  761. */
  762. async getDataByFullPath(options, fullPath) {
  763. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  764. const sqlParam = [this.tableName, fullPath];
  765. const resultData = await this.db.query(sql, sqlParam);
  766. const sql1 = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  767. const sqlParam1 = [this.ctx.service.contract.tableName, fullPath];
  768. const resultData1 = await this.db.query(sql1, sqlParam1);
  769. return resultData.concat(resultData1).sort((a, b) => a.order - b.order);
  770. }
  771. async getChildBetween(options, pid, order1, order2) {
  772. const sql = 'SELECT * FROM ?? WHERE '+ this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `order` > ? AND `order` < ? ORDER BY `order` ASC';
  773. const sqlParam = [this.tableName, pid, order1, order2];
  774. const data = await this.db.query(sql, sqlParam);
  775. const sql1 = 'SELECT * FROM ?? WHERE '+ this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `order` > ? AND `order` < ? ORDER BY `order` ASC';
  776. const sqlParam1 = [this.ctx.service.contract.tableName, pid, order1, order2];
  777. const data1 = await this.db.query(sql1, sqlParam1);
  778. const resultData = data.concat(data1).sort((a, b) => a.order - b.order);
  779. return resultData;
  780. }
  781. /**
  782. * 根据 父节点ID 和 节点排序order 获取全部后节点数据
  783. * @param {Number} mid - master id
  784. * @param {Number} pid - 父节点id
  785. * @param {Number} order - 排序
  786. * @return {Array}
  787. */
  788. async getNextsData(options, pid, order) {
  789. const sql = 'SELECT * FROM ?? WHERE '+ this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `order` > ? ORDER BY `order` ASC';
  790. const sqlParam = [this.tableName, pid, order];
  791. const data = await this.db.query(sql, sqlParam);
  792. const sql1 = 'SELECT * FROM ?? WHERE '+ this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `order` > ? ORDER BY `order` ASC';
  793. const sqlParam1 = [this.ctx.service.contract.tableName, pid, order];
  794. const data1 = await this.db.query(sql1, sqlParam1);
  795. // data和data1合并且按order排序
  796. const resultData = data.concat(data1).sort((a, b) => a.order - b.order);
  797. return resultData;
  798. }
  799. /**
  800. * 获取最末的子节点
  801. * @param {Number} mid - masterId
  802. * @param {Number} pid - 父节点id
  803. * @return {Object}
  804. */
  805. async getLastChildData(options, pid, transaction = null) {
  806. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? ORDER BY `order` DESC';
  807. const sqlParam = [this.tableName, pid];
  808. const resultData = await this.db.queryOne(sql, sqlParam);
  809. const sql1 = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? ORDER BY `order` DESC';
  810. const sqlParam1 = [this.ctx.service.contract.tableName, pid];
  811. const resultData1 = await this.db.queryOne(sql1, sqlParam1);
  812. // 比较两个结果,返回order大的
  813. if (resultData && resultData1) {
  814. return resultData.order > resultData1.order ? resultData : resultData1;
  815. } else {
  816. return resultData || resultData1;
  817. }
  818. }
  819. /**
  820. * 选中节点的后兄弟节点,全部变为当前节点的子节点
  821. * @param {Object} selectData - 选中节点
  822. * @return {Object}
  823. * @private
  824. */
  825. async _syncUpLevelNexts(options, select, transaction = null) {
  826. // 查询selectData的lastChild
  827. const lastChild = await this.getLastChildData(options, select[this.setting.kid]);
  828. const nexts = await this.getNextsData(options, select[this.setting.pid], select[this.setting.order]);
  829. if (this._.findIndex(nexts, function (item) {
  830. return item.c_code;
  831. }) !== -1) {
  832. throw '存在合同节点不可升级';
  833. }
  834. if (nexts && nexts.length > 0) {
  835. // 修改nextsData pid, 排序
  836. // this.initSqlBuilder();
  837. // this.sqlBuilder.setUpdateData(this.setting.pid, {
  838. // value: select[this.setting.kid],
  839. // });
  840. // const orderInc = lastChild ? lastChild[this.setting.order] - select[this.setting.order] : - select[this.setting.order];
  841. // this.sqlBuilder.setUpdateData(this.setting.order, {
  842. // value: Math.abs(orderInc),
  843. // selfOperate: orderInc > 0 ? '+' : '-',
  844. // });
  845. // this.sqlBuilder.setAndWhere(this.setting.mid, {
  846. // value: select[this.setting.mid],
  847. // operate: '=',
  848. // });
  849. // this.sqlBuilder.setAndWhere(this.setting.pid, {
  850. // value: select[this.setting.pid],
  851. // operate: '=',
  852. // });
  853. // this.sqlBuilder.setAndWhere(this.setting.order, {
  854. // value: select[this.setting.order],
  855. // operate: '>',
  856. // });
  857. // const [sql1, sqlParam1] = this.sqlBuilder.build(this.tableName, 'update');
  858. const orderInc = lastChild ? lastChild[this.setting.order] - select[this.setting.order] : - select[this.setting.order];
  859. const sql1 = 'UPDATE ?? SET `'+ this.setting.pid + '` = '+ select[this.setting.kid] +' , `' + this.setting.order + '` = `'+ this.setting.order + '`' + (orderInc > 0 ? '+' : '-') + Math.abs(orderInc) + ' WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `' + this.setting.order + '` > ?';
  860. const sqlParam1 = [this.tableName, select[this.setting.pid], select[this.setting.order]];
  861. transaction ? await transaction.query(sql1, sqlParam1) : await this.db.query(sql1, sqlParam1);
  862. // 选中节点 isLeaf应为false
  863. if (select[this.setting.isLeaf]) {
  864. const updateData = { id: select.id };
  865. updateData[this.setting.isLeaf] = false;
  866. transaction ? await transaction.update(this.tableName, updateData) : await this.db.update(this.tableName, updateData);
  867. }
  868. // 修改nextsData及其子节点的fullPath
  869. const oldSubStr = this.db.escape(select[this.setting.pid] + '-');
  870. const newSubStr = this.db.escape(select[this.setting.kid] + '-');
  871. const sqlArr = [];
  872. sqlArr.push('Update ?? SET `' + this.setting.fullPath + '` = Replace(`' + this.setting.fullPath + '`,' + oldSubStr + ',' + newSubStr + ') Where ');
  873. sqlArr.push(this.ctx.helper._getOptionsSql(options));
  874. sqlArr.push(' And (');
  875. for (const data of nexts) {
  876. sqlArr.push('`' + this.setting.fullPath + '` Like ' + this.db.escape(data[this.setting.fullPath] + '%'));
  877. if (nexts.indexOf(data) < nexts.length - 1) {
  878. sqlArr.push(' Or ');
  879. }
  880. }
  881. sqlArr.push(')');
  882. const sql = sqlArr.join('');
  883. const resultData = transaction ? await transaction.query(sql, [this.tableName]) : await this.db.query(sql, [this.tableName]);
  884. transaction ? await transaction.query(sql, [this.ctx.service.contract.tableName]) : await this.db.query(sql, [this.ctx.service.contract.tableName]);
  885. return resultData;
  886. }
  887. }
  888. /**
  889. * 升级selectData, 同步修改所有子节点
  890. * @param {Object} selectData - 升级操作,选中节点
  891. * @return {Object}
  892. * @private
  893. */
  894. async _syncUplevelChildren(options, select, transaction = null) {
  895. // const children = await this.getDataByFullPath(options, select[this.setting.fullPath] + '-%');
  896. // if (this._.findIndex(children, function (item) {
  897. // return item.c_code;
  898. // }) !== -1) {
  899. // throw '存在合同节点不可升级';
  900. // }
  901. // this.initSqlBuilder();
  902. // this.sqlBuilder.setAndWhere(this.setting.mid, {
  903. // value: select[this.setting.mid],
  904. // operate: '=',
  905. // });
  906. // this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  907. // value: this.db.escape(select[this.setting.fullPath] + '-%'),
  908. // operate: 'like',
  909. // });
  910. // this.sqlBuilder.setUpdateData(this.setting.level, {
  911. // value: 1,
  912. // selfOperate: '-',
  913. // });
  914. // this.sqlBuilder.setUpdateData(this.setting.fullPath, {
  915. // value: [this.setting.fullPath, this.db.escape(`-${select[this.setting.pid]}-`), this.db.escape('-')],
  916. // literal: 'Replace',
  917. // });
  918. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  919. const sql = 'UPDATE ?? SET ' + this.setting.level + ' = ' + this.setting.level + ' -1, ' + this.setting.fullPath + ' = ' +
  920. 'Replace('+ [this.setting.fullPath, this.db.escape(`-${select[this.setting.pid]}-`), this.db.escape('-')].join(',') +') ' +
  921. 'WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  922. const sqlParam = [this.tableName, select[this.setting.fullPath] + '-%'];
  923. const data = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  924. transaction ? await transaction.query(sql, [this.ctx.service.contract.tableName, select[this.setting.fullPath] + '-%']) : await this.db.query(sql, [this.ctx.service.contract.tableName, select[this.setting.fullPath] + '-%']);
  925. return data;
  926. }
  927. /**
  928. * 降级selectData, 同步修改所有子节点
  929. * @param {Object} selectData - 选中节点
  930. * @param {Object} preData - 选中节点的前一节点(降级后为父节点)
  931. * @return {Promise<*>}
  932. * @private
  933. */
  934. async _syncDownlevelChildren(options, select, newFullPath, transaction = null) {
  935. // const children = await this.getDataByFullPath(options, select[this.setting.fullPath] + '-%');
  936. // if (this._.findIndex(children, function (item) {
  937. // return item.c_code;
  938. // }) !== -1) {
  939. // throw '存在合同节点不可降级';
  940. // }
  941. // this.initSqlBuilder();
  942. // this.sqlBuilder.setAndWhere(this.setting.mid, {
  943. // value: select[this.setting.mid],
  944. // operate: '=',
  945. // });
  946. // this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  947. // value: this.db.escape(select[this.setting.fullPath] + '-%'),
  948. // operate: 'like',
  949. // });
  950. // this.sqlBuilder.setUpdateData(this.setting.level, {
  951. // value: 1,
  952. // selfOperate: '+',
  953. // });
  954. // this.sqlBuilder.setUpdateData(this.setting.fullPath, {
  955. // value: [this.setting.fullPath, this.db.escape(select[this.setting.fullPath] + '-'), this.db.escape(newFullPath + '-')],
  956. // literal: 'Replace',
  957. // });
  958. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  959. const sql = 'UPDATE ?? SET ' + this.setting.level + ' = ' + this.setting.level + ' + 1, ' + this.setting.fullPath + ' = ' +
  960. 'Replace(' + [[this.setting.fullPath, this.db.escape(select[this.setting.fullPath] + '-'), this.db.escape(newFullPath + '-')]].join(',') + ') ' +
  961. 'WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  962. const sqlParam = [this.tableName, select[this.setting.fullPath] + '-%'];
  963. const data = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  964. transaction ? await transaction.query(sql, [this.ctx.service.contract.tableName, select[this.setting.fullPath] + '-%']) : await this.db.query(sql, [this.ctx.service.contract.tableName, select[this.setting.fullPath] + '-%']);
  965. return data;
  966. }
  967. }
  968. return ContractTree;
  969. };