change_audit.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. module.exports = app => {
  11. class ChangeAudit extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'change_audit';
  21. }
  22. /**
  23. * 获取最后一位审批人
  24. * @param {int} cid - 变更令id
  25. * @param {int} times - 次数
  26. * @param {int} site - 位置
  27. * @return {void}
  28. */
  29. async getLastUser(cid, times, site = 1) {
  30. this.initSqlBuilder();
  31. this.sqlBuilder.setAndWhere('cid', {
  32. value: this.db.escape(cid),
  33. operate: '=',
  34. });
  35. this.sqlBuilder.setAndWhere('times', {
  36. value: times,
  37. operate: '=',
  38. });
  39. this.sqlBuilder.setAndWhere('status', {
  40. value: 1,
  41. operate: '!=',
  42. });
  43. if (site === 0) {
  44. this.sqlBuilder.setAndWhere('usite', {
  45. value: site,
  46. operate: '=',
  47. });
  48. }
  49. const u_sort = [['usort', 'DESC']];
  50. this.sqlBuilder.orderBy = u_sort;
  51. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  52. const data = await this.db.queryOne(sql, sqlParam);
  53. return data;
  54. }
  55. /**
  56. * 获取最后一位退回的审批人
  57. * @param {int} cid - 变更令id
  58. * @param {int} times - 次数
  59. * @return {void}
  60. */
  61. async getLastBackUser(cid, times) {
  62. this.initSqlBuilder();
  63. this.sqlBuilder.setAndWhere('cid', {
  64. value: this.db.escape(cid),
  65. operate: '=',
  66. });
  67. this.sqlBuilder.setAndWhere('times', {
  68. value: times,
  69. operate: '=',
  70. });
  71. this.sqlBuilder.setAndWhere('status', {
  72. value: 6,
  73. operate: '=',
  74. });
  75. const u_sort = [['usort', 'DESC']];
  76. this.sqlBuilder.orderBy = u_sort;
  77. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  78. const data = await this.db.queryOne(sql, sqlParam);
  79. return data;
  80. }
  81. /**
  82. * 获取当前审批人查看info时的状态
  83. * @param {Object} change - 变更令数据
  84. * @return {void}
  85. */
  86. async getStatusByChange(change) {
  87. const statusConst = audit.flow.status;
  88. const auditStatusConst = audit.flow.auditStatus;
  89. const uid = this.ctx.session.sessionUser.accountId;
  90. const changeAuditInfo = await this.getAllDataByCondition({ where: { cid: change.cid, times: change.times, uid }, order: ['id', 'DESC'] });
  91. if (changeAuditInfo === null) {
  92. // 无权限查看此变更令
  93. return 0;
  94. }
  95. if (change.status === statusConst.uncheck && uid === change.uid) {
  96. // 待上报
  97. return 1;
  98. } else if (change.status === statusConst.back && uid === change.uid) {
  99. // 待重新上报
  100. return 2;
  101. } else if (change.status === statusConst.back && uid !== change.uid) {
  102. // 被退回但你不是原报人
  103. return 3;
  104. } else if (change.status === statusConst.checked) {
  105. // 已完成
  106. return 4;
  107. } else if (change.status === statusConst.checkNo) {
  108. // 已终止
  109. return 5;
  110. } else if (change.status === statusConst.checking && changeAuditInfo.status === auditStatusConst.checking) {
  111. // 待你审批
  112. return 6;
  113. } else if (change.status === statusConst.checking && changeAuditInfo.status !== auditStatusConst.checking) {
  114. // 审批中但你未到你审批或你已审批
  115. return 7;
  116. }
  117. // 无权限查看此变更令
  118. return 0;
  119. }
  120. /**
  121. * 根据用户查看此变更状态获取审批人列表
  122. * @param {Object} change - 变更令
  123. * @param {int} status - 状态
  124. * @return {object} list - 列表
  125. */
  126. async getListByStatus(change, status) {
  127. let sql = '';
  128. let sqlParam = '';
  129. switch (status) {
  130. case 1:// 待上报
  131. case 2:// 待重新上报
  132. sql = 'SELECT * FROM ?? WHERE ' +
  133. 'cid = ? AND times = ? GROUP BY usite';
  134. sqlParam = [this.tableName, change.cid,
  135. change.times];
  136. break;
  137. case 3: // 被退回但你不是原报人
  138. case 4:// 已完成
  139. case 5:// 已终止
  140. case 7:// 审批中但你未到你审批或你已审批
  141. // 获取完整的审批顺序
  142. sql = 'SELECT * FROM ?? WHERE ' +
  143. 'cid = ? ORDER BY usort';
  144. sqlParam = [this.tableName, change.cid];
  145. break;
  146. case 6:// 待你审批
  147. default:// 无权限查看此变更令
  148. break;
  149. }
  150. const list = await this.db.query(sql, sqlParam);
  151. return list;
  152. }
  153. /**
  154. * 删除变更令审批人列表
  155. * @param {Object} transaction - 事务
  156. * @param {Object} cid - 变更令id
  157. * @param {int} times - 次数
  158. * @return {object} 返回结果
  159. */
  160. async deleteAuditData(transaction, cid, times) {
  161. this.initSqlBuilder();
  162. this.sqlBuilder.setAndWhere('cid', {
  163. value: this.db.escape(cid),
  164. operate: '=',
  165. });
  166. this.sqlBuilder.setAndWhere('times', {
  167. value: times,
  168. operate: '=',
  169. });
  170. this.sqlBuilder.setAndWhere('usite', {
  171. value: 0,
  172. operate: '!=',
  173. });
  174. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'delete');
  175. const result = await transaction.query(sql, sqlParam);
  176. return result;
  177. }
  178. /**
  179. * 获取不重复列表
  180. * @param {Object} cid - 变更令id
  181. * @param {int} times - 次数
  182. * @return {object} 返回结果
  183. */
  184. async getListGroupByTimes(cid, times) {
  185. const sql = 'SELECT * FROM ?? WHERE ' +
  186. 'cid = ? AND times = ? GROUP BY usite';
  187. const sqlParam = [this.tableName, cid, times];
  188. const list = await this.db.query(sql, sqlParam);
  189. return list;
  190. }
  191. }
  192. return ChangeAudit;
  193. };