ledger_audit.js 24 KB

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