financial_transfer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 FinancialTransfer 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';
  17. }
  18. async getList(spid, pageLimit = 0) {
  19. let sql = 'SELECT ft.*, pa.`name` as username, pa.`company` FROM ?? as ft LEFT JOIN ?? as pa ON ft.`uid` = pa.`id` WHERE ft.`spid` = ? ORDER BY ft.`id` DESC';
  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.projectAccount.tableName, spid];
  27. const list = await this.db.query(sql, sqlParams);
  28. for (const l of list) {
  29. l.files = await this.ctx.service.financialTransferAtt.getAtt(l.id);
  30. }
  31. return list;
  32. }
  33. async addTransfer(spid, date, remark) {
  34. const node = await this.getDataByCondition({ spid, t_time: date });
  35. if (node) {
  36. throw '资金划拨年月已存在,请勿重复';
  37. }
  38. let pre_hb_tp = 0;
  39. const ftList = await this.getAllDataByCondition({ where: { spid } });
  40. if (ftList.length > 0) {
  41. pre_hb_tp = this.ctx.helper.sum(this._.map(ftList, 'total_price'));
  42. }
  43. const insertData = {
  44. spid,
  45. pre_hb_tp,
  46. t_time: date,
  47. create_time: new Date(),
  48. remark,
  49. uid: this.ctx.session.sessionUser.accountId,
  50. };
  51. const result = await this.db.insert(this.tableName, insertData);
  52. if (result.affectedRows === 1) {
  53. return result.insertId;
  54. }
  55. return false;
  56. }
  57. async delTransfer(trid) {
  58. if (!trid) {
  59. throw '参数有误';
  60. }
  61. const node = await this.getDataById(trid);
  62. if (!node) {
  63. throw '资金划拨不存在';
  64. }
  65. const transaction = await this.db.beginTransaction();
  66. try {
  67. await transaction.delete(this.tableName, { id: node.id });
  68. // 删除合同附件
  69. const attList = await this.ctx.service.financialTransferAtt.getAllDataByCondition({ where: { trid } });
  70. const tenderAttList = await this.ctx.service.financialTransferTenderAtt.getAllDataByCondition({ where: { trid } });
  71. await this.ctx.helper.delFiles(this._.concat(attList, tenderAttList));
  72. // 重算截止上次划拨金额
  73. const fttList = await this.ctx.service.financialTransferTender.getAllDataByCondition({ where: { trid } });
  74. await transaction.delete(this.ctx.service.financialTransferTender.tableName, { trid });
  75. for (const ftt of fttList) {
  76. await this.ctx.service.financialTransferTender.calcPreHbTp(transaction, node, ftt);
  77. }
  78. await transaction.commit();
  79. } catch (err) {
  80. await transaction.rollback();
  81. throw err;
  82. }
  83. return true;
  84. }
  85. async lockTransfer(trid, lock) {
  86. if (!trid) {
  87. throw '参数有误';
  88. }
  89. const node = await this.getDataById(trid);
  90. if (!node) {
  91. throw '该资金划拨不存在';
  92. }
  93. if (node.uid !== this.ctx.session.sessionUser.accountId) {
  94. throw '您没有权限操作';
  95. }
  96. const result = await this.db.update(this.tableName, { id: node.id, is_lock: lock });
  97. return result.affectedRows === 1;
  98. }
  99. async saveTransfer(data) {
  100. if (!data.id) {
  101. throw '参数有误';
  102. }
  103. const node = await this.getDataById(data.id);
  104. if (!node) {
  105. throw '该资金划拨不存在';
  106. }
  107. if (node.uid !== this.ctx.session.sessionUser.accountId) {
  108. throw '您没有权限操作';
  109. }
  110. const result = await this.db.update(this.tableName, data);
  111. return result.affectedRows === 1;
  112. }
  113. }
  114. return FinancialTransfer;
  115. };