ledger_audit.js 23 KB

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