change_project_xs_audit.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').changeProject;
  10. const pushType = require('../const/audit').pushType;
  11. module.exports = app => {
  12. class ChangeProjectXsAudit extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'change_project_xs_audit';
  22. }
  23. /**
  24. * 获取协审人列表
  25. *
  26. * @param auditorId
  27. * @return {Promise<*>}
  28. */
  29. async getAuditList(changeId) {
  30. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`cpid`, la.`aid` ' +
  31. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  32. ' WHERE la.`cpid` = ? ORDER BY la.`id`';
  33. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, changeId];
  34. return await this.db.query(sql, sqlParam);
  35. }
  36. /**
  37. * 获取单个协审人
  38. *
  39. * @param auditorId
  40. * @return {Promise<*>}
  41. */
  42. async getOneAudit(cpid, aid) {
  43. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`cpid`, la.`aid` ' +
  44. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  45. ' WHERE la.`cpid` = ? AND la.`aid` = ?';
  46. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpid, aid];
  47. return await this.db.queryOne(sql, sqlParam);
  48. }
  49. /**
  50. * 新增协审人
  51. *
  52. * @param {Number} cpId - 立项书id
  53. * @param {Number} auditorId - 审核人id
  54. * @param {Number} times - 第几次审批
  55. * @return {Promise<number>}
  56. */
  57. async addAuditor(cpId, auditorId) {
  58. const transaction = await this.db.beginTransaction();
  59. let flag = false;
  60. try {
  61. const data = {
  62. tid: this.ctx.tender.id,
  63. cpid: cpId,
  64. aid: auditorId,
  65. for_aid: this.ctx.session.sessionUser.accountId,
  66. in_time: new Date(),
  67. };
  68. const result = await transaction.insert(this.tableName, data);
  69. await transaction.commit();
  70. flag = result.effectRows = 1;
  71. } catch (err) {
  72. await transaction.rollback();
  73. throw err;
  74. }
  75. return flag;
  76. }
  77. /**
  78. * 移除协审人
  79. *
  80. * @param {Number} cpId - 变更立项书id
  81. * @param {Number} auditorId - 审核人id
  82. * @param {Number} times - 第几次审批
  83. * @return {Promise<boolean>}
  84. */
  85. async deleteAuditor(cpId, auditorId) {
  86. const transaction = await this.db.beginTransaction();
  87. try {
  88. const condition = { cpid: cpId, aid: auditorId };
  89. const auditor = await this.getDataByCondition(condition);
  90. if (!auditor) {
  91. throw '该审核人不存在';
  92. }
  93. await transaction.delete(this.tableName, condition);
  94. await transaction.commit();
  95. } catch (err) {
  96. await transaction.rollback();
  97. throw err;
  98. }
  99. return true;
  100. }
  101. /**
  102. * 获取审核人需要审核的期列表
  103. *
  104. * @param auditorId
  105. * @return {Promise<*>}
  106. */
  107. async getAuditChangeProject(auditorId) {
  108. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`cpid`,' +
  109. ' m.`status` As `mstatus`, m.`code` As `mcode`,' +
  110. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  111. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  112. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  113. ' and ma.`cpid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  114. const sqlParam = [this.tableName, this.ctx.service.changeProject.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.back, auditConst.status.back];
  115. return await this.db.query(sql, sqlParam);
  116. }
  117. /**
  118. * 复制上一期的协审人列表给最新一期
  119. *
  120. * @param transaction - 新增一期的事务
  121. * @param {Object} preMaterial - 上一期
  122. * @param {Object} newaMaterial - 最新一期
  123. * @return {Promise<*>}
  124. */
  125. async copyPreChangeProjectXsAuditors(transaction, preChange, newChange) {
  126. const auditors = await this.getAuditList(preChange.id);
  127. const newAuditors = [];
  128. for (const a of auditors) {
  129. const na = {
  130. tid: preChange.tid,
  131. cpid: newChange.id,
  132. aid: a.aid,
  133. for_aid: preChange.for_aid,
  134. in_time: new Date(),
  135. };
  136. newAuditors.push(na);
  137. }
  138. const result = await transaction.insert(this.tableName, newAuditors);
  139. return result.affectedRows === auditors.length;
  140. }
  141. async getAllAuditors(tenderId) {
  142. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  143. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  144. ' WHERE t.id = ?' +
  145. ' GROUP BY ma.aid';
  146. const sqlParam = [tenderId];
  147. return this.db.query(sql, sqlParam);
  148. }
  149. }
  150. return ChangeProjectXsAudit;
  151. };