ledger_audit.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. 'use strict';
  2. /**
  3. * 台账审批流程表
  4. *
  5. * @author Mai
  6. * @date 2018/5/25
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').flow;
  10. module.exports = app => {
  11. class LedgerAudit extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'ledger_audit';
  21. }
  22. /**
  23. * 获取标段审核人信息
  24. *
  25. * @param {Number} tenderId - 标段id
  26. * @param {Number} auditorId - 审核人id
  27. * @param {Number} times - 第几次审批
  28. * @returns {Promise<*>}
  29. */
  30. async getAuditor(tenderId, auditorId, times = 1) {
  31. const sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  32. 'FROM ?? AS la, ?? AS pa ' +
  33. 'WHERE la.`tender_id` = ? and la.`audit_id` = ? and la.`times` = ?' +
  34. ' and la.`audit_id` = pa.`id`';
  35. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditorId, times];
  36. return await this.db.queryOne(sql, sqlParam);
  37. }
  38. /**
  39. * 获取标段审核列表信息
  40. *
  41. * @param {Number} tenderId - 标段id
  42. * @param {Number} times - 第几次审批
  43. * @returns {Promise<*>}
  44. */
  45. async getAuditors(tenderId, times = 1) {
  46. const sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  47. 'FROM ?? AS la, ?? AS pa ' +
  48. 'WHERE la.`tender_id` = ? and la.`times` = ?' +
  49. ' and la.`audit_id` = pa.`id`';
  50. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, times];
  51. return await this.db.query(sql, sqlParam);
  52. }
  53. /**
  54. * 获取标段当前审核人
  55. *
  56. * @param {Number} tenderId - 标段id
  57. * @param {Number} times - 第几次审批
  58. * @returns {Promise<*>}
  59. */
  60. async getCurAuditor(tenderId, times = 1) {
  61. const sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  62. 'FROM ?? AS la, ?? AS pa ' +
  63. 'WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
  64. ' and la.`audit_id` = pa.`id`';
  65. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.checking, times];
  66. return await this.db.queryOne(sql, sqlParam);
  67. }
  68. /**
  69. * 获取最新审核顺序
  70. *
  71. * @param {Number} tenderId - 标段id
  72. * @param {Number} times - 第几次审批
  73. * @returns {Promise<number>}
  74. */
  75. async getNewOrder(tenderId, times = 1) {
  76. const sql = 'SELECT Max(??) As max_order FROM ?? Where `tender_id` = ? and `times` = ?';
  77. const sqlParam = ['audit_order', this.tableName, tenderId, times];
  78. const result = await this.db.queryOne(sql, sqlParam);
  79. return result && result.max_order ? result.max_order + 1 : 1;
  80. }
  81. /**
  82. * 新增审核人
  83. *
  84. * @param {Number} tenderId - 标段id
  85. * @param {Number} auditorId - 审核人id
  86. * @param {Number} times - 第几次审批
  87. * @returns {Promise<number>}
  88. */
  89. async addAuditor(tenderId, auditorId, times = 1) {
  90. const newOrder = await this.getNewOrder(tenderId, times);
  91. const data = {
  92. tender_id: tenderId,
  93. audit_id: auditorId,
  94. times: times,
  95. audit_order: newOrder,
  96. status: auditConst.status.uncheck,
  97. };
  98. const result = await this.db.insert(this.tableName, data);
  99. return result.effectRows = 1;
  100. }
  101. /**
  102. * 移除审核人时,同步其后审核人order
  103. * @param transaction - 事务
  104. * @param {Number} tenderId - 标段id
  105. * @param {Number} auditorId - 审核人id
  106. * @param {Number} times - 第几次审批
  107. * @returns {Promise<*>}
  108. * @private
  109. */
  110. async _syncOrderByDelete(transaction, tenderId, order, times) {
  111. this.initSqlBuilder();
  112. this.sqlBuilder.setAndWhere('tender_id', {
  113. value: tenderId,
  114. operate: '='
  115. });
  116. this.sqlBuilder.setAndWhere('audit_order', {
  117. value: order,
  118. operate: '>=',
  119. });
  120. this.sqlBuilder.setAndWhere('times', {
  121. value: times,
  122. operate: '=',
  123. });
  124. this.sqlBuilder.setUpdateData('audit_order', {
  125. value: 1,
  126. selfOperate: '-',
  127. });
  128. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  129. const data = await transaction.query(sql, sqlParam);
  130. return data;
  131. }
  132. /**
  133. * 移除审核人
  134. *
  135. * @param {Number} tenderId - 标段id
  136. * @param {Number} auditorId - 审核人id
  137. * @param {Number} times - 第几次审批
  138. * @returns {Promise<boolean>}
  139. */
  140. async deleteAuditor(tenderId, auditorId, times = 1) {
  141. const transaction = await this.db.beginTransaction();
  142. try {
  143. const condition = {tender_id: tenderId, audit_id: auditorId, times: times};
  144. const auditor = await this.getDataByCondition(condition);
  145. if (!auditor) {
  146. throw '该审核人不存在';
  147. }
  148. await this._syncOrderByDelete(transaction, tenderId, auditor.audit_order, times);
  149. await transaction.delete(this.tableName, condition);
  150. await transaction.commit();
  151. } catch(err) {
  152. await transaction.rollback();
  153. throw err;
  154. }
  155. return true;
  156. }
  157. /**
  158. * 开始审批
  159. *
  160. * @param {Number} tenderId - 标段id
  161. * @param {Number} times - 第几次审批
  162. * @returns {Promise<boolean>}
  163. */
  164. async start(tenderId, times = 1) {
  165. const audit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: 1});
  166. if (!audit) {
  167. throw '审核人信息错误';
  168. }
  169. const transaction = await this.db.beginTransaction();
  170. try {
  171. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: new Date()});
  172. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_status: auditConst.status.checking});
  173. await transaction.commit();
  174. } catch(err) {
  175. await transaction.rollback();
  176. throw err;
  177. }
  178. return true;
  179. }
  180. /**
  181. * 拷贝一份台账备份(每一次审批结束时)
  182. * @param {Number} tenderId - 标段Id
  183. * @param transaction - 事务
  184. * @param {Number} times - 次数
  185. * @returns {Promise<void>}
  186. * @private
  187. */
  188. async _copyHisytoryLedger(tenderId, transaction, times = 1) {
  189. const sql = 'Insert Into ?? ' +
  190. 'Select *, ? As times From ?? ' +
  191. 'Where ?? = ?';
  192. const sqlParam = [this.tableName + '_copy', times, this.ctx.service.ledger.tableName, 'tender_id', tenderId];
  193. return await transaction.query(sql, sqlParam);
  194. }
  195. /**
  196. * 审批
  197. * @param {Number} tenderId - 标段id
  198. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  199. * @param {Number} times - 第几次审批
  200. * @returns {Promise<void>}
  201. */
  202. async check(tenderId, checkType, opinion, times = 1) {
  203. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  204. throw '提交数据错误';
  205. }
  206. const transaction = await this.db.beginTransaction();
  207. try {
  208. // 整理当前流程审核人状态更新
  209. const time = new Date();
  210. const audit = await this.getDataByCondition({tender_id: tenderId, times: times, status: auditConst.status.checking});
  211. if (!audit) {
  212. throw '审核数据错误';
  213. }
  214. // 更新当前审核流程
  215. await transaction.update(this.tableName, {id: audit.id, status: checkType, opinion: opinion, end_time: time});
  216. if (checkType === auditConst.status.checked) {
  217. const nextAudit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: audit.audit_order + 1});
  218. // 无下一审核人表示,审核结束
  219. if (nextAudit) {
  220. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  221. } else {
  222. // 同步标段信息
  223. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_status: checkType});
  224. // 拷贝备份台账数据
  225. await this._copyHisytoryLedger(tenderId, transaction, times);
  226. }
  227. } else {
  228. // 同步标段信息
  229. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_times: times+1, ledger_status: checkType});
  230. // 拷贝新一次审核流程列表
  231. const auditors = await this.getAllDataByCondition({
  232. where: {tender_id: tenderId, times: times},
  233. columns: ['tender_id', 'audit_order', 'audit_id']
  234. });
  235. for (const a of auditors) {
  236. a.times = times + 1;
  237. a.status = auditConst.status.uncheck;
  238. }
  239. await transaction.insert(this.tableName, auditors);
  240. // 拷贝备份台账数据
  241. await this._copyHisytoryLedger(tenderId, transaction, times);
  242. }
  243. await transaction.commit();
  244. } catch (err) {
  245. await transaction.rollback();
  246. throw err;
  247. }
  248. }
  249. /**
  250. * 获取审核人需要审核的标段列表
  251. *
  252. * @param auditorId
  253. * @returns {Promise<*>}
  254. */
  255. async getAuditTender(auditorId) {
  256. const sql = 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`begin_time`, t.`id`, t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  257. 'FROM ?? AS la, ?? AS t ' +
  258. 'WHERE la.`audit_id` = ? and la.`status` = ?' +
  259. ' and la.`tender_id` = t.`id`';
  260. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking];
  261. return await this.db.query(sql, sqlParam);
  262. }
  263. }
  264. return LedgerAudit;
  265. };