ledger.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. 'use strict';
  2. /**
  3. * 标段--台账 数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/12/1
  7. * @version
  8. */
  9. const needField = {
  10. id: 'ledger_id',
  11. pid: 'ledger_pid',
  12. order: 'order',
  13. level: 'level',
  14. fullPath: 'full_path',
  15. isLeaf: 'is_leaf',
  16. };
  17. const keyFields = {
  18. table: ['id'],
  19. index: ['tender_id', 'ledger_id'],
  20. };
  21. // 以下字段仅可通过树结构操作改变,不可直接通过update方式从接口提交,发现时过滤
  22. const readOnlyFields = ['id', 'tender_id', 'ledger_id', 'ledger_pid', 'order', 'level', 'full_path', 'is_leaf'];
  23. const calcFields = ['unit_price', 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'deal_qty', 'deal_tp', 'dgn_qty1', 'dgn_qty2'];
  24. const upFields = ['unit_price'];
  25. const qtyFields = ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'quantity', 'deal_qty', 'dgn_qty1', 'dgn_qty2'];
  26. const tpFields = ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price', 'deal_tp'];
  27. const rootId = -1;
  28. const keyPre = 'tender_node_maxId:';
  29. module.exports = app => {
  30. class Ledger extends app.BaseBillsService {
  31. /**
  32. * 构造函数
  33. *
  34. * @param {Object} ctx - egg全局变量
  35. * @return {void}
  36. */
  37. constructor(ctx) {
  38. super(ctx, {
  39. mid: 'tender_id',
  40. kid: 'ledger_id',
  41. pid: 'ledger_pid',
  42. order: 'order',
  43. level: 'level',
  44. isLeaf: 'is_leaf',
  45. fullPath: 'full_path',
  46. keyPre: 'ledger_bills_maxLid:',
  47. uuid: true,
  48. });
  49. this.tableName = 'ledger';
  50. }
  51. /**
  52. * 新增数据(供内部或其他service类调用, controller不可直接使用)
  53. * @param {Array|Object} data - 新增数据
  54. * @param {Number} tenderId - 标段id
  55. * @param {Object} transaction - 新增事务
  56. * @return {Promise<boolean>} - {Promise<是否正确新增成功>}
  57. */
  58. async innerAdd(data, tenderId, transaction) {
  59. const datas = data instanceof Array ? data : [data];
  60. if (tenderId <= 0) {
  61. throw '标段id错误';
  62. }
  63. if (datas.length <= 0) {
  64. throw '插入数据为空';
  65. }
  66. if (!transaction) {
  67. throw '内部错误';
  68. }
  69. // 整理数据
  70. const insertData = [];
  71. for (const tmp of datas) {
  72. tmp.ledger_id = tmp.template_id;
  73. tmp.ledger_pid = tmp.pid;
  74. tmp.tender_id = tenderId;
  75. delete tmp.template_id;
  76. delete tmp.pid;
  77. tmp.id = this.uuid.v4();
  78. insertData.push(tmp);
  79. }
  80. const operate = await transaction.insert(this.tableName, insertData);
  81. return operate.affectedRows === datas.length;
  82. }
  83. /**
  84. * 新增数据
  85. *
  86. * @param {Object} data - 新增的数据(可批量)
  87. * @param {Number} tenderId - 标段id
  88. * @return {Boolean} - 返回新增的结果
  89. */
  90. async add(data, tenderId) {
  91. this.transaction = await this.db.beginTransaction();
  92. let result = false;
  93. try {
  94. result = await this.innerAdd(data, tenderId, this.transaction);
  95. if (!result) {
  96. throw '新增数据错误';
  97. }
  98. await this.transaction.commit();
  99. } catch (error) {
  100. await this.transaction.rollback();
  101. result = false;
  102. }
  103. return result;
  104. }
  105. /**
  106. * 根据节点Id获取数据
  107. *
  108. * @param {Number} tenderId - 标段id
  109. * @param {Number} nodeId - 项目节/工程量清单节点id
  110. * @return {Object} - 返回查询到的节点数据
  111. */
  112. async getDataByNodeId(tenderId, nodeId) {
  113. if ((nodeId <= 0) || (tenderId <= 0)) {
  114. return undefined;
  115. }
  116. this.initSqlBuilder();
  117. this.sqlBuilder.setAndWhere('tender_id', {
  118. value: tenderId,
  119. operate: '=',
  120. });
  121. this.sqlBuilder.setAndWhere('ledger_id', {
  122. value: nodeId,
  123. operate: '=',
  124. });
  125. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  126. const data = await this.db.queryOne(sql, sqlParam);
  127. return data;
  128. }
  129. /**
  130. * 根据节点Id获取数据
  131. * @param {Number} tenderId - 标段Id
  132. * @param {Array} nodesIds - 节点Id
  133. * @return {Array}
  134. */
  135. async getDataByNodeIds(tenderId, nodesIds) {
  136. if (tenderId <= 0) {
  137. return [];
  138. }
  139. this.initSqlBuilder();
  140. this.sqlBuilder.setAndWhere('tender_id', {
  141. value: tenderId,
  142. operate: '=',
  143. });
  144. this.sqlBuilder.setAndWhere('ledger_id', {
  145. value: nodesIds,
  146. operate: 'in',
  147. });
  148. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  149. const data = await this.db.query(sql, sqlParam);
  150. return this._.sortBy(data, function (d) {
  151. return nodesIds.indexOf(d.ledger_id);
  152. });
  153. }
  154. /**
  155. * 根据主键id获取数据
  156. * @param {Array|Number} id - 主键id
  157. * @return {Promise<*>}
  158. */
  159. async getDataByIds(id) {
  160. if (!id) {
  161. return;
  162. }
  163. const ids = id instanceof Array ? id : [id];
  164. if (ids.length === 0) {
  165. return;
  166. }
  167. this.initSqlBuilder();
  168. this.sqlBuilder.setAndWhere('id', {
  169. value: ids,
  170. operate: 'in',
  171. });
  172. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  173. const data = await this.db.query(sql, sqlParam);
  174. return data;
  175. }
  176. /**
  177. * 根据 父节点id 获取子节点
  178. * @param tenderId
  179. * @param nodeId
  180. * @return {Promise<*>}
  181. */
  182. async getChildrenByParentId(tenderId, nodeId) {
  183. if (tenderId <= 0 || !nodeId) {
  184. return undefined;
  185. }
  186. const nodeIds = nodeId instanceof Array ? nodeId : [nodeId];
  187. if (nodeIds.length === 0) {
  188. return [];
  189. }
  190. this.initSqlBuilder();
  191. this.sqlBuilder.setAndWhere('tender_id', {
  192. value: tenderId,
  193. operate: '=',
  194. });
  195. this.sqlBuilder.setAndWhere('ledger_pid', {
  196. value: nodeIds,
  197. operate: 'in',
  198. });
  199. this.sqlBuilder.orderBy = [['order', 'ASC']];
  200. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  201. const data = await this.db.query(sql, sqlParam);
  202. return data;
  203. }
  204. /**
  205. * 获取项目工程量
  206. * @param tenderId
  207. * @returns {Promise<*>}
  208. */
  209. async getGatherGclBills(tenderId) {
  210. const sql = 'SELECT `b_code`, `name`, `unit`, `unit_price`, ' +
  211. ' Sum(`quantity`) As `quantity`, Sum(`total_price`) As `total_price`, ' +
  212. ' Sum(`deal_qty`) As `deal_qty`, Sum(`deal_tp`) As `deal_tp` ' +
  213. ' From ?? ' +
  214. ' WHERE `tender_id` = ? And `b_code` And `is_leaf` ' +
  215. ' GROUP BY `b_code`, `name`, `unit`, `unit_price`';
  216. const sqlParam = [this.tableName, tenderId];
  217. return await this.db.query(sql, sqlParam);
  218. }
  219. async getTenderUsedUnits(tenderId) {
  220. const sql = 'SELECT unit ' +
  221. ' FROM ' + this.tableName +
  222. ' WHERE tender_id = ? and is_leaf = true' +
  223. ' GROUP By unit';
  224. const sqlParam = [tenderId];
  225. const units = await this.db.query(sql, sqlParam);
  226. return this._.map(units, 'unit');
  227. }
  228. /**
  229. * 统计子节点total_price
  230. * @param {Number} tenderId - 标段id
  231. * @param {Number} pid - 父节点id
  232. * @param {Number} order - order取值
  233. * @param {String} orderOperate - order比较操作符
  234. * @return {Promise<void>}
  235. */
  236. async addUpChildren(tenderId, pid, order, orderOperate) {
  237. this.initSqlBuilder();
  238. const sql = ['SELECT SUM(??) As value FROM ?? ', ' WHERE '];
  239. const sqlParam = ['total_price', this.tableName];
  240. sql.push(' ?? = ' + tenderId);
  241. sqlParam.push('tender_id');
  242. sql.push(' And ?? = ' + pid);
  243. sqlParam.push('ledger_pid');
  244. sql.push(' And ?? ' + orderOperate + ' ' + order);
  245. sqlParam.push('order');
  246. const result = await this.db.queryOne(sql.join(''), sqlParam);
  247. return result.value;
  248. }
  249. /**
  250. * 删除相关数据 用于继承
  251. * @param mid
  252. * @param deleteData
  253. * @returns {Promise<void>}
  254. * @private
  255. */
  256. async _deleteRelaData(mid, deleteData) {
  257. await this.ctx.service.pos.deletePosData(this.transaction, mid, this._.map(deleteData, 'id'));
  258. }
  259. _checkField(data, field) {
  260. const fields = field instanceof Array ? field : [field];
  261. for (const prop in data) {
  262. if (fields.indexOf(prop) >= 0) {
  263. return true;
  264. }
  265. }
  266. return false;
  267. }
  268. /**
  269. * 复制粘贴整块
  270. * @param {Number} tenderId - 标段Id
  271. * @param {Number} selectId - 选中几点Id
  272. * @param {Array} block - 复制节点Id
  273. * @return {Object} - 提价后的数据(其中新增粘贴数据,只返回第一层)
  274. */
  275. async pasteBlock(tenderId, selectId, block) {
  276. if ((tenderId <= 0) || (selectId <= 0)) {
  277. return [];
  278. }
  279. const selectData = await this.getDataByNodeId(this.ctx.tender.id, selectId);
  280. if (!selectData) {
  281. throw '位置数据错误';
  282. }
  283. const newParentPath = selectData.full_path.replace(selectData.ledger_id, '');
  284. const copyNodes = await this.getDataByNodeIds(tenderId, block);
  285. if (!copyNodes || copyNodes.length <= 0) {
  286. throw '复制数据错误';
  287. }
  288. let bSameParent = true;
  289. for (const node of copyNodes) {
  290. if (node.ledger_pid !== copyNodes[0].ledger_pid) {
  291. bSameParent = false;
  292. break;
  293. }
  294. }
  295. if (!bSameParent) {
  296. throw '复制数据错误:仅可操作同层节点';
  297. }
  298. const orgParentPath = copyNodes[0].full_path.replace(copyNodes[0].ledger_id, '');
  299. const newIds = [];
  300. this.transaction = await this.db.beginTransaction();
  301. try {
  302. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  303. await this._updateChildrenOrder(tenderId, selectData.ledger_pid, selectData.order + 1);
  304. // 数据库创建新增节点数据
  305. for (let iNode = 0; iNode < copyNodes.length; iNode++) {
  306. const node = copyNodes[iNode];
  307. let datas = await this.getDataByFullPath(tenderId, node.full_path + '%');
  308. datas = this._.sortBy(datas, 'level');
  309. const maxId = await this._getMaxLid(this.ctx.tender.id);
  310. const leafBillsId = [];
  311. // 计算粘贴数据中需更新部分
  312. for (let index = 0; index < datas.length; index++) {
  313. const data = datas[index];
  314. const newId = maxId + index + 1;
  315. const idChange = {
  316. org: data.id,
  317. };
  318. data.id = this.uuid.v4();
  319. idChange.new = data.id;
  320. data.tender_id = this.ctx.tender.id;
  321. if (!data.is_leaf) {
  322. for (const children of datas) {
  323. children.full_path = children.full_path.replace('-' + data.ledger_id, '-' + newId);
  324. if (children.ledger_pid === data.ledger_id) {
  325. children.ledger_pid = newId;
  326. }
  327. }
  328. } else {
  329. data.full_path = data.full_path.replace('-' + data.ledger_id, '-' + newId);
  330. }
  331. data.ledger_id = newId;
  332. data.full_path = data.full_path.replace(orgParentPath, newParentPath);
  333. if (data.ledger_pid === node.ledger_pid) {
  334. data.ledger_pid = selectData.ledger_pid;
  335. data.order = selectData.order + iNode + 1;
  336. }
  337. data.level = data.level + selectData.level - copyNodes[0].level;
  338. if (data.is_leaf) {
  339. leafBillsId.push(idChange);
  340. }
  341. newIds.push(data.id);
  342. }
  343. const newData = await this.transaction.insert(this.tableName, datas);
  344. for (const id of leafBillsId) {
  345. await this.ctx.service.pos.copyBillsPosData(id.org, id.new, this.transaction);
  346. }
  347. this._cacheMaxLid(tenderId, maxId + datas.length);
  348. }
  349. await this.transaction.commit();
  350. } catch (err) {
  351. await this.transaction.rollback();
  352. throw err;
  353. }
  354. // 查询应返回的结果
  355. const order = [];
  356. for (let i = 1; i <= copyNodes.length; i++) {
  357. order.push(selectData.order + i);
  358. }
  359. const createData = await this.getDataByIds(newIds);
  360. const updateData = await this.getNextsData(selectData.tender_id, selectData.ledger_pid, selectData.order + copyNodes.length);
  361. const posData = await this.ctx.service.pos.getPosData({ lid: newIds });
  362. return {
  363. ledger: { create: createData, update: updateData },
  364. pos: posData,
  365. };
  366. }
  367. /**
  368. *
  369. * @param tenderId
  370. * @param xmj
  371. * @param order
  372. * @param parentData
  373. * @return {Promise<*[]>}
  374. * @private
  375. */
  376. async _sortBatchInsertData(tenderId, xmj, order, parentData) {
  377. const result = [],
  378. newIds = [];
  379. let tp = 0;
  380. const maxId = await this._getMaxLid(tenderId);
  381. // 添加xmj数据
  382. const parent = {
  383. tender_id: tenderId,
  384. ledger_id: maxId + 1,
  385. ledger_pid: parentData.ledger_id,
  386. is_leaf: xmj.children.length === 0,
  387. order,
  388. level: parentData.level + 1,
  389. name: xmj.name,
  390. };
  391. parent.full_path = parentData.full_path + '-' + parent.ledger_id;
  392. // 添加gcl数据
  393. for (let i = 0, iLen = xmj.children.length; i < iLen; i++) {
  394. const gcl = xmj.children[i];
  395. const child = {
  396. tender_id: tenderId,
  397. ledger_id: maxId + 1 + i + 1,
  398. ledger_pid: parent.ledger_id,
  399. is_leaf: true,
  400. order: i + 1,
  401. level: parent.level + 1,
  402. b_code: gcl.b_code,
  403. name: gcl.name,
  404. unit: gcl.unit,
  405. unit_price: gcl.unit_price,
  406. quantity: gcl.quantity,
  407. };
  408. child.full_path = parent.full_path + '-' + child.ledger_id;
  409. child.total_price = this.ctx.helper.mul(child.unit_price, child.quantity, this.ctx.tender.info.decimal.tp);
  410. tp = this.ctx.helper.add(tp, child.total_price);
  411. result.push(child);
  412. newIds.push(child.ledger_id);
  413. }
  414. parent.total_price = tp;
  415. result.push(parent);
  416. newIds.push(parent.ledger_id);
  417. return [result, tp, newIds];
  418. }
  419. /**
  420. * 批量插入子项
  421. * @param {Number} tenderId - 标段Id
  422. * @param {Number} selectId - 选中节点Id
  423. * @param {Object} data - 批量插入数据
  424. * @return {Promise<void>}
  425. */
  426. async batchInsertChild(tenderId, selectId, data) {
  427. const result = { ledger: {}, pos: null };
  428. if ((tenderId <= 0) || (selectId <= 0)) {
  429. return result;
  430. }
  431. const selectData = await this.getDataByNodeId(tenderId, selectId);
  432. if (!selectData) {
  433. throw '位置数据错误';
  434. }
  435. this.transaction = await this.db.beginTransaction();
  436. const newIds = [];
  437. const lastChild = await this.getLastChildData(tenderId, selectId);
  438. try {
  439. // 更新父项isLeaf
  440. if (!lastChild) {
  441. await this.transaction.update(this.tableName, {
  442. id: selectData.id,
  443. is_leaf: false,
  444. unit_price: null,
  445. quantity: null,
  446. total_price: null,
  447. deal_qty: null,
  448. deal_tp: null,
  449. });
  450. }
  451. const order = lastChild ? lastChild.order : 0;
  452. // 计算id
  453. const maxId = await this._getMaxLid(tenderId);
  454. // 数据库创建新增节点数据
  455. for (let i = 0, iLen = data.length; i < iLen; i++) {
  456. // 合并新增数据
  457. const qd = {
  458. id: this.uuid.v4(),
  459. tender_id: tenderId,
  460. ledger_id: maxId + i + 1,
  461. ledger_pid: selectData.ledger_id,
  462. is_leaf: true,
  463. order: order + i + 1,
  464. level: selectData.level + 1,
  465. b_code: data[i].b_code,
  466. name: data[i].name,
  467. unit: data[i].unit,
  468. unit_price: data[i].price,
  469. };
  470. qd.full_path = selectData.full_path + '-' + qd.ledger_id;
  471. const insertResult = await this.transaction.insert(this.tableName, qd);
  472. newIds.push(qd.id);
  473. if (data[i].pos.length > 0) {
  474. await this.ctx.service.pos.insertLedgerPosData(this.transaction, tenderId, qd, data[i].pos);
  475. await this.calcNode(qd, this.transaction);
  476. }
  477. }
  478. this._cacheMaxLid(tenderId, maxId + data.length);
  479. await this.transaction.commit();
  480. } catch (err) {
  481. await this.transaction.rollback();
  482. throw err;
  483. }
  484. // 查询应返回的结果
  485. result.ledger.create = await this.getDataByIds(newIds);
  486. if (!lastChild) {
  487. result.ledger.update = await this.getDataByIds([selectData.id]);
  488. }
  489. result.pos = await this.ctx.service.pos.getPosData({ lid: newIds });
  490. return result;
  491. }
  492. /**
  493. * 批量插入后项
  494. * @param {Number} tenderId - 标段Id
  495. * @param {Number} selectId - 选中节点Id
  496. * @param {Object} data - 批量插入数据
  497. * @return {Promise<void>}
  498. */
  499. async batchInsertNext(tenderId, selectId, data) {
  500. const result = { ledger: {}, pos: null };
  501. if ((tenderId <= 0) || (selectId <= 0)) {
  502. return result;
  503. }
  504. const selectData = await this.getDataByNodeId(tenderId, selectId);
  505. if (!selectData) {
  506. throw '位置数据错误';
  507. }
  508. const parentData = await this.getDataByNodeId(tenderId, selectData.ledger_pid);
  509. if (!parentData) {
  510. throw '位置数据错误';
  511. }
  512. this.transaction = await this.db.beginTransaction();
  513. const newIds = [];
  514. try {
  515. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  516. await this._updateChildrenOrder(tenderId, selectData.ledger_pid, selectData.order + 1, data.length);
  517. // 计算id和order
  518. const maxId = await this._getMaxLid(tenderId);
  519. const order = selectData.order;
  520. // 数据库创建新增节点数据
  521. for (let i = 0, iLen = data.length; i < iLen; i++) {
  522. // 合并新增数据
  523. const qd = {
  524. id: this.uuid.v4(),
  525. tender_id: tenderId,
  526. ledger_id: maxId + i + 1,
  527. ledger_pid: parentData.ledger_id,
  528. is_leaf: true,
  529. order: order + i + 1,
  530. level: parentData.level + 1,
  531. b_code: data[i].b_code,
  532. name: data[i].name,
  533. unit: data[i].unit,
  534. unit_price: data[i].price,
  535. };
  536. qd.full_path = parentData.full_path + '-' + qd.ledger_id;
  537. const insertResult = await this.transaction.insert(this.tableName, qd);
  538. newIds.push(qd.id);
  539. if (data[i].pos.length > 0) {
  540. await this.ctx.service.pos.insertLedgerPosData(this.transaction, tenderId, qd, data[i].pos);
  541. await this.calcNode(qd, this.transaction);
  542. }
  543. }
  544. this._cacheMaxLid(tenderId, maxId + data.length);
  545. await this.transaction.commit();
  546. } catch (err) {
  547. await this.transaction.rollback();
  548. throw err;
  549. }
  550. // 查询应返回的结果
  551. result.ledger.create = await this.getDataByIds(newIds);
  552. result.ledger.update = await this.getNextsData(selectData.tender_id, selectData.ledger_pid, selectData.order + data.length);
  553. result.pos = await this.ctx.service.pos.getPosData({ lid: newIds });
  554. return result;
  555. }
  556. /**
  557. *
  558. * @param node
  559. * @param transaction
  560. * @returns {Promise<void>}
  561. * @private
  562. */
  563. async calcNode(node, transaction) {
  564. const info = this.ctx.tender.info;
  565. const precision = this.ctx.helper.findPrecision(info.precision, node.unit);
  566. const calcQtySql = 'SELECT SUM(`sgfh_qty`) As `sgfh_qty`, SUM(`sjcl_qty`) As `sjcl_qty`, SUM(`qtcl_qty`) As `qtcl_qty`, SUM(`quantity`) As `quantity` FROM ?? WHERE `lid` = ?';
  567. const data = await transaction.queryOne(calcQtySql, [this.ctx.service.pos.tableName, node.id]);
  568. data.id = node.id;
  569. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  570. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  571. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  572. data.quantity = this.round(data.quantity, precision.value);
  573. data.sgfh_tp = this.ctx.helper.mul(data.sgfh_qty, node.unit_price, info.decimal.tp);
  574. data.sjcl_tp = this.ctx.helper.mul(data.sjcl_qty, node.unit_price, info.decimal.tp);
  575. data.qtcl_tp = this.ctx.helper.mul(data.qtcl_qty, node.unit_price, info.decimal.tp);
  576. data.total_price = this.ctx.helper.mul(data.quantity, node.unit_price, info.decimal.tp);
  577. const result = await transaction.update(this.tableName, data);
  578. }
  579. /**
  580. *
  581. * @param {Number} tid - 标段id
  582. * @param {Number} id - 需要计算的节点的id
  583. * @param {Object} transaction - 操作所属事务,没有则创建
  584. * @return {Promise<void>}
  585. */
  586. async calc(tid, id, transaction) {
  587. const node = await transaction.get(this.tableName, {id: id});
  588. if (!node) {
  589. throw '数据错误';
  590. }
  591. await this.calcNode(node, transaction);
  592. }
  593. async _importCacheTreeNodes(transaction, nodes) {
  594. const datas = [];
  595. for (const node of nodes) {
  596. datas.push({
  597. id: node.id,
  598. tender_id: this.ctx.tender.id,
  599. ledger_id: node.ledger_id,
  600. ledger_pid: node.ledger_pid,
  601. level: node.level,
  602. order: node.order,
  603. is_leaf: !node.children || node.children.length === 0,
  604. full_path: node.full_path,
  605. code: node.code,
  606. b_code: node.b_code,
  607. name: node.name,
  608. unit: node.unit,
  609. sgfh_qty: node.sgfh_qty,
  610. sgfh_tp: node.sgfh_tp,
  611. quantity: node.quantity,
  612. unit_price: node.unit_price,
  613. total_price: node.total_price,
  614. dgn_qty1: node.dgn_qty1,
  615. dgn_qty2: node.dgn_qty2,
  616. memo: node.memo,
  617. drawing_code: node.drawing_code,
  618. });
  619. }
  620. await transaction.insert(this.tableName, datas);
  621. return datas;
  622. }
  623. /**
  624. * 导入Excel数据
  625. * @param excelData
  626. * @returns {Promise<void>}
  627. */
  628. async importExcel(templateId, excelData) {
  629. const AnalysisExcel = require('../lib/analysis_excel').AnalysisExcelTree;
  630. const analysisExcel = new AnalysisExcel(this.ctx);
  631. const tempData = await this.ctx.service.tenderNodeTemplate.getData(templateId, true);
  632. const cacheTree = analysisExcel.analysisData(excelData, tempData);
  633. const cacheKey = keyPre + this.ctx.tender.id;
  634. const orgMaxId = parseInt(await this.cache.get(cacheKey));
  635. const transaction = await this.db.beginTransaction();
  636. try {
  637. await transaction.delete(this.tableName, {tender_id: this.ctx.tender.id});
  638. await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id});
  639. const datas = [];
  640. for (const node of cacheTree.items) {
  641. datas.push({
  642. id: node.id,
  643. tender_id: this.ctx.tender.id,
  644. ledger_id: node.ledger_id,
  645. ledger_pid: node.ledger_pid,
  646. level: node.level,
  647. order: node.order,
  648. is_leaf: !node.children || node.children.length === 0,
  649. full_path: node.full_path,
  650. code: node.code,
  651. b_code: node.b_code,
  652. name: node.name,
  653. unit: node.unit,
  654. sgfh_qty: node.sgfh_qty,
  655. sgfh_tp: node.sgfh_tp,
  656. quantity: node.quantity,
  657. unit_price: node.unit_price,
  658. total_price: node.total_price,
  659. dgn_qty1: node.dgn_qty1,
  660. dgn_qty2: node.dgn_qty2,
  661. memo: node.memo,
  662. drawing_code: node.drawing_code,
  663. });
  664. }
  665. await transaction.insert(this.tableName, datas);
  666. if (cacheTree.pos && cacheTree.pos.length > 0) {
  667. await transaction.insert(this.ctx.service.pos.tableName, cacheTree.pos);
  668. }
  669. await transaction.commit();
  670. this.cache.set(cacheKey, cacheTree.items.length + 1, 'EX', this.ctx.app.config.cacheTime);
  671. return {bills: datas, pos: cacheTree.pos}
  672. } catch (err) {
  673. await transaction.rollback();
  674. if (orgMaxId) {
  675. this.cache.set(cacheKey, cacheTree.keyNodeId, 'EX', this.ctx.app.config.cacheTime);
  676. }
  677. throw err;
  678. }
  679. }
  680. }
  681. return Ledger;
  682. };