revise_audit.js 18 KB

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