contract_supplement.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const contractConst = require('../const/contract');
  7. const auditConst = require('../const/audit').contract;
  8. const auditType = require('../const/audit').auditType;
  9. module.exports = app => {
  10. class ContractSupplement extends BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'contract_supplement';
  20. this.dataId = 'id';
  21. }
  22. async getSupplements(options, cid) {
  23. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND `cid` = ? ORDER BY `create_time` DESC';
  24. const sqlParams = [this.tableName, cid];
  25. const list = await this.db.query(sql, sqlParams);
  26. const shenpi_status = this.ctx.contract_tender ? this.ctx.subProject.page_show.openContractTenderShenpi : this.ctx.subProject.page_show.openContractSubProjectShenpi;
  27. if (list.length > 0) {
  28. const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: list.map(item => item.uid) } });
  29. for (const l of list) {
  30. const userInfo = userList.find(item => item.id === l.uid);
  31. l.username = userInfo ? userInfo.name : '';
  32. l.files = await this.ctx.service.contractSupplementAtt.getAtt(l.id);
  33. if (shenpi_status) {
  34. await this.ctx.service.contractSpAudit.loadUser(l);
  35. await this.ctx.service.contractSpAudit.loadAuditViewData(l);
  36. await this.ctx.service.contractSpAudit.checkShenpi(l);
  37. if (l.status !== auditConst.status.checked || !l.final_auditor_str) {
  38. l.curAuditors2 = await this.ctx.service.contractSpAudit.getAuditorsByStatus(l.cid, l.id, l.status, l.times);
  39. if (l.status === auditConst.status.checked && !l.final_auditor_str) {
  40. const final_auditor_str = l.curAuditors2[0].audit_type === auditType.key.common
  41. ? l.curAuditors2[0].name + (l.curAuditors2[0].role ? '-' + l.curAuditors2[0].role : '')
  42. : this.ctx.helper.transFormToChinese(l.curAuditors2[0].audit_order) + '审';
  43. await this.defaultUpdate({ final_auditor_str, id: l.id });
  44. l.final_auditor_str = final_auditor_str;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. return list;
  51. }
  52. async add(options, cid, data) {
  53. const node = await this.ctx.service.contract.getDataById(cid);
  54. if (!node) {
  55. throw '合同不存在';
  56. }
  57. const transaction = await this.db.beginTransaction();
  58. try {
  59. const insertData = {
  60. spid: options.spid || null,
  61. tid: options.tid || null,
  62. contract_type: options.contract_type,
  63. cid,
  64. uid: this.ctx.session.sessionUser.accountId,
  65. code: data.code,
  66. name: data.name,
  67. price: data.price || 0,
  68. party_a: data.party_a || 0,
  69. party_b: data.party_b || 0,
  70. sign_time: data.sign_time || null,
  71. address: data.address,
  72. content: data.content,
  73. create_time: new Date(),
  74. need_shenpi: options.spid ? this.ctx.subProject.page_show.openContractSubProjectShenpi : this.ctx.subProject.page_show.openContractTenderShenpi,
  75. };
  76. const result = await transaction.insert(this.tableName, insertData);
  77. await this.calcContract(transaction, node);
  78. await this.ctx.service.contractSpAudit.makeAudits(transaction, options, cid, result.insertId, this.ctx.session.sessionUser.accountId);
  79. await transaction.commit();
  80. } catch (err) {
  81. await transaction.rollback();
  82. throw err;
  83. }
  84. return { supplements: await this.getSupplements(options, cid), node: { update: node } };
  85. }
  86. async save(options, cid, data) {
  87. if (!data.id) {
  88. throw '参数有误';
  89. }
  90. const node = await this.ctx.service.contract.getDataById(cid);
  91. if (!node) {
  92. throw '合同不存在';
  93. }
  94. const csInfo = await this.getDataById(data.id);
  95. if (!csInfo) {
  96. throw '补充合同不存在';
  97. }
  98. const transaction = await this.db.beginTransaction();
  99. try {
  100. await transaction.update(this.tableName, data);
  101. await this.calcContract(transaction, node);
  102. await transaction.commit();
  103. } catch (err) {
  104. await transaction.rollback();
  105. throw err;
  106. }
  107. return { supplements: await this.getSupplements(options, cid), node: { update: node } };
  108. }
  109. async del(options, cid, csid) {
  110. if (!csid) {
  111. throw '参数有误';
  112. }
  113. const node = await this.ctx.service.contract.getDataById(cid);
  114. if (!node) {
  115. throw '合同不存在';
  116. }
  117. const csInfo = await this.getDataById(csid);
  118. if (!csInfo) {
  119. throw '补充合同不存在';
  120. }
  121. const transaction = await this.db.beginTransaction();
  122. try {
  123. await transaction.delete(this.tableName, { id: csid });
  124. // 删除合同附件
  125. const attList = await this.ctx.service.contractSupplementAtt.getAllDataByCondition({ where: { csid } });
  126. await this.ctx.helper.delFiles(attList);
  127. await transaction.delete(this.ctx.service.contractSupplementAtt.tableName, { csid });
  128. await transaction.delete(this.ctx.service.contractSpAudit.tableName, { csid });
  129. await this.calcContract(transaction, node);
  130. await transaction.commit();
  131. } catch (err) {
  132. await transaction.rollback();
  133. throw err;
  134. }
  135. return { supplements: await this.getSupplements(options, cid), node: { update: node } };
  136. }
  137. async calcContract(transaction, node) {
  138. const supplementsList = await transaction.query('SELECT `price` FROM ?? WHERE `cid` = ?', [this.tableName, node.id]);
  139. let supplement_price = 0;
  140. for (const l of supplementsList) {
  141. supplement_price = this.ctx.helper.add(supplement_price, l.price);
  142. }
  143. node.supplement_price = supplement_price;
  144. node.exist_supplement = supplementsList.length === 0 ? 0 : 1;
  145. await transaction.update(this.ctx.service.contract.tableName, node);
  146. }
  147. }
  148. return ContractSupplement;
  149. };