revise_bills.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 = ctx.app.config.table_depart.heavy;
  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: 0,
  114. quantity: 0, total_price: 0,
  115. deal_qty: 0, deal_tp: 0,
  116. sgfh_qty: 0, sgfh_tp: 0,
  117. sjcl_qty: 0, sjcl_tp: 0,
  118. qtcl_qty: 0, qtcl_tp: 0,
  119. ex_qty1: 0, ex_tp1: 0,
  120. });
  121. }
  122. // 数据库创建新增节点数据
  123. await this.transaction.insert(this.tableName, bills);
  124. if (pos.length > 0) {
  125. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  126. }
  127. this._cacheMaxLid(tid, maxId + data.length);
  128. await this.transaction.commit();
  129. } catch (err) {
  130. await this.transaction.rollback();
  131. throw err;
  132. }
  133. // 查询应返回的结果
  134. result.ledger.create = bills;
  135. if (!lastChild) result.ledger.update = await this.getDataById(select.id);
  136. result.pos = pos;
  137. return result;
  138. }
  139. /**
  140. * 批量插入后项
  141. * @param {Number} tid - 台账id
  142. * @param {uuid} rid - 修订id
  143. * @param {Number} lid - 节点id
  144. * @param {Object} data - 批量插入数据
  145. * @return {Promise<void>}
  146. */
  147. async batchInsertNext(tid, rid, lid, data) {
  148. const result = { ledger: {}, pos: null };
  149. if (!tid || !rid || !lid) return result;
  150. const select = await this.getDataByKid(tid, lid);
  151. if (!select) {
  152. throw '位置数据错误';
  153. }
  154. const parentData = await this.getDataByKid(tid, select.ledger_pid);
  155. if (!parentData) {
  156. throw '位置数据错误';
  157. }
  158. // 计算id和order
  159. const maxId = await this._getMaxLid(tid);
  160. const order = select.order;
  161. // 整理数据
  162. const bills = [], pos = [], newIds = [];
  163. for (let i = 0, iLen = data.length; i < iLen; i++) {
  164. // 合并新增数据
  165. const qd = {
  166. crid: rid,
  167. id: this.uuid.v4(),
  168. tender_id: tid,
  169. ledger_id: maxId + i + 1,
  170. ledger_pid: parentData.ledger_id,
  171. is_leaf: true,
  172. order: order + i + 1,
  173. level: parentData.level + 1,
  174. b_code: data[i].b_code,
  175. name: data[i].name,
  176. unit: data[i].unit,
  177. unit_price: data[i].price,
  178. check_calc: 1,
  179. };
  180. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  181. qd.sgfh_qty = 0;
  182. for (const p of data[i].pos) {
  183. const inP = {
  184. id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
  185. add_stage: 0, add_stage_order: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  186. name: p.name, drawing_code: p.drawing_code, porder: p.porder,
  187. };
  188. if (p.quantity) {
  189. inP.sgfh_qty = this.round(p.quantity, precision.value);
  190. inP.quantity = inP.sgfh_qty;
  191. }
  192. qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
  193. pos.push(inP);
  194. }
  195. qd.full_path = parentData.full_path + '-' + qd.ledger_id;
  196. qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
  197. qd.quantity = qd.sgfh_qty;
  198. qd.total_price = qd.sgfh_tp;
  199. bills.push(qd);
  200. newIds.push(qd.id);
  201. }
  202. this.transaction = await this.db.beginTransaction();
  203. try {
  204. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  205. await this._updateChildrenOrder(tid, select.ledger_pid, select.order+1);
  206. // 数据库创建新增节点数据
  207. await this.transaction.insert(this.tableName, bills);
  208. if (pos.length > 0) {
  209. await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
  210. }
  211. this._cacheMaxLid(tid, maxId + data.length);
  212. await this.transaction.commit();
  213. } catch (err) {
  214. await this.transaction.rollback();
  215. throw err;
  216. }
  217. // 查询应返回的结果
  218. result.ledger.create = bills;
  219. result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length);
  220. result.pos = pos;
  221. return result;
  222. }
  223. async sumLoad(lid, rid, tenders, subUp) {
  224. const conn = await this.db.beginTransaction();
  225. try {
  226. const maxId = await this._getMaxLid(this.ctx.tender.id);
  227. const select = await this.getDataById(lid);
  228. const sumLoad = new SumLoad(this.ctx);
  229. const loadTree = await sumLoad.updateGatherGcl(select, maxId, tenders, {
  230. crid: rid,
  231. }, subUp);
  232. const result = loadTree.getUpdateData();
  233. if (result.errors.length > 100) throw '您导入的数据存在大量数据错误,请您仔细检查';
  234. this._cacheMaxLid(this.ctx.tender.id, loadTree.keyNodeId);
  235. const his = await this.ctx.service.sumLoadHistory.saveReviseHistory(this.ctx.tender.id, rid, lid, tenders, result.errors);
  236. if (result.update.length > 0) await conn.updateRows(this.tableName, result.update);
  237. if (result.create.length > 0)await conn.insert(this.tableName, result.create);
  238. await conn.commit();
  239. return { create: result.create, update: result.update, sumLoadHis: his};
  240. } catch (err) {
  241. await conn.rollback();
  242. throw (err.stack ? err : '导入工程量数据出错');
  243. }
  244. }
  245. }
  246. return ReviseBills;
  247. };