tender_info.js 29 KB

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