shenpi_audit.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use strict';
  2. /**
  3. * 版本数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. const shenpiConst = require('../const/shenpi');
  10. module.exports = app => {
  11. class ShenpiAudit extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'shenpi_audit';
  21. }
  22. async getAudit(tid, type, status) {
  23. const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  24. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ?';
  25. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  26. return await this.db.queryOne(sql, sqlParam);
  27. }
  28. async getAuditList(tid, type, status) {
  29. const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  30. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? ORDER BY sp.id ASC';
  31. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  32. return await this.db.query(sql, sqlParam);
  33. }
  34. async addAudit(data) {
  35. const insertData = {
  36. tid: this.ctx.tender.id,
  37. sp_type: data.code,
  38. sp_status: data.status,
  39. audit_id: data.audit_id,
  40. };
  41. const result = await this.db.insert(this.tableName, insertData);
  42. return result.effectRows === 1;
  43. }
  44. async removeAudit(data) {
  45. const delData = {
  46. tid: this.ctx.tender.id,
  47. sp_type: data.code,
  48. sp_status: data.status,
  49. audit_id: data.audit_id,
  50. };
  51. return await this.db.delete(this.tableName, delData);
  52. }
  53. async copyAudit2otherTender(data) {
  54. const transaction = await this.db.beginTransaction();
  55. try {
  56. const shenpi_status = parseInt(data.status);
  57. // 1.复制修改当前审批到其他的tender_info里
  58. // 2.删除其他的shenpiAudit
  59. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  60. const tenderInfoUpdateList = [];
  61. const tenders = [];
  62. for (const tid of data.tidList.split(',')) {
  63. // 获取原报
  64. const tender = await this.ctx.service.tender.getDataById(tid);
  65. if (tender) {
  66. tenders.push({ id: parseInt(tid), user_id: tender.user_id });
  67. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tid);
  68. // 把当前期状态复制到其他标段里
  69. if (shenpiInfo[data.code] !== shenpi_status) {
  70. shenpiInfo[data.code] = shenpi_status;
  71. tenderInfoUpdateList.push({ row: { shenpi: JSON.stringify(shenpiInfo) }, where: { tid: parseInt(tid) } });
  72. }
  73. }
  74. }
  75. if (tenderInfoUpdateList.length > 0) await transaction.updateRows(this.ctx.service.tenderInfo.tableName, tenderInfoUpdateList);
  76. const insertList = [];
  77. const needYB = ['ledger', 'revise', 'change'];
  78. const canYB = needYB.indexOf(data.code) !== -1;
  79. for (const t of tenders) {
  80. if (shenpi_status !== shenpiConst.sp_status.sqspr) {
  81. await transaction.delete(this.tableName, { tid: t.id, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status });
  82. for (const aid of data.aidList.split(',')) {
  83. if (aid && parseInt(aid) !== t.user_id || (parseInt(aid) === t.user_id && canYB)) {
  84. const insertData = {
  85. tid: t.id,
  86. sp_type: shenpiConst.sp_type[data.code],
  87. sp_status: shenpi_status,
  88. audit_id: parseInt(aid),
  89. };
  90. insertList.push(insertData);
  91. }
  92. }
  93. }
  94. }
  95. // console.log(tenderInfoUpdateList, insertList);
  96. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  97. await transaction.commit();
  98. return true;
  99. } catch (err) {
  100. await transaction.rollback();
  101. throw err;
  102. }
  103. }
  104. async copyAudit2otherShenpi(data) {
  105. const transaction = await this.db.beginTransaction();
  106. try {
  107. const shenpi_status = parseInt(data.status);
  108. // 1.修改当前审批到它的tender_info里
  109. // 2.删除相同审批状态的shenpiAudit
  110. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  111. const insertList = [];
  112. const needYB = ['ledger', 'revise', 'change'];
  113. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(this.ctx.tender.id);
  114. for (const code of data.shenpiList.split(',')) {
  115. // 把当前审批状态复制到其他标段里
  116. if (shenpiInfo[code] !== shenpi_status) {
  117. shenpiInfo[code] = shenpi_status;
  118. }
  119. if (shenpiInfo[code] !== shenpiConst.sp_status.sqspr) {
  120. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code], sp_status: shenpiInfo[code] });
  121. for (const aid of data.aidList.split(',')) {
  122. if (aid && parseInt(aid) !== this.ctx.tender.data.user_id || (parseInt(aid) === this.ctx.tender.data.user_id && needYB.indexOf(code) !== -1)) {
  123. const insertData = {
  124. tid: this.ctx.tender.id,
  125. sp_type: shenpiConst.sp_type[code],
  126. sp_status: shenpi_status,
  127. audit_id: parseInt(aid),
  128. };
  129. insertList.push(insertData);
  130. }
  131. }
  132. }
  133. }
  134. await transaction.update(this.ctx.service.tenderInfo.tableName,
  135. {
  136. shenpi: JSON.stringify(shenpiInfo),
  137. },
  138. {
  139. where: {
  140. tid: this.ctx.tender.id,
  141. },
  142. });
  143. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  144. await transaction.commit();
  145. return true;
  146. } catch (err) {
  147. await transaction.rollback();
  148. throw err;
  149. }
  150. }
  151. }
  152. return ShenpiAudit;
  153. };