revise_audit.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').revise;
  10. const smsTypeConst = require('../const/sms_type');
  11. const SMS = require('../lib/sms');
  12. module.exports = app => {
  13. class ReviseAudit extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'revise_audit';
  23. }
  24. /**
  25. * 获取标段审核人信息
  26. *
  27. * @param {Number} reviseId - 修订id
  28. * @param {Number} auditorId - 审核人id
  29. * @param {Number} times - 第几次审批
  30. * @returns {Promise<*>}
  31. */
  32. async getAuditor(reviseId, 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.`rid` = ? and la.`audit_id` = ? and la.`times` = ?' +
  36. ' and la.`audit_id` = pa.`id`';
  37. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, reviseId, auditorId, times];
  38. return await this.db.queryOne(sql, sqlParam);
  39. }
  40. /**
  41. * 获取标段审核列表信息
  42. *
  43. * @param {Number} reviseId - 修订id
  44. * @param {Number} times - 第几次审批
  45. * @returns {Promise<*>}
  46. */
  47. async getAuditors(reviseId, times = 1) {
  48. const sql = 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`,' +
  49. ' pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`' +
  50. ' FROM ' + this.tableName + ' AS la ' +
  51. ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id`' +
  52. ' ORDER BY la.`audit_order`';
  53. const sqlParam = [reviseId, times];
  54. return await this.db.query(sql, sqlParam);
  55. }
  56. /**
  57. * 获取标段当前审核人
  58. *
  59. * @param {Number} reviseId - 修订id
  60. * @param {Number} times - 第几次审批
  61. * @returns {Promise<*>}
  62. */
  63. async getCurAuditor(reviseId, 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` ' +
  65. 'FROM ?? AS la, ?? AS pa ' +
  66. 'WHERE la.`rid` = ? and la.`status` = ? and la.`times` = ?' +
  67. ' and la.`audit_id` = pa.`id`';
  68. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, reviseId, auditConst.status.checking, times];
  69. return await this.db.queryOne(sql, sqlParam);
  70. }
  71. /**
  72. * 获取最新审核顺序
  73. *
  74. * @param {Number} reviseId - 修订id
  75. * @param {Number} times - 第几次审批
  76. * @returns {Promise<number>}
  77. */
  78. async getNewOrder(reviseId, times = 1) {
  79. const sql = 'SELECT Max(??) As max_order FROM ?? Where `rid` = ? and `times` = ?';
  80. const sqlParam = ['audit_order', this.tableName, reviseId, times];
  81. const result = await this.db.queryOne(sql, sqlParam);
  82. return result && result.max_order ? result.max_order + 1 : 1;
  83. }
  84. /**
  85. * 新增审核人
  86. *
  87. * @param {Object} revise - 修订
  88. * @param {Number} auditorId - 审核人id
  89. * @param {Number} times - 第几次审批
  90. * @returns {Promise<number>}
  91. */
  92. async addAuditor(revise, auditorId) {
  93. const times = revise.times ? revise.times : 1;
  94. const newOrder = await this.getNewOrder(revise.id, times);
  95. const data = {
  96. tender_id: revise.tid,
  97. audit_id: auditorId,
  98. times: times,
  99. audit_order: newOrder,
  100. status: auditConst.status.uncheck,
  101. rid: revise.id,
  102. in_time: new Date(),
  103. };
  104. const result = await this.db.insert(this.tableName, data);
  105. return result.effectRows = 1;
  106. }
  107. /**
  108. * 移除审核人时,同步其后审核人order
  109. * @param transaction - 事务
  110. * @param {Number} reviseId - 修订id
  111. * @param {Number} auditorId - 审核人id
  112. * @param {Number} times - 第几次审批
  113. * @returns {Promise<*>}
  114. * @private
  115. */
  116. async _syncOrderByDelete(transaction, reviseId, order, times) {
  117. this.initSqlBuilder();
  118. this.sqlBuilder.setAndWhere('rid', {
  119. value: this.db.escape(reviseId),
  120. operate: '='
  121. });
  122. this.sqlBuilder.setAndWhere('audit_order', {
  123. value: order,
  124. operate: '>=',
  125. });
  126. this.sqlBuilder.setAndWhere('times', {
  127. value: times,
  128. operate: '=',
  129. });
  130. this.sqlBuilder.setUpdateData('audit_order', {
  131. value: 1,
  132. selfOperate: '-',
  133. });
  134. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  135. const data = await transaction.query(sql, sqlParam);
  136. return data;
  137. }
  138. /**
  139. * 移除审核人
  140. *
  141. * @param {Object} revise - 修订
  142. * @param {Number} auditorId - 审核人id
  143. * @param {Number} times - 第几次审批
  144. * @returns {Promise<boolean>}
  145. */
  146. async deleteAuditor(revise, auditorId) {
  147. const times = revise.times ? revise.times : 1;
  148. const transaction = await this.db.beginTransaction();
  149. try {
  150. const condition = {rid: revise.id, audit_id: auditorId, times: times};
  151. const auditor = await this.getDataByCondition(condition);
  152. if (!auditor) {
  153. throw '该审核人不存在';
  154. }
  155. await this._syncOrderByDelete(transaction, revise.id, auditor.audit_order, times);
  156. await transaction.delete(this.tableName, condition);
  157. await transaction.commit();
  158. } catch(err) {
  159. await transaction.rollback();
  160. throw err;
  161. }
  162. return true;
  163. }
  164. /**
  165. * 开始审批
  166. *
  167. * @param {Object} revise - 修订
  168. * @param {Number} times - 第几次审批
  169. * @returns {Promise<boolean>}
  170. */
  171. async start(revise, times = 1) {
  172. const audit = await this.getDataByCondition({rid: revise.id, times: times, audit_order: 1});
  173. if (!audit) throw '审核人信息错误';
  174. const time = new Date();
  175. const transaction = await this.db.beginTransaction();
  176. try {
  177. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: time});
  178. const reviseData = {
  179. id: revise.id, status: auditConst.status.checking,
  180. };
  181. if (revise.times === 1) {
  182. reviseData.begin_time = time;
  183. }
  184. await transaction.update(this.ctx.service.ledgerRevise.tableName, reviseData);
  185. // 添加短信通知-需要审批提醒功能
  186. const smsUser = await this.ctx.service.projectAccount.getDataById(audit.audit_id);
  187. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  188. const smsType = JSON.parse(smsUser.sms_type);
  189. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  190. const sms = new SMS(this.ctx);
  191. const tenderName = await sms.contentChange(this.ctx.tender.data.name);
  192. const content = '【纵横计量支付】' + tenderName + '台账修订需要您审批。';
  193. sms.send(smsUser.auth_mobile, content);
  194. }
  195. }
  196. await transaction.commit();
  197. } catch (err) {
  198. await transaction.rollback();
  199. throw err;
  200. }
  201. return true;
  202. }
  203. async _replaceLedgerByRevise(transaction, revise) {
  204. const sqlParam = [revise.tid];
  205. await transaction.delete(this.ctx.service.ledger.tableName, {tender_id: revise.tid});
  206. const bSql = 'Insert Into ' + this.ctx.service.ledger.tableName +
  207. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  208. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  209. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id)' +
  210. ' Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  211. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  212. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id' +
  213. ' From ' + this.ctx.service.reviseBills.tableName +
  214. ' Where `tender_id` = ?';
  215. await transaction.query(bSql, sqlParam);
  216. await transaction.delete(this.ctx.service.pos.tableName, {tid: revise.tid});
  217. const pSql = 'Insert Into ' + this.ctx.service.pos.tableName +
  218. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  219. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder)' +
  220. ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  221. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder' +
  222. ' From ' + this.ctx.service.revisePos.tableName +
  223. ' Where `tid` = ?';
  224. await transaction.query(pSql, sqlParam);
  225. }
  226. /**
  227. * 审批
  228. * @param {Object} revise - 修订
  229. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  230. * @param {String} opinion - 审批意见
  231. * @param {Number} times - 第几次审批
  232. * @returns {Promise<void>}
  233. */
  234. async check(revise, checkType, opinion, times = 1) {
  235. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) throw '提交数据错误';
  236. const audit = await this.getDataByCondition({rid: revise.id, times: times, status: auditConst.status.checking});
  237. if (!audit) throw '审核数据错误';
  238. const transaction = await this.db.beginTransaction();
  239. try {
  240. // 整理当前流程审核人状态更新
  241. const time = new Date();
  242. // 更新当前审核流程
  243. await transaction.update(this.tableName, {
  244. id: audit.id, status: checkType, opinion: opinion, end_time: time,
  245. bills_file: revise.bills_file, pos_file: revise.pos_file,
  246. });
  247. if (checkType === auditConst.status.checked) {
  248. const nextAudit = await this.getDataByCondition({rid: revise.id, times: times, audit_order: audit.audit_order + 1});
  249. // 无下一审核人表示,审核结束
  250. if (nextAudit) {
  251. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  252. // 添加短信通知-需要审批提醒功能
  253. const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.audit_id);
  254. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  255. const smsType = JSON.parse(smsUser.sms_type);
  256. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  257. const sms = new SMS(this.ctx);
  258. const tenderName = await sms.contentChange(this.ctx.tender.data.name);
  259. const content = '【纵横计量支付】' + tenderName + '台帐修订需要您审批。';
  260. sms.send(smsUser.auth_mobile, content);
  261. }
  262. }
  263. } else {
  264. // 同步修订信息
  265. await transaction.update(this.ctx.service.ledgerRevise.tableName, {id: revise.id, status: checkType, end_time: time});
  266. // 拷贝修订数据至台账
  267. await this._replaceLedgerByRevise(transaction, revise);
  268. const sum = await this.ctx.service.reviseBills.addUp({tender_id: revise.tid, is_leaf: true});
  269. await transaction.update(this.ctx.service.tender.tableName, {
  270. id: revise.tid, total_price: sum.total_price, deal_tp: sum.deal_tp
  271. });
  272. // 添加短信通知-审批通过提醒功能
  273. const smsUser = await this.ctx.service.projectAccount.getDataById(revise.uid);
  274. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  275. const smsType = JSON.parse(smsUser.sms_type);
  276. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  277. const sms = new SMS(this.ctx);
  278. const tenderName = await sms.contentChange(this.ctx.tender.data.name);
  279. const content = '【纵横计量支付】' + tenderName + '台账修订审批通过,请登录系统处理。';
  280. sms.send(smsUser.auth_mobile, content);
  281. }
  282. }
  283. }
  284. } else {
  285. // 同步修订信息
  286. await transaction.update(this.ctx.service.ledgerRevise.tableName, {id: revise.id, times: times+1, status: checkType});
  287. // 拷贝新一次审核流程列表
  288. const auditors = await this.getAllDataByCondition({
  289. where: {rid: revise.id, times: times},
  290. columns: ['tender_id', 'rid', 'audit_order', 'audit_id']
  291. });
  292. for (const a of auditors) {
  293. a.times = times + 1;
  294. a.status = auditConst.status.uncheck;
  295. a.in_time = time;
  296. }
  297. await transaction.insert(this.tableName, auditors);
  298. // 添加短信通知-审批退回提醒功能
  299. const smsUser = await this.ctx.service.projectAccount.getDataById(revise.uid);
  300. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  301. const smsType = JSON.parse(smsUser.sms_type);
  302. if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  303. const sms = new SMS(this.ctx);
  304. const tenderName = await sms.contentChange(this.ctx.tender.data.name);
  305. const content = '【纵横计量支付】' + tenderName + '台账修订审批退回,请登录系统处理。';
  306. sms.send(smsUser.auth_mobile, content);
  307. }
  308. }
  309. }
  310. await transaction.commit();
  311. } catch (err) {
  312. await transaction.rollback();
  313. throw err;
  314. }
  315. }
  316. /**
  317. * 获取审核人需要审核的标段列表
  318. *
  319. * @param auditorId
  320. * @returns {Promise<*>}
  321. */
  322. async getAuditRevise(auditorId) {
  323. const sql = 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`begin_time`, ra.`end_time`,' +
  324. ' r.id, r.corder, r.uid, r.status, r.content,' +
  325. ' t.id As t_id, t.`name` As t_name, t.`project_id` As t_pid, t.`type` As t_type, t.`user_id` As t_uid, t.`status` As t_status, ' +
  326. ' p.name As audit_name, p.role As audit_role, p.company As audit_company' +
  327. ' FROM ' + this.tableName + ' AS ra' +
  328. ' Left Join ' + this.ctx.service.ledgerRevise.tableName + ' As r On ra.rid = r.id' +
  329. ' Left Join '+ this.ctx.service.tender.tableName +' AS t On r.tid = t.id' +
  330. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' As p On ra.audit_id = p.id' +
  331. ' WHERE ((ra.`audit_id` = ? and ra.`status` = ?) OR' +
  332. ' (r.`uid` = ? and r.`status` = ? and ra.`status` = ? and ra.`times` = (r.`times`-1)))';
  333. const sqlParam = [auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  334. return await this.db.query(sql, sqlParam);
  335. }
  336. /**
  337. * 获取 某时间后 审批进度 更新的台账
  338. * @param {Integer} projectId - 项目id
  339. * @param {Integer} auditorId - 查询人id
  340. * @param {Date} noticeTime - 查询事件
  341. * @returns {Promise<*>}
  342. */
  343. async getNoticeRevise(projectId, auditorId, noticeTime) {
  344. const sql = 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`end_time`, ra.`status`,' +
  345. ' r.id, r.corder, r.uid, r.status, r.content, ' +
  346. ' t.`id` As t_id, t.`name` As t_name, t.`project_id` As t_pid, t.`type` As t_type, t.`user_id` As t_uid, ' +
  347. ' pa.name As `ru_name`, pa.role As `ru_role`, pa.company As `ru_company`' +
  348. ' FROM ' + this.tableName + ' As ra' +
  349. ' INNER JOIN ' + this.ctx.service.ledgerRevise.tableName + ' As r ON ra.audit_id <> ? and ra.end_time > ?' +
  350. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' As t ON r.`tid` = t.`id` and t.project_id = ?' +
  351. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON ra.`audit_id` = pa.`id`' +
  352. ' GROUP By t.`id`' +
  353. ' ORDER By ra.`end_time`';
  354. const sqlParam = [auditorId, noticeTime, projectId];
  355. return await this.db.query(sql, sqlParam);
  356. }
  357. }
  358. return ReviseAudit;
  359. };