ledger_audit.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. const SmsAliConst = require('../const/sms_alitemplate');
  13. const pushType = require('../const/audit').pushType;
  14. module.exports = app => {
  15. class LedgerAudit extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'ledger_audit';
  25. }
  26. /**
  27. * 获取标段审核人信息
  28. *
  29. * @param {Number} tenderId - 标段id
  30. * @param {Number} auditorId - 审核人id
  31. * @param {Number} times - 第几次审批
  32. * @returns {Promise<*>}
  33. */
  34. async getAuditor(tenderId, auditorId, times = 1) {
  35. 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` ' +
  36. 'FROM ?? AS la, ?? AS pa ' +
  37. 'WHERE la.`tender_id` = ? and la.`audit_id` = ? and la.`times` = ?' +
  38. ' and la.`audit_id` = pa.`id`';
  39. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditorId, times];
  40. return await this.db.queryOne(sql, sqlParam);
  41. }
  42. /**
  43. * 获取标段审核人最后一位的名称
  44. *
  45. * @param {Number} tenderId - 标段id
  46. * @param {Number} auditorId - 审核人id
  47. * @param {Number} times - 第几次审批
  48. * @returns {Promise<*>}
  49. */
  50. async getStatusName(tenderId, times) {
  51. const sql = 'SELECT pa.`name` ' +
  52. 'FROM ?? AS la, ?? AS pa ' +
  53. 'WHERE la.`tender_id` = ?' +
  54. ' and la.`audit_id` = pa.`id` and la.`status` != ? ORDER BY la.`times` DESC, la.`audit_order` DESC';
  55. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.uncheck];
  56. return await this.db.queryOne(sql, sqlParam);
  57. }
  58. /**
  59. * 获取标段审核列表信息
  60. *
  61. * @param {Number} tenderId - 标段id
  62. * @param {Number} times - 第几次审批
  63. * @returns {Promise<*>}
  64. */
  65. async getAuditors(tenderId, times = 1) {
  66. 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`, g.`sort` ' +
  67. 'FROM ?? AS la, ?? AS pa, (SELECT `audit_id`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `tender_id` = ? AND `times` = ? GROUP BY `audit_id`) as g ' +
  68. 'WHERE la.`tender_id` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`audit_order`';
  69. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, tenderId, times, tenderId, times];
  70. const result = await this.db.query(sql, sqlParam);
  71. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `tender_id` = ? AND `times` = ? GROUP BY `audit_id`) as a';
  72. const sqlParam2 = [this.tableName, tenderId, times];
  73. const count = await this.db.queryOne(sql2, sqlParam2);
  74. for (const i in result) {
  75. result[i].max_sort = count.num;
  76. }
  77. return result;
  78. }
  79. /**
  80. * 获取标段当前审核人
  81. *
  82. * @param {Number} tenderId - 标段id
  83. * @param {Number} times - 第几次审批
  84. * @returns {Promise<*>}
  85. */
  86. async getCurAuditor(tenderId, times = 1) {
  87. 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` ' +
  88. 'FROM ?? AS la, ?? AS pa ' +
  89. 'WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
  90. ' and la.`audit_id` = pa.`id`';
  91. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.checking, times];
  92. return await this.db.queryOne(sql, sqlParam);
  93. }
  94. /**
  95. * 获取最新审核顺序
  96. *
  97. * @param {Number} tenderId - 标段id
  98. * @param {Number} times - 第几次审批
  99. * @returns {Promise<number>}
  100. */
  101. async getNewOrder(tenderId, times = 1) {
  102. const sql = 'SELECT Max(??) As max_order FROM ?? Where `tender_id` = ? and `times` = ?';
  103. const sqlParam = ['audit_order', this.tableName, tenderId, times];
  104. const result = await this.db.queryOne(sql, sqlParam);
  105. return result && result.max_order ? result.max_order + 1 : 1;
  106. }
  107. /**
  108. * 新增审核人
  109. *
  110. * @param {Number} tenderId - 标段id
  111. * @param {Number} auditorId - 审核人id
  112. * @param {Number} times - 第几次审批
  113. * @returns {Promise<number>}
  114. */
  115. async addAuditor(tenderId, auditorId, times = 1) {
  116. const newOrder = await this.getNewOrder(tenderId, times);
  117. const data = {
  118. tender_id: tenderId,
  119. audit_id: auditorId,
  120. times: times,
  121. audit_order: newOrder,
  122. status: auditConst.status.uncheck,
  123. };
  124. const result = await this.db.insert(this.tableName, data);
  125. return result.effectRows = 1;
  126. }
  127. /**
  128. * 移除审核人时,同步其后审核人order
  129. * @param transaction - 事务
  130. * @param {Number} tenderId - 标段id
  131. * @param {Number} auditorId - 审核人id
  132. * @param {Number} times - 第几次审批
  133. * @returns {Promise<*>}
  134. * @private
  135. */
  136. async _syncOrderByDelete(transaction, tenderId, order, times) {
  137. this.initSqlBuilder();
  138. this.sqlBuilder.setAndWhere('tender_id', {
  139. value: tenderId,
  140. operate: '='
  141. });
  142. this.sqlBuilder.setAndWhere('audit_order', {
  143. value: order,
  144. operate: '>=',
  145. });
  146. this.sqlBuilder.setAndWhere('times', {
  147. value: times,
  148. operate: '=',
  149. });
  150. this.sqlBuilder.setUpdateData('audit_order', {
  151. value: 1,
  152. selfOperate: '-',
  153. });
  154. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  155. const data = await transaction.query(sql, sqlParam);
  156. return data;
  157. }
  158. /**
  159. * 移除审核人
  160. *
  161. * @param {Number} tenderId - 标段id
  162. * @param {Number} auditorId - 审核人id
  163. * @param {Number} times - 第几次审批
  164. * @returns {Promise<boolean>}
  165. */
  166. async deleteAuditor(tenderId, auditorId, times = 1) {
  167. const transaction = await this.db.beginTransaction();
  168. try {
  169. const condition = {tender_id: tenderId, audit_id: auditorId, times: times};
  170. const auditor = await this.getDataByCondition(condition);
  171. if (!auditor) {
  172. throw '该审核人不存在';
  173. }
  174. await this._syncOrderByDelete(transaction, tenderId, auditor.audit_order, times);
  175. await transaction.delete(this.tableName, condition);
  176. await transaction.commit();
  177. } catch (err) {
  178. await transaction.rollback();
  179. throw err;
  180. }
  181. return true;
  182. }
  183. /**
  184. * 开始审批
  185. *
  186. * @param {Number} tenderId - 标段id
  187. * @param {Number} times - 第几次审批
  188. * @returns {Promise<boolean>}
  189. */
  190. async start(tenderId, times = 1) {
  191. const audit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: 1});
  192. if (!audit) {
  193. throw '审核人信息错误';
  194. }
  195. const sum = await this.ctx.service.ledger.addUp({tender_id: tenderId/*, is_leaf: true*/});
  196. const transaction = await this.db.beginTransaction();
  197. try {
  198. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: new Date()});
  199. await transaction.update(this.ctx.service.tender.tableName, {
  200. id: tenderId, ledger_status: auditConst.status.checking,
  201. total_price: sum.total_price, deal_tp: sum.deal_tp,
  202. });
  203. // 添加短信通知-需要审批提醒功能
  204. // await this.ctx.helper.sendUserSms(audit.audit_id, smsTypeConst.const.TZ,
  205. // smsTypeConst.judge.approval.toString(), '台帐需要您审批。');
  206. await this.ctx.helper.sendAliSms(audit.audit_id, smsTypeConst.const.TZ,
  207. smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  208. await transaction.commit();
  209. } catch (err) {
  210. await transaction.rollback();
  211. throw err;
  212. }
  213. return true;
  214. }
  215. /**
  216. * 审批
  217. * @param {Number} tenderId - 标段id
  218. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  219. * @param {Number} times - 第几次审批
  220. * @returns {Promise<void>}
  221. */
  222. async check(tenderId, checkType, opinion, times = 1) {
  223. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  224. throw '提交数据错误';
  225. }
  226. const pid = this.ctx.session.sessionProject.id;
  227. const transaction = await this.db.beginTransaction();
  228. try {
  229. // 整理当前流程审核人状态更新
  230. const time = new Date();
  231. const audit = await this.getDataByCondition({ tender_id: tenderId, times, status: auditConst.status.checking });
  232. if (!audit) {
  233. throw '审核数据错误';
  234. }
  235. // 更新当前审核流程
  236. await transaction.update(this.tableName, { id: audit.id, status: checkType, opinion, end_time: time });
  237. if (checkType === auditConst.status.checked) {
  238. const nextAudit = await this.getDataByCondition({ tender_id: tenderId, times, audit_order: audit.audit_order + 1 });
  239. // 无下一审核人表示,审核结束
  240. if (nextAudit) {
  241. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  242. // await this.ctx.helper.sendUserSms(nextAudit.audit_id, smsTypeConst.const.TZ,
  243. // smsTypeConst.judge.approval.toString(), '台帐需要您审批。');
  244. await this.ctx.helper.sendAliSms(nextAudit.audit_id, smsTypeConst.const.TZ,
  245. smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  246. } else {
  247. // 同步标段信息
  248. await transaction.update(this.ctx.service.tender.tableName, { id: tenderId, ledger_status: checkType });
  249. // 添加短信通知-审批通过提醒功能
  250. // const mobile_array = [];
  251. // const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  252. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  253. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  254. // const smsType = JSON.parse(smsUser1.sms_type);
  255. // if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  256. // mobile_array.push(smsUser1.auth_mobile);
  257. // }
  258. // }
  259. // const auditList = await this.getAuditors(tenderId, times);
  260. // for (const user of auditList) {
  261. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.audit_id);
  262. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  263. // const smsType = JSON.parse(smsUser.sms_type);
  264. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  265. // mobile_array.push(smsUser.auth_mobile);
  266. // }
  267. // }
  268. // }
  269. // if (mobile_array.length > 0) {
  270. // const sms = new SMS(this.ctx);
  271. // const tenderName = await sms.contentChange(tenderInfo.name);
  272. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  273. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  274. // const content = '【纵横计量支付】' + ptmsg + '台账审批通过,请登录系统处理。';
  275. // sms.send(mobile_array, content);
  276. // }
  277. const auditList = await this.getAuditors(tenderId, times);
  278. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id);
  279. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.TZ,
  280. // smsTypeConst.judge.result.toString(), '台账审批通过,请登录系统处理。');
  281. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ,
  282. smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, { status: SmsAliConst.status.success });
  283. // 添加到消息推送表
  284. const noticeContent = await this.getNoticeContent(audit.tender_id, pid)
  285. await transaction.insert('zh_notice', { pid, type: pushType.ledger, uid: this.ctx.tender.data.user_id, status: auditConst.status.checked, is_read: 0, content: noticeContent });
  286. }
  287. } else {
  288. // 同步标段信息
  289. await transaction.update(this.ctx.service.tender.tableName, { id: tenderId, ledger_times: times + 1, ledger_status: checkType});
  290. // 拷贝新一次审核流程列表
  291. const auditors = await this.getAllDataByCondition({
  292. where: { tender_id: tenderId, times },
  293. columns: ['tender_id', 'audit_order', 'audit_id'],
  294. });
  295. for (const a of auditors) {
  296. a.times = times + 1;
  297. a.status = auditConst.status.uncheck;
  298. }
  299. await transaction.insert(this.tableName, auditors);
  300. // 添加短信通知-审批退回提醒功能
  301. // const mobile_array = [];
  302. // const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  303. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  304. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  305. // const smsType = JSON.parse(smsUser1.sms_type);
  306. // if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  307. // mobile_array.push(smsUser1.auth_mobile);
  308. // }
  309. // }
  310. // for (const user of auditors) {
  311. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.audit_id);
  312. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  313. // const smsType = JSON.parse(smsUser.sms_type);
  314. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  315. // mobile_array.push(smsUser.auth_mobile);
  316. // }
  317. // }
  318. // }
  319. // if (mobile_array.length > 0) {
  320. // const sms = new SMS(this.ctx);
  321. // const tenderName = await sms.contentChange(tenderInfo.name);
  322. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  323. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  324. // const content = '【纵横计量支付】' + ptmsg + '台账审批退回,请登录系统处理。';
  325. // sms.send(mobile_array, content);
  326. // }
  327. const auditList = await this.getAuditors(tenderId, times);
  328. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id);
  329. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.TZ,
  330. // smsTypeConst.judge.result.toString(), '台账审批退回,请登录系统处理。');
  331. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ,
  332. smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, { status: SmsAliConst.status.back });
  333. // 添加到消息推送表
  334. const noticeContent = await this.getNoticeContent(audit.tender_id, pid)
  335. await transaction.insert('zh_notice', { pid, type: pushType.ledger, uid: this.ctx.tender.data.user_id, status: auditConst.status.checkNo, is_read: 0, content: noticeContent });
  336. }
  337. await transaction.commit();
  338. } catch (err) {
  339. await transaction.rollback();
  340. throw err;
  341. }
  342. }
  343. /**
  344. * 获取审核人需要审核的标段列表
  345. *
  346. * @param auditorId
  347. * @returns {Promise<*>}
  348. */
  349. async getAuditTender(auditorId) {
  350. 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` ' +
  351. 'FROM ?? AS la, ?? AS t ' +
  352. '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)))' +
  353. ' and la.`tender_id` = t.`id`';
  354. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  355. return await this.db.query(sql, sqlParam);
  356. }
  357. /**
  358. * 获取 某时间后 审批进度 更新的台账
  359. * @param {Integer} projectId - 项目id
  360. * @param {Integer} auditorId - 查询人id
  361. * @param {Date} noticeTime - 查询事件
  362. * @returns {Promise<*>}
  363. */
  364. async getNoticeTender(pid, uid, noticeTime) {
  365. // const sql = 'SELECT * FROM (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`, ' +
  366. // ' pa.name As `lu_name`, pa.role As `lu_role`, pa.company As `lu_company`' +
  367. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t ' +
  368. // ' LEFT JOIN ?? As la ON la.`tender_id` = t.`id`' +
  369. // ' LEFT JOIN ?? As pa ON la.`audit_id` = pa.`id`' +
  370. // ' WHERE la.`end_time` > ? and t.`project_id` = ?' +
  371. // ' ORDER By la.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id` ORDER BY new_t.`end_time`';
  372. // const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, this.tableName, this.ctx.service.projectAccount.tableName,
  373. // noticeTime, projectId];
  374. // return await this.db.query(sql, sqlParam);
  375. let notice = await this.db.select('zh_notice', {
  376. where: { pid, type: pushType.ledger, uid, is_read: 0 },
  377. });
  378. notice = notice.map(v => {
  379. const extra = JSON.parse(v.content)
  380. delete v.content
  381. return { ...v, ...extra }
  382. })
  383. return notice;
  384. }
  385. /**
  386. * 用于添加推送所需的content内容
  387. * @param {Number} id 台账id
  388. * @param {Number} pid 项目id
  389. */
  390. async getNoticeContent(id, pid) {
  391. const noticeSql = 'SELECT * FROM (SELECT ' +
  392. ' t.`id` As `tid`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  393. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  394. ' LEFT JOIN ?? As pa ON t.`user_id` = pa.`id`' +
  395. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  396. const noticeSqlParam = [this.ctx.service.tender.tableName, id, this.ctx.service.projectAccount.tableName, pid];
  397. const content = await this.db.query(noticeSql, noticeSqlParam);
  398. return content.length ? JSON.stringify(content[0]) : '';
  399. }
  400. }
  401. return LedgerAudit;
  402. };