ledger_audit.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 tenderName = await sms.contentChange(tenderInfo.name);
  183. const content = '【纵横计量支付】' + tenderName + '台帐需要您审批。';
  184. sms.send(smsUser.auth_mobile, content);
  185. }
  186. }
  187. await transaction.commit();
  188. } catch (err) {
  189. await transaction.rollback();
  190. throw err;
  191. }
  192. return true;
  193. }
  194. /**
  195. * 拷贝一份台账备份(每一次审批结束时)
  196. * @param {Number} tenderId - 标段Id
  197. * @param transaction - 事务
  198. * @param {Number} times - 次数
  199. * @returns {Promise<void>}
  200. * @private
  201. */
  202. async _copyHisytoryLedger(tenderId, transaction, times = 1) {
  203. const sql = 'Insert Into ?? ' +
  204. 'Select *, ? As times From ?? ' +
  205. 'Where ?? = ?';
  206. const sqlParam = [this.tableName + '_copy', times, this.ctx.service.ledger.tableName, 'tender_id', tenderId];
  207. return await transaction.query(sql, sqlParam);
  208. }
  209. /**
  210. * 审批
  211. * @param {Number} tenderId - 标段id
  212. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  213. * @param {Number} times - 第几次审批
  214. * @returns {Promise<void>}
  215. */
  216. async check(tenderId, checkType, opinion, times = 1) {
  217. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  218. throw '提交数据错误';
  219. }
  220. const transaction = await this.db.beginTransaction();
  221. try {
  222. // 整理当前流程审核人状态更新
  223. const time = new Date();
  224. const audit = await this.getDataByCondition({tender_id: tenderId, times: times, status: auditConst.status.checking});
  225. if (!audit) {
  226. throw '审核数据错误';
  227. }
  228. // 更新当前审核流程
  229. await transaction.update(this.tableName, {id: audit.id, status: checkType, opinion: opinion, end_time: time});
  230. if (checkType === auditConst.status.checked) {
  231. const nextAudit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: audit.audit_order + 1});
  232. // 无下一审核人表示,审核结束
  233. if (nextAudit) {
  234. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  235. // 添加短信通知-需要审批提醒功能
  236. const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.audit_id);
  237. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  238. const smsType = JSON.parse(smsUser.sms_type);
  239. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  240. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  241. const sms = new SMS(this.ctx);
  242. const tenderName = await sms.contentChange(tenderInfo.name);
  243. const content = '【纵横计量支付】' + tenderName + '台帐需要您审批。';
  244. sms.send(smsUser.auth_mobile, content);
  245. }
  246. }
  247. } else {
  248. // 同步标段信息
  249. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_status: checkType});
  250. // 拷贝备份台账数据
  251. await this._copyHisytoryLedger(tenderId, transaction, times);
  252. // 添加短信通知-审批通过提醒功能
  253. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  254. const smsUser = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  255. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  256. const smsType = JSON.parse(smsUser.sms_type);
  257. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  258. const sms = new SMS(this.ctx);
  259. const tenderName = await sms.contentChange(tenderInfo.name);
  260. const content = '【纵横计量支付】' + tenderName + '台账审批通过,请登录系统处理。';
  261. sms.send(smsUser.auth_mobile, content);
  262. }
  263. }
  264. }
  265. } else {
  266. // 同步标段信息
  267. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_times: times+1, ledger_status: checkType});
  268. // 拷贝新一次审核流程列表
  269. const auditors = await this.getAllDataByCondition({
  270. where: {tender_id: tenderId, times: times},
  271. columns: ['tender_id', 'audit_order', 'audit_id']
  272. });
  273. for (const a of auditors) {
  274. a.times = times + 1;
  275. a.status = auditConst.status.uncheck;
  276. }
  277. await transaction.insert(this.tableName, auditors);
  278. // 拷贝备份台账数据
  279. await this._copyHisytoryLedger(tenderId, transaction, times);
  280. // 添加短信通知-审批退回提醒功能
  281. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  282. const smsUser = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  283. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  284. const smsType = JSON.parse(smsUser.sms_type);
  285. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  286. const sms = new SMS(this.ctx);
  287. const tenderName = await sms.contentChange(tenderInfo.name);
  288. const content = '【纵横计量支付】' + tenderName + '台账审批退回,请登录系统处理。';
  289. sms.send(smsUser.auth_mobile, content);
  290. }
  291. }
  292. }
  293. await transaction.commit();
  294. } catch (err) {
  295. await transaction.rollback();
  296. throw err;
  297. }
  298. }
  299. /**
  300. * 获取审核人需要审核的标段列表
  301. *
  302. * @param auditorId
  303. * @returns {Promise<*>}
  304. */
  305. async getAuditTender(auditorId) {
  306. 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` ' +
  307. 'FROM ?? AS la, ?? AS t ' +
  308. '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)))' +
  309. ' and la.`tender_id` = t.`id`';
  310. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  311. return await this.db.query(sql, sqlParam);
  312. }
  313. /**
  314. * 获取 某时间后 审批进度 更新的台账
  315. * @param {Integer} projectId - 项目id
  316. * @param {Integer} auditorId - 查询人id
  317. * @param {Date} noticeTime - 查询事件
  318. * @returns {Promise<*>}
  319. */
  320. async getNoticeTender(projectId, auditorId, noticeTime) {
  321. 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`, ' +
  322. ' pa.name As `lu_name`, pa.role As `lu_role`, pa.company As `lu_company`' +
  323. ' FROM ?? As t ' +
  324. ' LEFT JOIN ?? As la ON la.`tender_id` = t.`id`' +
  325. ' LEFT JOIN ?? As pa ON la.`audit_id` = pa.`id`' +
  326. ' WHERE la.`audit_id` <> ? and la.`end_time` > ? and t.`project_id` = ?' +
  327. ' GROUP By t.`id`' +
  328. ' ORDER By la.`end_time`';
  329. const sqlParam = [this.ctx.service.tender.tableName, this.tableName, this.ctx.service.projectAccount.tableName,
  330. auditorId, noticeTime, projectId];
  331. return await this.db.query(sql, sqlParam);
  332. }
  333. }
  334. return LedgerAudit;
  335. };