ledger_audit.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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} auditorId - 审核人id
  45. * @param {Number} times - 第几次审批
  46. * @returns {Promise<*>}
  47. */
  48. async getStatusName(tenderId) {
  49. const sql = 'SELECT pa.`name` ' +
  50. 'FROM ?? AS la, ?? AS pa ' +
  51. 'WHERE la.`tender_id` = ?' +
  52. ' and la.`audit_id` = pa.`id` and la.`status` != ? ORDER BY la.`id` 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 = '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` ' +
  65. '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 ' +
  66. '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`';
  67. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, tenderId, times, tenderId, times];
  68. const result = await this.db.query(sql, sqlParam);
  69. 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';
  70. const sqlParam2 = [this.tableName, tenderId, times];
  71. const count = await this.db.queryOne(sql2, sqlParam2);
  72. for (const i in result) {
  73. result[i].max_sort = count.num;
  74. }
  75. return result;
  76. }
  77. /**
  78. * 获取标段当前审核人
  79. *
  80. * @param {Number} tenderId - 标段id
  81. * @param {Number} times - 第几次审批
  82. * @returns {Promise<*>}
  83. */
  84. async getCurAuditor(tenderId, times = 1) {
  85. 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` ' +
  86. 'FROM ?? AS la, ?? AS pa ' +
  87. 'WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
  88. ' and la.`audit_id` = pa.`id`';
  89. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.checking, times];
  90. return await this.db.queryOne(sql, sqlParam);
  91. }
  92. /**
  93. * 获取最新审核顺序
  94. *
  95. * @param {Number} tenderId - 标段id
  96. * @param {Number} times - 第几次审批
  97. * @returns {Promise<number>}
  98. */
  99. async getNewOrder(tenderId, times = 1) {
  100. const sql = 'SELECT Max(??) As max_order FROM ?? Where `tender_id` = ? and `times` = ?';
  101. const sqlParam = ['audit_order', this.tableName, tenderId, times];
  102. const result = await this.db.queryOne(sql, sqlParam);
  103. return result && result.max_order ? result.max_order + 1 : 1;
  104. }
  105. /**
  106. * 新增审核人
  107. *
  108. * @param {Number} tenderId - 标段id
  109. * @param {Number} auditorId - 审核人id
  110. * @param {Number} times - 第几次审批
  111. * @returns {Promise<number>}
  112. */
  113. async addAuditor(tenderId, auditorId, times = 1) {
  114. const newOrder = await this.getNewOrder(tenderId, times);
  115. const data = {
  116. tender_id: tenderId,
  117. audit_id: auditorId,
  118. times: times,
  119. audit_order: newOrder,
  120. status: auditConst.status.uncheck,
  121. };
  122. const result = await this.db.insert(this.tableName, data);
  123. return result.effectRows = 1;
  124. }
  125. /**
  126. * 移除审核人时,同步其后审核人order
  127. * @param transaction - 事务
  128. * @param {Number} tenderId - 标段id
  129. * @param {Number} auditorId - 审核人id
  130. * @param {Number} times - 第几次审批
  131. * @returns {Promise<*>}
  132. * @private
  133. */
  134. async _syncOrderByDelete(transaction, tenderId, order, times) {
  135. this.initSqlBuilder();
  136. this.sqlBuilder.setAndWhere('tender_id', {
  137. value: tenderId,
  138. operate: '='
  139. });
  140. this.sqlBuilder.setAndWhere('audit_order', {
  141. value: order,
  142. operate: '>=',
  143. });
  144. this.sqlBuilder.setAndWhere('times', {
  145. value: times,
  146. operate: '=',
  147. });
  148. this.sqlBuilder.setUpdateData('audit_order', {
  149. value: 1,
  150. selfOperate: '-',
  151. });
  152. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  153. const data = await transaction.query(sql, sqlParam);
  154. return data;
  155. }
  156. /**
  157. * 移除审核人
  158. *
  159. * @param {Number} tenderId - 标段id
  160. * @param {Number} auditorId - 审核人id
  161. * @param {Number} times - 第几次审批
  162. * @returns {Promise<boolean>}
  163. */
  164. async deleteAuditor(tenderId, auditorId, times = 1) {
  165. const transaction = await this.db.beginTransaction();
  166. try {
  167. const condition = {tender_id: tenderId, audit_id: auditorId, times: times};
  168. const auditor = await this.getDataByCondition(condition);
  169. if (!auditor) {
  170. throw '该审核人不存在';
  171. }
  172. await this._syncOrderByDelete(transaction, tenderId, auditor.audit_order, times);
  173. await transaction.delete(this.tableName, condition);
  174. await transaction.commit();
  175. } catch(err) {
  176. await transaction.rollback();
  177. throw err;
  178. }
  179. return true;
  180. }
  181. /**
  182. * 开始审批
  183. *
  184. * @param {Number} tenderId - 标段id
  185. * @param {Number} times - 第几次审批
  186. * @returns {Promise<boolean>}
  187. */
  188. async start(tenderId, times = 1) {
  189. const audit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: 1});
  190. if (!audit) {
  191. throw '审核人信息错误';
  192. }
  193. const sum = await this.ctx.service.ledger.addUp({tender_id: tenderId/*, is_leaf: true*/});
  194. const transaction = await this.db.beginTransaction();
  195. try {
  196. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: new Date()});
  197. await transaction.update(this.ctx.service.tender.tableName, {
  198. id: tenderId, ledger_status: auditConst.status.checking,
  199. total_price: sum.total_price, deal_tp: sum.deal_tp,
  200. });
  201. // 添加短信通知-需要审批提醒功能
  202. const smsUser = await this.ctx.service.projectAccount.getDataById(audit.audit_id);
  203. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  204. const smsType = JSON.parse(smsUser.sms_type);
  205. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  206. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  207. const sms = new SMS(this.ctx);
  208. const tenderName = await sms.contentChange(tenderInfo.name);
  209. const content = '【纵横计量支付】' + tenderName + '台帐需要您审批。';
  210. sms.send(smsUser.auth_mobile, content);
  211. }
  212. }
  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 {Number} times - 第几次审批
  225. * @returns {Promise<void>}
  226. */
  227. async check(tenderId, checkType, opinion, times = 1) {
  228. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  229. throw '提交数据错误';
  230. }
  231. const transaction = await this.db.beginTransaction();
  232. try {
  233. // 整理当前流程审核人状态更新
  234. const time = new Date();
  235. const audit = await this.getDataByCondition({tender_id: tenderId, times: times, status: auditConst.status.checking});
  236. if (!audit) {
  237. throw '审核数据错误';
  238. }
  239. // 更新当前审核流程
  240. await transaction.update(this.tableName, {id: audit.id, status: checkType, opinion: opinion, end_time: time});
  241. if (checkType === auditConst.status.checked) {
  242. const nextAudit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: audit.audit_order + 1});
  243. // 无下一审核人表示,审核结束
  244. if (nextAudit) {
  245. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  246. // 添加短信通知-需要审批提醒功能
  247. const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.audit_id);
  248. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  249. const smsType = JSON.parse(smsUser.sms_type);
  250. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  251. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  252. const sms = new SMS(this.ctx);
  253. const tenderName = await sms.contentChange(tenderInfo.name);
  254. const content = '【纵横计量支付】' + tenderName + '台帐需要您审批。';
  255. sms.send(smsUser.auth_mobile, content);
  256. }
  257. }
  258. } else {
  259. // 同步标段信息
  260. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_status: checkType});
  261. // 添加短信通知-审批通过提醒功能
  262. const mobile_array = [];
  263. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  264. const smsUser1 = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  265. if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  266. const smsType = JSON.parse(smsUser1.sms_type);
  267. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  268. mobile_array.push(smsUser1.auth_mobile);
  269. }
  270. }
  271. const auditList = await this.getAuditors(tenderId, times);
  272. for (const user of auditList) {
  273. const smsUser = await this.ctx.service.projectAccount.getDataById(user.audit_id);
  274. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  275. const smsType = JSON.parse(smsUser.sms_type);
  276. if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  277. mobile_array.push(smsUser.auth_mobile);
  278. }
  279. }
  280. }
  281. if (mobile_array.length > 0) {
  282. const sms = new SMS(this.ctx);
  283. const tenderName = await sms.contentChange(tenderInfo.name);
  284. const content = '【纵横计量支付】' + tenderName + '台账审批通过,请登录系统处理。';
  285. sms.send(mobile_array, content);
  286. }
  287. }
  288. } else {
  289. // 同步标段信息
  290. await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_times: times+1, ledger_status: checkType});
  291. // 拷贝新一次审核流程列表
  292. const auditors = await this.getAllDataByCondition({
  293. where: {tender_id: tenderId, times: times},
  294. columns: ['tender_id', 'audit_order', 'audit_id']
  295. });
  296. for (const a of auditors) {
  297. a.times = times + 1;
  298. a.status = auditConst.status.uncheck;
  299. }
  300. await transaction.insert(this.tableName, auditors);
  301. // 添加短信通知-审批退回提醒功能
  302. const mobile_array = [];
  303. const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
  304. const smsUser1 = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
  305. if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  306. const smsType = JSON.parse(smsUser1.sms_type);
  307. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  308. mobile_array.push(smsUser1.auth_mobile);
  309. }
  310. }
  311. for (const user of auditors) {
  312. const smsUser = await this.ctx.service.projectAccount.getDataById(user.audit_id);
  313. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  314. const smsType = JSON.parse(smsUser.sms_type);
  315. if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  316. mobile_array.push(smsUser.auth_mobile);
  317. }
  318. }
  319. }
  320. if (mobile_array.length > 0) {
  321. const sms = new SMS(this.ctx);
  322. const tenderName = await sms.contentChange(tenderInfo.name);
  323. const content = '【纵横计量支付】' + tenderName + '台账审批退回,请登录系统处理。';
  324. sms.send(mobile_array, content);
  325. }
  326. }
  327. await transaction.commit();
  328. } catch (err) {
  329. await transaction.rollback();
  330. throw err;
  331. }
  332. }
  333. /**
  334. * 获取审核人需要审核的标段列表
  335. *
  336. * @param auditorId
  337. * @returns {Promise<*>}
  338. */
  339. async getAuditTender(auditorId) {
  340. 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` ' +
  341. 'FROM ?? AS la, ?? AS t ' +
  342. '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)))' +
  343. ' and la.`tender_id` = t.`id`';
  344. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  345. return await this.db.query(sql, sqlParam);
  346. }
  347. /**
  348. * 获取 某时间后 审批进度 更新的台账
  349. * @param {Integer} projectId - 项目id
  350. * @param {Integer} auditorId - 查询人id
  351. * @param {Date} noticeTime - 查询事件
  352. * @returns {Promise<*>}
  353. */
  354. async getNoticeTender(projectId, auditorId, noticeTime) {
  355. 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`, ' +
  356. ' pa.name As `lu_name`, pa.role As `lu_role`, pa.company As `lu_company`' +
  357. ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t ' +
  358. ' LEFT JOIN ?? As la ON la.`tender_id` = t.`id`' +
  359. ' LEFT JOIN ?? As pa ON la.`audit_id` = pa.`id`' +
  360. ' WHERE la.`end_time` > ? and t.`project_id` = ?' +
  361. ' ORDER By la.`end_time` DESC';
  362. const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, this.tableName, this.ctx.service.projectAccount.tableName,
  363. noticeTime, projectId];
  364. return await this.db.query(sql, sqlParam);
  365. }
  366. }
  367. return LedgerAudit;
  368. };