stage_stash.js 27 KB

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