stage_stash.js 29 KB

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