financial_transfer.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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) {
  19. const 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. const sqlParams = [this.tableName, this.ctx.service.projectAccount.tableName, spid];
  21. const list = await this.db.query(sql, sqlParams);
  22. for (const l of list) {
  23. l.files = await this.ctx.service.financialTransferAtt.getAtt(l.id);
  24. }
  25. return list;
  26. }
  27. async addTransfer(spid, date, remark) {
  28. const node = await this.getDataByCondition({ spid, t_time: date });
  29. if (node) {
  30. throw '资金划拨年月已存在,请勿重复';
  31. }
  32. const insertData = {
  33. spid,
  34. t_time: date,
  35. create_time: new Date(),
  36. remark,
  37. uid: this.ctx.session.sessionUser.accountId,
  38. };
  39. const result = await this.db.insert(this.tableName, insertData);
  40. if (result.affectedRows === 1) {
  41. return result.insertId;
  42. }
  43. return false;
  44. }
  45. async delTransfer(trid) {
  46. if (!trid) {
  47. throw '参数有误';
  48. }
  49. const node = await this.getDataById(trid);
  50. if (!node) {
  51. throw '资金划拨不存在';
  52. }
  53. const transaction = await this.db.beginTransaction();
  54. try {
  55. await transaction.delete(this.tableName, { id: node.id });
  56. // 删除合同附件
  57. const attList = await this.ctx.service.financialTransferAtt.getAllDataByCondition({ where: { trid } });
  58. const tenderAttList = await this.ctx.service.financialTransferTenderAtt.getAllDataByCondition({ where: { trid } });
  59. await this.ctx.helper.delFiles(this._.concat(attList, tenderAttList));
  60. await transaction.delete(this.ctx.service.financialTransferTender.tableName, { trid });
  61. await transaction.commit();
  62. } catch (err) {
  63. await transaction.rollback();
  64. throw err;
  65. }
  66. return true;
  67. }
  68. async lockTransfer(trid, lock) {
  69. if (!trid) {
  70. throw '参数有误';
  71. }
  72. const node = await this.getDataById(trid);
  73. if (!node) {
  74. throw '该资金划拨不存在';
  75. }
  76. if (node.uid !== this.ctx.session.sessionUser.accountId) {
  77. throw '您没有权限操作';
  78. }
  79. const result = await this.db.update(this.tableName, { id: node.id, is_lock: lock });
  80. return result.affectedRows === 1;
  81. }
  82. async saveTransfer(data) {
  83. if (!data.id) {
  84. throw '参数有误';
  85. }
  86. const node = await this.getDataById(data.id);
  87. if (!node) {
  88. throw '该资金划拨不存在';
  89. }
  90. if (node.uid !== this.ctx.session.sessionUser.accountId) {
  91. throw '您没有权限操作';
  92. }
  93. const result = await this.db.update(this.tableName, data);
  94. return result.affectedRows === 1;
  95. }
  96. }
  97. return FinancialTransfer;
  98. };