base_bills_service.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const TreeService = require('./base_tree_service');
  10. // sql拼装器
  11. const rootId = -1;
  12. const calcFields = ['unit_price', 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'deal_qty', 'deal_tp', 'dgn_qty1', 'dgn_qty2'];
  13. const readOnlyFields = ['id', 'tender_id', 'ledger_id', 'ledger_pid', 'order', 'level', 'full_path', 'is_leaf'];
  14. const upFields = ['unit_price'];
  15. const qtyFields = ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'quantity', 'deal_qty', 'dgn_qty1', 'dgn_qty2'];
  16. const tpFields = ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price', 'deal_tp'];
  17. const measureType = require('../const/tender').measureType;
  18. const math = require('mathjs');
  19. class BaseBillsSerivce extends TreeService {
  20. // 继承方法
  21. clearParentingData(data) {
  22. data.unit_price = null;
  23. data.sgfh_qty = null;
  24. data.sgfh_tp = null;
  25. data.sjcl_qty = null;
  26. data.sjcl_tp = null;
  27. data.qtcl_qty = null;
  28. data.qtcl_tp = null;
  29. data.quantity = null;
  30. data.total_price = null;
  31. data.deal_qty = null;
  32. data.deal_tp = null;
  33. }
  34. /**
  35. * 从标准数据中提取有效数据
  36. * @param {Object} stdData - 从标准库中查询所得
  37. * @returns {name, unit, source, code, b_code}
  38. * @private
  39. */
  40. _filterStdData(stdData) {
  41. const result = {
  42. name: stdData.name,
  43. unit: stdData.unit,
  44. source: stdData.source,
  45. node_type: stdData.node_type,
  46. };
  47. result.code = stdData.code ? stdData.code : '';
  48. result.b_code = stdData.b_code ? stdData.b_code : '';
  49. return result;
  50. }
  51. async addBillsNode(tenderId, selectId, data, reviseId) {
  52. if (data) {
  53. if (reviseId) data.crid = reviseId;
  54. return await this.addNode(tenderId, selectId, data);
  55. } else {
  56. return await this.addNode(tenderId, selectId, {crid: reviseId});
  57. }
  58. }
  59. /**
  60. * 新增子节点,并排在所有子节点的末尾
  61. * @param {Number} tenderId - 标段Id
  62. * @param {Number} selectId - 父节点Id
  63. * @param {Object} data - 新增节点数据(编号名称等)
  64. * @returns {Promise<*>}
  65. */
  66. async addChild(tenderId, selectId, data, reviseId) {
  67. if ((tenderId <= 0) || (selectId <= 0)) {
  68. return [];
  69. }
  70. const selectData = await this.getDataByKid(tenderId, selectId);
  71. if (!selectData) {
  72. throw '新增节点数据错误';
  73. }
  74. const children = await this.getChildrenByParentId(tenderId, selectId);
  75. const maxId = await this._getMaxLid(tenderId);
  76. data.id = this.uuid.v4();
  77. data.tender_id = tenderId;
  78. data.ledger_id = maxId + 1;
  79. data.ledger_pid = selectData.ledger_id;
  80. data.level = selectData.level + 1;
  81. data.order = children.length + 1;
  82. data.full_path = selectData.full_path + '-' + data.ledger_id;
  83. data.is_leaf = true;
  84. if (reviseId) data.crid = reviseId;
  85. this.transaction = await this.db.beginTransaction();
  86. try {
  87. const result = await this.transaction.insert(this.tableName, data);
  88. if (children.length === 0) {
  89. await this.transaction.update(this.tableName,
  90. {
  91. is_leaf: false,
  92. sgfh_qty: null, sgfh_tp: null, qtcl_qty: null, qtcl_tp: null, sjcl_qty: null, sjcl_tp: null,
  93. quantity: null, unit_price: null, total_price: null, deal_qty: null, deal_tp: null
  94. },
  95. { where: {tender_id: tenderId, ledger_id: selectData.ledger_id} });
  96. }
  97. await this.transaction.commit();
  98. } catch(err) {
  99. this.transaction.rollback();
  100. throw err;
  101. }
  102. this._cacheMaxLid(tenderId, maxId + 1);
  103. // 查询应返回的结果
  104. const resultData = {};
  105. resultData.create = await this.getDataByKid(tenderId, data.ledger_id);
  106. if (children.length === 0) {
  107. resultData.update = await this.getDataByKid(tenderId, selectId);
  108. }
  109. return resultData;
  110. }
  111. /**
  112. * 添加节点(来自标准清单)
  113. * @param {Number} tenderId
  114. * @param {Number} selectId
  115. * @param {Object} stdData
  116. * @return {Promise<*>}
  117. */
  118. async addStdNode(tenderId, selectId, stdData, reviseId) {
  119. const newData = this._filterStdData(stdData);
  120. const result = await this.addBillsNode(tenderId, selectId, newData, reviseId);
  121. return result;
  122. }
  123. /**
  124. * 添加标准节点,将选择的标准节点,添加为子节点(排序为末尾)
  125. * @param {Number} tenderId - 标段Id
  126. * @param {Number} selectId - 添加目标节点Id
  127. * @param {Object} stdData - 标准节点数据
  128. * @returns {Promise<*>}
  129. */
  130. async addStdNodeAsChild(tenderId, selectId, stdData, reviseId) {
  131. const newData = this._filterStdData(stdData);
  132. const result = await this.addChild(tenderId, selectId, newData, reviseId);
  133. return result;
  134. }
  135. /**
  136. * 根据parentData, data新增数据(新增为parentData的最后一个子项)
  137. * @param {Number} tenderId - 标段id
  138. * @param {Object} parentData - 父项数据
  139. * @param {Object} data - 新增节点,初始数据
  140. * @return {Promise<*>} - 新增结果
  141. * @private
  142. */
  143. async _addChildNodeData(tenderId, parentData, data, reviseId) {
  144. if (tenderId <= 0) {
  145. return undefined;
  146. }
  147. if (!data) {
  148. data = {};
  149. }
  150. const pid = parentData ? parentData.ledger_id : rootId;
  151. const maxId = await this._getMaxLid(tenderId);
  152. data.id = this.uuid.v4();
  153. data.tender_id = tenderId;
  154. data.ledger_id = maxId + 1;
  155. data.ledger_pid = pid;
  156. if (data.order === undefined) {
  157. data.order = 1;
  158. }
  159. data.level = parentData ? parentData.level + 1 : 1;
  160. data.full_path = parentData ? parentData.full_path + '-' + data.ledger_id : '' + data.ledger_id;
  161. if (data.is_leaf === undefined) {
  162. data.is_leaf = true;
  163. }
  164. if (reviseId) data.crid = reviseId;
  165. const result = await this.transaction.insert(this.tableName, data);
  166. this._cacheMaxLid(tenderId, maxId + 1);
  167. return [result, data];
  168. }
  169. /**
  170. * 根据parentData, data新增数据(自动排序)
  171. * @param tenderId
  172. * @param parentData
  173. * @param data
  174. * @return {Promise<void>}
  175. * @private
  176. */
  177. async _addChildAutoOrder(tenderId, parentData, data, reviseId) {
  178. const self = this;
  179. const findPreData = function(list, a) {
  180. if (!list || list.length === 0) { return null; }
  181. for (let i = 0, iLen = list.length; i < iLen; i++) {
  182. if (self.ctx.helper.compareCode(list[i].code, a.code) > 0) {
  183. return i > 0 ? list[i - 1] : null;
  184. }
  185. }
  186. return list[list.length - 1];
  187. };
  188. const pid = parentData ? parentData.ledger_id : rootId;
  189. const children = await this.getChildrenByParentId(tenderId, pid);
  190. const preData = findPreData(children, data);
  191. if (!preData || children.indexOf(preData) < children.length - 1) {
  192. await this._updateChildrenOrder(tenderId, pid, preData ? preData.order + 1 : 1);
  193. }
  194. data.order = preData ? preData.order + 1 : 1;
  195. const [addResult, node] = await this._addChildNodeData(tenderId, parentData, data, reviseId);
  196. return [addResult, node];
  197. }
  198. /**
  199. * 添加节点,并同步添加父节点
  200. * @param {Number} tenderId - 标段id
  201. * @param {Number} selectId - 选中节点id
  202. * @param {Object} stdData - 节点数据
  203. * @param {StandardLib} stdLib - 标准库
  204. * @return {Promise<void>}
  205. */
  206. async addStdNodeWithParent(tenderId, stdData, stdLib, reviseId) {
  207. // 查询完整标准清单,并按层次排序
  208. const fullLevel = await stdLib.getFullLevelDataByFullPath(stdData.list_id, stdData.full_path);
  209. fullLevel.sort(function(x, y) {
  210. return x.level - y.level;
  211. });
  212. let isNew = false,
  213. node,
  214. firstNew,
  215. updateParent,
  216. addResult;
  217. const expandIds = [];
  218. this.transaction = await this.db.beginTransaction();
  219. try {
  220. // 从最顶层节点依次查询是否存在,否则添加
  221. for (let i = 0, len = fullLevel.length; i < len; i++) {
  222. const stdNode = fullLevel[i];
  223. if (isNew) {
  224. const newData = this._filterStdData(stdNode);
  225. newData.is_leaf = (i === len - 1);
  226. [addResult, node] = await this._addChildNodeData(tenderId, node, newData, reviseId);
  227. } else {
  228. const parent = node;
  229. node = await this.getDataByCondition({
  230. tender_id: tenderId,
  231. ledger_pid: parent ? parent.ledger_id : rootId,
  232. code: stdNode.code,
  233. name: stdNode.name,
  234. });
  235. if (!node) {
  236. isNew = true;
  237. const newData = this._filterStdData(stdNode);
  238. newData.is_leaf = (i === len - 1);
  239. [addResult, node] = await this._addChildAutoOrder(tenderId, parent, newData, reviseId);
  240. if (parent && parent.is_leaf) {
  241. await this.transaction.update(this.tableName, { id: parent.id, is_leaf: false,
  242. unit_price: null, quantity: null, total_price: null, deal_qty: null, deal_tp: null});
  243. updateParent = parent;
  244. }
  245. firstNew = node;
  246. } else {
  247. expandIds.push(node.ledger_id);
  248. }
  249. }
  250. }
  251. await this.transaction.commit();
  252. } catch (err) {
  253. await this.transaction.rollback();
  254. throw err;
  255. }
  256. // 查询应返回的结果
  257. let createData = [],
  258. updateData = [];
  259. if (firstNew) {
  260. createData = await this.getDataByFullPath(tenderId, firstNew.full_path + '%');
  261. updateData = await this.getNextsData(tenderId, firstNew.ledger_pid, firstNew.order);
  262. if (updateParent) {
  263. updateData.push(await this.getDataByCondition({ id: updateParent.id }));
  264. }
  265. }
  266. return { create: createData, update: updateData };
  267. }
  268. /**
  269. * 过滤data中update方式不可提交的字段
  270. * @param {Number} id - 主键key
  271. * @param {Object} data
  272. * @return {Object<{id: *}>}
  273. * @private
  274. */
  275. _filterUpdateInvalidField(id, data) {
  276. const result = {
  277. id,
  278. };
  279. for (const prop in data) {
  280. if (readOnlyFields.indexOf(prop) === -1) {
  281. result[prop] = data[prop];
  282. }
  283. }
  284. return result;
  285. }
  286. /**
  287. * newData中,以orgData为基准,过滤掉orgData中未定义或值相等的部分
  288. * @param {Object} orgData
  289. * @param {Object} newData
  290. * @private
  291. */
  292. _filterChangedField(orgData, newData) {
  293. const result = {};
  294. let bChanged = false;
  295. for (const prop in orgData) {
  296. if (this._.isEmpty(newData[prop]) && newData[prop] !== orgData[prop]) {
  297. result[prop] = newData[prop];
  298. bChanged = true;
  299. }
  300. }
  301. return bChanged ? result : undefined;
  302. }
  303. /**
  304. * 检查data中是否含有计算字段
  305. * @param {Object} data
  306. * @return {boolean}
  307. * @private
  308. */
  309. _checkCalcField(data) {
  310. for (const prop in data) {
  311. if (calcFields.indexOf(prop) >= 0) {
  312. return true;
  313. }
  314. }
  315. return false;
  316. }
  317. /**
  318. * 提交数据 - 响应计算(增量方式计算)
  319. * @param {Number} tenderId
  320. * @param {Object} data
  321. * @return {Promise<*>}
  322. */
  323. async updateCalc(tenderId, data) {
  324. // 简单验证数据
  325. if (tenderId <= 0 || !this.ctx.tender) {
  326. throw '标段不存在';
  327. }
  328. const info = this.ctx.tender.info;
  329. if (!data) {
  330. throw '提交数据错误';
  331. }
  332. const datas = data instanceof Array ? data : [data];
  333. const ids = [];
  334. for (const row of datas) {
  335. if (tenderId !== row.tender_id) {
  336. throw '提交数据错误';
  337. }
  338. ids.push(row.id);
  339. }
  340. this.transaction = await this.db.beginTransaction();
  341. try {
  342. for (const row of datas) {
  343. const updateNode = await this.getDataById(row.id);
  344. if (!updateNode || tenderId !== updateNode.tender_id || row.ledger_id !== updateNode.ledger_id) {
  345. throw '提交数据错误';
  346. }
  347. let updateData;
  348. if (row.unit) {
  349. if (row.sgfh_qty === undefined) { row.sgfh_qty = updateNode.sgfh_qty; }
  350. if (row.sjcl_qty === undefined) { row.sjcl_qty = updateNode.sjcl_qty; }
  351. if (row.qtcl_qty === undefined) { row.qtcl_qty = updateNode.qtcl_qty; }
  352. if (row.deal_qty === undefined) { row.deal_qty = updateNode.deal_qty; }
  353. }
  354. if (row.b_code) {
  355. row.dgn_qty1 = null;
  356. row.dgn_qty2 = null;
  357. row.code = null;
  358. }
  359. if (row.code) {
  360. row.b_code = null;
  361. }
  362. if (this._checkCalcField(row)) {
  363. let calcData = JSON.parse(JSON.stringify(row));
  364. const precision = this.ctx.helper.findPrecision(info.precision, row.unit ? row.unit : updateNode.unit);
  365. // 数量保留小数位数
  366. this.ctx.helper.checkFieldPrecision(calcData, qtyFields, precision.value);
  367. // 单位保留小数位数
  368. this.ctx.helper.checkFieldPrecision(calcData, upFields, info.decimal.up);
  369. // 未提交单价则读取数据库单价
  370. if (row.unit_price === undefined) calcData.unit_price = updateNode.unit_price;
  371. // 计算
  372. if (row.sgfh_qty !== undefined || row.sjcl_qty !== undefined || row.qtcl_qty !== undefined ||
  373. row.deal_qty !== undefined || row.unit_price) {
  374. if (row.sgfh_qty === undefined) calcData.sgfh_qty = updateNode.sgfh_qty;
  375. if (row.sjcl_qty === undefined) calcData.sjcl_qty = updateNode.sjcl_qty;
  376. if (row.qtcl_qty === undefined) calcData.qtcl_qty = updateNode.qtcl_qty;
  377. if (row.deal_qty === undefined) calcData.deal_qty = updateNode.deal_qty;
  378. calcData.quantity = this.ctx.helper.sum([calcData.sgfh_qty, calcData.sjcl_qty, calcData.qtcl_qty]);
  379. calcData.sgfh_tp = this.ctx.helper.mul(calcData.sgfh_qty, calcData.unit_price, info.decimal.tp);
  380. calcData.sjcl_tp = this.ctx.helper.mul(calcData.sjcl_qty, calcData.unit_price, info.decimal.tp);
  381. calcData.qtcl_tp = this.ctx.helper.mul(calcData.qtcl_qty, calcData.unit_price, info.decimal.tp);
  382. calcData.total_price = this.ctx.helper.mul(calcData.quantity, calcData.unit_price, info.decimal.tp);
  383. calcData.deal_tp = this.ctx.helper.mul(calcData.deal_qty, calcData.unit_price, info.decimal.tp);
  384. } else if (row.sgfh_tp !== undefined || row.sjcl_tp !== undefined || row.qtcl_tp !== undefined || row.deal_tp !== undefined) {
  385. calcData.sgfh_qty = null;
  386. calcData.sjcl_qty = null;
  387. calcData.qtcl_qty = null;
  388. calcData.quantity = null;
  389. calcData.deal_qty = null;
  390. calcData.sgfh_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.sgfh_tp, info.decimal.tp) : updateNode.sgfh_tp;
  391. calcData.sjcl_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.sjcl_tp, info.decimal.tp) : updateNode.sjcl_tp;
  392. calcData.qtcl_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.qtcl_tp, info.decimal.tp) : updateNode.qtcl_tp;
  393. calcData.total_price = this.ctx.helper.sum([calcData.sgfh_tp, calcData.sjcl_tp, calcData.qtcl_tp]);
  394. calcData.deal_tp = (row.deal_tp !== undefined) ? this.ctx.helper.round(calcData.row.deal_tp, info.decimal.tp) : updateNode.deal_tp;
  395. } else if (row.unit_price !== undefined) {
  396. calcData.sgfh_tp = this.ctx.helper.mul(calcData.sgfh_qty, calcData.unit_price, info.decimal.tp);
  397. calcData.sjcl_tp = this.ctx.helper.mul(calcData.sjcl_qty, calcData.unit_price, info.decimal.tp);
  398. calcData.qtcl_tp = this.ctx.helper.mul(calcData.qtcl_qty, calcData.unit_price, info.decimal.tp);
  399. calcData.total_price = this.ctx.helper.mul(calcData.quantity, calcData.unit_price, info.decimal.tp);
  400. calcData.deal_tp = this.ctx.helper.mul(calcData.deal_qty, calcData.unit_price, info.decimal.tp);
  401. }
  402. updateData = this._filterUpdateInvalidField(updateNode.id, calcData);
  403. } else {
  404. updateData = this._filterUpdateInvalidField(updateNode.id, row);
  405. }
  406. await this.transaction.update(this.tableName, updateData);
  407. }
  408. await this.transaction.commit();
  409. this.transaction = null;
  410. } catch (err) {
  411. await this.transaction.rollback();
  412. this.transaction = null;
  413. throw err;
  414. }
  415. return { update: await this.getDataById(ids) };
  416. }
  417. // 统计方法
  418. async addUp(condition) {
  419. const sql = 'SELECT Sum(total_price) As total_price, Sum(deal_tp) As deal_tp' +
  420. ' FROM ' + this.tableName + this.ctx.helper.whereSql(condition);
  421. const result = await this.db.queryOne(sql);
  422. return result;
  423. }
  424. // 导入Excel
  425. async importGclExcel(id, sheet, data) {
  426. const node = await this.getDataById(id);
  427. if (!node || !node.is_leaf || node.tender_id !== this.ctx.tender.id) throw '数据错误';
  428. const maxId = await this._getMaxLid(this.ctx.tender.id);
  429. const AnalysisExcel = require('../lib/analysis_excel').AnalysisGclExcelTree;
  430. const analysisExcel = new AnalysisExcel(this.ctx);
  431. const cacheData = analysisExcel.analysisData(sheet, node, maxId, data);
  432. if (!cacheData) throw '导入数据错误,请检查Excel文件后重试';
  433. const datas = [];
  434. for (const node of cacheData.items) {
  435. const data = {
  436. id: node.id, tender_id: this.ctx.tender.id,
  437. ledger_id: node.ledger_id,
  438. ledger_pid: node.ledger_pid,
  439. level: node.level,
  440. order: node.order,
  441. is_leaf: !node.children || node.children.length === 0,
  442. full_path: node.full_path,
  443. b_code: node.b_code,
  444. name: node.name,
  445. unit: node.unit,
  446. unit_price: node.unit_price,
  447. crid: node.crid,
  448. };
  449. if (this.ctx.tender.data.measure_type === measureType.tz.value) {
  450. data.sgfh_qty = node.quantity;
  451. data.sgfh_tp = node.total_price;
  452. data.quantity = node.quantity;
  453. data.total_price = node.total_price;
  454. } else if (this.ctx.tender.data.measure_type === measureType.gcl.value) {
  455. data.deal_qty = node.quantity;
  456. data.deal_tp = node.total_price;
  457. }
  458. datas.push(data);
  459. }
  460. const conn = await this.db.beginTransaction();
  461. try {
  462. await this.db.update(this.tableName, {id: node.id, is_leaf: false});
  463. await this.db.insert(this.tableName, datas);
  464. await conn.commit();
  465. } catch(err) {
  466. await conn.rollback();
  467. throw err;
  468. }
  469. this._cacheMaxLid(this.ctx.tender.id, cacheData.keyNodeId);
  470. node.is_leaf = false;
  471. return { create: datas, update: [node]};
  472. }
  473. async deal2sgfh(tid) {
  474. const sql = 'UPDATE ' + this.tableName + ' SET sgfh_qty = deal_qty,' +
  475. ' quantity = IFNULL(deal_qty, 0) + IFNULL(sjcl_qty, 0) + IFNULL(qtcl_qty, 0), ' +
  476. ' sgfh_tp = deal_tp,' +
  477. ' total_price = IFNULL(deal_tp, 0) + IFNULL(sjcl_tp, 0) + IFNULL(qtcl_tp, 0)' +
  478. ' WHERE tender_id = ?';
  479. const sqlParam = [tid];
  480. await this.db.query(sql, sqlParam);
  481. }
  482. _calcExpr(data, field, expr, defaultValue, precision) {
  483. if (expr) {
  484. try {
  485. data[field] = this.ctx.helper.round(math(expr), precision.value);
  486. } catch (err) {
  487. }
  488. } else {
  489. data[field] = this.ctx.helper.round(defaultValue, precision.value);
  490. }
  491. }
  492. async pasteBlockData (tid, sid, pasteData, defaultData) {
  493. if ((tid <= 0) || (sid <= 0)) return [];
  494. if (!pasteData || pasteData.length <= 0) throw '复制数据错误';
  495. for (const pd of pasteData) {
  496. if (!pd || pd.length <= 0) throw '复制数据错误';
  497. pd.sort(function (x, y) {
  498. return x.level - y.level
  499. });
  500. if (pd[0].ledger_pid !== pasteData[0][0].ledger_pid) throw '复制数据错误:仅可操作同层节点';
  501. }
  502. const selectData = await this.getDataByKid(tid, sid);
  503. if (!selectData) throw '粘贴数据错误';
  504. const newParentPath = selectData.full_path.replace(selectData.ledger_id, '');
  505. const pasteBillsData = [], pastePosData = [], leafBillsId = [];
  506. const tpDecimal = this.ctx.tender.info.decimal.tp;
  507. let maxId = await this._getMaxLid(this.ctx.tender.id);
  508. for (const [i, pd] of pasteData.entries()) {
  509. for (const d of pd) {
  510. d.children = pd.filter(function (x) {
  511. return x.ledger_pid === d.ledger_id;
  512. });
  513. }
  514. const pbd = [];
  515. for (const [j, d] of pd.entries()) {
  516. const newBills = {
  517. id: this.uuid.v4(),
  518. tender_id: tid,
  519. ledger_id: maxId + j + 1,
  520. ledger_pid: j === 0 ? selectData.ledger_pid : d.ledger_pid,
  521. level: d.level + selectData.level - pd[0].level,
  522. order: j === 0 ? selectData.order + i + 1 : d.order,
  523. is_leaf: d.is_leaf,
  524. code: d.code,
  525. b_code: d.b_code,
  526. name: d.name,
  527. unit: d.unit,
  528. unit_price: this.ctx.helper.round(d.unit_price, this.ctx.tender.info.decimal.up),
  529. source: d.source,
  530. remark: d.remark,
  531. drawing_code: d.drawaing_code,
  532. memo: d.memo,
  533. node_type: d.node_type,
  534. sgfh_expr: d.sgfh_expr,
  535. sjcl_expr: d.sjcl_expr,
  536. qtcl_expr: d.qtcl_expr,
  537. };
  538. for (const c of d.children) {
  539. c.ledger_pid = newBills.ledger_id;
  540. }
  541. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, newBills.unit);
  542. if (d.pos && d.pos.length > 0) {
  543. for (const pos of d.pos) {
  544. const newPos = {
  545. id: this.uuid.v4(),
  546. tid: tid,
  547. lid: newBills.id,
  548. name: pos.name,
  549. drawing_code: pos.drawing_code,
  550. add_user: this.ctx.session.sessionUser.accountId,
  551. in_time: new Date(),
  552. porder: pos.porder,
  553. position: pos.position,
  554. sgfh_expr: pos.sgfh_expr ? pos.sgfh_expr : '',
  555. sjcl_expr: pos.sjcl_expr ? pos.sjcl_expr : '',
  556. qtcl_expr: pos.qtcl_expr ? pos.qtcl_expr : '',
  557. add_stage: 0,
  558. add_times: 0,
  559. };
  560. this._calcExpr(newPos, 'sgfh_qty', pos.sgfh_expr, pos.sgfh_qty, precision);
  561. this._calcExpr(newPos, 'sjcl_qty', pos.sjcl_expr, pos.sjcl_qty, precision);
  562. this._calcExpr(newPos, 'qtcl_qty', pos.qtcl_expr, pos.qtcl_qty, precision);
  563. newPos.quantity = this.ctx.helper.add(newPos.sgfh_qty,
  564. this.ctx.helper.add(newPos.sjcl_qty, newPos.qtcl_qty));
  565. newBills.sgfh_qty = this.ctx.helper.add(newBills.sgfh_qty, newPos.sgfh_qty);
  566. newBills.sjcl_qty = this.ctx.helper.add(newBills.sjcl_qty, newPos.sjcl_qty);
  567. newBills.qtcl_qty = this.ctx.helper.add(newBills.qtcl_qty, newPos.qtcl_qty);
  568. if (defaultData) this.ctx.helper._.assignIn(newPos, defaultData);
  569. pastePosData.push(newPos);
  570. }
  571. } else {
  572. this._calcExpr(newBills, 'sgfh_qty', newBills.sgfh_expr, d.sgfh_qty, precision);
  573. this._calcExpr(newBills, 'sjcl_qty', newBills.sjcl_expr, d.sjcl_qty, precision);
  574. this._calcExpr(newBills, 'qtcl_qty', newBills.qtcl_expr, d.qtcl_qty, precision);
  575. }
  576. newBills.quantity = this.ctx.helper.add(newBills.sgfh_qty,
  577. this.ctx.helper.add(newBills.sjcl_qty, newBills.qtcl_qty));
  578. newBills.sgfh_tp = this.ctx.helper.mul(newBills.sgfh_qty, newBills.unit_price, tpDecimal);
  579. newBills.sjcl_tp = this.ctx.helper.mul(newBills.qtcl_qty, newBills.unit_price, tpDecimal);
  580. newBills.qtcl_tp = this.ctx.helper.mul(newBills.sjcl_qty, newBills.unit_price, tpDecimal);
  581. newBills.total_price = this.ctx.helper.mul(newBills.quantity, newBills.unit_price, tpDecimal);
  582. if (defaultData) this.ctx.helper._.assignIn(newBills, defaultData);
  583. pbd.push(newBills);
  584. }
  585. for (const d of pbd) {
  586. const parent = pbd.find(function (x) {
  587. return x.ledger_id === d.ledger_pid;
  588. });
  589. d.full_path = parent
  590. ? parent.full_path + '-' + d.ledger_id
  591. : newParentPath + '-' + d.ledger_id;
  592. if (defaultData) this.ctx.helper._.assignIn(pbd, defaultData);
  593. pasteBillsData.push(d);
  594. }
  595. maxId = maxId + pbd.length;
  596. }
  597. this.transaction = await this.db.beginTransaction();
  598. try {
  599. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  600. await this._updateChildrenOrder(tid, selectData.ledger_pid, selectData.order + 1, pasteData.length);
  601. // 数据库创建新增节点数据
  602. if (pasteBillsData.length > 0) {
  603. const newData = await this.transaction.insert(this.tableName, pasteBillsData);
  604. }
  605. this._cacheMaxLid(tid, maxId);
  606. if (pastePosData.length > 0) {
  607. await this.transaction.insert(this.ctx.service.pos.tableName, pastePosData);
  608. }
  609. await this.transaction.commit();
  610. } catch (err) {
  611. await this.transaction.rollback();
  612. throw err;
  613. }
  614. // 查询应返回的结果
  615. const updateData = await this.getNextsData(selectData.tender_id, selectData.ledger_pid, selectData.order + pasteData.length);
  616. return {
  617. ledger: { create: pasteBillsData, update: updateData },
  618. pos: pastePosData,
  619. };
  620. }
  621. }
  622. module.exports = BaseBillsSerivce;