financial_transfer_tender.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, pageLimit = 0) {
  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. // if (pageLimit) {
  21. // const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
  22. // const offset = limit * (this.ctx.page - 1);
  23. // const limitString = offset >= 0 ? offset + ',' + limit : limit;
  24. // sql += ' LIMIT ' + limitString;
  25. // }
  26. const sqlParams = [this.tableName, this.ctx.service.tender.tableName, trid];
  27. const list = await this.db.query(sql, sqlParams);
  28. for (const l of list) {
  29. l.files = await this.ctx.service.financialTransferTenderAtt.getAtt(l.id);
  30. }
  31. return list;
  32. }
  33. async addTenders(transferInfo, tenders) {
  34. const transaction = await this.db.beginTransaction();
  35. try {
  36. const insertDatas = [];
  37. // const needCalc = [];
  38. const updateDatas = [];
  39. const ftList = await this.ctx.service.financialTransfer.getAllDataByCondition({ where: { spid: transferInfo.spid } });
  40. const beforeFt = this._.filter(ftList, function(item) {
  41. return item.id < transferInfo.id;
  42. });
  43. const afterFt = this._.filter(ftList, function(item) {
  44. return item.id > transferInfo.id;
  45. });
  46. for (const t of tenders) {
  47. const tender = await this.ctx.service.tender.getDataById(t);
  48. if (!tender) {
  49. throw '标段不存在';
  50. }
  51. const ftt = await this.getDataByCondition({ spid: transferInfo.spid, trid: transferInfo.id, tid: t });
  52. if (ftt) {
  53. throw '资金划拨标段已存在';
  54. }
  55. // await this.ctx.service.tenderCache.loadTenderCache(tender, this.ctx.session.sessionUser.accountId);
  56. // if (tender.stage_tp) {
  57. // tender.end_sf_tp = this.ctx.helper.add(tender.stage_tp.pre_sf_tp, tender.stage_tp.sf_tp);
  58. // } else if (tender.lastStage) {
  59. // tender.end_sf_tp = this.ctx.helper.add(tender.lastStage.pre_sf_tp, tender.lastStage.sf_tp);
  60. // }
  61. // 找出已添加的划拨标段,得出pre_hb_tp
  62. let pre_hb_tp = 0;
  63. if (beforeFt.length > 0) {
  64. const beforeFttList = await this.getAllDataByCondition({ where: { spid: transferInfo.spid, trid: this._.map(beforeFt, 'id'), tid: t } });
  65. if (beforeFttList && beforeFttList.length > 0) {
  66. pre_hb_tp = this._.sumBy(beforeFttList, 'hb_tp');
  67. }
  68. }
  69. // const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: t.tid, order: t.sorder } });
  70. const insertData = {
  71. spid: transferInfo.spid,
  72. trid: transferInfo.id,
  73. tid: t,
  74. uid: this.ctx.session.sessionUser.accountId,
  75. hb_tp: 0,
  76. pre_hb_tp,
  77. };
  78. if (afterFt.length > 0) {
  79. const afterFttList = await this.getAllDataByCondition({ where: { spid: transferInfo.spid, trid: this._.map(afterFt, 'id'), tid: t } });
  80. if (afterFttList && afterFttList.length > 0) {
  81. for (const aftt of afterFttList) {
  82. aftt.pre_hb_tp = this.ctx.helper.add(aftt.pre_hb_tp, insertData.hb_tp);
  83. updateDatas.push({ id: aftt.id, pre_hb_tp: aftt.pre_hb_tp });
  84. // needCalc.push(aftt.trid);
  85. }
  86. }
  87. }
  88. insertDatas.push(insertData);
  89. }
  90. await transaction.insert(this.tableName, insertDatas);
  91. if (updateDatas.length > 0) {
  92. await transaction.updateRows(this.tableName, updateDatas);
  93. }
  94. // 计算划拨金额
  95. await this.calcHbTp(transferInfo.id, transaction);
  96. if (afterFt.length > 0) {
  97. const newFtList = await transaction.select(this.ctx.service.financialTransfer.tableName, { where: { spid: transferInfo.spid } });
  98. // 更新划拨金额
  99. const updatePreHbTp = [];
  100. for (const tr of afterFt) {
  101. // await this.calcHbTp(tr, transaction);
  102. const thisFt = this._.find(newFtList, { id: tr.id });
  103. let pre_hb_tp = 0;
  104. const beforeFtList = this._.filter(newFtList, function(item) {
  105. return item.id < tr.id;
  106. });
  107. if (beforeFtList && beforeFtList.length > 0) {
  108. pre_hb_tp = this.ctx.helper.sum(this._.map(beforeFtList, 'total_price'));
  109. }
  110. if (pre_hb_tp !== thisFt.pre_hb_tp) {
  111. updatePreHbTp.push({ id: tr.id, pre_hb_tp });
  112. }
  113. }
  114. if (updatePreHbTp.length > 0) {
  115. await transaction.updateRows(this.ctx.service.financialTransfer.tableName, updatePreHbTp);
  116. }
  117. }
  118. await transaction.commit();
  119. return true;
  120. } catch (e) {
  121. console.log(e);
  122. await transaction.rollback();
  123. return false;
  124. }
  125. }
  126. async delTenders(transferInfo, ftid) {
  127. if (!ftid) {
  128. throw '参数有误';
  129. }
  130. const node = await this.getDataById(ftid);
  131. if (!node) {
  132. throw '资金划拨标段不存在';
  133. }
  134. const transaction = await this.db.beginTransaction();
  135. try {
  136. await transaction.delete(this.tableName, { id: node.id });
  137. // 删除合同附件
  138. const attList = await this.ctx.service.financialTransferTenderAtt.getAllDataByCondition({ where: { ftid } });
  139. await this.ctx.helper.delFiles(attList);
  140. await this.calcHbTp(node.trid, transaction);
  141. await this.calcPreHbTp(transaction, transferInfo, node);
  142. await transaction.commit();
  143. } catch (err) {
  144. await transaction.rollback();
  145. throw err;
  146. }
  147. return true;
  148. }
  149. async updateHbTp(transferInfo, ftid, hb_tp) {
  150. if (!ftid) {
  151. throw '参数有误';
  152. }
  153. const node = await this.getDataById(ftid);
  154. if (!node) {
  155. throw '资金划拨标段不存在';
  156. }
  157. if (node.is_lock) {
  158. throw '资金划拨标段已锁定,无法修改';
  159. }
  160. const transaction = await this.db.beginTransaction();
  161. try {
  162. await transaction.update(this.tableName, { id: node.id, hb_tp });
  163. await this.calcHbTp(node.trid, transaction);
  164. await this.calcPreHbTp(transaction, transferInfo, node, hb_tp);
  165. await transaction.commit();
  166. } catch (err) {
  167. await transaction.rollback();
  168. throw err;
  169. }
  170. return true;
  171. }
  172. async updateTender(transferInfo, postData) {
  173. const keys = ['source'];
  174. if (!this._.includes(keys, postData.key)) {
  175. throw '参数有误';
  176. }
  177. const node = await this.getDataById(postData.node);
  178. if (!node) {
  179. throw '资金划拨标段不存在';
  180. }
  181. if (node.is_lock) {
  182. throw '资金划拨标段已锁定,无法修改';
  183. }
  184. const transaction = await this.db.beginTransaction();
  185. try {
  186. const updateData = {
  187. id: node.id,
  188. };
  189. updateData[postData.key] = postData.value;
  190. await transaction.update(this.tableName, updateData);
  191. await transaction.commit();
  192. } catch (err) {
  193. await transaction.rollback();
  194. throw err;
  195. }
  196. return true;
  197. }
  198. async calcPreHbTp(transaction, transferInfo, ftt, newHbTp = 0) {
  199. const ftList = await transaction.select(this.ctx.service.financialTransfer.tableName, { where: { spid: transferInfo.spid } });
  200. const afterFt = this._.filter(ftList, function(item) {
  201. return item.id > transferInfo.id;
  202. });
  203. if (afterFt.length === 0) return;
  204. const updateDatas = [];
  205. const afterFttList = await this.getAllDataByCondition({ where: { spid: transferInfo.spid, trid: this._.map(afterFt, 'id'), tid: ftt.tid } });
  206. if (afterFttList && afterFttList.length > 0) {
  207. for (const aftt of afterFttList) {
  208. aftt.pre_hb_tp = this.ctx.helper.add(this.ctx.helper.sub(aftt.pre_hb_tp, ftt.hb_tp), newHbTp);
  209. updateDatas.push({ id: aftt.id, pre_hb_tp: aftt.pre_hb_tp });
  210. }
  211. }
  212. if (updateDatas.length > 0) {
  213. await transaction.updateRows(this.tableName, updateDatas);
  214. }
  215. if (afterFt.length > 0) {
  216. // 更新划拨金额
  217. const updatePreHbTp = [];
  218. for (const tr of afterFt) {
  219. // await this.calcHbTp(tr, transaction);
  220. const thisFt = this._.find(ftList, { id: tr.id });
  221. let pre_hb_tp = 0;
  222. const beforeFtList = this._.filter(ftList, function(item) {
  223. return item.id < tr.id;
  224. });
  225. if (beforeFtList && beforeFtList.length > 0) {
  226. pre_hb_tp = this.ctx.helper.sum(this._.map(beforeFtList, 'total_price'));
  227. }
  228. if (pre_hb_tp !== thisFt.pre_hb_tp) {
  229. updatePreHbTp.push({ id: tr.id, pre_hb_tp });
  230. }
  231. }
  232. if (updatePreHbTp.length > 0) {
  233. await transaction.updateRows(this.ctx.service.financialTransfer.tableName, updatePreHbTp);
  234. }
  235. }
  236. }
  237. async calcHbTp(trid, transaction = null) {
  238. const sql = 'SELECT SUM(hb_tp) as hb_tp FROM ?? WHERE `trid` = ?';
  239. const sqlParams = [this.tableName, trid];
  240. const result = transaction ? await transaction.queryOne(sql, sqlParams) : await this.db.queryOne(sql, sqlParams);
  241. 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 });
  242. }
  243. }
  244. return FinancialTransferTender;
  245. };