contract_tree.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. /**
  271. * tenderId标段中, 删除选中节点及其子节点
  272. *
  273. * @param {Number} tenderId - 标段id
  274. * @param {Number} selectId - 选中节点id
  275. * @return {Array} - 被删除的数据
  276. */
  277. async deleteNode(options, kid) {
  278. if (kid <= 0) return [];
  279. const select = await this.getDataByKid(options, kid);
  280. if (!select) throw '删除节点数据错误';
  281. const parent = await this.getDataByKid(options, select[this.setting.pid]);
  282. // 获取将要被删除的数据
  283. const deleteData = await this.getDataByFullPath(options, select[this.setting.fullPath] + '-%');
  284. deleteData.unshift(select);
  285. if (deleteData.length === 0) throw '删除节点数据错误';
  286. const transaction = await this.db.beginTransaction();
  287. try {
  288. // 删除
  289. if (select.c_code) {
  290. if (select.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) throw '当前合同无权删除';
  291. const contractPays = await this.ctx.service.contractPay.getDataByCondition({ cid: select.id });
  292. if (contractPays) throw '还存在合同支付项,无法删除';
  293. await transaction.delete(this.ctx.service.contract.tableName, { id: select.id });
  294. const attList = await this.ctx.service.contractAtt.getAllDataByCondition({ where: { cid: select.id } });
  295. await this.ctx.helper.delFiles(attList);
  296. await transaction.delete(this.ctx.service.contractAtt.tableName, { cid: select.id });
  297. } else {
  298. await transaction.delete(this.tableName, { id: select.id });
  299. const delOptions = this._.cloneDeep(options);
  300. delOptions.contract_id = this._.map(deleteData, 'contract_id');
  301. await transaction.delete(this.ctx.service.contractTreeAudit.tableName, delOptions);
  302. const contracts = this.ctx.helper._.filter(deleteData, function (item) {
  303. return item.c_code;
  304. });
  305. if (contracts.length > 0) {
  306. const contractUids = this.ctx.helper._.uniq(this.ctx.helper._.map(contracts, 'uid'));
  307. if (contractUids.length > 1 || !(contractUids[0] === this.ctx.session.sessionUser.accountId || this.ctx.session.sessionUser.is_admin)) throw '存在合同你无权删除';
  308. const contractPays = await transaction.select(this.ctx.service.contractPay.tableName, { where: { cid: this.ctx.helper._.map(contracts, 'id') } });
  309. if (contractPays.length > 0) throw '还存在合同支付项,无法删除';
  310. const attList = await this.ctx.service.contractAtt.getAllDataByCondition({ where: { cid: this.ctx.helper._.map(contracts, 'id') } });
  311. await this.ctx.helper.delFiles(attList);
  312. await transaction.delete(this.ctx.service.contractAtt.tableName, { cid: this.ctx.helper._.map(contracts, 'id') });
  313. }
  314. const operate = await this._deletePosterity(options, select, transaction);
  315. }
  316. // 选中节点--父节点 只有一个子节点时,应升级isLeaf
  317. if (parent) {
  318. const condition = { ...options };
  319. condition[this.setting.pid] = select[this.setting.pid];
  320. const count = await this.db.count(this.tableName, condition);
  321. const count1 = await this.db.count(this.ctx.service.contract.tableName, condition);
  322. const sum = count + count1;
  323. if (sum === 1) {
  324. const updateParent = {id: parent.id };
  325. updateParent[this.setting.isLeaf] = true;
  326. await transaction.update(this.tableName, updateParent);
  327. }
  328. }
  329. // 选中节点--全部后节点 order--
  330. await this._updateChildrenOrder(options, select[this.setting.pid], select[this.setting.order] + 1, -1, transaction);
  331. await transaction.commit();
  332. } catch (err) {
  333. await transaction.rollback();
  334. throw err;
  335. }
  336. // 查询结果
  337. const updateData = await this.getNextsData(options, select[this.setting.pid], select[this.setting.order] - 1);
  338. if (parent) {
  339. const updateData1 = await this.getDataByKid(options, select[this.setting.pid]);
  340. if (updateData1[this.setting.isLeaf]) {
  341. updateData.push(updateData1);
  342. }
  343. }
  344. return { delete: deleteData, update: updateData };
  345. }
  346. async deleteNodes(options, kid, count) {
  347. const _ = this.ctx.helper._;
  348. if ((kid <= 0) || (count <= 0)) return [];
  349. const selects = await this.getDataByKidAndCount(options, kid, count);
  350. const first = selects[0];
  351. const parent = await this.getDataByKid(options, first[this.setting.pid]);
  352. const condition = { ...options };
  353. condition[this.setting.pid] = parent[this.setting.kid];
  354. const childCount1 = parent ? await this.count(condition) : -1;
  355. const childCount2 = parent ? await this.db.count(this.ctx.service.contract.tableName, condition) : -1;
  356. const childCount = childCount1 + childCount2;
  357. let deleteData = [];
  358. for (const s of selects) {
  359. deleteData = deleteData.concat(await this.getDataByFullPath(options, s[this.setting.fullPath] + '-%'));
  360. deleteData.push(s);
  361. }
  362. const transaction = await this.db.beginTransaction();
  363. try {
  364. // 删除
  365. for (const s of selects) {
  366. if (s.c_code) {
  367. if (s.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) throw '存在合同你无权删除';
  368. const contractPays = await this.ctx.service.contractPay.getDataByCondition({ cid: s.id });
  369. if (contractPays) throw '部分合同还存在合同支付项,无法删除';
  370. await transaction.delete(this.ctx.service.contract.tableName, { id: s.id });
  371. const attList = await this.ctx.service.contractAtt.getAllDataByCondition({ where: { cid: s.id } });
  372. await this.ctx.helper.delFiles(attList);
  373. await transaction.delete(this.ctx.service.contractAtt.tableName, { cid: s.id });
  374. } else {
  375. await transaction.delete(this.tableName, { id: s.id });
  376. const contracts = _.filter(deleteData, function (item) {
  377. return item.c_code && _.includes(s.full_path, item.full_path);
  378. });
  379. if (contracts.length > 0) {
  380. const contractUids = _.uniq(_.map(contracts, 'uid'));
  381. if (contractUids.length > 1 || !(contractUids[0] === this.ctx.session.sessionUser.accountId || this.ctx.session.sessionUser.is_admin)) throw '存在合同你无权删除';
  382. const contractPays = await transaction.select(this.ctx.service.contractPay.tableName, { where: { cid: _.map(contracts, 'id') } });
  383. if (contractPays.length > 0) throw '还存在合同支付项,无法删除';
  384. const attList = await this.ctx.service.contractAtt.getAllDataByCondition({ where: { cid: _.map(contracts, 'id') } });
  385. await this.ctx.helper.delFiles(attList);
  386. await transaction.delete(this.ctx.service.contractAtt.tableName, { cid: _.map(contracts, 'id') });
  387. }
  388. }
  389. const operate = await this._deletePosterity(options, s, transaction);
  390. }
  391. // 选中节点--父节点 只有一个子节点时,应升级isLeaf
  392. if (parent && childCount === count) {
  393. const updateParent = {id: parent.id };
  394. updateParent[this.setting.isLeaf] = true;
  395. await transaction.update(this.tableName, updateParent);
  396. }
  397. // 选中节点--全部后节点 order--
  398. await this._updateChildrenOrder(options, first[this.setting.pid], first[this.setting.order] + count, -count, transaction);
  399. const delOptions = this._.cloneDeep(options);
  400. delOptions.contract_id = this._.map(deleteData, 'contract_id');
  401. await transaction.delete(this.ctx.service.contractTreeAudit.tableName, delOptions);
  402. await transaction.commit();
  403. } catch (err) {
  404. await transaction.rollback();
  405. throw err;
  406. }
  407. const updateData = await this.getNextsData(options, first[this.setting.pid], first[this.setting.order] - 1);
  408. if (parent && childCount === count) {
  409. const updateData1 = await this.getDataByKid(options, parent[this.setting.kid]);
  410. updateData.push(updateData1);
  411. }
  412. return { delete: deleteData, update: updateData };
  413. }
  414. async delete(options, kid, count = null) {
  415. if (count && count > 1) {
  416. return await this.deleteNodes(options, kid, count);
  417. } else {
  418. return await this.deleteNode(options, kid);
  419. }
  420. }
  421. /**
  422. * 上移节点
  423. *
  424. * @param {Number} mid - master id
  425. * @param {Number} kid - 选中节点id
  426. * @return {Array} - 发生改变的数据
  427. */
  428. async upMoveNode(options, kid, count) {
  429. if (!count) count = 1;
  430. if (!kid || (kid <= 0)) return null;
  431. const selects = await this.getDataByKidAndCount(options, kid, count);
  432. if (selects.length !== count) throw '上移节点数据错误';
  433. const first = selects[0];
  434. const pre = await this.getDataByParentAndOrder(options, first[this.setting.pid], first[this.setting.order] - 1);
  435. if (!pre) throw '节点不可上移';
  436. const order = [];
  437. const transaction = await this.db.beginTransaction();
  438. try {
  439. for (const s of selects) {
  440. const sData = { id: s.id };
  441. sData[this.setting.order] = s[this.setting.order] - 1;
  442. !s.c_code ? await transaction.update(this.tableName, sData) : await transaction.update(this.ctx.service.contract.tableName, sData);
  443. order.push(s[this.setting.order] - 1);
  444. }
  445. const pData = { id: pre.id };
  446. pData[this.setting.order] = pre[this.setting.order] + count;
  447. !pre.c_code ? await transaction.update(this.tableName, pData) : await transaction.update(this.ctx.service.contract.tableName, pData);
  448. order.push(pre[this.setting.order] + count);
  449. await transaction.commit();
  450. } catch (err) {
  451. await transaction.rollback();
  452. throw err;
  453. }
  454. const resultData = await this.getDataByParentAndOrder(options, first[this.setting.pid], order);
  455. return { update: resultData };
  456. }
  457. /**
  458. * 下移节点
  459. *
  460. * @param {Number} mid - master id
  461. * @param {Number} kid - 选中节点id
  462. * @return {Array} - 发生改变的数据
  463. */
  464. async downMoveNode(options, kid, count) {
  465. if (!count) count = 1;
  466. if (!kid || (kid <= 0)) return null;
  467. const selects = await this.getDataByKidAndCount(options, kid, count);
  468. if (selects.length !== count) {
  469. throw '下移节点数据错误';
  470. }
  471. const last = selects[count - 1];
  472. const next = await this.getDataByParentAndOrder(options, last[this.setting.pid], last[this.setting.order] + 1);
  473. if (!next) {
  474. throw '节点不可下移';
  475. }
  476. const order = [];
  477. const transaction = await this.db.beginTransaction();
  478. try {
  479. for (const s of selects) {
  480. const sData = { id: s.id };
  481. sData[this.setting.order] = s[this.setting.order] + 1;
  482. !s.c_code ? await transaction.update(this.tableName, sData) : await transaction.update(this.ctx.service.contract.tableName, sData);
  483. order.push(s[this.setting.order] + 1);
  484. }
  485. const nData = { id: next.id };
  486. nData[this.setting.order] = next[this.setting.order] - count;
  487. !next.c_code ? await transaction.update(this.tableName, nData) : await transaction.update(this.ctx.service.contract.tableName, nData);
  488. order.push(next[this.setting.order] - count);
  489. await transaction.commit();
  490. } catch (err) {
  491. await transaction.rollback();
  492. throw err;
  493. }
  494. const resultData = await this.getDataByParentAndOrder(options, last[this.setting.pid], order);
  495. return { update: resultData };
  496. }
  497. /**
  498. * 升级节点
  499. *
  500. * @param {Number} tenderId - 标段id
  501. * @param {Number} selectId - 选中节点id
  502. * @return {Array} - 发生改变的数据
  503. */
  504. async upLevelNode(options, kid, count) {
  505. if (!count) count = 1;
  506. const selects = await this.getDataByKidAndCount(options, kid, count);
  507. if (selects.length !== count) throw '升级节点数据错误';
  508. const first = selects[0], last = selects[count - 1];
  509. const parent = await this.getDataByKid(options, first[this.setting.pid]);
  510. if (!parent) throw '升级节点数据错误';
  511. const newPath = [];
  512. const transaction = await this.db.beginTransaction();
  513. try {
  514. // 选中节点--父节点 选中节点为firstChild时,修改isLeaf
  515. if (first[this.setting.order] === 1) {
  516. const updateParentData = { id: parent.id };
  517. updateParentData[this.setting.isLeaf] = true;
  518. await transaction.update(this.tableName, updateParentData);
  519. }
  520. // 选中节点--父节点--全部后兄弟节点 order+1
  521. await this._updateChildrenOrder(options, parent[this.setting.pid], parent[this.setting.order] + 1, count, transaction);
  522. for (const [i, s] of selects.entries()) {
  523. // 选中节点 修改pid, order, fullPath, level, isLeaf, 清空计算项
  524. const updateData = { id: s.id };
  525. updateData[this.setting.pid] = parent[this.setting.pid];
  526. updateData[this.setting.order] = parent[this.setting.order] + i + 1;
  527. updateData[this.setting.level] = s[this.setting.level] - 1;
  528. updateData[this.setting.fullPath] = s[this.setting.fullPath].replace(`-${s[this.setting.pid]}-`, '-');
  529. newPath.push(updateData[this.setting.fullPath]);
  530. if (s[this.setting.isLeaf] && s.id === last.id) {
  531. const nexts = await this.getNextsData(options, parent[this.setting.kid], last[this.setting.order]);
  532. if (nexts.length > 0) {
  533. updateData[this.setting.isLeaf] = false;
  534. }
  535. }
  536. await transaction.update(this.tableName, updateData);
  537. // 选中节点--全部子节点(含孙) level-1, fullPath变更
  538. await this._syncUplevelChildren(options, s, transaction);
  539. }
  540. // 选中节点--全部后兄弟节点 收编为子节点 修改pid, order, fullPath
  541. await this._syncUpLevelNexts(options, last, transaction);
  542. await transaction.commit();
  543. } catch (err) {
  544. await transaction.rollback();
  545. throw err;
  546. }
  547. // 查询修改的数据
  548. let updateData = await this.getNextsData(options, parent[this.setting.pid], parent[this.setting.order] - 1);
  549. for (const path of newPath) {
  550. const children = await this.getDataByFullPath(options, path + '-%');
  551. updateData = updateData.concat(children);
  552. }
  553. return { update: updateData };
  554. }
  555. /**
  556. * 降级节点
  557. *
  558. * @param {Number} tenderId - 标段id
  559. * @param {Number} selectId - 选中节点id
  560. * @return {Array} - 发生改变的数据
  561. */
  562. async downLevelNode(options, kid, count) {
  563. if (!count) count = 1;
  564. const selects = await this.getDataByKidAndCount(options, kid, count);
  565. if (!selects) throw '降级节点数据错误';
  566. const first = selects[0], last = selects[count - 1];
  567. const pre = await this.getDataByParentAndOrder(options, first[this.setting.pid], first[this.setting.order] - 1);
  568. if (!pre) throw '节点不可降级';
  569. const preLastChild = await this.getLastChildData(options, pre[this.setting.kid]);
  570. const newPath = [];
  571. const transaction = await this.db.beginTransaction();
  572. try {
  573. // 选中节点--全部后节点 order--
  574. await this._updateChildrenOrder(options, first[this.setting.pid], last[this.setting.order] + 1, -count, transaction);
  575. for (const [i, s] of selects.entries()) {
  576. // 选中节点 修改pid, level, order, fullPath
  577. const updateData = { id: s.id };
  578. updateData[this.setting.pid] = pre[this.setting.kid];
  579. updateData[this.setting.order] = preLastChild ? preLastChild[this.setting.order] + i + 1 : i + 1;
  580. updateData[this.setting.level] = s[this.setting.level] + 1;
  581. if (s[this.setting.level] === 1) {
  582. updateData[this.setting.fullPath] = pre[this.setting.kid] + '-' + s[this.setting.kid];
  583. } else {
  584. const index = s[this.setting.fullPath].lastIndexOf(s[this.setting.kid]);
  585. updateData[this.setting.fullPath] = s[this.setting.fullPath].substring(0, index-1) + '-' + pre[this.setting.kid] + '-' + s[this.setting.kid];
  586. }
  587. newPath.push(updateData[this.setting.fullPath]);
  588. await transaction.update(this.tableName, updateData);
  589. // 选中节点--全部子节点(含孙) level++, fullPath
  590. await this._syncDownlevelChildren(options, s, updateData[this.setting.fullPath], transaction);
  591. }
  592. // 选中节点--前兄弟节点 isLeaf应为false, 清空计算相关字段
  593. const updateData2 = { id: pre.id };
  594. updateData2[this.setting.isLeaf] = false;
  595. await transaction.update(this.tableName, updateData2);
  596. await transaction.commit();
  597. } catch (err) {
  598. await transaction.rollback();
  599. throw err;
  600. }
  601. // 查询修改的数据
  602. let updateData = await this.getNextsData(options, pre[this.setting.pid], pre[this.setting.order] - 1);
  603. // 选中节点及子节点
  604. for (const p of newPath) {
  605. updateData = updateData.concat(await this.getDataByFullPath(options, p + '-%'));
  606. }
  607. updateData = updateData.concat(await this.getDataById(selects.map(x => { return x.id; })));
  608. // 选中节点--原前兄弟节点&全部后兄弟节点
  609. return { update: updateData };
  610. }
  611. async pasteBlockData(options, kid, pasteData, defaultData) {
  612. if (!options[this.setting.type]) throw '参数有误';
  613. if (!pasteData || pasteData.length <= 0) throw '复制数据错误';
  614. for (const pd of pasteData) {
  615. if (!pd || pd.length <= 0) throw '复制数据错误';
  616. pd.sort(function (x, y) {
  617. return x.level - y.level
  618. });
  619. if (pd[0].contract_pid !== pasteData[0][0].contract_pid) throw '复制数据错误:仅可操作同层节点';
  620. }
  621. const selectData = await this.getDataByKid(options, kid);
  622. if (!selectData) throw '粘贴数据错误';
  623. const newParentPath = selectData.full_path.replace(selectData.contract_id, '');
  624. const pasteBillsData = [];
  625. let maxId = await this._getMaxLid(options);
  626. for (const [i, pd] of pasteData.entries()) {
  627. for (const d of pd) {
  628. d.children = pd.filter(function (x) {
  629. return x.contract_pid === d.contract_id;
  630. });
  631. }
  632. const pbd = [];
  633. for (const [j, d] of pd.entries()) {
  634. const newBills = {
  635. id: this.uuid.v4(),
  636. spid: options.spid || null,
  637. tid: options.tid || null,
  638. contract_type: options.contract_type,
  639. contract_id: maxId + j + 1,
  640. contract_pid: j === 0 ? selectData.contract_pid : d.contract_pid,
  641. level: d.level + selectData.level - pd[0].level,
  642. order: j === 0 ? selectData.order + i + 1 : d.order,
  643. is_leaf: d.is_leaf,
  644. code: d.code,
  645. name: d.name,
  646. remark: d.remark,
  647. };
  648. for (const c of d.children) {
  649. c.contract_pid = newBills.contract_id;
  650. }
  651. pbd.push(newBills);
  652. }
  653. for (const d of pbd) {
  654. const parent = pbd.find(function (x) {
  655. return x.contract_id === d.contract_pid;
  656. });
  657. d.full_path = parent
  658. ? parent.full_path + '-' + d.contract_id
  659. : newParentPath + d.contract_id;
  660. if (defaultData) this.ctx.helper._.assignIn(pbd, defaultData);
  661. pasteBillsData.push(d);
  662. }
  663. maxId = maxId + pbd.length;
  664. }
  665. const transaction = await this.db.beginTransaction();
  666. try {
  667. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  668. await this._updateChildrenOrder(options, selectData.ledger_pid, selectData.order + 1, pasteData.length, transaction);
  669. // 数据库创建新增节点数据
  670. if (pasteBillsData.length > 0) {
  671. const newData = await transaction.insert(this.tableName, pasteBillsData);
  672. }
  673. this._cacheMaxLid(options, maxId);
  674. await transaction.commit();
  675. } catch (err) {
  676. await transaction.rollback();
  677. throw err;
  678. }
  679. // 查询应返回的结果
  680. const updateData = await this.getNextsData(options, selectData.contract_pid, selectData.order + pasteData.length);
  681. return {
  682. ledger: { create: pasteBillsData, update: updateData },
  683. };
  684. }
  685. /**
  686. * 删除节点
  687. * @param {Number} tenderId - 标段id
  688. * @param {Object} deleteData - 删除节点数据
  689. * @return {Promise<*>}
  690. * @private
  691. */
  692. async _deletePosterity(options, node, transaction = null) {
  693. const sql = 'DELETE FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  694. const sqlParam = [this.tableName, node[this.setting.fullPath] + '-%'];
  695. const result = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam)
  696. const sql1 = 'DELETE FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  697. const sqlParam1 = [this.ctx.service.contract.tableName, node[this.setting.fullPath] + '-%'];
  698. const result1 = transaction ? await transaction.query(sql1, sqlParam1) : await this.db.query(sql1, sqlParam1)
  699. return result;
  700. }
  701. /**
  702. * 根据fullPath获取数据 fullPath Like ‘1.2.3%’(传参fullPath = '1.2.3%')
  703. * @param {Number} tenderId - 标段id
  704. * @param {String} fullPath - 路径
  705. * @return {Promise<void>}
  706. */
  707. async getDataByFullPath(options, fullPath) {
  708. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  709. const sqlParam = [this.tableName, fullPath];
  710. const resultData = await this.db.query(sql, sqlParam);
  711. const sql1 = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  712. const sqlParam1 = [this.ctx.service.contract.tableName, fullPath];
  713. const resultData1 = await this.db.query(sql1, sqlParam1);
  714. return resultData.concat(resultData1).sort((a, b) => a.order - b.order);
  715. }
  716. async getChildBetween(options, pid, order1, order2) {
  717. const sql = 'SELECT * FROM ?? WHERE '+ this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `order` > ? AND `order` < ? ORDER BY `order` ASC';
  718. const sqlParam = [this.tableName, pid, order1, order2];
  719. const data = await this.db.query(sql, sqlParam);
  720. const sql1 = 'SELECT * FROM ?? WHERE '+ this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `order` > ? AND `order` < ? ORDER BY `order` ASC';
  721. const sqlParam1 = [this.ctx.service.contract.tableName, pid, order1, order2];
  722. const data1 = await this.db.query(sql1, sqlParam1);
  723. const resultData = data.concat(data1).sort((a, b) => a.order - b.order);
  724. return resultData;
  725. }
  726. /**
  727. * 根据 父节点ID 和 节点排序order 获取全部后节点数据
  728. * @param {Number} mid - master id
  729. * @param {Number} pid - 父节点id
  730. * @param {Number} order - 排序
  731. * @return {Array}
  732. */
  733. async getNextsData(options, pid, order) {
  734. const sql = 'SELECT * FROM ?? WHERE '+ this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `order` > ? ORDER BY `order` ASC';
  735. const sqlParam = [this.tableName, pid, order];
  736. const data = await this.db.query(sql, sqlParam);
  737. const sql1 = 'SELECT * FROM ?? WHERE '+ this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `order` > ? ORDER BY `order` ASC';
  738. const sqlParam1 = [this.ctx.service.contract.tableName, pid, order];
  739. const data1 = await this.db.query(sql1, sqlParam1);
  740. // data和data1合并且按order排序
  741. const resultData = data.concat(data1).sort((a, b) => a.order - b.order);
  742. return resultData;
  743. }
  744. /**
  745. * 获取最末的子节点
  746. * @param {Number} mid - masterId
  747. * @param {Number} pid - 父节点id
  748. * @return {Object}
  749. */
  750. async getLastChildData(options, pid, transaction = null) {
  751. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? ORDER BY `order` DESC';
  752. const sqlParam = [this.tableName, pid];
  753. const resultData = await this.db.queryOne(sql, sqlParam);
  754. const sql1 = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? ORDER BY `order` DESC';
  755. const sqlParam1 = [this.ctx.service.contract.tableName, pid];
  756. const resultData1 = await this.db.queryOne(sql1, sqlParam1);
  757. // 比较两个结果,返回order大的
  758. if (resultData && resultData1) {
  759. return resultData.order > resultData1.order ? resultData : resultData1;
  760. } else {
  761. return resultData || resultData1;
  762. }
  763. }
  764. /**
  765. * 选中节点的后兄弟节点,全部变为当前节点的子节点
  766. * @param {Object} selectData - 选中节点
  767. * @return {Object}
  768. * @private
  769. */
  770. async _syncUpLevelNexts(options, select, transaction = null) {
  771. // 查询selectData的lastChild
  772. const lastChild = await this.getLastChildData(options, select[this.setting.kid]);
  773. const nexts = await this.getNextsData(options, select[this.setting.pid], select[this.setting.order]);
  774. if (nexts && nexts.length > 0) {
  775. // 修改nextsData pid, 排序
  776. // this.initSqlBuilder();
  777. // this.sqlBuilder.setUpdateData(this.setting.pid, {
  778. // value: select[this.setting.kid],
  779. // });
  780. // const orderInc = lastChild ? lastChild[this.setting.order] - select[this.setting.order] : - select[this.setting.order];
  781. // this.sqlBuilder.setUpdateData(this.setting.order, {
  782. // value: Math.abs(orderInc),
  783. // selfOperate: orderInc > 0 ? '+' : '-',
  784. // });
  785. // this.sqlBuilder.setAndWhere(this.setting.mid, {
  786. // value: select[this.setting.mid],
  787. // operate: '=',
  788. // });
  789. // this.sqlBuilder.setAndWhere(this.setting.pid, {
  790. // value: select[this.setting.pid],
  791. // operate: '=',
  792. // });
  793. // this.sqlBuilder.setAndWhere(this.setting.order, {
  794. // value: select[this.setting.order],
  795. // operate: '>',
  796. // });
  797. // const [sql1, sqlParam1] = this.sqlBuilder.build(this.tableName, 'update');
  798. const orderInc = lastChild ? lastChild[this.setting.order] - select[this.setting.order] : - select[this.setting.order];
  799. const sql1 = 'UPDATE ?? SET `' + this.setting.order + '` = ' + (orderInc > 0 ? '+' : '-') + Math.abs(orderInc) + ' WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ? AND `' + this.setting.order + '` > ?';
  800. const sqlParam1 = [this.tableName, select[this.setting.kid], select[this.setting.pid], select[this.setting.order]];
  801. transaction ? await transaction.query(sql1, sqlParam1) : await this.db.query(sql1, sqlParam1);
  802. // 选中节点 isLeaf应为false
  803. if (select[this.setting.isLeaf]) {
  804. const updateData = { id: select.id };
  805. updateData[this.setting.isLeaf] = false;
  806. transaction ? await transaction.update(this.tableName, updateData) : await this.db.update(this.tableName, updateData);
  807. }
  808. // 修改nextsData及其子节点的fullPath
  809. const oldSubStr = this.db.escape(select[this.setting.pid] + '-');
  810. const newSubStr = this.db.escape(select[this.setting.kid] + '-');
  811. const sqlArr = [];
  812. sqlArr.push('Update ?? SET `' + this.setting.fullPath + '` = Replace(`' + this.setting.fullPath + '`,' + oldSubStr + ',' + newSubStr + ') Where');
  813. sqlArr.push('(`' + this.ctx.helper._getOptionsSql(options) + ')');
  814. sqlArr.push(' And (');
  815. for (const data of nexts) {
  816. sqlArr.push('`' + this.setting.fullPath + '` Like ' + this.db.escape(data[this.setting.fullPath] + '%'));
  817. if (nexts.indexOf(data) < nexts.length - 1) {
  818. sqlArr.push(' Or ');
  819. }
  820. }
  821. sqlArr.push(')');
  822. const sql = sqlArr.join('');
  823. const resultData = transaction ? await transaction.query(sql, [this.tableName]) : await this.db.query(sql, [this.tableName]);
  824. return resultData;
  825. }
  826. }
  827. /**
  828. * 升级selectData, 同步修改所有子节点
  829. * @param {Object} selectData - 升级操作,选中节点
  830. * @return {Object}
  831. * @private
  832. */
  833. async _syncUplevelChildren(options, select, transaction = null) {
  834. // this.initSqlBuilder();
  835. // this.sqlBuilder.setAndWhere(this.setting.mid, {
  836. // value: select[this.setting.mid],
  837. // operate: '=',
  838. // });
  839. // this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  840. // value: this.db.escape(select[this.setting.fullPath] + '-%'),
  841. // operate: 'like',
  842. // });
  843. // this.sqlBuilder.setUpdateData(this.setting.level, {
  844. // value: 1,
  845. // selfOperate: '-',
  846. // });
  847. // this.sqlBuilder.setUpdateData(this.setting.fullPath, {
  848. // value: [this.setting.fullPath, this.db.escape(`-${select[this.setting.pid]}-`), this.db.escape('-')],
  849. // literal: 'Replace',
  850. // });
  851. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  852. const sql = 'UPDATE ?? SET ' + this.setting.level + ' = ' + this.setting.level + ' -1, ' + this.setting.fullPath + ' = ' +
  853. 'Replace('+ [this.setting.fullPath, this.db.escape(`-${select[this.setting.pid]}-`), this.db.escape('-')].join(',') +') ' +
  854. 'WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  855. const sqlParam = [this.tableName, select[this.setting.fullPath] + '-%'];
  856. const data = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  857. return data;
  858. }
  859. /**
  860. * 降级selectData, 同步修改所有子节点
  861. * @param {Object} selectData - 选中节点
  862. * @param {Object} preData - 选中节点的前一节点(降级后为父节点)
  863. * @return {Promise<*>}
  864. * @private
  865. */
  866. async _syncDownlevelChildren(options, select, newFullPath, transaction = null) {
  867. // this.initSqlBuilder();
  868. // this.sqlBuilder.setAndWhere(this.setting.mid, {
  869. // value: select[this.setting.mid],
  870. // operate: '=',
  871. // });
  872. // this.sqlBuilder.setAndWhere(this.setting.fullPath, {
  873. // value: this.db.escape(select[this.setting.fullPath] + '-%'),
  874. // operate: 'like',
  875. // });
  876. // this.sqlBuilder.setUpdateData(this.setting.level, {
  877. // value: 1,
  878. // selfOperate: '+',
  879. // });
  880. // this.sqlBuilder.setUpdateData(this.setting.fullPath, {
  881. // value: [this.setting.fullPath, this.db.escape(select[this.setting.fullPath] + '-'), this.db.escape(newFullPath + '-')],
  882. // literal: 'Replace',
  883. // });
  884. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  885. const sql = 'UPDATE ?? SET ' + this.setting.level + ' = ' + this.setting.level + ' + 1, ' + this.setting.fullPath + ' = ' +
  886. 'Replace(' + [[this.setting.fullPath, this.db.escape(select[this.setting.fullPath] + '-'), this.db.escape(newFullPath + '-')]].join(',') + ') ' +
  887. 'WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.fullPath + ' LIKE ?';
  888. const sqlParam = [this.tableName, select[this.setting.fullPath] + '-%'];
  889. const data = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  890. return data;
  891. }
  892. }
  893. return ContractTree;
  894. };