contract_supplement.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. const type = 'supplement';
  30. for (const l of list) {
  31. const userInfo = userList.find(item => item.id === l.uid);
  32. l.username = userInfo ? userInfo.name : '';
  33. l.files = await this.ctx.service.contractSupplementAtt.getAtt(l.id);
  34. if (shenpi_status) {
  35. await this.ctx.service.contractSpAudit.loadUser(l, type);
  36. await this.ctx.service.contractSpAudit.loadAuditViewData(l, type);
  37. await this.ctx.service.contractSpAudit.checkShenpi(l, type);
  38. if (l.status !== auditConst.status.checked || !l.final_auditor_str) {
  39. l.curAuditors2 = await this.ctx.service.contractSpAudit.getAuditorsByStatus(l, type, l.status, l.times);
  40. if (l.status === auditConst.status.checked && !l.final_auditor_str) {
  41. const final_auditor_str = l.curAuditors2[0].audit_type === auditType.key.common
  42. ? l.curAuditors2[0].name + (l.curAuditors2[0].role ? '-' + l.curAuditors2[0].role : '')
  43. : this.ctx.helper.transFormToChinese(l.curAuditors2[0].audit_order) + '审';
  44. await this.defaultUpdate({ final_auditor_str, id: l.id });
  45. l.final_auditor_str = final_auditor_str;
  46. }
  47. }
  48. }
  49. }
  50. }
  51. return list;
  52. }
  53. async add(options, cid, data) {
  54. const node = await this.ctx.service.contract.getDataById(cid);
  55. if (!node) {
  56. throw '合同不存在';
  57. }
  58. const transaction = await this.db.beginTransaction();
  59. try {
  60. const insertData = {
  61. spid: options.spid || null,
  62. tid: options.tid || null,
  63. contract_type: options.contract_type,
  64. cid,
  65. uid: this.ctx.session.sessionUser.accountId,
  66. code: data.code,
  67. name: data.name,
  68. price: data.price || 0,
  69. party_a: data.party_a,
  70. party_b: data.party_b,
  71. sign_time: data.sign_time || null,
  72. address: data.address,
  73. content: data.content,
  74. create_time: new Date(),
  75. need_shenpi: options.spid ? this.ctx.subProject.page_show.openContractSubProjectShenpi : this.ctx.subProject.page_show.openContractTenderShenpi,
  76. };
  77. const result = await transaction.insert(this.tableName, insertData);
  78. await this.calcContract(transaction, node);
  79. insertData.id = result.insertId;
  80. await this.ctx.service.contractSpAudit.makeAudits(transaction, options, insertData, 'supplement', this.ctx.session.sessionUser.accountId);
  81. await transaction.commit();
  82. } catch (err) {
  83. await transaction.rollback();
  84. throw err;
  85. }
  86. return { supplements: await this.getSupplements(options, cid), node: { update: node } };
  87. }
  88. async save(options, cid, data) {
  89. if (!data.id) {
  90. throw '参数有误';
  91. }
  92. const node = await this.ctx.service.contract.getDataById(cid);
  93. if (!node) {
  94. throw '合同不存在';
  95. }
  96. const csInfo = await this.getDataById(data.id);
  97. if (!csInfo) {
  98. throw '补充合同不存在';
  99. }
  100. const transaction = await this.db.beginTransaction();
  101. try {
  102. await transaction.update(this.tableName, data);
  103. await this.calcContract(transaction, node);
  104. await transaction.commit();
  105. } catch (err) {
  106. await transaction.rollback();
  107. throw err;
  108. }
  109. return { supplements: await this.getSupplements(options, cid), node: { update: node } };
  110. }
  111. async del(options, cid, csid) {
  112. if (!csid) {
  113. throw '参数有误';
  114. }
  115. const node = await this.ctx.service.contract.getDataById(cid);
  116. if (!node) {
  117. throw '合同不存在';
  118. }
  119. const csInfo = await this.getDataById(csid);
  120. if (!csInfo) {
  121. throw '补充合同不存在';
  122. }
  123. const transaction = await this.db.beginTransaction();
  124. try {
  125. await transaction.delete(this.tableName, { id: csid });
  126. // 删除合同附件
  127. const attList = await this.ctx.service.contractSupplementAtt.getAllDataByCondition({ where: { csid } });
  128. await this.ctx.helper.delFiles(attList);
  129. await transaction.delete(this.ctx.service.contractSupplementAtt.tableName, { csid });
  130. await transaction.delete(this.ctx.service.contractSpAudit.tableName, { csid });
  131. await this.calcContract(transaction, node);
  132. await transaction.commit();
  133. } catch (err) {
  134. await transaction.rollback();
  135. throw err;
  136. }
  137. return { supplements: await this.getSupplements(options, cid), node: { update: node } };
  138. }
  139. async calcContract(transaction, node) {
  140. const supplementsList = await transaction.query('SELECT `price` FROM ?? WHERE `cid` = ?', [this.tableName, node.id]);
  141. let supplement_price = 0;
  142. for (const l of supplementsList) {
  143. supplement_price = this.ctx.helper.add(supplement_price, l.price);
  144. }
  145. node.supplement_price = supplement_price;
  146. node.total_price = this.ctx.helper.add(node.price, supplement_price);
  147. node.exist_supplement = supplementsList.length === 0 ? 0 : 1;
  148. await transaction.update(this.ctx.service.contract.tableName, node);
  149. }
  150. }
  151. return ContractSupplement;
  152. };