revise_bills.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const SumLoad = require('../lib/sum_load');
  10. module.exports = app => {
  11. class ReviseBills extends app.BaseBillsService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx, {
  20. mid: 'tender_id',
  21. kid: 'ledger_id',
  22. pid: 'ledger_pid',
  23. order: 'order',
  24. level: 'level',
  25. isLeaf: 'is_leaf',
  26. fullPath: 'full_path',
  27. keyPre: 'revise_bills_maxLid:',
  28. uuid: true,
  29. }, 'revisePos');
  30. this.depart = 10;
  31. this.tableName = 'revise_bills';
  32. }
  33. async _deleteRelaData (mid, deleteData) {
  34. await this.ctx.service.revisePos.deletePosData(this.transaction, mid, this._.map(deleteData, 'id'));
  35. }
  36. /**
  37. * 新增节点
  38. * @param {Number} tid - 台账id
  39. * @param {uuid} rid - 修订id
  40. * @param {Number} lid - 清单节点id
  41. * @returns {Promise<void>}
  42. */
  43. async addReviseNode(tid, rid, lid, count) {
  44. if (!rid) return null;
  45. return await this.addNodeBatch(tid, lid, {crid: rid, check_calc: 1}, count);
  46. }
  47. /**
  48. * 批量插入子项
  49. * @param {uuid} rid - 修订id
  50. * @param {Number} lid - 节点id
  51. * @param {Object} data - 批量插入数据
  52. * @return {Promise<void>}
  53. */
  54. async batchInsertChild(tid, rid, lid, data) {
  55. const result = { ledger: {}, pos: null };
  56. if (!tid || !rid || !lid) return result;
  57. const select = await this.getDataByKid(tid, lid);
  58. if (!select) {
  59. throw '位置数据错误';
  60. }
  61. // 计算id和order
  62. const maxId = await this._getMaxLid(tid);
  63. const lastChild = await this.getLastChildData(tid, lid);
  64. const order = lastChild ? lastChild.order : 0;
  65. // 整理数据
  66. const bills = [], pos = [], newIds = [];
  67. for (let i = 0, iLen = data.length; i < iLen; i++) {
  68. // 合并新增数据
  69. const qd = {
  70. crid: rid,
  71. id: this.uuid.v4(),
  72. tender_id: tid,
  73. ledger_id: maxId + i + 1,
  74. ledger_pid: select.ledger_id,
  75. is_leaf: true,
  76. order: order + i + 1,
  77. level: select.level + 1,
  78. b_code: data[i].b_code,
  79. name: data[i].name,
  80. unit: data[i].unit,
  81. unit_price: data[i].price,
  82. check_calc: 1,
  83. };
  84. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  85. qd.sgfh_qty = 0;
  86. for (const p of data[i].pos) {
  87. const inP = {
  88. id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
  89. add_stage: 0, add_stage_order: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  90. name: p.name, drawing_code: p.drawing_code, porder: p.porder,
  91. };
  92. if (p.quantity) {
  93. inP.sgfh_qty = this.round(p.quantity, precision.value);
  94. inP.quantity = inP.sgfh_qty;
  95. }
  96. qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
  97. pos.push(inP);
  98. }
  99. qd.full_path = select.full_path + '-' + qd.ledger_id;
  100. qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
  101. qd.quantity = qd.sgfh_qty;
  102. qd.total_price = qd.sgfh_tp;
  103. bills.push(qd);
  104. newIds.push(qd.id);
  105. }
  106. this.transaction = await this.db.beginTransaction();
  107. try {
  108. // 更新父项isLeaf
  109. if (!lastChild) {
  110. await this.transaction.update(this.tableName, {
  111. id: select.id,
  112. is_leaf: false,
  113. unit_price: null,
  114. quantity: null,
  115. total_price: null,
  116. deal_qty: null,
  117. deal_tp: null,
  118. });
  119. }
  120. // 数据库创建新增节点数据
  121. await this.transaction.insert(this.tableName, bills);
  122. if (pos.length > 0) {
  123. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  124. }
  125. this._cacheMaxLid(tid, maxId + data.length);
  126. await this.transaction.commit();
  127. } catch (err) {
  128. await this.transaction.rollback();
  129. throw err;
  130. }
  131. // 查询应返回的结果
  132. result.ledger.create = bills;
  133. if (!lastChild) result.ledger.update = await this.getDataById(select.id);
  134. result.pos = pos;
  135. return result;
  136. }
  137. /**
  138. * 批量插入后项
  139. * @param {Number} tid - 台账id
  140. * @param {uuid} rid - 修订id
  141. * @param {Number} lid - 节点id
  142. * @param {Object} data - 批量插入数据
  143. * @return {Promise<void>}
  144. */
  145. async batchInsertNext(tid, rid, lid, data) {
  146. const result = { ledger: {}, pos: null };
  147. if (!tid || !rid || !lid) return result;
  148. const select = await this.getDataByKid(tid, lid);
  149. if (!select) {
  150. throw '位置数据错误';
  151. }
  152. const parentData = await this.getDataByKid(tid, select.ledger_pid);
  153. if (!parentData) {
  154. throw '位置数据错误';
  155. }
  156. // 计算id和order
  157. const maxId = await this._getMaxLid(tid);
  158. const order = select.order;
  159. // 整理数据
  160. const bills = [], pos = [], newIds = [];
  161. for (let i = 0, iLen = data.length; i < iLen; i++) {
  162. // 合并新增数据
  163. const qd = {
  164. crid: rid,
  165. id: this.uuid.v4(),
  166. tender_id: tid,
  167. ledger_id: maxId + i + 1,
  168. ledger_pid: parentData.ledger_id,
  169. is_leaf: true,
  170. order: order + i + 1,
  171. level: parentData.level + 1,
  172. b_code: data[i].b_code,
  173. name: data[i].name,
  174. unit: data[i].unit,
  175. unit_price: data[i].price,
  176. check_calc: 1,
  177. };
  178. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  179. qd.sgfh_qty = 0;
  180. for (const p of data[i].pos) {
  181. const inP = {
  182. id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
  183. add_stage: 0, add_stage_order: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  184. name: p.name, drawing_code: p.drawing_code, porder: p.porder,
  185. };
  186. if (p.quantity) {
  187. inP.sgfh_qty = this.round(p.quantity, precision.value);
  188. inP.quantity = inP.sgfh_qty;
  189. }
  190. qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
  191. pos.push(inP);
  192. }
  193. qd.full_path = parentData.full_path + '-' + qd.ledger_id;
  194. qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
  195. qd.quantity = qd.sgfh_qty;
  196. qd.total_price = qd.sgfh_tp;
  197. bills.push(qd);
  198. newIds.push(qd.id);
  199. }
  200. this.transaction = await this.db.beginTransaction();
  201. try {
  202. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  203. await this._updateChildrenOrder(tid, select.ledger_pid, select.order+1);
  204. // 数据库创建新增节点数据
  205. await this.transaction.insert(this.tableName, bills);
  206. if (pos.length > 0) {
  207. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  208. }
  209. this._cacheMaxLid(tid, maxId + data.length);
  210. await this.transaction.commit();
  211. } catch (err) {
  212. await this.transaction.rollback();
  213. throw err;
  214. }
  215. // 查询应返回的结果
  216. result.ledger.create = bills;
  217. result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length);
  218. result.pos = pos;
  219. return result;
  220. }
  221. async sumLoad(lid, rid, tenders) {
  222. const conn = await this.db.beginTransaction();
  223. try {
  224. const maxId = await this._getMaxLid(this.ctx.tender.id);
  225. const select = await this.getDataById(lid);
  226. const sumLoad = new SumLoad(this.ctx);
  227. const loadTree = await sumLoad.updateGatherGcl(select, maxId, tenders, {
  228. crid: rid,
  229. });
  230. const result = loadTree.getUpdateData();
  231. if (result.errors.length > 100) throw '您导入的数据存在大量数据错误,请您仔细检查';
  232. this._cacheMaxLid(this.ctx.tender.id, loadTree.keyNodeId);
  233. const his = await this.ctx.service.sumLoadHistory.saveReviseHistory(this.ctx.tender.id, rid, lid, tenders, result.errors);
  234. if (result.update.length > 0) await conn.updateRows(this.tableName, result.update);
  235. if (result.create.length > 0)await conn.insert(this.tableName, result.create);
  236. await conn.commit();
  237. return { create: result.create, update: result.update, sumLoadHis: his};
  238. } catch (err) {
  239. await conn.rollback();
  240. throw (err.stack ? err : '导入工程量数据出错');
  241. }
  242. }
  243. }
  244. return ReviseBills;
  245. };