stage_stash.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. class loadStageExcelTree {
  11. constructor(ctx) {
  12. this.ctx = ctx;
  13. this.info = ctx.tender.info;
  14. this.decimal = ctx.tender.info.decimal;
  15. this.settleStatus = ctx.service.settle.settleStatus;
  16. this.insertBills = [];
  17. this.insertPos = [];
  18. this.updateBills = [];
  19. this.updatePos = [];
  20. this.insertDgn = [];
  21. this.updateDgn = [];
  22. }
  23. init(source) {
  24. this.default = source.default;
  25. const LedgerModel = require('../lib/ledger');
  26. this.ledgerTree = new LedgerModel.billsTree(this.ctx, {
  27. id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [],
  28. });
  29. this.pos = new LedgerModel.pos({ id: 'id', ledgerId: 'lid' });
  30. this.ledgerTree.loadDatas(source.ledgerData);
  31. this.pos.loadDatas(source.posData);
  32. this.stageBills = source.stageBills;
  33. this.stagePos = source.stagePos;
  34. this.stageBillsDgn = source.stageBillsDgn;
  35. this.preStageBills = source.preStageBills;
  36. }
  37. findNode(node, parent) {
  38. const _ = this.ctx.helper._;
  39. const sibling = parent ? parent.children : this.ledgerTree.children;
  40. const self = this;
  41. return sibling.find(x => {
  42. if (node.is_leaf !== x.is_leaf) return false;
  43. if (node.b_code) {
  44. if (x.has_pos === undefined) {
  45. const relaPos = self.pos.getLedgerPos(x.id);
  46. x.has_pos = !!(relaPos && relaPos.length > 0);
  47. }
  48. return node.b_code === _.trimEnd(x.b_code) && node.name === _.trimEnd(x.name) && node.unit === _.trimEnd(x.unit)
  49. && node.unit_price === x.unit_price && node.has_pos === x.has_pos;
  50. } else {
  51. return node.code === _.trimEnd(x.code) && node.name === _.trimEnd(x.name);
  52. }
  53. });
  54. }
  55. calcContractTp(node, contract_qty, qc_minus_qty) {
  56. const correct = this.ctx.subProject.page_show.correctCalcContractTp;
  57. if (this.info.calc_type === 'tp') {
  58. const preSb = this.preStageBills.find(x => { return x.lid === node.id; });
  59. let activeQty = this.ctx.helper.add(node.quantity, qc_minus_qty);
  60. let end_contract_qty = contract_qty;
  61. if (preSb) {
  62. activeQty = this.ctx.helper.add(activeQty, preSb.qc_minus_qty);
  63. end_contract_qty = this.ctx.helper.add(end_contract_qty, preSb.contract_qty);
  64. }
  65. const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), node.total_price, this.decimal.tp);
  66. return preSb ? this.ctx.helper.sub(end_contract_tp, preSb.pre_contract_tp) : end_contract_tp;
  67. } else if (this.info.calc_type === 'up') {
  68. if (correct) {
  69. const preSb = this.preStageBills.find(x => { return x.lid === node.id; });
  70. let activeQty = this.ctx.helper.add(node.quantity, qc_minus_qty);
  71. let end_contract_qty = contract_qty;
  72. if (preSb) {
  73. activeQty = this.ctx.helper.add(activeQty, preSb.qc_minus_qty);
  74. end_contract_qty = this.ctx.helper.add(end_contract_qty, preSb.contract_qty);
  75. }
  76. const end_contract_tp = this.ctx.helper.mul(end_contract_qty, node.unit_price, this.decimal.tp);
  77. return activeQty === end_contract_qty ? this.ctx.helper.sub(end_contract_tp, preSb.pre_contract_tp) : this.ctx.helper.mul(contract_qty, node.unit_price, this.decimal.tp);
  78. } else {
  79. return this.ctx.helper.mul(contract_qty, node.unit_price, this.decimal.tp);
  80. }
  81. }
  82. }
  83. loadLeaf(node, source) {
  84. const curStageBills = this.stageBills.find(csb => { return csb.lid === source.id; });
  85. if (node.has_pos) {
  86. const sourcePos = this.pos.getLedgerPos(source.id);
  87. const sourceStagePos = this.stagePos.filter(sp => { return sp.lid === source.id; });
  88. let contract_qty = 0;
  89. for (const p of node.pos) {
  90. if (!p.contract_qty) continue;
  91. const sp = sourcePos.find(x => { return x.name === p.name; });
  92. if (!sp || sp.settle_status === this.settleStatus.finish) continue;
  93. contract_qty = this.ctx.helper.add(contract_qty, p.contract_qty);
  94. let ssp = sourceStagePos.find(x => { return x.pid === sp.id; });
  95. if (ssp) {
  96. this.updatePos.push({ id: ssp.id, contract_qty: p.contract_qty, postil: p.postil || ssp.postil || '' });
  97. sourceStagePos.splice(sourceStagePos.indexOf(ssp), 1);
  98. } else {
  99. this.insertPos.push({
  100. tid: this.default.tid, sid: this.default.sid, said: this.default.said, times: 1, order: 0,
  101. lid: source.id, pid:sp.id, contract_qty: p.contract_qty, postil: p.postil || ''
  102. });
  103. }
  104. }
  105. for (const ssp of sourceStagePos) {
  106. contract_qty = this.ctx.helper.add(contract_qty, ssp.contract_qty);
  107. }
  108. const contract_tp = this.calcContractTp(source, contract_qty, curStageBills ? curStageBills.qc_minus_qty : 0);
  109. if (curStageBills) {
  110. this.updateBills.push({ id: curStageBills.id, contract_qty, contract_tp, postil: node.postil || curStageBills.postil || '' });
  111. } else {
  112. if (contract_qty) this.insertBills.push({
  113. tid: this.default.tid, sid: this.default.sid, said: this.default.said, times: 1, order: 0,
  114. lid: source.id, contract_qty, contract_tp, postil: node.postil || ''
  115. });
  116. }
  117. } else {
  118. if (!node.contract_qty && !node.contract_tp) return;
  119. const contract_qty = source.is_tp ? 0 : node.contract_qty;
  120. const contract_tp = contract_qty
  121. ? this.calcContractTp(source, contract_qty, curStageBills ? curStageBills.qc_minus_qty : 0)
  122. : source.is_tp ? this.ctx.helper.round(node.contract_tp, this.decimal.tp) : 0;
  123. if (curStageBills) {
  124. this.updateBills.push({ id: curStageBills.id, contract_qty: contract_qty, contract_tp: contract_tp, postil: node.postil || curStageBills.postil || '' });
  125. } else {
  126. this.insertBills.push({
  127. tid: this.default.tid, sid: this.default.sid, said: this.default.said, times: 1, order: 0,
  128. lid: source.id, contract_qty: contract_qty, contract_tp: contract_tp, postil: node.postil || ''
  129. });
  130. }
  131. }
  132. }
  133. loadDgn(node, cur) {
  134. if (!node.deal_dgn_qty1 && !node.deal_dgn_qty2 && !node.c_dgn_qty1 && !node.c_dgn_qty2) return;
  135. const dgn = this.stageBillsDgn.find(x => { return x.id === cur.id; });
  136. if (dgn) {
  137. this.updateDgn.push({ id: cur.id, deal_dgn_qty1: node.deal_dgn_qty1, deal_dgn_qty2: node.deal_dgn_qty2, c_dgn_qty1: node.c_dgn_qty1, c_dgn_qty2: node.c_dgn_qty2 });
  138. } else {
  139. this.insertDgn.push({ id: cur.id, tid: this.default.tid, deal_dgn_qty1: node.deal_dgn_qty1, deal_dgn_qty2: node.deal_dgn_qty2, c_dgn_qty1: node.c_dgn_qty1, c_dgn_qty2: node.c_dgn_qty2 });
  140. }
  141. }
  142. loadNode(node, parent) {
  143. node.is_leaf = !node.children || node.children.length === 0 ? 1 : 0;
  144. node.has_pos = !!(node.pos && node.pos.length > 0);
  145. const cur = this.findNode(node, parent);
  146. if (!cur || cur.settle_status === this.settleStatus.finish) return;
  147. if (cur) {
  148. if (!node.b_code) this.loadDgn(node, cur);
  149. if (node.is_leaf) {
  150. this.loadLeaf(node, cur);
  151. } else {
  152. for (const c of node.children) {
  153. this.loadNode(c, cur);
  154. }
  155. }
  156. }
  157. }
  158. load(excelTree, source) {
  159. this.init(source);
  160. for (const node of excelTree.roots) {
  161. this.loadNode(node, null);
  162. }
  163. }
  164. loadDealNode(node, parent, pos) {
  165. node.pos = pos.getLedgerPos(node.id) || [];
  166. node.has_pos = node.pos.length > 0;
  167. const cur = this.findNode(node, parent);
  168. if (!cur || cur.settle_status === this.settleStatus.finish) return;
  169. if (cur) {
  170. if (!node.b_code) this.loadDgn(node, cur);
  171. if (node.is_leaf) {
  172. this.loadLeaf(node, cur);
  173. } else {
  174. for (const c of node.children) {
  175. this.loadDealNode(c, cur, pos);
  176. }
  177. }
  178. }
  179. }
  180. loadDeal(dealTree, dealPos, source) {
  181. this.init(source);
  182. for (const node of dealTree.children) {
  183. this.loadDealNode(node, null, dealPos);
  184. }
  185. }
  186. }
  187. module.exports = app => {
  188. class StageStash extends app.BaseService {
  189. /**
  190. * 构造函数
  191. *
  192. * @param {Object} ctx - egg全局变量
  193. * @return {void}
  194. */
  195. constructor(ctx) {
  196. super(ctx);
  197. this.tableName = 'stage_stash';
  198. }
  199. async getValidStash(tid) {
  200. const sql = `SELECT id, sorder, create_time, uid, uname, remark FROM ${this.tableName} WHERE tid = ? and valid and TO_DAYS(NOW()) - TO_DAYS(create_time) <= 30 ORDER BY create_time DESC`;
  201. return await this.db.query(sql, [tid]);
  202. }
  203. async addStash(stage, remark = '') {
  204. const bills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  205. const pos = await this.ctx.service.stagePos.getLastestStageData2(stage.tid, stage.id);
  206. const change = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  207. const timestamp = (new Date()).getTime();
  208. const filepath = `${this.ctx.session.sessionProject.id}-${stage.tid}-${timestamp}.json`;
  209. await this.ctx.hisOss.put(this.ctx.stashOssPath + filepath, Buffer.from(JSON.stringify({bills, pos, change}), 'utf8'));
  210. const result = await this.db.insert(this.tableName, {
  211. pid: this.ctx.session.sessionProject.id, tid: stage.tid, sid: stage.id, sorder: stage.order,
  212. uid: this.ctx.session.sessionUser.accountId, uname: this.ctx.session.sessionUser.name,
  213. filepath, info: JSON.stringify({ status: stage.status, flow: stage.curAuditorIds.length > 0 ? stage.curAuditorIds : stage.uid }), remark
  214. });
  215. return result.insertId;
  216. }
  217. async delStash(id) {
  218. const stash = await this.getDataById(id);
  219. if (!stash) throw '暂存计量不存在';
  220. if (stash.tid !== this.ctx.tender.id) throw '非当前标段暂存计量,不可操作';
  221. await this.deleteById(id);
  222. await this.ctx.hisOss.delete(this.ctx.stashOssPath + stash.filepath);
  223. }
  224. async loadLedgerDataFromOss(filepath) {
  225. const File = await this.ctx.hisOss.get(this.ctx.stashOssPath + filepath);
  226. if (File.res.status !== 200) return '获取暂存计量有误';
  227. const result = JSON.parse(File.content);
  228. return result;
  229. }
  230. async loadAndAnalysis(filepath) {
  231. const data = await this.loadLedgerDataFromOss(filepath);
  232. const billsIndex = {};
  233. for (const b of data.bills) {
  234. billsIndex[b.lid] = b;
  235. }
  236. for (const p of data.pos) {
  237. if (!billsIndex[p.lid]) continue;
  238. if (!billsIndex[p.lid].pos) billsIndex[p.lid].pos = [];
  239. billsIndex[p.lid].pos.push(p);
  240. }
  241. for (const c of data.change) {
  242. if (!billsIndex[c.lid]) continue;
  243. if (billsIndex[c.lid].pos) {
  244. const p = billsIndex[c.lid].pos.find(x => { return x.pid === c.pid });
  245. if (!p) continue;
  246. if (!p.change) p.change = [];
  247. p.change.push(c);
  248. } else {
  249. if (!billsIndex[c.lid].change) billsIndex[c.lid].change = [];
  250. billsIndex[c.lid].change.push(c);
  251. }
  252. }
  253. return data.bills;
  254. }
  255. async reCalcStashData(stage, data) {
  256. const correct = this.ctx.subProject.page_show.correctCalcContractTp;
  257. const calcContractTp = function(helper, info, stageBills, bills) {
  258. if (info.calc_type === 'tp') {
  259. let activeQty = helper.add(bills.quantity, stageBills.qc_minus_qty);
  260. let end_contract_qty = stageBills.contract_qty;
  261. activeQty = helper.add(activeQty, bills.pre_qc_minus_qty);
  262. end_contract_qty = helper.add(end_contract_qty, bills.pre_contract_qty);
  263. const end_contract_tp = helper.mul(helper.div(end_contract_qty, activeQty), bills.total_price, decimal.tp);
  264. return helper.sub(end_contract_tp, bills.pre_contract_tp);
  265. } else if (info.calc_type === 'up') {
  266. if (correct) {
  267. let activeQty = helper.add(bills.quantity, stageBills.qc_minus_qty);
  268. let end_contract_qty = stageBills.contract_qty;
  269. activeQty = helper.add(activeQty, bills.pre_qc_minus_qty);
  270. end_contract_qty = helper.add(end_contract_qty, bills.pre_contract_qty);
  271. const end_contract_tp = helper.mul(end_contract_qty, bills.unit_price, decimal.tp);
  272. return activeQty === end_contract_qty ? helper.sub(end_contract_tp, bills.pre_contract_tp) : helper.mul(bills.unit_price, stageBills.contract_qty, info.decimal.tp);
  273. } else {
  274. return helper.mul(bills.unit_price, stageBills.contract_qty, info.decimal.tp);
  275. }
  276. }
  277. };
  278. const decimal = this.ctx.tender.info.decimal;
  279. const insertBillsData = [], insertPosData = [], insertChangeData = [];
  280. const settleStatus = this.ctx.service.settle.settleStatus;
  281. const bills = await this.ctx.service.ledger.getAllDataByCondition({ where: { tender_id: stage.tid, is_leaf: true } });
  282. const extraData = await this.ctx.service.ledgerExtra.getData(stage.tid, ['id', 'is_tp']);
  283. const settleStatusBills = stage.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
  284. const preStageBills = stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(stage.tid, this.ctx.stage.preCheckedStage.order) : [];
  285. this.ctx.helper.assignRelaData(bills, [
  286. { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
  287. { data: settleStatusBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
  288. { data: preStageBills, field: ['contract_qty', 'contract_tp', 'qc_minus_qty'], prefix: 'pre_', relaId: 'lid'},
  289. ]);
  290. const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: stage.tid } });
  291. const settleStatusPos = stage.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
  292. this.ctx.helper.assignRelaData(pos, [
  293. { data: settleStatusPos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
  294. ]);
  295. const billsIndex = {};
  296. for (const b of bills) {
  297. billsIndex[b.id] = b;
  298. }
  299. for (const p of pos) {
  300. if (!billsIndex[p.lid]) continue;
  301. if (!billsIndex[p.lid].pos) billsIndex[p.lid].pos = [];
  302. billsIndex[p.lid].pos.push(p);
  303. }
  304. const said = this.ctx.session.sessionUser.accountId;
  305. for (const d of data) {
  306. const b = billsIndex[d.lid]; //bills.find(x => { return x.id === d.lid });
  307. if (!b || b.settle_status === settleStatus.finish) continue;
  308. const nbs = {
  309. tid: stage.tid, sid: stage.id, said, lid: b.id, times: 1, order: 0,
  310. contract_qty: 0, qc_qty: 0, qc_minus_qty: 0, positive_qc_qty: 0, negative_qc_qty: 0,
  311. contract_tp: 0, qc_tp: 0, positive_qc_tp: 0, negative_qc_tp: 0
  312. };
  313. if (d.pos) {
  314. for (const bp of d.pos) {
  315. const p = b.pos ? b.pos.find(x => { return x.id === bp.pid}) : null;
  316. if (!p || p.settle_status === settleStatus.finish) continue;
  317. const nps = { tid: stage.tid, sid: stage.id, said, lid: b.id, pid: p.id, times: 1, order: 0 };
  318. nps.contract_qty = this.ctx.helper.round(bp.contract_qty, decimal.qty);
  319. nps.qc_qty = 0;
  320. nps.qc_minus_qty = 0;
  321. nps.positive_qc_qty = 0;
  322. nps.negative_qc_qty = 0;
  323. insertPosData.push(nps);
  324. if (bp.change) {
  325. for (const c of bp.change) {
  326. if (!c.qty) continue;
  327. const ncs = { tid: stage.tid, sid: stage.id, lid: b.id, pid: p.id, stimes: 1, sorder: 0, cid: c.cid, cbid: c.cbid, minus: c.minus, no_value: c.no_value };
  328. const validQty = await this.ctx.service.stageChangeFinal.getChangeBillsValidQty(c.cbid);
  329. ncs.qty = ncs.minus ? Math.max(validQty, c.qty) : Math.min(validQty, c.qty);
  330. insertChangeData.push(ncs);
  331. if (!ncs.no_value) {
  332. nps.qc_qty = this.ctx.helper.add(nps.qc_qty, ncs.qty);
  333. if (ncs.minus) {
  334. nps.negative_qc_qty = this.ctx.helper.add(nps.negative_qc_qty, ncs.qty);
  335. } else {
  336. nps.positive_qc_qty = this.ctx.helper.add(nps.positive_qc_qty, ncs.qty);
  337. }
  338. } else {
  339. nps.qc_minus_qty = this.ctx.helper.add(nps.qc_minus_qty, ncs.qty);
  340. }
  341. }
  342. }
  343. nbs.contract_qty = this.ctx.helper.add(nbs.contract_qty, nps.contract_qty);
  344. nbs.qc_qty = this.ctx.helper.add(nbs.qc_qty, nps.qc_qty);
  345. nbs.qc_minus_qty = this.ctx.helper.add(nbs.qc_minus_qty, nps.qc_minus_qty);
  346. nbs.positive_qc_qty = this.ctx.helper.add(nbs.positive_qc_qty, nps.positive_qc_qty);
  347. nbs.negative_qc_qty = this.ctx.helper.add(nbs.negative_qc_qty, nps.negative_qc_qty);
  348. }
  349. nbs.contract_tp = calcContractTp(this.ctx.helper, this.ctx.tender.info, nbs, b);
  350. nbs.qc_tp = this.ctx.helper.mul(nbs.qc_qty, b.unit_price, decimal.tp);
  351. nbs.positive_qc_tp = this.ctx.helper.mul(nbs.positive_qc_qty, b.unit_price, decimal.tp);
  352. nbs.negative_qc_tp = this.ctx.helper.mul(nbs.negative_qc_qty, b.unit_price, decimal.tp);
  353. } else {
  354. if (b.is_tp) {
  355. nbs.contract_tp = this.ctx.helper.round(d.contract_tp, decimal.tp);
  356. } else {
  357. nbs.contract_qty = this.ctx.helper.round(d.contract_qty, decimal.qty);
  358. if (d.change) {
  359. for (const c of d.change) {
  360. if (!c.qty) continue;
  361. const ncs = { tid: stage.tid, sid: stage.id, lid: b.id, pid: -1, stimes: 1, sorder: 0, cid: c.cid, cbid: c.cbid, minus: c.minus, no_value: c.no_value };
  362. const validQty = await this.ctx.service.stageChangeFinal.getChangeBillsValidQty(c.cbid);
  363. ncs.qty = ncs.minus ? Math.max(validQty, c.qty) : Math.min(validQty, c.qty);
  364. insertChangeData.push(ncs);
  365. if (!ncs.no_value) {
  366. nbs.qc_qty = this.ctx.helper.add(nbs.qc_qty, ncs.qty);
  367. if (ncs.minus) {
  368. nbs.negative_qc_qty = this.ctx.helper.add(nbs.negative_qc_qty, ncs.qty);
  369. } else {
  370. nbs.positive_qc_qty = this.ctx.helper.add(nbs.positive_qc_qty, ncs.qty);
  371. }
  372. } else {
  373. nbs.qc_minus_qty = this.ctx.helper.add(nbs.qc_minus_qty, ncs.qty);
  374. }
  375. }
  376. }
  377. nbs.contract_tp = calcContractTp(this.ctx.helper, this.ctx.tender.info, nbs, b);
  378. nbs.qc_tp = this.ctx.helper.mul(nbs.qc_qty, b.unit_price, decimal.tp);
  379. nbs.positive_qc_tp = this.ctx.helper.mul(nbs.positive_qc_qty, b.unit_price, decimal.tp);
  380. nbs.negative_qc_tp = this.ctx.helper.mul(nbs.negative_qc_qty, b.unit_price, decimal.tp);
  381. }
  382. }
  383. insertBillsData.push(nbs);
  384. }
  385. return [insertBillsData, insertPosData, insertChangeData]
  386. }
  387. async recover(stage, id) {
  388. const stash = await this.getDataById(id);
  389. if (!stash) throw '暂存计量不存在';
  390. if (stash.tid !== stage.tid) throw '暂存计量不可导入';
  391. const stashData = await this.loadAndAnalysis(stash.filepath);
  392. const [insertBills, insertPos, insertChange] = await this.reCalcStashData(stage, stashData);
  393. const conn = await this.db.beginTransaction();
  394. try {
  395. await conn.delete(this.ctx.service.stageBills.tableName, { sid: stage.id });
  396. await conn.delete(this.ctx.service.stageChange.tableName, { sid: stage.id });
  397. await conn.delete(this.ctx.service.stagePos.tableName, { sid: stage.id });
  398. if (insertBills.length > 0) conn.insert(this.ctx.service.stageBills.tableName, insertBills);
  399. if (insertPos.length > 0) conn.insert(this.ctx.service.stagePos.tableName, insertPos);
  400. if (insertChange.length > 0) conn.insert(this.ctx.service.stageChange.tableName, insertChange);
  401. await conn.commit();
  402. } catch (err) {
  403. await conn.rollback();
  404. throw err;
  405. }
  406. };
  407. /**
  408. * 导入Excel期计量(仅导入合同计量)
  409. * 该方法本应该独立或者在stage下,但是跟stage_stash业务非常类似,暂归类于此
  410. * @param stage
  411. * @param sheet
  412. * @returns {Promise<void>}
  413. */
  414. async loadExcelSheet(stage, excelData) {
  415. const AnalysisExcel = require('../lib/analysis_excel').AnalysisStageExcelTree;
  416. const analysisExcel = new AnalysisExcel(this.ctx, this.ctx.service.ledger.setting);
  417. try {
  418. const templateId = await this.ctx.service.valuation.getValuationTemplate(
  419. this.ctx.tender.data.valuation, this.ctx.tender.data.measure_type);
  420. const tempData = await this.ctx.service.tenderNodeTemplate.getData(templateId, true);
  421. const cacheTree = analysisExcel.analysisData(excelData, tempData, { filterZeroGcl: false });
  422. const ledgerData = await this.ctx.service.ledger.getAllDataByCondition({
  423. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  424. where: { tender_id: stage.tid},
  425. });
  426. const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']);
  427. const settleStatusBills = stage.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
  428. this.ctx.helper.assignRelaData(ledgerData, [
  429. { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
  430. { data: settleStatusBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
  431. ]);
  432. const posData = await this.ctx.service.pos.getAllDataByCondition({
  433. columns: ['id', 'lid', 'name', 'porder'],
  434. where: { tid: stage.tid },
  435. });
  436. const settleStatusPos = stage.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
  437. this.ctx.helper.assignRelaData(posData, [
  438. { data: settleStatusPos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
  439. ]);
  440. const stageBills = await this.ctx.service.stageBills.getAllDataByCondition({ where: { sid: stage.id } });
  441. const stagePos = await this.ctx.service.stagePos.getAllDataByCondition({ where: { sid: stage.id } });
  442. const stageBillsDgn = await this.ctx.service.stageBillsDgn.getAllDataByCondition({ where: { tid: stage.tid } });
  443. const preStageBills = stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(stage.tid, this.ctx.stage.preCheckedStage.order) : [];
  444. const loadModal = new loadStageExcelTree(this.ctx);
  445. loadModal.load(cacheTree, { ledgerData, posData, stageBills, stagePos, stageBillsDgn, preStageBills, default: { tid: stage.tid, sid: stage.id, said: this.ctx.session.sessionUser.accountId } });
  446. const conn = await this.db.beginTransaction();
  447. try {
  448. if (loadModal.insertBills.length > 0) conn.insert(this.ctx.service.stageBills.tableName, loadModal.insertBills);
  449. if (loadModal.updateBills.length > 0) conn.updateRows(this.ctx.service.stageBills.tableName, loadModal.updateBills);
  450. if (loadModal.insertPos.length > 0) conn.insert(this.ctx.service.stagePos.tableName, loadModal.insertPos);
  451. if (loadModal.updatePos.length > 0) conn.updateRows(this.ctx.service.stagePos.tableName, loadModal.updatePos);
  452. if (loadModal.insertDgn.length > 0) conn.insert(this.ctx.service.stageBillsDgn.tableName, loadModal.insertDgn);
  453. if (loadModal.updateDgn.length > 0) conn.updateRows(this.ctx.service.stageBillsDgn.tableName, loadModal.updateDgn);
  454. await conn.commit();
  455. } catch (err) {
  456. await conn.rollback();
  457. this.ctx.log(err);
  458. throw '保存导入数据失败';
  459. }
  460. } catch (err) {
  461. this.ctx.log(err);
  462. throw '解析Excel错误';
  463. }
  464. }
  465. async loadStageDealData(stage, dealData) {
  466. try {
  467. const dealTree = new Ledger.billsTree(this.ctx, {
  468. id: 'ledger_id',
  469. pid: 'ledger_pid',
  470. order: 'order',
  471. level: 'level',
  472. rootId: -1,
  473. });
  474. const dealPos = new Ledger.pos({ id: 'id', ledgerId: 'lid' });
  475. dealTree.loadDatas(dealData.ledger);
  476. dealPos.loadDatas(dealData.pos);
  477. const ledgerData = await this.ctx.service.ledger.getAllDataByCondition({
  478. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'b_code', 'name', 'unit', 'unit_price', 'quantity', 'total_price'],
  479. where: { tender_id: stage.tid},
  480. });
  481. const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']);
  482. const settleStatusBills = stage.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
  483. this.ctx.helper.assignRelaData(ledgerData, [
  484. { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
  485. { data: settleStatusBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
  486. ]);
  487. const posData = await this.ctx.service.pos.getAllDataByCondition({
  488. columns: ['id', 'lid', 'name', 'porder'],
  489. where: { tid: stage.tid },
  490. });
  491. const settleStatusPos = stage.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
  492. this.ctx.helper.assignRelaData(posData, [
  493. { data: settleStatusPos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
  494. ]);
  495. const stageBills = await this.ctx.service.stageBills.getAllDataByCondition({ where: { sid: stage.id } });
  496. const stagePos = await this.ctx.service.stagePos.getAllDataByCondition({ where: { sid: stage.id } });
  497. const stageBillsDgn = await this.ctx.service.stageBillsDgn.getAllDataByCondition({ where: { tid: stage.tid } });
  498. const preStageBills = stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(stage.tid, this.ctx.stage.preCheckedStage.order) : [];
  499. const loadModal = new loadStageExcelTree(this.ctx);
  500. loadModal.loadDeal(dealTree, dealPos, { ledgerData, posData, stageBills, stagePos, stageBillsDgn, preStageBills, default: { tid: stage.tid, sid: stage.id, said: this.ctx.session.sessionUser.accountId } });
  501. const conn = await this.db.beginTransaction();
  502. try {
  503. if (loadModal.insertBills.length > 0) conn.insert(this.ctx.service.stageBills.tableName, loadModal.insertBills);
  504. if (loadModal.updateBills.length > 0) conn.updateRows(this.ctx.service.stageBills.tableName, loadModal.updateBills);
  505. if (loadModal.insertPos.length > 0) conn.insert(this.ctx.service.stagePos.tableName, loadModal.insertPos);
  506. if (loadModal.updatePos.length > 0) conn.updateRows(this.ctx.service.stagePos.tableName, loadModal.updatePos);
  507. if (loadModal.insertDgn.length > 0) conn.insert(this.ctx.service.stageBillsDgn.tableName, loadModal.insertDgn);
  508. if (loadModal.updateDgn.length > 0) conn.updateRows(this.ctx.service.stageBillsDgn.tableName, loadModal.updateDgn);
  509. await conn.commit();
  510. } catch (err) {
  511. await conn.rollback();
  512. this.ctx.log(err);
  513. throw '保存导入数据失败';
  514. }
  515. } catch (err) {
  516. this.ctx.log(err);
  517. throw '导入本期合同计量';
  518. }
  519. }
  520. }
  521. return StageStash;
  522. };