ledger_audit.js 18 KB

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