tender_info.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 ex_stage_tp1 = this.ctx.helper.mul(b.unit_price, sb.ex_stage_qty1, decimal.tp);
  246. if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp && ex_stage_tp1 === sb.ex_stage_tp1) continue;
  247. if (sb.times === stage.times && sb.order === 0) {
  248. updateStageBills.push({
  249. id: sb.id, contract_tp, qc_tp, ex_stage_tp1,
  250. });
  251. } else {
  252. insertStageBills.push({
  253. tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
  254. times: stage.times, order: 0,
  255. contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
  256. qc_qty: sb.qc_qty, qc_tp,
  257. ex_stage_qty1: sb.ex_stage_qty1, ex_stage_tp1,
  258. postil: sb.postil,
  259. });
  260. }
  261. }
  262. }
  263. }
  264. async _reCalcStageBillsTp(stages, updateStageBills, insertStageBills, decimal) {
  265. stages.sort((x, y) => { return x.order - y.order; });
  266. const preStage = stages[0].order > 1 ? await this.ctx.service.stage.getDataByCondition({ tid: stages[0].tid, order: stages[0].order - 1 }) : null;
  267. const preStageBills = preStage ? await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: preStage.tid, sid: preStage.id } }) : [];
  268. for (const stage of stages) {
  269. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  270. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  271. columns: ['id', 'unit_price', 'quantity', 'total_price'],
  272. where: { tender_id: stage.tid, is_leaf: true },
  273. });
  274. for (const sb of stageBills) {
  275. const preSb = preStageBills.find(x => { return x.lid === sb.lid; });
  276. const b = bills.find(x => {return x.id === sb.lid});
  277. if (!b) continue;
  278. let activeQty = this.ctx.helper.add(b.quantity, sb.qc_minus_qty);
  279. let end_contract_qty = sb.contract_qty;
  280. if (preSb) {
  281. activeQty = this.ctx.helper.add(activeQty, preSb.qc_minus_qty);
  282. end_contract_qty = this.ctx.helper.add(end_contract_qty, preSb.contract_qty);
  283. }
  284. const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), b.total_price, decimal.tp);
  285. const contract_tp = preSb ? this.ctx.helper.sub(end_contract_tp, preSb.contract_tp) : end_contract_tp;
  286. const qc_tp = this.ctx.helper.mul(b.unit_price, sb.qc_qty, decimal.tp);
  287. const ex_stage_tp1 = this.ctx.helper.mul(b.unit_price, sb.ex_stage_qty1, decimal.tp);
  288. if (contract_tp == sb.contract_tp && qc_tp === sb.qc_tp && ex_stage_tp1 === sb.ex_stage_tp1) continue;
  289. if (sb.times === stage.times && sb.order === 0) {
  290. updateStageBills.push({
  291. id: sb.id, contract_tp, qc_tp, ex_stage_tp1,
  292. });
  293. } else {
  294. insertStageBills.push({
  295. tid: stage.tid, lid: sb.lid, sid: stage.id, said: this.ctx.session.sessionUser.accountId,
  296. times: stage.times, order: 0,
  297. contract_qty: sb.contract_qty, contract_expr: sb.contract_expr, contract_tp,
  298. qc_qty: sb.qc_qty, qc_tp,
  299. ex_stage_qty1: sb.ex_stage_qty1, ex_stage_tp1,
  300. postil: sb.postil,
  301. });
  302. }
  303. }
  304. }
  305. }
  306. async _reCalcStageBills(tenderId, newDecimal, oldDecimal) {
  307. const updateStageBills = [], insertStageBills = [];
  308. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  309. if (stages.length === 0 || newDecimal.tp === oldDecimal.tp) return [updateStageBills, insertStageBills];
  310. switch (this.ctx.tender.info.calc_type) {
  311. case 'up':
  312. await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, newDecimal);
  313. break;
  314. case 'tp':
  315. await this._reCalcStageBillsTp(stages, updateStageBills, insertStageBills, newDecimal);
  316. break;
  317. default:
  318. await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, newDecimal);
  319. break;
  320. }
  321. return [updateStageBills, insertStageBills];
  322. }
  323. async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
  324. const changeSj = [], changeSb = [], changeSo = [];
  325. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  326. if (stages.length === 0) return [changeSj, changeSb, changeSo];
  327. const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
  328. const calcUp = upDecimal < oldDecimal.up,
  329. calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
  330. for (const stage of stages) {
  331. if (calcUp || calcTp) {
  332. const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
  333. columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
  334. where: { sid: stage.id }
  335. });
  336. for (const sj of stageJgcl) {
  337. const cj = { id: sj.id };
  338. cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
  339. cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
  340. cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
  341. changeSj.push(cj);
  342. }
  343. }
  344. if (calcTp) {
  345. const stageChangeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
  346. columns: ['id', 'tp'],
  347. where: { sid: stage.id }
  348. });
  349. for (const cb of stageChangeSb) {
  350. cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
  351. }
  352. changeSb.push(...stageChangeSb);
  353. const stageChangeSo = await this.ctx.service.stageOther.getAllDataByCondition({
  354. columns: ['id', 'total_price', 'tp'],
  355. where: { sid: stage.id }
  356. });
  357. for (const co of stageChangeSo) {
  358. if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
  359. co.tp = this.ctx.helper.round(co.tp, tpDecimal);
  360. }
  361. changeSo.push(...stageChangeSo);
  362. }
  363. }
  364. return [changeSj, changeSb, changeSo];
  365. }
  366. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  367. const changeAdvanceBills = [];
  368. const caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
  369. if (caclPayTp) {
  370. // 获取预付款需要修改的相关记录
  371. const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
  372. columns: ['id', 'cur_amount', 'prev_amount', 'prev_total_amount'],
  373. where: { status: [advanceConst.status.uncheck, advanceConst.status.checkNo], tid: tenderId },
  374. });
  375. const decimal = newDecimal.pay ? newDecimal.payTp : newDecimal.tp;
  376. // 根据精度重新计算相关金额
  377. for (const ad of ad_bills) {
  378. const cb = { id: ad.id };
  379. const cur_amount = this.ctx.helper.round(ad.cur_amount, decimal);
  380. cb.cur_amount = cur_amount;
  381. cb.prev_total_amount = this.ctx.helper.add(cur_amount, ad.prev_amount);
  382. changeAdvanceBills.push(cb);
  383. }
  384. }
  385. const [billsService] = await this._getLedgerService();
  386. const changeBills = await this._reCalcLedger(tenderId, billsService, newDecimal, oldDecimal);
  387. const [updateStageBills, insertStageBills] = await this._reCalcStageBills(tenderId, newDecimal, oldDecimal);
  388. const [changeSj, changeSb, changeSo] = await this._reCalcStageExtra(tenderId, newDecimal, oldDecimal);
  389. if (changeBills.length > 0 ||
  390. changeAdvanceBills.length > 0 ||
  391. updateStageBills.length > 0 || insertStageBills.length > 0 ||
  392. changeSj.length > 0 || changeSb.length > 0 || changeSo.length > 0) {
  393. const transaction = await this.db.beginTransaction();
  394. try {
  395. await transaction.update(this.tableName,
  396. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  397. if (changeBills.length > 0) await transaction.updateRows(billsService.tableName, changeBills);
  398. if (updateStageBills.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateStageBills);
  399. if (insertStageBills.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertStageBills);
  400. if (changeSj.length > 0) await transaction.updateRows(this.ctx.service.stageJgcl.tableName, changeSj);
  401. if (changeSb.length > 0) await transaction.updateRows(this.ctx.service.stageBonus.tableName, changeSb);
  402. if (changeSo.length > 0) await transaction.updateRows(this.ctx.service.stageOther.tableName, changeSo);
  403. if (changeAdvanceBills.length) await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
  404. await transaction.commit();
  405. } catch (error) {
  406. await transaction.rollback();
  407. throw error;
  408. }
  409. } else {
  410. await this.db.update(this.tableName,
  411. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  412. }
  413. }
  414. async _reCalcStageBills2(tenderId, calcType) {
  415. const updateStageBills = [], insertStageBills = [];
  416. const stages = await this.ctx.service.stage.getUnCompleteStages(tenderId);
  417. if (stages.length === 0) return [updateStageBills, insertStageBills];
  418. switch (calcType) {
  419. case 'up':
  420. await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
  421. break;
  422. case 'tp':
  423. await this._reCalcStageBillsTp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
  424. break;
  425. default:
  426. await this._reCalcStageBillsUp(stages, updateStageBills, insertStageBills, this.ctx.tender.info.decimal);
  427. break;
  428. }
  429. return [updateStageBills, insertStageBills];
  430. }
  431. async saveCalcType(tenderId, calcType) {
  432. if (!this.ctx.tender.info.calc_type && calcType === 'up') return;
  433. if (this.ctx.tender.info.calc_type && calcType === this.ctx.tender.info.calc_type) return;
  434. const [updateStageBills, insertStageBills] = await this._reCalcStageBills2(tenderId, calcType);
  435. const transaction = await this.db.beginTransaction();
  436. try {
  437. await transaction.update(this.tableName, { calc_type: calcType }, { where: { tid: tenderId } });
  438. if (updateStageBills.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateStageBills);
  439. if (insertStageBills.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertStageBills);
  440. await transaction.commit();
  441. } catch(err) {
  442. await transaction.rollback();
  443. throw '保存计价类型失败';
  444. }
  445. }
  446. /**
  447. * 获取标段审批相关信息 (审批设置用)
  448. * @param tenderId
  449. * @return {Promise<void>}
  450. */
  451. async getTenderShenpiInfo(tenderId) {
  452. const defaultInfo = await this.getDefaultInfo(tenderId);
  453. const info = await this.getDataByCondition({ tid: tenderId });
  454. // 还没选择模式的标段不应该可以选择审批流程设置
  455. if (!info) {
  456. return false;
  457. }
  458. const defaultShenpiInfo = JSON.parse(JSON.stringify(defaultInfo.shenpi));
  459. const shenpiInfo = !info.shenpi || info.shenpi === null || info.shenpi === '' ? defaultShenpiInfo : JSON.parse(info.shenpi);
  460. // 查漏补缺
  461. for (const sp in defaultShenpiInfo) {
  462. if (!shenpiInfo[sp]) {
  463. shenpiInfo[sp] = defaultShenpiInfo[sp];
  464. }
  465. }
  466. return shenpiInfo;
  467. }
  468. /**
  469. * 拷贝标段数据至当前标段
  470. * @param {number} id - 当前标段id
  471. * @param {number} copy_id - 被拷贝的标段id
  472. * @param {Array} typeArr - 需要拷贝的类型数组
  473. */
  474. async copyTenderHandler(id, copy_id, typeArr) {
  475. const columns = [];
  476. typeArr.forEach(item => {
  477. if (item === 'tender') {
  478. columns.push('deal_info', 'construction_unit', 'tech_param', 'bid_info');
  479. } else if (item === 'chapter') {
  480. columns.push('chapter');
  481. } else if (item === 'pay_account') {
  482. columns.push('pay_account');
  483. }
  484. });
  485. if (!columns.length) throw '未选择需要拷贝的设置';
  486. const [data] = await this.getAllDataByCondition({
  487. where: {
  488. tid: copy_id,
  489. },
  490. columns,
  491. });
  492. const isUpdate = await this.update(data, { tid: id });
  493. if (isUpdate) {
  494. await this.ctx.service.tender.update({ copy_id }, { id });
  495. }
  496. }
  497. }
  498. return TenderInfo;
  499. };