ledger_audit.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 =
  36. '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` ' +
  37. 'FROM ?? AS la, ?? AS pa ' +
  38. 'WHERE la.`tender_id` = ? and la.`audit_id` = ? and la.`times` = ?' +
  39. ' and la.`audit_id` = pa.`id`'
  40. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditorId, times]
  41. return await this.db.queryOne(sql, sqlParam)
  42. }
  43. /**
  44. * 获取标段审核人最后一位的名称
  45. *
  46. * @param {Number} tenderId - 标段id
  47. * @param {Number} auditorId - 审核人id
  48. * @param {Number} times - 第几次审批
  49. * @returns {Promise<*>}
  50. */
  51. async getStatusName(tenderId, times) {
  52. const sql = 'SELECT pa.`name` ' + 'FROM ?? AS la, ?? AS pa ' + 'WHERE la.`tender_id` = ?' + ' and la.`audit_id` = pa.`id` and la.`status` != ? ORDER BY la.`times` DESC, la.`audit_order` DESC'
  53. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.uncheck]
  54. return await this.db.queryOne(sql, sqlParam)
  55. }
  56. /**
  57. * 获取标段审核列表信息
  58. *
  59. * @param {Number} tenderId - 标段id
  60. * @param {Number} times - 第几次审批
  61. * @returns {Promise<*>}
  62. */
  63. async getAuditors(tenderId, times = 1) {
  64. const sql =
  65. '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` ' +
  66. '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 ' +
  67. '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`'
  68. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, tenderId, times, tenderId, times]
  69. const result = await this.db.query(sql, sqlParam)
  70. 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'
  71. const sqlParam2 = [this.tableName, tenderId, times]
  72. const count = await this.db.queryOne(sql2, sqlParam2)
  73. for (const i in result) {
  74. result[i].max_sort = count.num
  75. }
  76. return result
  77. }
  78. /**
  79. * 获取标段当前审核人
  80. *
  81. * @param {Number} tenderId - 标段id
  82. * @param {Number} times - 第几次审批
  83. * @returns {Promise<*>}
  84. */
  85. async getCurAuditor(tenderId, times = 1) {
  86. const sql =
  87. '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,
  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 }
  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, 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, {
  199. id: audit.id,
  200. status: auditConst.status.checking,
  201. begin_time: new Date()
  202. })
  203. await transaction.update(this.ctx.service.tender.tableName, {
  204. id: tenderId,
  205. ledger_status: auditConst.status.checking,
  206. total_price: sum.total_price,
  207. deal_tp: sum.deal_tp
  208. })
  209. // 添加短信通知-需要审批提醒功能
  210. // await this.ctx.helper.sendUserSms(audit.audit_id, smsTypeConst.const.TZ,
  211. // smsTypeConst.judge.approval.toString(), '台帐需要您审批。');
  212. await this.ctx.helper.sendAliSms(audit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check)
  213. await transaction.commit()
  214. } catch (err) {
  215. await transaction.rollback()
  216. throw err
  217. }
  218. return true
  219. }
  220. /**
  221. * 审批
  222. * @param {Number} tenderId - 标段id
  223. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  224. * @param {String} opinion 审批意见
  225. * @param {Number} times - 第几次审批
  226. * @returns {Promise<void>}
  227. */
  228. async check(tenderId, checkType, opinion, times = 1) {
  229. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  230. throw '提交数据错误'
  231. }
  232. const pid = this.ctx.session.sessionProject.id
  233. const transaction = await this.db.beginTransaction()
  234. try {
  235. // 整理当前流程审核人状态更新
  236. const time = new Date()
  237. const audit = await this.getDataByCondition({
  238. tender_id: tenderId,
  239. times,
  240. status: auditConst.status.checking
  241. })
  242. if (!audit) {
  243. throw '审核数据错误'
  244. }
  245. // 获取审核人列表
  246. const auditList = await this.getAuditors(tenderId, times)
  247. // 添加推送
  248. const noticeContent = await this.getNoticeContent(audit.tender_id, pid, audit.audit_id)
  249. const records = [
  250. {
  251. pid,
  252. type: pushType.ledger,
  253. uid: this.ctx.tender.data.user_id,
  254. status: auditConst.status.checked,
  255. content: noticeContent
  256. }
  257. ]
  258. auditList.forEach(audit => {
  259. records.push({
  260. pid,
  261. type: pushType.ledger,
  262. uid: audit.audit_id,
  263. status: auditConst.status.checked,
  264. content: noticeContent
  265. })
  266. })
  267. await transaction.insert('zh_notice', records)
  268. // 更新当前审核流程
  269. await transaction.update(this.tableName, { id: audit.id, status: checkType, opinion, end_time: time })
  270. if (checkType === auditConst.status.checked) {
  271. const nextAudit = await this.getDataByCondition({
  272. tender_id: tenderId,
  273. times,
  274. audit_order: audit.audit_order + 1
  275. })
  276. // 无下一审核人表示,审核结束
  277. if (nextAudit) {
  278. await transaction.update(this.tableName, {
  279. id: nextAudit.id,
  280. status: auditConst.status.checking,
  281. begin_time: time
  282. })
  283. // await this.ctx.helper.sendUserSms(nextAudit.audit_id, smsTypeConst.const.TZ,
  284. // smsTypeConst.judge.approval.toString(), '台帐需要您审批。');
  285. await this.ctx.helper.sendAliSms(nextAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check)
  286. } else {
  287. // 同步标段信息
  288. await transaction.update(this.ctx.service.tender.tableName, {
  289. id: tenderId,
  290. ledger_status: checkType
  291. })
  292. // 添加短信通知-审批通过提醒功能
  293. // const mobile_array = [];
  294. // const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  295. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  296. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  297. // const smsType = JSON.parse(smsUser1.sms_type);
  298. // if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  299. // mobile_array.push(smsUser1.auth_mobile);
  300. // }
  301. // }
  302. // const auditList = await this.getAuditors(tenderId, times);
  303. // for (const user of auditList) {
  304. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.audit_id);
  305. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  306. // const smsType = JSON.parse(smsUser.sms_type);
  307. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  308. // mobile_array.push(smsUser.auth_mobile);
  309. // }
  310. // }
  311. // }
  312. // if (mobile_array.length > 0) {
  313. // const sms = new SMS(this.ctx);
  314. // const tenderName = await sms.contentChange(tenderInfo.name);
  315. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  316. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  317. // const content = '【纵横计量支付】' + ptmsg + '台账审批通过,请登录系统处理。';
  318. // sms.send(mobile_array, content);
  319. // }
  320. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id)
  321. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.TZ,
  322. // smsTypeConst.judge.result.toString(), '台账审批通过,请登录系统处理。');
  323. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, { status: SmsAliConst.status.success })
  324. }
  325. } else {
  326. // 同步标段信息
  327. await transaction.update(this.ctx.service.tender.tableName, {
  328. id: tenderId,
  329. ledger_times: times + 1,
  330. ledger_status: checkType
  331. })
  332. // 拷贝新一次审核流程列表
  333. const auditors = await this.getAllDataByCondition({
  334. where: { tender_id: tenderId, times },
  335. columns: ['tender_id', 'audit_order', 'audit_id']
  336. })
  337. for (const a of auditors) {
  338. a.times = times + 1
  339. a.status = auditConst.status.uncheck
  340. }
  341. await transaction.insert(this.tableName, auditors)
  342. // 添加短信通知-审批退回提醒功能
  343. // const mobile_array = [];
  344. // const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  345. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  346. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  347. // const smsType = JSON.parse(smsUser1.sms_type);
  348. // if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  349. // mobile_array.push(smsUser1.auth_mobile);
  350. // }
  351. // }
  352. // for (const user of auditors) {
  353. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.audit_id);
  354. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  355. // const smsType = JSON.parse(smsUser.sms_type);
  356. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  357. // mobile_array.push(smsUser.auth_mobile);
  358. // }
  359. // }
  360. // }
  361. // if (mobile_array.length > 0) {
  362. // const sms = new SMS(this.ctx);
  363. // const tenderName = await sms.contentChange(tenderInfo.name);
  364. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  365. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  366. // const content = '【纵横计量支付】' + ptmsg + '台账审批退回,请登录系统处理。';
  367. // sms.send(mobile_array, content);
  368. // }
  369. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id)
  370. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.TZ,
  371. // smsTypeConst.judge.result.toString(), '台账审批退回,请登录系统处理。');
  372. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, { status: SmsAliConst.status.back })
  373. }
  374. await transaction.commit()
  375. } catch (err) {
  376. await transaction.rollback()
  377. throw err
  378. }
  379. }
  380. /**
  381. * 获取审核人需要审核的标段列表
  382. *
  383. * @param auditorId
  384. * @returns {Promise<*>}
  385. */
  386. async getAuditTender(auditorId) {
  387. const sql =
  388. '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` ' +
  389. 'FROM ?? AS la, ?? AS t ' +
  390. '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)))' +
  391. ' and la.`tender_id` = t.`id`'
  392. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo]
  393. return await this.db.query(sql, sqlParam)
  394. }
  395. /**
  396. * 获取 某时间后 审批进度 更新的台账
  397. * @param {Integer} pid - 项目id
  398. * @param {Integer} uid - 查询人id
  399. * @param {Date} noticeTime - 查询事件
  400. * @returns {Promise<*>}
  401. */
  402. async getNoticeTender(pid, uid, noticeTime) {
  403. // 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`, ' +
  404. // ' pa.name As `lu_name`, pa.role As `lu_role`, pa.company As `lu_company`' +
  405. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t ' +
  406. // ' LEFT JOIN ?? As la ON la.`tender_id` = t.`id`' +
  407. // ' LEFT JOIN ?? As pa ON la.`audit_id` = pa.`id`' +
  408. // ' WHERE la.`end_time` > ? and t.`project_id` = ?' +
  409. // ' ORDER By la.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id` ORDER BY new_t.`end_time`';
  410. // const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, this.tableName, this.ctx.service.projectAccount.tableName,
  411. // noticeTime, projectId];
  412. // return await this.db.query(sql, sqlParam);
  413. let notice = await this.db.select('zh_notice', {
  414. where: { pid, type: pushType.ledger, uid },
  415. orders: [['create_time', 'desc']],
  416. limit: 10,
  417. offset: 0
  418. })
  419. notice = notice.map(v => {
  420. const extra = JSON.parse(v.content)
  421. delete v.content
  422. return { ...v, ...extra }
  423. })
  424. return notice
  425. }
  426. /**
  427. * 用于添加推送所需的content内容
  428. * @param {Number} id 标段id
  429. * @param {Number} pid 项目id
  430. * @param {Number} uid 审核人id
  431. */
  432. async getNoticeContent(id, pid, uid) {
  433. const noticeSql =
  434. 'SELECT * FROM (SELECT ' +
  435. ' t.`id` As `tid`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  436. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  437. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  438. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`'
  439. const noticeSqlParam = [this.ctx.service.tender.tableName, id, this.ctx.service.projectAccount.tableName, uid, pid]
  440. const content = await this.db.query(noticeSql, noticeSqlParam)
  441. return content.length ? JSON.stringify(content[0]) : ''
  442. }
  443. }
  444. return LedgerAudit
  445. }