contract_tree.js 62 KB

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