ledger_audit.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. 'use strict';
  2. /**
  3. * 台账审批流程表
  4. *
  5. * @author Mai
  6. * @date 2018/5/25
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').ledger;
  10. const smsTypeConst = require('../const/sms_type');
  11. const SMS = require('../lib/sms');
  12. module.exports = app => {
  13. class LedgerAudit extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'ledger_audit';
  23. }
  24. /**
  25. * 获取标段审核人信息
  26. *
  27. * @param {Number} tenderId - 标段id
  28. * @param {Number} auditorId - 审核人id
  29. * @param {Number} times - 第几次审批
  30. * @returns {Promise<*>}
  31. */
  32. async getAuditor(tenderId, auditorId, times = 1) {
  33. 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` ' +
  34. 'FROM ?? AS la, ?? AS pa ' +
  35. 'WHERE la.`tender_id` = ? and la.`audit_id` = ? and la.`times` = ?' +
  36. ' and la.`audit_id` = pa.`id`';
  37. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditorId, times];
  38. return await this.db.queryOne(sql, sqlParam);
  39. }
  40. /**
  41. * 获取标段审核列表信息
  42. *
  43. * @param {Number} tenderId - 标段id
  44. * @param {Number} times - 第几次审批
  45. * @returns {Promise<*>}
  46. */
  47. async getAuditors(tenderId, times = 1) {
  48. 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` ' +
  49. 'FROM ?? AS la, ?? AS pa ' +
  50. 'WHERE la.`tender_id` = ? and la.`times` = ?' +
  51. ' and la.`audit_id` = pa.`id` order by la.`audit_order`';
  52. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, times];
  53. return await this.db.query(sql, sqlParam);
  54. }
  55. /**
  56. * 获取标段当前审核人
  57. *
  58. * @param {Number} tenderId - 标段id
  59. * @param {Number} times - 第几次审批
  60. * @returns {Promise<*>}
  61. */
  62. async getCurAuditor(tenderId, times = 1) {
  63. 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` ' +
  64. 'FROM ?? AS la, ?? AS pa ' +
  65. 'WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
  66. ' and la.`audit_id` = pa.`id`';
  67. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.checking, times];
  68. return await this.db.queryOne(sql, sqlParam);
  69. }
  70. /**
  71. * 获取最新审核顺序
  72. *
  73. * @param {Number} tenderId - 标段id
  74. * @param {Number} times - 第几次审批
  75. * @returns {Promise<number>}
  76. */
  77. async getNewOrder(tenderId, times = 1) {
  78. const sql = 'SELECT Max(??) As max_order FROM ?? Where `tender_id` = ? and `times` = ?';
  79. const sqlParam = ['audit_order', this.tableName, tenderId, times];
  80. const result = await this.db.queryOne(sql, sqlParam);
  81. return result && result.max_order ? result.max_order + 1 : 1;
  82. }
  83. /**
  84. * 新增审核人
  85. *
  86. * @param {Number} tenderId - 标段id
  87. * @param {Number} auditorId - 审核人id
  88. * @param {Number} times - 第几次审批
  89. * @returns {Promise<number>}
  90. */
  91. async addAuditor(tenderId, auditorId, times = 1) {
  92. const newOrder = await this.getNewOrder(tenderId, times);
  93. const data = {
  94. tender_id: tenderId,
  95. audit_id: auditorId,
  96. times: times,
  97. audit_order: newOrder,
  98. status: auditConst.status.uncheck,
  99. };
  100. const result = await this.db.insert(this.tableName, data);
  101. return result.effectRows = 1;
  102. }
  103. /**
  104. * 移除审核人时,同步其后审核人order
  105. * @param transaction - 事务
  106. * @param {Number} tenderId - 标段id
  107. * @param {Number} auditorId - 审核人id
  108. * @param {Number} times - 第几次审批
  109. * @returns {Promise<*>}
  110. * @private
  111. */
  112. async _syncOrderByDelete(transaction, tenderId, order, times) {
  113. this.initSqlBuilder();
  114. this.sqlBuilder.setAndWhere('tender_id', {
  115. value: tenderId,
  116. operate: '='
  117. });
  118. this.sqlBuilder.setAndWhere('audit_order', {
  119. value: order,
  120. operate: '>=',
  121. });
  122. this.sqlBuilder.setAndWhere('times', {
  123. value: times,
  124. operate: '=',
  125. });
  126. this.sqlBuilder.setUpdateData('audit_order', {
  127. value: 1,
  128. selfOperate: '-',
  129. });
  130. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  131. const data = await transaction.query(sql, sqlParam);
  132. return data;
  133. }
  134. /**
  135. * 移除审核人
  136. *
  137. * @param {Number} tenderId - 标段id
  138. * @param {Number} auditorId - 审核人id
  139. * @param {Number} times - 第几次审批
  140. * @returns {Promise<boolean>}
  141. */
  142. async deleteAuditor(tenderId, auditorId, times = 1) {
  143. const transaction = await this.db.beginTransaction();
  144. try {
  145. const condition = {tender_id: tenderId, audit_id: auditorId, times: times};
  146. const auditor = await this.getDataByCondition(condition);
  147. if (!auditor) {
  148. throw '该审核人不存在';
  149. }
  150. await this._syncOrderByDelete(transaction, tenderId, auditor.audit_order, times);
  151. await transaction.delete(this.tableName, condition);
  152. await transaction.commit();
  153. } catch(err) {
  154. await transaction.rollback();
  155. throw err;
  156. }
  157. return true;
  158. }
  159. /**
  160. * 开始审批
  161. *
  162. * @param {Number} tenderId - 标段id
  163. * @param {Number} times - 第几次审批
  164. * @returns {Promise<boolean>}
  165. */
  166. async start(tenderId, times = 1) {
  167. const audit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: 1});
  168. if (!audit) {
  169. throw '审核人信息错误';
  170. }
  171. const transaction = await this.db.beginTransaction();
  172. try {
  173. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: new Date()});
  174. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_status: auditConst.status.checking});
  175. // 添加短信通知-需要审批提醒功能
  176. const smsUser = await this.ctx.service.projectAccount.getDataById(audit.audit_id);
  177. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  178. const smsType = JSON.parse(smsUser.sms_type);
  179. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  180. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  181. const sms = new SMS(this.ctx);
  182. const content = '【纵横计量支付】' + tenderInfo.name + '台帐需要您审批。';
  183. sms.send(smsUser.auth_mobile, content);
  184. }
  185. }
  186. await transaction.commit();
  187. } catch (err) {
  188. await transaction.rollback();
  189. throw err;
  190. }
  191. return true;
  192. }
  193. /**
  194. * 拷贝一份台账备份(每一次审批结束时)
  195. * @param {Number} tenderId - 标段Id
  196. * @param transaction - 事务
  197. * @param {Number} times - 次数
  198. * @returns {Promise<void>}
  199. * @private
  200. */
  201. async _copyHisytoryLedger(tenderId, transaction, times = 1) {
  202. const sql = 'Insert Into ?? ' +
  203. 'Select *, ? As times From ?? ' +
  204. 'Where ?? = ?';
  205. const sqlParam = [this.tableName + '_copy', times, this.ctx.service.ledger.tableName, 'tender_id', tenderId];
  206. return await transaction.query(sql, sqlParam);
  207. }
  208. /**
  209. * 审批
  210. * @param {Number} tenderId - 标段id
  211. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  212. * @param {Number} times - 第几次审批
  213. * @returns {Promise<void>}
  214. */
  215. async check(tenderId, checkType, opinion, times = 1) {
  216. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  217. throw '提交数据错误';
  218. }
  219. const transaction = await this.db.beginTransaction();
  220. try {
  221. // 整理当前流程审核人状态更新
  222. const time = new Date();
  223. const audit = await this.getDataByCondition({tender_id: tenderId, times: times, status: auditConst.status.checking});
  224. if (!audit) {
  225. throw '审核数据错误';
  226. }
  227. // 更新当前审核流程
  228. await transaction.update(this.tableName, {id: audit.id, status: checkType, opinion: opinion, end_time: time});
  229. if (checkType === auditConst.status.checked) {
  230. const nextAudit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: audit.audit_order + 1});
  231. // 无下一审核人表示,审核结束
  232. if (nextAudit) {
  233. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  234. // 添加短信通知-需要审批提醒功能
  235. const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.audit_id);
  236. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  237. const smsType = JSON.parse(smsUser.sms_type);
  238. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  239. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  240. const sms = new SMS(this.ctx);
  241. const content = '【纵横计量支付】' + tenderInfo.name + '台帐需要您审批。';
  242. sms.send(smsUser.auth_mobile, content);
  243. }
  244. }
  245. } else {
  246. // 同步标段信息
  247. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_status: checkType});
  248. // 拷贝备份台账数据
  249. await this._copyHisytoryLedger(tenderId, transaction, times);
  250. // 添加短信通知-审批通过提醒功能
  251. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  252. const smsUser = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  253. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  254. const smsType = JSON.parse(smsUser.sms_type);
  255. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  256. const sms = new SMS(this.ctx);
  257. const content = '【纵横计量支付】' + tenderInfo.name + '台账审批通过。';
  258. sms.send(smsUser.auth_mobile, content);
  259. }
  260. }
  261. }
  262. } else {
  263. // 同步标段信息
  264. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_times: times+1, ledger_status: checkType});
  265. // 拷贝新一次审核流程列表
  266. const auditors = await this.getAllDataByCondition({
  267. where: {tender_id: tenderId, times: times},
  268. columns: ['tender_id', 'audit_order', 'audit_id']
  269. });
  270. for (const a of auditors) {
  271. a.times = times + 1;
  272. a.status = auditConst.status.uncheck;
  273. }
  274. await transaction.insert(this.tableName, auditors);
  275. // 拷贝备份台账数据
  276. await this._copyHisytoryLedger(tenderId, transaction, times);
  277. // 添加短信通知-审批退回提醒功能
  278. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  279. const smsUser = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  280. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  281. const smsType = JSON.parse(smsUser.sms_type);
  282. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  283. const sms = new SMS(this.ctx);
  284. const content = '【纵横计量支付】' + tenderInfo.name + '台账审批退回。';
  285. sms.send(smsUser.auth_mobile, content);
  286. }
  287. }
  288. }
  289. await transaction.commit();
  290. } catch (err) {
  291. await transaction.rollback();
  292. throw err;
  293. }
  294. }
  295. /**
  296. * 获取审核人需要审核的标段列表
  297. *
  298. * @param auditorId
  299. * @returns {Promise<*>}
  300. */
  301. async getAuditTender(auditorId) {
  302. const sql = 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`begin_time`, la.`end_time`, t.`id`, t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`ledger_status` ' +
  303. 'FROM ?? AS la, ?? AS t ' +
  304. 'WHERE ((la.`audit_id` = ? and la.`status` = ?) OR (t.`user_id` = ? and t.`ledger_status` = ? and la.`status` = ? and la.`times` = (t.`ledger_times`-1)))' +
  305. ' and la.`tender_id` = t.`id`';
  306. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  307. return await this.db.query(sql, sqlParam);
  308. }
  309. /**
  310. * 获取 某时间后 审批进度 更新的台账
  311. * @param {Integer} projectId - 项目id
  312. * @param {Integer} auditorId - 查询人id
  313. * @param {Date} noticeTime - 查询事件
  314. * @returns {Promise<*>}
  315. */
  316. async getNoticeTender(projectId, auditorId, noticeTime) {
  317. const sql = 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`end_time`, la.`status`, t.`id`, t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  318. ' pa.name As `lu_name`, pa.role As `lu_role`, pa.company As `lu_company`' +
  319. ' FROM ?? As t ' +
  320. ' LEFT JOIN ?? As la ON la.`tender_id` = t.`id`' +
  321. ' LEFT JOIN ?? As pa ON la.`audit_id` = pa.`id`' +
  322. ' WHERE la.`audit_id` <> ? and la.`end_time` > ? and t.`project_id` = ?' +
  323. ' GROUP By t.`id`' +
  324. ' ORDER By la.`end_time`';
  325. const sqlParam = [this.ctx.service.tender.tableName, this.tableName, this.ctx.service.projectAccount.tableName,
  326. auditorId, noticeTime, projectId];
  327. return await this.db.query(sql, sqlParam);
  328. }
  329. }
  330. return LedgerAudit;
  331. };