ledger.js 29 KB

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