financial_transfer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. const insertData = {
  39. spid,
  40. t_time: date,
  41. create_time: new Date(),
  42. remark,
  43. uid: this.ctx.session.sessionUser.accountId,
  44. };
  45. const result = await this.db.insert(this.tableName, insertData);
  46. if (result.affectedRows === 1) {
  47. return result.insertId;
  48. }
  49. return false;
  50. }
  51. async delTransfer(trid) {
  52. if (!trid) {
  53. throw '参数有误';
  54. }
  55. const node = await this.getDataById(trid);
  56. if (!node) {
  57. throw '资金划拨不存在';
  58. }
  59. const transaction = await this.db.beginTransaction();
  60. try {
  61. await transaction.delete(this.tableName, { id: node.id });
  62. // 删除合同附件
  63. const attList = await this.ctx.service.financialTransferAtt.getAllDataByCondition({ where: { trid } });
  64. const tenderAttList = await this.ctx.service.financialTransferTenderAtt.getAllDataByCondition({ where: { trid } });
  65. await this.ctx.helper.delFiles(this._.concat(attList, tenderAttList));
  66. await transaction.delete(this.ctx.service.financialTransferTender.tableName, { trid });
  67. await transaction.commit();
  68. } catch (err) {
  69. await transaction.rollback();
  70. throw err;
  71. }
  72. return true;
  73. }
  74. async lockTransfer(trid, lock) {
  75. if (!trid) {
  76. throw '参数有误';
  77. }
  78. const node = await this.getDataById(trid);
  79. if (!node) {
  80. throw '该资金划拨不存在';
  81. }
  82. if (node.uid !== this.ctx.session.sessionUser.accountId) {
  83. throw '您没有权限操作';
  84. }
  85. const result = await this.db.update(this.tableName, { id: node.id, is_lock: lock });
  86. return result.affectedRows === 1;
  87. }
  88. async saveTransfer(data) {
  89. if (!data.id) {
  90. throw '参数有误';
  91. }
  92. const node = await this.getDataById(data.id);
  93. if (!node) {
  94. throw '该资金划拨不存在';
  95. }
  96. if (node.uid !== this.ctx.session.sessionUser.accountId) {
  97. throw '您没有权限操作';
  98. }
  99. const result = await this.db.update(this.tableName, data);
  100. return result.affectedRows === 1;
  101. }
  102. }
  103. return FinancialTransfer;
  104. };