tender_info.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/10/30
  7. * @version
  8. */
  9. const infoConst = require('../const/tender_info');
  10. const parseInfo = infoConst.parseInfo;
  11. const arrayInfo = infoConst.arrayInfo;
  12. const advanceConst = require('../const/audit').advance;
  13. const auditConst = require('../const/audit');
  14. const measureType = require('../const/tender').measureType;
  15. module.exports = app => {
  16. class TenderInfo extends app.BaseService {
  17. /**
  18. * 构造函数
  19. *
  20. * @param {Object} ctx - egg全局变量
  21. * @return {void}
  22. */
  23. constructor(ctx) {
  24. super(ctx);
  25. this.tableName = 'tender_info';
  26. }
  27. async getDefaultInfo (tenderId) {
  28. const tender = await this.ctx.service.tender.getTender(tenderId);
  29. return tender.measure_type === measureType.tz.value ? infoConst.tzDefaultInfo : infoConst.gclDefaultInfo;
  30. }
  31. /**
  32. * 新增 标段相关信息
  33. *
  34. * @param tenderId - 标段Id
  35. * @param projectId - 项目Id
  36. * @param transaction - 事务
  37. * @return {Promise<void>}
  38. */
  39. async addTenderInfo(tenderId, projectId, transaction) {
  40. const defaultInfo = await this.getDefaultInfo(tenderId);
  41. const info = JSON.parse(JSON.stringify(defaultInfo));
  42. info.tid = tenderId;
  43. info.pid = projectId;
  44. for (const pi of parseInfo) {
  45. info[pi] = JSON.stringify(info[pi]);
  46. }
  47. for (const pi of arrayInfo) {
  48. info[pi] = JSON.stringify(info[pi]);
  49. }
  50. if (transaction) {
  51. await transaction.insert(this.tableName, info);
  52. } else {
  53. await this.db.insert(this.tableName, info);
  54. }
  55. return info;
  56. }
  57. /**
  58. * 获取标段相关信息
  59. * @param tenderId
  60. * @return {Promise<void>}
  61. */
  62. async getTenderInfo(tenderId, projectId) {
  63. const defaultInfo = await this.getDefaultInfo(tenderId);
  64. let info = await this.getDataByCondition({ tid: tenderId });
  65. // 兼容不存在info的情况
  66. if (!info) info = await this.addTenderInfo(tenderId, projectId || this.ctx.session.sessionProject.id);
  67. for (const pi of parseInfo) {
  68. info[pi] = !info[pi] || info[pi] === '' ? (pi === 'shenpi' ? JSON.parse(JSON.stringify(defaultInfo[pi])) : defaultInfo[pi]) : JSON.parse(info[pi]);
  69. this.ctx.helper._.defaultsDeep(info[pi], defaultInfo[pi]);
  70. }
  71. for (const ai of arrayInfo) {
  72. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  73. }
  74. if (info.decimal) {
  75. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  76. }
  77. info.checkOverInfo = infoConst.transOverRangeCheck(this.ctx.helper, info.over_range_check);
  78. return info;
  79. }
  80. /**
  81. * 获取标段相关信息(报表用)
  82. * @param tenderId
  83. * @return {Promise<void>}
  84. */
  85. async getTenderInfoEx(tenderId) {
  86. const defaultInfo = await this.getDefaultInfo(tenderId);
  87. const sql = 'select t2.name, t1.* from zh_tender_info t1 inner join zh_tender t2 on t2.id = t1.tid where t1.tid = ?';
  88. const sqlParam = [tenderId];
  89. const list = await this.db.query(sql, sqlParam);
  90. const info = list[0];
  91. const len = info.deal_info.length;
  92. info.deal_info = info.deal_info.slice(0, len - 1) + ',"name":"' + info.name + '"' + info.deal_info.slice(len - 1);
  93. for (const pi of parseInfo) {
  94. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  95. this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
  96. }
  97. for (const ai of arrayInfo) {
  98. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  99. }
  100. if (info.decimal) {
  101. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  102. }
  103. return info;
  104. }
  105. /**
  106. * 保存标段相关信息
  107. *
  108. * @param data
  109. * @return {Promise<void>}
  110. */
  111. async saveTenderInfo(tenderId, data) {
  112. for (const di in data) {
  113. if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
  114. data[di] = JSON.stringify(data[di]);
  115. }
  116. }
  117. await this.db.update(this.tableName, data, { where: { tid: tenderId } });
  118. }
  119. async _getLedgerService() {
  120. try {
  121. if (this.ctx.tender.data.ledger_status === auditConst.ledger.status.checked) {
  122. const revise = await this.ctx.service.ledgerRevise.getLastestRevise(this.ctx.tender.id);
  123. if (revise.status === auditConst.revise.status.uncheck || revise.status === auditConst.revise.status.checkNo) {
  124. return [this.ctx.service.reviseBills, this.ctx.service.revisePos];
  125. }
  126. } else {
  127. return [this.ctx.service.ledger, this.ctx.service.pos];
  128. }
  129. } catch(err) {
  130. }
  131. return [];
  132. }
  133. async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
  134. const changePrecision = [];
  135. const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
  136. const defUnits = this._.map(newPrecision, 'units');
  137. const otherUnits = units.filter(function(u) { return defUnits.indexOf(u) === -1; });
  138. let changeUnits = [];
  139. for (const prop in newPrecision) {
  140. if (oldPrecision[prop]) {
  141. if (newPrecision[prop].value < oldPrecision[prop].value) {
  142. changePrecision.push(newPrecision[prop]);
  143. }
  144. } else {
  145. changePrecision.push(newPrecision[prop]);
  146. }
  147. }
  148. for (const cp of changePrecision) {
  149. if (cp.unit) {
  150. changeUnits.push(cp.unit);
  151. } else {
  152. changeUnits.push(otherUnits);
  153. }
  154. }
  155. changeUnits = this._.flatten(changeUnits);
  156. const [billsService, posService] = await this._getLedgerService();
  157. if (changeUnits.length > 0 && billsService && posService) {
  158. const bills = await billsService.getAllDataByCondition({
  159. columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'ex_qty1'],
  160. where: { tender_id: tenderId, unit: changeUnits, is_leaf: true },
  161. });
  162. const pos = changeUnits.length > 0 ? await posService.getPosDataByUnits(tenderId, changeUnits) : [];
  163. for (const b of bills) {
  164. const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
  165. const bPos = this._.filter(pos, { lid: b.id });
  166. if (bPos.length > 0) {
  167. let sgfh_qty = 0, sjcl_qty = 0, qtcl_qty = 0, quantity = 0, ex_qty1 = 0;
  168. for (const p of bPos) {
  169. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'ex_qty1'], precision.value);
  170. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  171. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  172. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  173. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  174. quantity = this.ctx.helper.add(quantity, p.quantity);
  175. ex_qty1 = this.ctx.helper.add(quantity, p.ex_qty1);
  176. }
  177. b.sgfh_qty = sgfh_qty;
  178. b.sjcl_qty = sjcl_qty;
  179. b.qtcl_qty = qtcl_qty;
  180. b.quantity = quantity;
  181. b.ex_qty1 = ex_qty1;
  182. // this.ctx.helper.checkFieldPrecision(b, ['deal_qty'], precision.value);
  183. } else {
  184. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'ex_qty1'], precision.value);
  185. }
  186. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  187. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  188. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  189. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  190. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  191. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  192. b.ex_tp1 = this.ctx.helper.mul(b.ex_qty1, b.unit_price, decimal.tp);
  193. }
  194. const transaction = await this.db.beginTransaction();
  195. try {
  196. await transaction.update(this.tableName,
  197. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  198. if (bills.length > 0) await transaction.updateRows(billsService.tableName, bills);
  199. if (pos.length > 0) await transaction.updateRows(posService.tableName, pos);
  200. await transaction.commit();
  201. } catch (err) {
  202. await transaction.rollback();
  203. throw err;
  204. }
  205. } else {
  206. await this.db.update(this.tableName,
  207. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  208. }
  209. }
  210. async _reCalcLedger(tenderId, billsService, newDecimal, oldDecimal) {
  211. if (!billsService) return [];
  212. const changeBills = [];
  213. const calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
  214. if (calcUp || calcTp) {
  215. const bills = await billsService.getAllDataByCondition({
  216. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity', 'ex_qty1'],
  217. where: { tender_id: tenderId, is_leaf: true },
  218. });
  219. for (const b of bills) {
  220. const cb = { id: b.id };
  221. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  222. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  223. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  224. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  225. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  226. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  227. cb.ex_tp1 = this.ctx.helper.mul(b.ex_qty1, cb.unit_price, newDecimal.tp);
  228. changeBills.push(cb);
  229. }
  230. }
  231. return changeBills;
  232. }
  233. async _reCalcStageBillsUp(stages, updateStageBills, insertStageBills, decimal) {
  234. for (const stage of stages) {
  235. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  236. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  237. columns: ['id', 'unit_price'],
  238. where: { tender_id: stage.tid, is_leaf: true },
  239. });
  240. for (const sb of stageBills) {
  241. const b = bills.find(x => {return x.id === sb.lid});
  242. if (!b) continue;
  243. const contract_tp = this.ctx.helper.mul(b.unit_price, sb.contract_qty, decimal.tp);
  244. const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, decimal.tp);
  245. const positive_qc_tp = this.ctx.helper.mul(b.unit_price, sb.positive_qc_qty, decimal.tp);
  246. const negative_qc_tp = this.ctx.helper.mul(b.unit_price, sb.negative_qc_qty, decimal.tp);
  247. const ex_stage_tp1 = this.ctx.helper.mul(b.unit_price, sb.ex_stage_qty1, decimal.tp);
  248. if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp && ex_stage_tp1 === sb.ex_stage_tp1
  249. && positive_qc_tp === sb.positive_qc_tp && negative_qc_tp === sb.negative_qc_tp) continue;
  250. if (sb.times === stage.times && sb.order === 0) {
  251. updateStageBills.push({
  252. id: sb.id, contract_tp, qc_tp, positive_qc_tp, negative_qc_tp, ex_stage_tp1,
  253. });
  254. } else {
  255. insertStageBills.push({
  256. tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
  257. times: stage.times, order: 0,
  258. contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
  259. qc_qty: sb.qc_qty, qc_tp, qc_minus_qty: sb.qc_minus_qty,
  260. positive_qc_qty: sb.positive_qc_qty, positive_qc_tp: positive_qc_tp,
  261. negative_qc_qty: sb.negative_qc_qty, negative_qc_tp: negative_qc_tp,
  262. ex_stage_qty1: sb.ex_stage_qty1, ex_stage_tp1,
  263. postil: sb.postil,
  264. });
  265. }
  266. }
  267. }
  268. }
  269. async _reCalcStageBillsTp(stages, updateStageBills, insertStageBills, decimal) {
  270. stages.sort((x, y) => { return x.order - y.order; });
  271. const preStage = stages[0].order > 1 ? await this.ctx.service.stage.getDataByCondition({ tid: stages[0].tid, order: stages[0].order - 1 }) : null;
  272. const preStageBills = preStage ? await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: preStage.tid, sid: preStage.id } }) : [];
  273. for (const stage of stages) {
  274. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  275. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  276. columns: ['id', 'unit_price', 'quantity', 'total_price'],
  277. where: { tender_id: stage.tid, is_leaf: true },
  278. });
  279. for (const sb of stageBills) {
  280. const preSb = preStageBills.find(x => { return x.lid === sb.lid; });
  281. const b = bills.find(x => {return x.id === sb.lid});
  282. if (!b) continue;
  283. let activeQty = this.ctx.helper.add(b.quantity, sb.qc_minus_qty);
  284. let end_contract_qty = sb.contract_qty;
  285. if (preSb) {
  286. activeQty = this.ctx.helper.add(activeQty, preSb.qc_minus_qty);
  287. end_contract_qty = this.ctx.helper.add(end_contract_qty, preSb.contract_qty);
  288. }
  289. const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), b.total_price, decimal.tp);
  290. const contract_tp = preSb ? this.ctx.helper.sub(end_contract_tp, preSb.contract_tp) : end_contract_tp;
  291. const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, decimal.tp);
  292. const positive_qc_tp = this.ctx.helper.mul(b.unit_price, sb.positive_qc_qty, decimal.tp);
  293. const negative_qc_tp = this.ctx.helper.mul(b.unit_price, sb.negative_qc_qty, decimal.tp);
  294. const ex_stage_tp1 = this.ctx.helper.mul(b.unit_price, sb.ex_stage_qty1, decimal.tp);
  295. if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp && ex_stage_tp1 === sb.ex_stage_tp1
  296. && positive_qc_tp === sb.positive_qc_tp && negative_qc_tp === sb.negative_qc_tp) continue;
  297. if (sb.times === stage.times && sb.order === 0) {
  298. updateStageBills.push({
  299. id: sb.id, contract_tp, qc_tp, ex_stage_tp1, positive_qc_tp, negative_qc_tp,
  300. });
  301. } else {
  302. insertStageBills.push({
  303. tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
  304. times: stage.times, order: 0,
  305. contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
  306. qc_qty: sb.qc_qty, qc_tp, qc_minus_qty: sb.qc_minus_qty,
  307. positive_qc_qty: sb.positive_qc_qty, positive_qc_tp: positive_qc_tp,
  308. negative_qc_qty: sb.negative_qc_qty, negative_qc_tp: negative_qc_tp,
  309. ex_stage_qty1: sb.ex_stage_qty1, ex_stage_tp1,
  310. postil: sb.postil,
  311. });
  312. }
  313. }
  314. }
  315. }
  316. async _reCalcStageBills(tenderId, newDecimal, oldDecimal) {
  317. const updateStageBills = [], insertStageBills = [];
  318. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  319. if (stages.length === 0 || newDecimal.tp === oldDecimal.tp) return [updateStageBills, insertStageBills];
  320. switch (this.ctx.tender.info.calc_type) {
  321. case 'up':
  322. await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, newDecimal);
  323. break;
  324. case 'tp':
  325. await this._reCalcStageBillsTp(stages, updateStageBills, insertStageBills, newDecimal);
  326. break;
  327. default:
  328. await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, newDecimal);
  329. break;
  330. }
  331. return [updateStageBills, insertStageBills];
  332. }
  333. async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
  334. const changeSj = [], changeSb = [], changeSo = [];
  335. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  336. if (stages.length === 0) return [changeSj, changeSb, changeSo];
  337. const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
  338. const calcUp = upDecimal < oldDecimal.up,
  339. calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
  340. for (const stage of stages) {
  341. if (calcUp || calcTp) {
  342. const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
  343. columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
  344. where: { sid: stage.id }
  345. });
  346. for (const sj of stageJgcl) {
  347. const cj = { id: sj.id };
  348. cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
  349. cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
  350. cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
  351. changeSj.push(cj);
  352. }
  353. }
  354. if (calcTp) {
  355. const stageChangeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
  356. columns: ['id', 'tp'],
  357. where: { sid: stage.id }
  358. });
  359. for (const cb of stageChangeSb) {
  360. cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
  361. }
  362. changeSb.push(...stageChangeSb);
  363. const stageChangeSo = await this.ctx.service.stageOther.getAllDataByCondition({
  364. columns: ['id', 'total_price', 'tp'],
  365. where: { sid: stage.id }
  366. });
  367. for (const co of stageChangeSo) {
  368. if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
  369. co.tp = this.ctx.helper.round(co.tp, tpDecimal);
  370. }
  371. changeSo.push(...stageChangeSo);
  372. }
  373. }
  374. return [changeSj, changeSb, changeSo];
  375. }
  376. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  377. const changeAdvanceBills = [];
  378. const caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
  379. if (caclPayTp) {
  380. // 获取预付款需要修改的相关记录
  381. const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
  382. columns: ['id', 'cur_amount', 'prev_amount', 'prev_total_amount'],
  383. where: { status: [advanceConst.status.uncheck, advanceConst.status.checkNo], tid: tenderId },
  384. });
  385. const decimal = newDecimal.pay ? newDecimal.payTp : newDecimal.tp;
  386. // 根据精度重新计算相关金额
  387. for (const ad of ad_bills) {
  388. const cb = { id: ad.id };
  389. const cur_amount = this.ctx.helper.round(ad.cur_amount, decimal);
  390. cb.cur_amount = cur_amount;
  391. cb.prev_total_amount = this.ctx.helper.add(cur_amount, ad.prev_amount);
  392. changeAdvanceBills.push(cb);
  393. }
  394. }
  395. const [billsService] = await this._getLedgerService();
  396. const changeBills = await this._reCalcLedger(tenderId, billsService, newDecimal, oldDecimal);
  397. const [updateStageBills, insertStageBills] = await this._reCalcStageBills(tenderId, newDecimal, oldDecimal);
  398. const [changeSj, changeSb, changeSo] = await this._reCalcStageExtra(tenderId, newDecimal, oldDecimal);
  399. if (changeBills.length > 0 ||
  400. changeAdvanceBills.length > 0 ||
  401. updateStageBills.length > 0 || insertStageBills.length > 0 ||
  402. changeSj.length > 0 || changeSb.length > 0 || changeSo.length > 0) {
  403. const transaction = await this.db.beginTransaction();
  404. try {
  405. await transaction.update(this.tableName,
  406. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  407. if (changeBills.length > 0) await transaction.updateRows(billsService.tableName, changeBills);
  408. if (updateStageBills.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateStageBills);
  409. if (insertStageBills.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertStageBills);
  410. if (changeSj.length > 0) await transaction.updateRows(this.ctx.service.stageJgcl.tableName, changeSj);
  411. if (changeSb.length > 0) await transaction.updateRows(this.ctx.service.stageBonus.tableName, changeSb);
  412. if (changeSo.length > 0) await transaction.updateRows(this.ctx.service.stageOther.tableName, changeSo);
  413. if (changeAdvanceBills.length) await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
  414. await transaction.commit();
  415. } catch (error) {
  416. await transaction.rollback();
  417. throw error;
  418. }
  419. } else {
  420. await this.db.update(this.tableName,
  421. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  422. }
  423. }
  424. async _reCalcStageBills2(tenderId, calcType) {
  425. const updateStageBills = [], insertStageBills = [];
  426. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  427. if (stages.length === 0) return [updateStageBills, insertStageBills];
  428. switch (calcType) {
  429. case 'up':
  430. await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
  431. break;
  432. case 'tp':
  433. await this._reCalcStageBillsTp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
  434. break;
  435. default:
  436. await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
  437. break;
  438. }
  439. return [updateStageBills, insertStageBills];
  440. }
  441. async saveCalcType(tenderId, calcType) {
  442. if (!this.ctx.tender.info.calc_type && calcType === 'up') return;
  443. if (this.ctx.tender.info.calc_type && calcType === this.ctx.tender.info.calc_type) return;
  444. const [updateStageBills, insertStageBills] = await this._reCalcStageBills2(tenderId, calcType);
  445. const transaction = await this.db.beginTransaction();
  446. try {
  447. await transaction.update(this.tableName, { calc_type: calcType }, { where: { tid: tenderId } });
  448. if (updateStageBills.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateStageBills);
  449. if (insertStageBills.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertStageBills);
  450. await transaction.commit();
  451. } catch(err) {
  452. await transaction.rollback();
  453. throw '保存计价类型失败';
  454. }
  455. }
  456. /**
  457. * 获取标段审批相关信息 (审批设置用)
  458. * @param tenderId
  459. * @return {Promise<void>}
  460. */
  461. async getTenderShenpiInfo(tenderId) {
  462. const defaultInfo = await this.getDefaultInfo(tenderId);
  463. const info = await this.getDataByCondition({ tid: tenderId });
  464. // 还没选择模式的标段不应该可以选择审批流程设置
  465. if (!info) {
  466. return false;
  467. }
  468. const defaultShenpiInfo = JSON.parse(JSON.stringify(defaultInfo.shenpi));
  469. const shenpiInfo = !info.shenpi || info.shenpi === null || info.shenpi === '' ? defaultShenpiInfo : JSON.parse(info.shenpi);
  470. // 查漏补缺
  471. for (const sp in defaultShenpiInfo) {
  472. if (!shenpiInfo[sp]) {
  473. shenpiInfo[sp] = defaultShenpiInfo[sp];
  474. }
  475. }
  476. return shenpiInfo;
  477. }
  478. /**
  479. * 拷贝标段数据至当前标段
  480. * @param {number} id - 当前标段id
  481. * @param {number} copy_id - 被拷贝的标段id
  482. * @param {Array} typeArr - 需要拷贝的类型数组
  483. */
  484. async copyTenderHandler(id, copy_id, typeArr) {
  485. const columns = [];
  486. typeArr.forEach(item => {
  487. if (item === 'tender') {
  488. columns.push('deal_info', 'construction_unit', 'tech_param', 'bid_info');
  489. } else if (item === 'chapter') {
  490. columns.push('chapter');
  491. } else if (item === 'pay_account') {
  492. columns.push('pay_account');
  493. }
  494. });
  495. if (!columns.length) throw '未选择需要拷贝的设置';
  496. const [data] = await this.getAllDataByCondition({
  497. where: {
  498. tid: copy_id,
  499. },
  500. columns,
  501. });
  502. const isUpdate = await this.update(data, { tid: id });
  503. if (isUpdate) {
  504. await this.ctx.service.tender.update({ copy_id }, { id });
  505. }
  506. }
  507. }
  508. return TenderInfo;
  509. };