change_audit.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. * @param {int} status - 状态
  28. * @return {void}
  29. */
  30. async getLastUser(cid, times, site = 1, status = 1) {
  31. this.initSqlBuilder();
  32. this.sqlBuilder.setAndWhere('cid', {
  33. value: this.db.escape(cid),
  34. operate: '=',
  35. });
  36. this.sqlBuilder.setAndWhere('times', {
  37. value: times,
  38. operate: '=',
  39. });
  40. if (status === 1) {
  41. this.sqlBuilder.setAndWhere('status', {
  42. value: 1,
  43. operate: '!=',
  44. });
  45. }
  46. if (site === 0) {
  47. this.sqlBuilder.setAndWhere('usite', {
  48. value: site,
  49. operate: '=',
  50. });
  51. }
  52. const u_sort = [['usort', 'DESC']];
  53. this.sqlBuilder.orderBy = u_sort;
  54. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  55. const data = await this.db.queryOne(sql, sqlParam);
  56. return data;
  57. }
  58. /**
  59. * 获取最后一位退回的审批人
  60. * @param {int} cid - 变更令id
  61. * @param {int} times - 次数
  62. * @return {void}
  63. */
  64. async getLastBackUser(cid, times) {
  65. this.initSqlBuilder();
  66. this.sqlBuilder.setAndWhere('cid', {
  67. value: this.db.escape(cid),
  68. operate: '=',
  69. });
  70. this.sqlBuilder.setAndWhere('times', {
  71. value: times,
  72. operate: '=',
  73. });
  74. this.sqlBuilder.setAndWhere('status', {
  75. value: 6,
  76. operate: '=',
  77. });
  78. const u_sort = [['usort', 'DESC']];
  79. this.sqlBuilder.orderBy = u_sort;
  80. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  81. const data = await this.db.queryOne(sql, sqlParam);
  82. return data;
  83. }
  84. /**
  85. * 获取当前审批人查看info时的状态
  86. * @param {Object} change - 变更令数据
  87. * @return {void}
  88. */
  89. async getStatusByChange(change) {
  90. const statusConst = audit.flow.status;
  91. const auditStatusConst = audit.flow.auditStatus;
  92. const uid = this.ctx.session.sessionUser.accountId;
  93. const changeAuditInfo = await this.getAllDataByCondition({ where: { cid: change.cid, times: change.times, uid }, orders: [['id', 'desc']], limit: 1, offset: 0 });
  94. if (changeAuditInfo === null || changeAuditInfo[0].status === undefined) {
  95. // 无权限查看此变更令
  96. return 0;
  97. }
  98. if (change.status === statusConst.uncheck && uid === change.uid) {
  99. // 待上报
  100. return 1;
  101. } else if (change.status === statusConst.back && uid === change.uid) {
  102. // 待重新上报
  103. return 2;
  104. } else if (change.status === statusConst.back && uid !== change.uid) {
  105. // 被退回但你不是原报人
  106. return 3;
  107. } else if (change.status === statusConst.checked) {
  108. // 已完成
  109. return 4;
  110. } else if (change.status === statusConst.checkNo) {
  111. // 已终止
  112. return 5;
  113. } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo[0].status === auditStatusConst.checking) {
  114. // 待你审批
  115. return 6;
  116. } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo[0].status !== auditStatusConst.checking) {
  117. // 审批中但你未到你审批或你已审批
  118. return 7;
  119. }
  120. // 无权限查看此变更令
  121. return 0;
  122. }
  123. /**
  124. * 根据用户查看此变更状态获取审批人列表
  125. * @param {Object} change - 变更令
  126. * @param {int} status - 状态
  127. * @return {object} list - 列表
  128. */
  129. async getListByStatus(change, status) {
  130. let sql = '';
  131. let sqlParam = '';
  132. switch (status) {
  133. case 1:// 待上报
  134. case 2:// 待重新上报
  135. sql = 'SELECT * FROM ?? WHERE ' +
  136. 'cid = ? AND times = ? GROUP BY usite';
  137. sqlParam = [this.tableName, change.cid,
  138. change.times];
  139. break;
  140. case 3: // 被退回但你不是原报人
  141. case 4:// 已完成
  142. case 5:// 已终止
  143. case 7:// 审批中但你未到你审批或你已审批
  144. // 获取完整的审批顺序
  145. sql = 'SELECT * FROM ?? WHERE ' +
  146. 'cid = ? ORDER BY usort';
  147. sqlParam = [this.tableName, change.cid];
  148. break;
  149. case 6: // 审批中
  150. sql = 'SELECT * FROM (SELECT MAX(usort) as ust FROM ?? ' +
  151. 'WHERE cid = ? and times = ? GROUP BY usite ) as b ' +
  152. 'JOIN ?? as a ON a.usort = b.ust WHERE cid = ? and times = ? ORDER BY usite';
  153. sqlParam = [this.tableName, change.cid, change.times, this.tableName, change.cid, change.times];
  154. break;
  155. default:// 无权限查看此变更令
  156. break;
  157. }
  158. const list = await this.db.query(sql, sqlParam);
  159. return list;
  160. }
  161. /**
  162. * 删除变更令审批人列表
  163. * @param {Object} transaction - 事务
  164. * @param {Object} cid - 变更令id
  165. * @param {int} times - 次数
  166. * @return {object} 返回结果
  167. */
  168. async deleteAuditData(transaction, cid, times) {
  169. this.initSqlBuilder();
  170. this.sqlBuilder.setAndWhere('cid', {
  171. value: this.db.escape(cid),
  172. operate: '=',
  173. });
  174. this.sqlBuilder.setAndWhere('times', {
  175. value: times,
  176. operate: '=',
  177. });
  178. this.sqlBuilder.setAndWhere('usite', {
  179. value: 0,
  180. operate: '!=',
  181. });
  182. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'delete');
  183. const result = await transaction.query(sql, sqlParam);
  184. return result;
  185. }
  186. /**
  187. * 获取不重复列表
  188. * @param {Object} cid - 变更令id
  189. * @param {int} times - 次数
  190. * @return {object} 返回结果
  191. */
  192. async getListGroupByTimes(cid, times) {
  193. const sql = 'SELECT * FROM ?? WHERE ' +
  194. 'cid = ? AND times = ? GROUP BY usite';
  195. const sqlParam = [this.tableName, cid, times];
  196. const list = await this.db.query(sql, sqlParam);
  197. return list;
  198. }
  199. /**
  200. * 获取比sort大的审批人列表
  201. * @param {Object} cid - 变更令id
  202. * @param {int} usort - 排序
  203. * @return {object} 返回结果
  204. */
  205. async getNextAuditList(cid, usort) {
  206. const sql = 'SELECT * FROM ?? WHERE ' +
  207. 'cid = ? AND usort > ?';
  208. const sqlParam = [this.tableName, cid, usort];
  209. const list = await this.db.query(sql, sqlParam);
  210. return list;
  211. }
  212. /**
  213. * 获取除当前次数的审批人列表
  214. * @param {Object} cid - 变更令id
  215. * @param {int} times - 次数
  216. * @return {object} 返回结果
  217. */
  218. async getListByBack(cid, times) {
  219. this.initSqlBuilder();
  220. this.sqlBuilder.setAndWhere('cid', {
  221. value: this.db.escape(cid),
  222. operate: '=',
  223. });
  224. this.sqlBuilder.setAndWhere('times', {
  225. value: times,
  226. operate: '!=',
  227. });
  228. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  229. const result = await this.db.query(sql, sqlParam);
  230. return result;
  231. }
  232. /**
  233. * 获取 审核人 待审批的() 变更令列表
  234. * @param uid
  235. * @returns {Promise<void>}
  236. */
  237. async getAuditChange(uid) {
  238. const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ' +
  239. ' c.`code` As `ccode`, c.`name` As `cname`,' +
  240. ' t.`name`, t.`type`, t.`user_id` ' +
  241. ' FROM ?? AS ca, ?? AS c, ?? As t ' +
  242. ' WHERE ca.`uid` = ? and ca.`status` = ?' +
  243. ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id`';
  244. const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, uid, 2];
  245. return await this.db.query(sql, sqlParam);
  246. }
  247. }
  248. return ChangeAudit;
  249. };