financial_transfer_tender.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. module.exports = app => {
  7. class FinancialTransferTender extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'financial_transfer_tender';
  17. }
  18. async getList(trid) {
  19. const sql = 'SELECT ftt.*, t.`name` as name FROM ?? as ftt LEFT JOIN ?? as t ON ftt.`tid` = t.`id` WHERE ftt.`trid` = ?';
  20. const sqlParams = [this.tableName, this.ctx.service.tender.tableName, trid];
  21. const list = await this.db.query(sql, sqlParams);
  22. for (const l of list) {
  23. l.files = await this.ctx.service.financialTransferTenderAtt.getAtt(l.id);
  24. }
  25. return list;
  26. }
  27. async addTenders(transferInfo, tenders) {
  28. const transaction = await this.db.beginTransaction();
  29. try {
  30. const insertDatas = [];
  31. for (const t of tenders) {
  32. const tender = await this.ctx.service.tender.getDataById(t.tid);
  33. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: t.tid, order: t.sorder } });
  34. const insertData = {
  35. spid: transferInfo.spid,
  36. trid: transferInfo.id,
  37. tid: t.tid,
  38. sorder: t.sorder.join(','),
  39. uid: this.ctx.session.sessionUser.accountId,
  40. total_price: tender.total_price,
  41. contract_tp: this._.sumBy(stages, 'contract_tp'),
  42. qc_tp: this._.sumBy(stages, 'qc_tp'),
  43. pc_tp: this._.sumBy(stages, 'pc_tp'),
  44. yf_tp: this._.sumBy(stages, 'yf_tp'),
  45. sf_tp: this._.sumBy(stages, 'sf_tp'),
  46. hb_tp: this._.sumBy(stages, 'sf_tp'),
  47. };
  48. insertDatas.push(insertData);
  49. }
  50. await transaction.insert(this.tableName, insertDatas);
  51. await this.calcHbTp(transferInfo.id, transaction);
  52. await transaction.commit();
  53. return true;
  54. } catch (e) {
  55. console.log(e);
  56. await transaction.rollback();
  57. return false;
  58. }
  59. }
  60. async delTenders(ftid) {
  61. if (!ftid) {
  62. throw '参数有误';
  63. }
  64. const node = await this.getDataById(ftid);
  65. if (!node) {
  66. throw '资金划拨标段不存在';
  67. }
  68. const transaction = await this.db.beginTransaction();
  69. try {
  70. await transaction.delete(this.tableName, { id: node.id });
  71. // 删除合同附件
  72. const attList = await this.ctx.service.financialTransferTenderAtt.getAllDataByCondition({ where: { ftid } });
  73. await this.ctx.helper.delFiles(attList);
  74. await this.calcHbTp(node.trid, transaction);
  75. await transaction.commit();
  76. } catch (err) {
  77. await transaction.rollback();
  78. throw err;
  79. }
  80. return true;
  81. }
  82. async updateHbTp(ftid, hb_tp) {
  83. if (!ftid) {
  84. throw '参数有误';
  85. }
  86. const node = await this.getDataById(ftid);
  87. if (!node) {
  88. throw '资金划拨标段不存在';
  89. }
  90. if (node.is_lock) {
  91. throw '资金划拨标段已锁定,无法修改';
  92. }
  93. const transaction = await this.db.beginTransaction();
  94. try {
  95. await transaction.update(this.tableName, { id: node.id, hb_tp });
  96. await this.calcHbTp(node.trid, transaction);
  97. await transaction.commit();
  98. } catch (err) {
  99. await transaction.rollback();
  100. throw err;
  101. }
  102. return true;
  103. }
  104. async calcHbTp(trid, transaction = null) {
  105. const sql = 'SELECT SUM(hb_tp) as hb_tp FROM ?? WHERE `trid` = ?';
  106. const sqlParams = [this.tableName, trid];
  107. const result = transaction ? await transaction.queryOne(sql, sqlParams) : await this.db.queryOne(sql, sqlParams);
  108. transaction ? await transaction.update(this.ctx.service.financialTransfer.tableName, { id: trid, total_price: result.hb_tp }) : await this.db.update(this.ctx.service.financialTransfer.tableName, { id: trid, total_price: result.hb_tp });
  109. }
  110. }
  111. return FinancialTransferTender;
  112. };