ledger_audit.js 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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 auditType = require('../const/audit').auditType;
  11. const smsTypeConst = require('../const/sms_type');
  12. const SMS = require('../lib/sms');
  13. const SmsAliConst = require('../const/sms_alitemplate');
  14. const wxConst = require('../const/wechat_template');
  15. const shenpiConst = require('../const/shenpi');
  16. const pushType = require('../const/audit').pushType;
  17. const pushOperate = require('../const/spec_3f').pushOperate;
  18. module.exports = app => {
  19. class LedgerAudit extends app.BaseService {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局变量
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. this.tableName = 'ledger_audit';
  29. }
  30. /**
  31. * 获取标段审核人信息
  32. *
  33. * @param {Number} tenderId - 标段id
  34. * @param {Number} auditorId - 审核人id
  35. * @param {Number} times - 第几次审批
  36. * @return {Promise<*>}
  37. */
  38. async getAuditor(tenderId, auditorId, times = 1) {
  39. const sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`audit_type`, la.`audit_ledger_id`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  40. ' FROM ?? AS la Left Join ?? AS pa ON la.`audit_id` = pa.`id`' +
  41. ' WHERE la.`tender_id` = ? and la.`audit_id` = ? and la.`times` = ?';
  42. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditorId, times];
  43. return await this.db.queryOne(sql, sqlParam);
  44. }
  45. async getAuditorByOrder(tenderId, order, times = 1) {
  46. const sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`audit_type`, la.`audit_ledger_id`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  47. ' FROM ?? AS la Left Join ?? AS pa ON la.`audit_id` = pa.`id`' +
  48. ' WHERE la.`tender_id` = ? and la.`audit_order` = ? and la.`times` = ?';
  49. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, order, times];
  50. return await this.db.queryOne(sql, sqlParam);
  51. }
  52. async getAuditorsByOrder(tenderId, order, times) {
  53. const sql =
  54. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  55. ' la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_ledger_id ' +
  56. ' FROM ' + this.tableName + ' AS la' +
  57. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`audit_id` = pa.`id`' +
  58. ' WHERE la.`tender_id` = ? and la.`audit_order` = ? and la.`times` = ?' +
  59. ' ORDER BY `audit_order` DESC';
  60. const sqlParam = [tenderId, order, times ? times: 1];
  61. return await this.db.query(sql, sqlParam);
  62. }
  63. async getLastestAuditor(tenderId, times, status) {
  64. const sql =
  65. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  66. ' la.`times`, la.`audit_order`, la.`audit_type`, la.`audit_ledger_id`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  67. ' FROM ' + this.tableName + ' AS la' +
  68. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`audit_id` = pa.`id`' +
  69. ' WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
  70. ' ORDER BY `audit_order` DESC';
  71. const sqlParam = [tenderId, status, times ? times : 1];
  72. return await this.db.queryOne(sql, sqlParam);
  73. }
  74. async getLastestAuditors(tenderId, times, status) {
  75. const sql =
  76. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.audit_type, la.audit_order, la.audit_ledger_id,' +
  77. ' la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  78. ' FROM ' + this.tableName + ' AS la' +
  79. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`audit_id` = pa.`id`' +
  80. ' WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
  81. ' ORDER BY `audit_order` DESC';
  82. const sqlParam = [tenderId, status, times ? times: 1];
  83. const result = await this.db.query(sql, sqlParam);
  84. if (result.length === 0) return [];
  85. return result.filter(x => { return x.audit_order === result[0].audit_order });
  86. }
  87. async getAuditorsByStatus(tenderId, status, times = 1) {
  88. let auditor = [];
  89. let sql = '';
  90. let sqlParam = '';
  91. let cur;
  92. switch (status) {
  93. case auditConst.status.checking:
  94. case auditConst.status.checked:
  95. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where tender_id = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`audit_order` DESC', [tenderId, times, status]);
  96. if (!cur) return [];
  97. sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.audit_order, la.audit_type, la.audit_ledger_id ' +
  98. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id` ' +
  99. ' WHERE la.`tender_id` = ? and la.`audit_order` = ? and la.`times` = ?';
  100. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, cur.audit_order, times];
  101. auditor = await this.db.query(sql, sqlParam);
  102. break;
  103. case auditConst.status.checkNo:
  104. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where tender_id = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`audit_order` DESC', [tenderId, parseInt(times) - 1, status]);
  105. if (!cur) return [];
  106. sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.audit_order, la.audit_type, la.audit_ledger_id ' +
  107. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id` ' +
  108. ' WHERE la.`tender_id` = ? and la.`audit_order` = ? and la.`times` = ?';
  109. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, cur.audit_order, parseInt(times) - 1];
  110. auditor = await this.db.query(sql, sqlParam);
  111. break;
  112. case auditConst.status.uncheck:
  113. default:
  114. break;
  115. }
  116. return auditor;
  117. }
  118. /**
  119. * 获取标段审核列表信息
  120. *
  121. * @param {Number} tenderId - 标段id
  122. * @param {Number} times - 第几次审批
  123. * @return {Promise<*>}
  124. */
  125. async getAuditors(tenderId, times = 1, order_sort = 'asc') {
  126. const sql = 'SELECT la.id, la.audit_id, la.times, la.audit_order, la.audit_type, la.audit_ledger_id, la.status, la.opinion, la.begin_time, la.end_time, ' +
  127. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pa.sign_path' +
  128. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.audit_id = pa.id` +
  129. ' WHERE la.tender_id = ? AND la.times = ?' +
  130. ' ORDER BY la.audit_order ' + order_sort;
  131. const sqlParam = [tenderId, times];
  132. const result = await this.db.query(sql, sqlParam);
  133. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  134. for (const i in result) {
  135. result[i].max_sort = max_sort;
  136. }
  137. return result;
  138. }
  139. /**
  140. * 获取标段当前审核人
  141. *
  142. * @param {Number} tenderId - 标段id
  143. * @param {Number} times - 第几次审批
  144. * @return {Promise<*>}
  145. */
  146. async getCurAuditor(tenderId, times = 1) {
  147. const sql =
  148. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`audit_type`, la.`audit_ledger_id`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  149. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  150. ' WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?';
  151. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.checking, times];
  152. return await this.db.queryOne(sql, sqlParam);
  153. }
  154. async getCurAuditors(tenderId, times = 1) {
  155. const sql =
  156. '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`, la.audit_type, la.audit_order, la.audit_ledger_id ' +
  157. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  158. ' WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?';
  159. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.checking, times];
  160. return await this.db.query(sql, sqlParam);
  161. }
  162. async getAuditorGroup(tenderId, times) {
  163. const auditors = await this.getAuditors(tenderId, times); // 全部参与的审批人
  164. return this.ctx.helper.groupAuditors(auditors, 'audit_order');
  165. }
  166. async getUserGroup(tenderId, times) {
  167. const group = await this.getAuditorGroup(tenderId, times);
  168. const sql =
  169. 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As tender_id, 0 As audit_order, 1 As audit_type' +
  170. ' FROM ' + this.ctx.service.tender.tableName + ' As t' +
  171. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  172. ' ON t.user_id = pa.id' +
  173. ' WHERE t.id = ?';
  174. const sqlParam = [times, tenderId, tenderId];
  175. const user = await this.db.queryOne(sql, sqlParam);
  176. group.unshift([ user ]);
  177. return group;
  178. }
  179. groupAuditorsUniq(group) {
  180. const helper = this.ctx.helper;
  181. const uniqGroup = [];
  182. for (const g of group) {
  183. const curAuditId = g.map(x => { return x.audit_id; });
  184. const sameGroup = uniqGroup.find(x => {
  185. if (!x) return false;
  186. if (x[0].audit_type !== g[0].audit_type) return false;
  187. const auditId = x.map(xa => { return xa.audit_id; });
  188. helper._.remove(auditId, function(a) { return curAuditId.indexOf(a) >= 0; });
  189. return auditId.length === 0;
  190. });
  191. if (!sameGroup) uniqGroup[g[0].audit_order] = g;
  192. }
  193. return uniqGroup.filter(x => { return !!x });
  194. }
  195. async getUniqUserGroup(tenderId, times) {
  196. const group = await this.getAuditorGroup(tenderId, times);
  197. // 台账因为可选原报人为审批人,这里有点不同,这里要先过滤再取原报插入
  198. const newGroup = this.groupAuditorsUniq(group);
  199. const sql =
  200. 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As tender_id, 0 As audit_order, 1 As audit_type' +
  201. ' FROM ' + this.ctx.service.tender.tableName + ' As t' +
  202. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  203. ' ON t.user_id = pa.id' +
  204. ' WHERE t.id = ?';
  205. const sqlParam = [times, tenderId, tenderId];
  206. const user = await this.db.queryOne(sql, sqlParam);
  207. newGroup.unshift([ user ]);
  208. return newGroup;
  209. }
  210. async getAuditorHistory(tenderId, times, reverse = false) {
  211. const history = [];
  212. if (times >= 1) {
  213. for (let i = 1; i <= times; i++) {
  214. const auditors = await this.getAuditors(tenderId, i);
  215. const group = this.ctx.helper.groupAuditors(auditors, 'audit_order');
  216. const historyGroup = [];
  217. let max_order = 0;
  218. for (const g of group) {
  219. const his = {
  220. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  221. audit_type: g[0].audit_type, order: g[0].audit_order,
  222. auditors: g
  223. };
  224. const curAuditId = g.map(x => { return x.audit_id; });
  225. const sameHis = historyGroup.find(x => {
  226. if (x.audit_type !== his.audit_type) return false;
  227. const auditId = x.auditors.map(xa => { return xa.audit_id; });
  228. this.ctx.helper._.remove(auditId, function(a) { return curAuditId.indexOf(a) >= 0; });
  229. return auditId.length === 0;
  230. });
  231. his.audit_order = sameHis ? sameHis.audit_order : his.order;
  232. if (!sameHis && his.audit_order > max_order) max_order = his.audit_order;
  233. if (his.audit_type === auditType.key.common) {
  234. his.name = g[0].name;
  235. } else {
  236. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  237. }
  238. his.is_final = his.audit_order === max_order;
  239. if (g[0].begin_time) {
  240. his.begin_time = g[0].begin_time;
  241. const beginTime = this.ctx.moment(g[0].begin_time);
  242. his.beginYear = beginTime.format('YYYY');
  243. his.beginDate = beginTime.format('MM-DD');
  244. his.beginTime = beginTime.format('HH:mm:ss');
  245. }
  246. let end_time;
  247. g.forEach(x => {
  248. if (x.status === auditConst.status.checkSkip) return;
  249. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  250. if (x.end_time && (!end_time || x.end_time > end_time)) {
  251. end_time = x.end_time;
  252. if (his.status !== auditConst.status.checking) his.status = x.status;
  253. }
  254. });
  255. if (end_time) {
  256. his.end_time = end_time;
  257. const endTime = this.ctx.moment(end_time);
  258. his.endYear = endTime.format('YYYY');
  259. his.endDate = endTime.format('MM-DD');
  260. his.endTime = endTime.format('HH:mm:ss');
  261. }
  262. historyGroup.push(his);
  263. }
  264. historyGroup.forEach(hg => {
  265. hg.is_final = hg.audit_order === max_order;
  266. });
  267. if (reverse) {
  268. history.push(historyGroup.reverse());
  269. } else {
  270. history.push(historyGroup);
  271. }
  272. }
  273. }
  274. return history;
  275. }
  276. async getUniqAuditor(tenderId, times) {
  277. const auditors = await this.getAuditors(tenderId, times); // 全部参与的审批人
  278. const result = [];
  279. auditors.forEach(x => {
  280. if (result.findIndex(r => { return x.audit_id === r.audit_id && x.audit_order === r.audit_order; }) < 0) {
  281. result.push(x);
  282. }
  283. });
  284. return result;
  285. }
  286. async loadLedgerUser(tender) {
  287. const status = auditConst.status;
  288. const accountId = this.ctx.session.sessionUser.accountId;
  289. tender.user = await this.ctx.service.projectAccount.getAccountInfoById(tender.user_id);
  290. tender.auditors = await this.getAuditors(tender.id, tender.ledger_times); // 全部参与的审批人
  291. tender.auditorIds = this._.map(tender.auditors, 'audit_id');
  292. tender.curAuditors = tender.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
  293. tender.curAuditorIds = this._.map(tender.curAuditors, 'audit_id');
  294. tender.flowAuditors = tender.curAuditors.length > 0 ? tender.auditors.filter(x => { return x.audit_order === tender.curAuditors[0].audit_order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  295. tender.flowAuditorIds = this._.map(tender.flowAuditors, 'audit_id');
  296. tender.nextAuditors = tender.curAuditors.length > 0 ? tender.auditors.filter(x => { return x.audit_order === tender.curAuditors[0].audit_order + 1; }) : [];
  297. tender.nextAuditorIds = this._.map(tender.nextAuditors, 'audit_id');
  298. tender.auditorGroups = this.ctx.helper.groupAuditors(tender.auditors, 'audit_order');
  299. tender.userGroups = this.groupAuditorsUniq(tender.auditorGroups);
  300. tender.userGroups.unshift([{
  301. audit_id: tender.user.id, order: 0, times: tender.ledger_times, audit_order: 0, audit_type: auditType.key.common,
  302. name: tender.user.name, role: tender.user.role, company: tender.user.company
  303. }]);
  304. tender.finalAuditorIds = tender.userGroups[tender.userGroups.length - 1].map(x => { return x.audit_id; });
  305. tender.relaAuditor = tender.auditors.find(x => { return x.audit_id === accountId });
  306. tender.assists = [];// await this.service.ledgerAuditAss.getData(tender); // 全部协同人
  307. tender.assists = tender.assists.filter(x => {
  308. return x.user_id === tender.user_id || tender.auditorIds.indexOf(x.user_id) >= 0;
  309. }); // 过滤无效协同人
  310. tender.userAssists = tender.assists.filter(x => { return x.user_id === tender.user_id; }); // 原报协同人
  311. tender.userAssistIds = this._.map(tender.userAssists, 'ass_user_id');
  312. tender.auditAssists = tender.assists.filter(x => { return x.user_id !== tender.user_id; }); // 审批协同人
  313. tender.auditAssistIds = this._.map(tender.auditAssists, 'ass_user_id');
  314. tender.relaAssists = tender.assists.filter(x => { return x.user_id === accountId }); // 登录人的协同人
  315. tender.userIds = tender.ledger_status === status.uncheck // 当前流程下全部参与人id
  316. ? [tender.user_id]
  317. : [tender.user_id, ...tender.userAssistIds, ...tender.auditorIds, ...tender.auditAssistIds];
  318. }
  319. async loadLedgerAuditViewData(tender) {
  320. const times = tender.ledger_status === auditConst.status.checkNo ? tender.ledger_times - 1 : tender.ledger_times;
  321. if (!tender.user) tender.user = await this.ctx.service.projectAccount.getAccountInfoById(tender.user_id);
  322. tender.auditHistory = await this.getAuditorHistory(tender.id, times);
  323. // 获取审批流程中左边列表
  324. if (tender.status === auditConst.status.checkNo && tender.user_id !== this.ctx.session.sessionUser.accountId) {
  325. const auditors = await this.getAuditors(tender.id, times); // 全部参与的审批人
  326. const auditorGroups = this.ctx.helper.groupAuditors(auditors, 'audit_order');
  327. tender.auditors2 = this.groupAuditorsUniq(auditorGroups);
  328. tender.auditors2.unshift([{
  329. audit_id: tender.user.id, order: 0, times: tender.ledger_times - 1, audit_order: 0, audit_type: auditType.key.common,
  330. name: tender.user.name, role: tender.user.role, company: tender.user.company
  331. }]);
  332. } else {
  333. tender.auditors2 = tender.userGroups;
  334. }
  335. if (tender.ledger_status === auditConst.status.uncheck || tender.ledger_status === auditConst.status.checkNo) {
  336. tender.auditorList = await this.getAuditors(tender.id, tender.ledger_times);
  337. }
  338. }
  339. /**
  340. * 获取标段审核人最后一位的名称
  341. *
  342. * @param {Number} tenderId - 标段id
  343. * @param {Number} auditorId - 审核人id
  344. * @param {Number} times - 第几次审批
  345. * @return {Promise<*>}
  346. */
  347. async getStatusName(tenderId, times) {
  348. const sql = 'SELECT pa.`name` FROM ?? AS la Left Join ?? AS pa ON la.`audit_id` = pa.`id`' +
  349. ' WHERE la.`tender_id` = ? and la.`status` != ? ORDER BY la.`times` DESC, la.`audit_order` DESC';
  350. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.uncheck];
  351. return await this.db.queryOne(sql, sqlParam);
  352. }
  353. async getFinalAuditGroup(tenderId, times = 1) {
  354. const sql =
  355. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`tender_id`, Max(la.`audit_order`) as max_order ' +
  356. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  357. ' WHERE la.`tender_id` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  358. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, times];
  359. const result = await this.db.query(sql, sqlParam);
  360. for (const r of result) {
  361. const auditor = await this.getDataByCondition({tender_id: tenderId, times: r.times, audit_order: r.max_order});
  362. r.status = auditor.status;
  363. r.opinion = auditor.opinion;
  364. r.begin_time = auditor.begin_time;
  365. r.end_time = auditor.end_time;
  366. }
  367. return result;
  368. }
  369. /**
  370. * 获取最新审核顺序
  371. *
  372. * @param {Number} tenderId - 标段id
  373. * @param {Number} times - 第几次审批
  374. * @return {Promise<number>}
  375. */
  376. async getNewOrder(tenderId, times = 1) {
  377. const sql = 'SELECT Max(??) As max_order FROM ?? Where `tender_id` = ? and `times` = ?';
  378. const sqlParam = ['audit_order', this.tableName, tenderId, times];
  379. const result = await this.db.queryOne(sql, sqlParam);
  380. return result && result.max_order ? result.max_order + 1 : 1;
  381. }
  382. /**
  383. * 新增审核人
  384. *
  385. * @param {Number} tenderId - 标段id
  386. * @param {Number} auditorId - 审核人id
  387. * @param {Number} times - 第几次审批
  388. * @return {Promise<number>}
  389. */
  390. async addAuditor(tenderId, auditorId, times = 1, is_gdzs = 0) {
  391. const transaction = await this.db.beginTransaction();
  392. try {
  393. let newOrder = await this.getNewOrder(tenderId, times);
  394. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  395. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  396. if (is_gdzs) await this._syncOrderByDelete(transaction, tenderId, newOrder, times, '+');
  397. const data = {
  398. tender_id: tenderId,
  399. audit_id: auditorId,
  400. times,
  401. audit_order: newOrder,
  402. status: auditConst.status.uncheck,
  403. };
  404. const result = await transaction.insert(this.tableName, data);
  405. await transaction.commit();
  406. return (result.effectRows = 1);
  407. } catch (err) {
  408. await transaction.rollback();
  409. throw err;
  410. }
  411. return false;
  412. }
  413. /**
  414. * 移除审核人时,同步其后审核人order
  415. * @param transaction - 事务
  416. * @param {Number} tenderId - 标段id
  417. * @param {Number} auditorId - 审核人id
  418. * @param {Number} times - 第几次审批
  419. * @return {Promise<*>}
  420. * @private
  421. */
  422. async _syncOrderByDelete(transaction, tenderId, order, times, selfOperate = '-') {
  423. this.initSqlBuilder();
  424. this.sqlBuilder.setAndWhere('tender_id', {
  425. value: tenderId,
  426. operate: '=',
  427. });
  428. this.sqlBuilder.setAndWhere('audit_order', {
  429. value: order,
  430. operate: '>=',
  431. });
  432. this.sqlBuilder.setAndWhere('times', {
  433. value: times,
  434. operate: '=',
  435. });
  436. this.sqlBuilder.setUpdateData('audit_order', {
  437. value: 1,
  438. selfOperate: selfOperate,
  439. });
  440. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  441. const data = await transaction.query(sql, sqlParam);
  442. return data;
  443. }
  444. /**
  445. * 移除审核人
  446. *
  447. * @param {Number} tenderId - 标段id
  448. * @param {Number} auditorId - 审核人id
  449. * @param {Number} times - 第几次审批
  450. * @return {Promise<boolean>}
  451. */
  452. async deleteAuditor(tenderId, auditorId, times = 1) {
  453. const transaction = await this.db.beginTransaction();
  454. try {
  455. const condition = { tender_id: tenderId, audit_id: auditorId, times };
  456. const auditor = await this.getDataByCondition(condition);
  457. if (!auditor) {
  458. throw '该审核人不存在';
  459. }
  460. await transaction.delete(this.tableName, { tender_id: tenderId, audit_order: auditor.audit_order, times});
  461. await this._syncOrderByDelete(transaction, tenderId, auditor.audit_order, times);
  462. await transaction.delete(this.tableName, condition);
  463. await transaction.commit();
  464. } catch (err) {
  465. await transaction.rollback();
  466. throw err;
  467. }
  468. return true;
  469. }
  470. /**
  471. * 开始审批
  472. *
  473. * @param {Number} tenderId - 标段id
  474. * @param {Number} times - 第几次审批
  475. * @return {Promise<boolean>}
  476. */
  477. async start(tenderId, times = 1) {
  478. const audits = await this.getAllDataByCondition({ where: { tender_id: tenderId, times, audit_order: 1 } });
  479. if (audits.length === 0) {
  480. if(this.ctx.tender.info.shenpi.ledger === shenpiConst.sp_status.gdspl) {
  481. throw '请联系管理员添加审批人';
  482. } else {
  483. throw '请先选择审批人,再上报数据';
  484. }
  485. }
  486. const sum = await this.ctx.service.ledger.addUp({ tender_id: tenderId /* , is_leaf: true*/ });
  487. // 拷贝备份台账数据
  488. const his_id = await this.ctx.service.ledgerHistory.backupLedgerHistory(this.ctx.tender.data);
  489. const transaction = await this.db.beginTransaction();
  490. try {
  491. const begin_time = new Date();
  492. const updateAuditData = audits.map(a => { return { id: a.id, status: auditConst.status.checking, begin_time }; });
  493. await transaction.updateRows(this.tableName, updateAuditData);
  494. await transaction.update(this.ctx.service.tender.tableName, {
  495. id: tenderId,
  496. ledger_status: auditConst.status.checking,
  497. total_price: sum.total_price,
  498. deal_tp: sum.deal_tp,
  499. his_id: his_id,
  500. });
  501. await this.ctx.service.tenderCache.updateLedgerCache4Start(transaction, tenderId, auditConst.status.checking, audits, sum);
  502. // 添加短信通知-需要审批提醒功能
  503. const auditorIds = audits.map(x => { return x.audit_id; });
  504. await this.ctx.helper.sendAliSms(auditorIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  505. // 微信模板通知
  506. const wechatData = {
  507. status: wxConst.status.check,
  508. tips: wxConst.tips.check,
  509. begin_time: Date.parse(new Date()),
  510. };
  511. await this.ctx.helper.sendWechat(auditorIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  512. for (const audit of audits) {
  513. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
  514. pid: this.ctx.session.sessionProject.id,
  515. tid: tenderId,
  516. uid: audit.audit_id,
  517. sp_type: 'ledger',
  518. sp_id: audit.id,
  519. table_name: this.tableName,
  520. template: wxConst.template.ledger,
  521. wx_data: wechatData,
  522. });
  523. }
  524. await transaction.commit();
  525. } catch (err) {
  526. await transaction.rollback();
  527. throw err;
  528. }
  529. return true;
  530. }
  531. async _checkNo(tenderId, opinion, times) {
  532. const accountId = this.ctx.session.sessionUser.accountId;
  533. const pid = this.ctx.session.sessionProject.id;
  534. const auditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, times, status: auditConst.status.checking } });
  535. if (auditors.length === 0) throw '审核数据错误';
  536. const selfAuditor = auditors.find(x => { return x.audit_id === accountId; });
  537. if (!selfAuditor) throw '当前标段您无权审批';
  538. const beginAuditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, audit_order: 1, times } });
  539. const time = new Date();
  540. const noticeContent = await this.getNoticeContent(selfAuditor.tender_id, pid, accountId, opinion);
  541. const defaultNoticeRecord = { pid, tid: selfAuditor.tender_id, type: pushType.ledger, status: auditConst.status.checkNo, content: noticeContent };
  542. const transaction = await this.db.beginTransaction();
  543. try {
  544. // 获取审核人列表,添加提醒
  545. const records = [{ uid: this.ctx.tender.data.user_id, ...defaultNoticeRecord } ];
  546. const auditList = await this.getAuditors(tenderId, times);
  547. auditList.forEach(audit => {
  548. records.push({ uid: audit.audit_id, ...defaultNoticeRecord});
  549. });
  550. await transaction.insert('zh_notice', records);
  551. const users = this._.uniq(this._.concat(this._.map(auditList, 'audit_id'), this.ctx.tender.data.user_id));
  552. // 更新当前审核流程
  553. const updateAuditData = auditors.map(x => {
  554. return {
  555. id: x.id,
  556. status: x.audit_id === selfAuditor.audit_id ? auditConst.status.checkNo : auditConst.status.checkSkip,
  557. opinion: x.audit_id === selfAuditor.audit_id ? opinion : '',
  558. end_time: x.audit_id === selfAuditor.audit_id ? time : null,
  559. };
  560. });
  561. await transaction.updateRows(this.tableName, updateAuditData);
  562. // 审批提醒
  563. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(auditors, 'id'));
  564. // 同步标段信息
  565. await transaction.update(this.ctx.service.tender.tableName, {
  566. id: tenderId, ledger_times: times + 1, ledger_status: auditConst.status.checkNo,
  567. });
  568. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checkNo, auditConst.status.checkNo, [{ audit_id: this.ctx.tender.data.user_id }]);
  569. // 拷贝新一次审核流程列表
  570. const orgAuditors = await this.getAllDataByCondition({
  571. where: { tender_id: tenderId, times },
  572. columns: ['tender_id', 'audit_order', 'audit_id', 'audit_type', 'audit_ledger_id'],
  573. });
  574. const insertAuditors = [];
  575. orgAuditors.forEach(x => {
  576. if (insertAuditors.find(y => { return y.audit_id === x.audit_id && y.audit_type === x.audit_type})) return;
  577. insertAuditors.push({ ...x, times: times + 1, status: auditConst.status.uncheck });
  578. });
  579. await transaction.insert(this.tableName, insertAuditors);
  580. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
  581. status: SmsAliConst.status.back,
  582. });
  583. // 微信模板通知
  584. const wechatData = {
  585. status: wxConst.status.back,
  586. tips: wxConst.tips.back,
  587. begin_time: Date.parse(beginAuditors[0].begin_time),
  588. };
  589. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
  590. await transaction.commit();
  591. } catch (err) {
  592. await transaction.rollback();
  593. throw err;
  594. }
  595. }
  596. async _checked(tenderId, opinion, times) {
  597. const accountId = this.ctx.session.sessionUser.accountId;
  598. const pid = this.ctx.session.sessionProject.id;
  599. const auditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, times, status: auditConst.status.checking } });
  600. if (auditors.length === 0) throw '审核数据错误';
  601. const selfAuditor = auditors.find(x => { return x.audit_id === accountId; });
  602. if (!selfAuditor) throw '当前标段您无权审批';
  603. const flowAuditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, times, audit_order: selfAuditor.audit_order } });
  604. const nextAuditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, times, audit_order: selfAuditor.audit_order + 1 } });
  605. const beginAuditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, audit_order: 1, times } });
  606. const time = new Date();
  607. const noticeContent = await this.getNoticeContent(selfAuditor.tender_id, pid, accountId, opinion);
  608. const defaultNoticeRecord = { pid, tid: selfAuditor.tender_id, type: pushType.ledger, status: auditConst.status.checked, content: noticeContent };
  609. const transaction = await this.db.beginTransaction();
  610. try {
  611. // 获取审核人列表,添加提醒
  612. const records = [{ uid: this.ctx.tender.data.user_id, ...defaultNoticeRecord } ];
  613. const auditList = await this.getAuditors(tenderId, times);
  614. auditList.forEach(audit => {
  615. records.push({ uid: audit.audit_id, ...defaultNoticeRecord});
  616. });
  617. await transaction.insert('zh_notice', records);
  618. const users = this._.uniq(this._.concat(this._.map(auditList, 'audit_id'), this.ctx.tender.data.user_id));
  619. // 更新当前审核流程
  620. await transaction.update(this.tableName, { id: selfAuditor.id, status: auditConst.status.checked, opinion, end_time: time });
  621. // 审批提醒
  622. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(auditors, 'id'));
  623. if (auditors.length === 1 || selfAuditor.audit_type === auditType.key.or) {
  624. // 或签更新他人审批状态
  625. if (selfAuditor.audit_type === auditType.key.or) {
  626. const updateOther = [];
  627. for (const audit of auditors) {
  628. if (audit.audit_id === selfAuditor.audit_id) continue;
  629. updateOther.push({ id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: time });
  630. }
  631. if (updateOther.length > 0) {
  632. await transaction.updateRows(this.tableName, updateOther);
  633. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, updateOther.map(x => { return x.id}));
  634. }
  635. }
  636. // 无下一审核人表示,审核结束
  637. if (nextAuditors.length > 0) {
  638. const nextAuditUpdateData = nextAuditors.map(x => { return {id: x.id, status: auditConst.status.checking, begin_time: time }; });
  639. await transaction.updateRows(this.tableName, nextAuditUpdateData);
  640. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checking, auditConst.status.checked, nextAuditors);
  641. const nextAuditorIds = nextAuditors.map(x => { return x.audit_id; });
  642. await this.ctx.helper.sendAliSms(nextAuditorIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  643. // 微信模板通知
  644. const wechatData = {
  645. status: wxConst.status.check,
  646. tips: wxConst.tips.check,
  647. begin_time: Date.parse(beginAuditors[0].begin_time),
  648. };
  649. await this.ctx.helper.sendWechat(nextAuditorIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  650. for (const audit of nextAuditors) {
  651. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
  652. pid: this.ctx.session.sessionProject.id,
  653. tid: tenderId,
  654. uid: audit.audit_id,
  655. sp_type: 'ledger',
  656. sp_id: audit.id,
  657. table_name: this.tableName,
  658. template: wxConst.template.ledger,
  659. wx_data: wechatData,
  660. });
  661. }
  662. } else {
  663. // 同步标段信息
  664. await transaction.update(this.ctx.service.tender.tableName, {
  665. id: tenderId, ledger_status: auditConst.status.checked,
  666. });
  667. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checked, auditConst.status.checked);
  668. // 审批通过,添加标记
  669. await this.ctx.service.tenderTag.saveTenderTag(tenderId, { ledger_time: time }, transaction);
  670. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
  671. status: SmsAliConst.status.success,
  672. });
  673. // 微信模板通知
  674. const wechatData = {
  675. status: wxConst.status.success,
  676. tips: wxConst.tips.success,
  677. begin_time: Date.parse(beginAuditors[0].begin_time),
  678. };
  679. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
  680. // 审批通过 - 检查三方特殊推送
  681. await this.ctx.service.specMsg.addLedgerMsg(transaction, pid, this.ctx.tender, pushOperate.ledger.checked);
  682. }
  683. }
  684. await transaction.commit();
  685. } catch (err) {
  686. await transaction.rollback();
  687. throw err;
  688. }
  689. }
  690. /**
  691. * 审批
  692. * @param {Number} tenderId - 标段id
  693. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  694. * @param {String} opinion 审批意见
  695. * @param {Number} times - 第几次审批
  696. * @return {Promise<void>}
  697. */
  698. async check(tenderId, checkType, opinion, times = 1) {
  699. switch (checkType) {
  700. case auditConst.status.checked: await this._checked(tenderId, opinion, times); break;
  701. case auditConst.status.checkNo: await this._checkNo(tenderId, opinion, times); break;
  702. default: throw '提交数据错误';
  703. }
  704. }
  705. /**
  706. * 重新审批
  707. * @param {Number} tenderId - 标段id
  708. * @param {Number} times - 第几次审批
  709. * @return {Promise<void>}
  710. */
  711. async checkAgain(tenderId, times = 1) {
  712. const time = new Date();
  713. const accountId = this.ctx.session.sessionUser.accountId;
  714. // 整理当前流程审核人状态更新
  715. const auditors = await this.getAllDataByCondition({
  716. where: { tender_id: tenderId, times },
  717. orders: [['audit_order', 'desc']],
  718. });
  719. if (auditors.length <= 0) throw '台账审核数据错误';
  720. const flowAuditors = auditors.filter(x => { return x.audit_order === auditors[0].audit_order; });
  721. const selfAudit = flowAuditors.find(x => { return x.audit_id === accountId; });
  722. if (!selfAudit) throw '当前台账您无权重审';
  723. const tender = this.ctx.service.tender.getDataById(tenderId);
  724. if (!tender) throw '标段数据错误';
  725. let otherAuditIds = [tender.user_id, ...auditors.map(x => { return x.audit_id })];
  726. otherAuditIds = this._.uniq(otherAuditIds).filter(x => { return x !== selfAudit.audit_id; });
  727. const checkAgainAuditor = flowAuditors.map(x => {
  728. return {
  729. tender_id: tenderId, times, audit_order: x.audit_order + 1, audit_id: x.audit_id,
  730. audit_type: x.audit_type, audit_ledger_id: x.audit_ledger_id,
  731. begin_time: time, end_time: time, opinion: '',
  732. status: x.audit_id === accountId ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  733. }
  734. });
  735. const checkingAuditor = flowAuditors.map(x => {
  736. return {
  737. tender_id: tenderId, times, audit_order: x.audit_order + 2, audit_id: x.audit_id,
  738. audit_type: x.audit_type, audit_ledger_id: x.audit_ledger_id,
  739. begin_time: time, end_time: null, opinion: '', status: auditConst.status.checking,
  740. };
  741. });
  742. const transaction = await this.db.beginTransaction();
  743. try {
  744. // 当前审批人2次添加至流程中
  745. await transaction.insert(this.tableName, checkAgainAuditor);
  746. const checking_result = await transaction.insert(this.tableName, checkingAuditor);
  747. // 同步标段信息
  748. await transaction.update(this.ctx.service.tender.tableName, {
  749. id: tenderId,
  750. ledger_status: auditConst.status.checking,
  751. });
  752. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checking, auditConst.status.checkAgain, checkingAuditor);
  753. if (otherAuditIds.length > 0) {
  754. await this.ctx.helper.sendAliSms(otherAuditIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  755. // 微信模板通知
  756. const wechatData = {
  757. status: wxConst.status.check,
  758. tips: wxConst.tips.check,
  759. begin_time: Date.parse(time),
  760. };
  761. await this.ctx.helper.sendWechat(selfAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  762. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
  763. pid: this.ctx.session.sessionProject.id,
  764. tid: this.ctx.tender.id,
  765. uid: selfAudit.audit_id,
  766. sp_type: 'ledger',
  767. sp_id: checking_result.insertId,
  768. table_name: this.tableName,
  769. template: wxConst.template.ledger,
  770. wx_data: wechatData,
  771. });
  772. }
  773. await transaction.commit();
  774. } catch (err) {
  775. await transaction.rollback();
  776. throw err;
  777. }
  778. }
  779. /**
  780. * 获取审核人需要审核的标段列表
  781. *
  782. * @param auditorId
  783. * @return {Promise<*>}
  784. */
  785. async getAuditTender(auditorId, spid = '') {
  786. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  787. const sql =
  788. '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`, t.`spid` ' +
  789. ' FROM ?? AS la Left Join ?? AS t ON la.`tender_id` = t.`id` ' +
  790. ' 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)))' + spSql +
  791. ' ORDER BY la.`begin_time` DESC';
  792. const sqlParam = [
  793. this.tableName,
  794. this.ctx.service.tender.tableName,
  795. auditorId,
  796. auditConst.status.checking,
  797. auditorId,
  798. auditConst.status.checkNo,
  799. auditConst.status.checkNo,
  800. ];
  801. return await this.db.query(sql, sqlParam);
  802. }
  803. /**
  804. * 获取审核人审核的次数
  805. *
  806. * @param auditorId
  807. * @return {Promise<*>}
  808. */
  809. async getCountByChecked(auditorId, spid = '') {
  810. if (spid) {
  811. const sql = 'SELECT count(*) as count FROM ?? AS la Left Join ?? AS t ON la.`tender_id` = t.`id` WHERE la.`audit_id` = ? AND la.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') AND t.`spid` = ?';
  812. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, spid];
  813. const result = await this.db.queryOne(sql, sqlParam);
  814. return result ? result.count : 0;
  815. }
  816. return await this.db.count(this.tableName, { audit_id: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
  817. }
  818. /**
  819. * 获取最近一次审批结束时间
  820. *
  821. * @param auditorId
  822. * @return {Promise<*>}
  823. */
  824. async getLastEndTimeByChecked(auditorId, spid = '') {
  825. const sqSql = spid ? ' AND t.`spid` = "' + spid + '"' : '';
  826. const sql = 'SELECT a.`end_time` FROM ?? AS a LEFT JOIN ?? AS t ON a.`tender_id` = t.`id` WHERE a.`audit_id` = ? ' +
  827. 'AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ')' + sqSql +
  828. ' ORDER BY a.`end_time` DESC';
  829. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId];
  830. const result = await this.db.queryOne(sql, sqlParam);
  831. return result ? result.end_time : null;
  832. }
  833. /**
  834. * 用于添加推送所需的content内容
  835. * @param {Number} id 标段id
  836. * @param {Number} pid 项目id
  837. * @param {Number} uid 审核人id
  838. */
  839. async getNoticeContent(id, pid, uid, opinion = '') {
  840. const noticeSql =
  841. 'SELECT * FROM (SELECT ' +
  842. ' t.`id` As `tid`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  843. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  844. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  845. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  846. const noticeSqlParam = [this.ctx.service.tender.tableName, id, this.ctx.service.projectAccount.tableName, uid, pid];
  847. const content = await this.db.query(noticeSql, noticeSqlParam);
  848. if (content.length) {
  849. content[0].opinion = opinion;
  850. }
  851. return content.length ? JSON.stringify(content[0]) : '';
  852. }
  853. /**
  854. * 获取审核人流程列表
  855. * @param {Number} tender_id - 标段id
  856. * @param {Number} times 审核次数
  857. * @return {Promise<Array>} 查询结果集
  858. */
  859. async getAuditGroupByList(tender_id, times = 1, transaction = null) {
  860. // const sql =
  861. // 'SELECT la.`audit_id`, la.`status`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.`audit_order` ' +
  862. // ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  863. // ' WHERE la.`tender_id` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  864. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tender_id, times];
  865. // return transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  866. const sql =
  867. 'SELECT la.`audit_id`, la.`status`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.`audit_order`, la.`audit_type` ' +
  868. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  869. ' WHERE la.`tender_id` = ? and la.`times` = ? ORDER BY la.`audit_order` DESC';
  870. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tender_id, times];
  871. const result = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  872. const audits = [];
  873. for (const r of result) {
  874. if (audits.findIndex(a => a.audit_id === r.audit_id) === -1) {
  875. audits.push(r);
  876. }
  877. }
  878. return audits.reverse();
  879. }
  880. /**
  881. * 获取审核人流程列表(包括原报)
  882. * @param {Number} tender_id - 标段id
  883. * @param {Number} times 审核次数
  884. * @return {Promise<Array>} 查询结果集(包括原报)
  885. */
  886. async getAuditorsWithOwner(tender_id, times = 1) {
  887. const result = await this.getAuditGroupByList(tender_id, times);
  888. const sql =
  889. 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As tender_id, 0 As `audit_order`' +
  890. ' FROM ' + this.ctx.service.tender.tableName + ' As s' +
  891. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  892. ' ON s.user_id = pa.id' +
  893. ' WHERE s.id = ?';
  894. const sqlParam = [times, tender_id, tender_id];
  895. const user = await this.db.queryOne(sql, sqlParam);
  896. result.unshift(user);
  897. return result;
  898. }
  899. async updateNewAuditList(tender, newList) {
  900. const transaction = await this.db.beginTransaction();
  901. try {
  902. // 先删除旧的审批流,再添加新的
  903. await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times });
  904. const newAuditors = [];
  905. for (const auditor of newList) {
  906. newAuditors.push({
  907. tender_id: tender.id, audit_id: auditor.audit_id,
  908. times: tender.ledger_times, audit_order: auditor.audit_order, status: auditConst.status.uncheck,
  909. audit_type: auditor.audit_type, audit_ledger_id: auditor.audit_type === auditType.key.union ? auditor.audit_ledger_id : '',
  910. })
  911. }
  912. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  913. await transaction.commit();
  914. } catch (err) {
  915. await transaction.rollback();
  916. throw err;
  917. }
  918. }
  919. async updateLastAudit(tender, auditList, lastId) {
  920. const transaction = await this.db.beginTransaction();
  921. try {
  922. // 先判断auditList里的audit_id是否与lastId相同,相同则删除并重新更新order
  923. const existAudit = auditList.find(x => { return x.audit_id === lastId });
  924. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.audit_order)}, 0) + 1 : 1; // 最大值 + 1
  925. if (existAudit) {
  926. await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times, audit_id: lastId });
  927. const sameOrder = auditList.filter(x => { return x.audit_order === existAudit.audit_order });
  928. if (sameOrder.length === 1) {
  929. const updateData = [];
  930. auditList.forEach(x => {
  931. if (x.audit_order <= existAudit.audit_order) return;
  932. updateData.push({id: x.id, audit_order: x.audit_order - 1});
  933. });
  934. if (updateData.length > 0) {
  935. await transaction.updateRows(updateData);
  936. }
  937. order = order - 1;
  938. }
  939. }
  940. // 添加终审
  941. const newAuditor = {
  942. tender_id: tender.id, audit_id: lastId,
  943. times: tender.ledger_times, audit_order: order, status: auditConst.status.uncheck,
  944. };
  945. await transaction.insert(this.tableName, newAuditor);
  946. await transaction.commit();
  947. } catch (err) {
  948. await transaction.rollback();
  949. throw err;
  950. }
  951. }
  952. /**
  953. * 删除本次审批流程
  954. * @param {Number} tenderId - 标段id
  955. * @param {Number} times - 第几次审批
  956. * @param {Object} data - 更改参数
  957. * @return {Promise<void>}
  958. */
  959. async saveAudit(tenderId, times, sp_group, data) {
  960. const transaction = await this.db.beginTransaction();
  961. try {
  962. const auditors = await this.getAuditGroupByList(tenderId, times);
  963. const now_audit = this._.find(auditors, { audit_id: data.old_aid });
  964. if (data.operate !== 'del') {
  965. const exist = await this.getDataByCondition({ tender_id: tenderId, times, audit_id: data.new_aid });
  966. if (exist) throw '该审核人已存在,请勿重复添加';
  967. }
  968. if (data.operate === 'add') {
  969. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  970. throw '当前人下无法操作新增';
  971. }
  972. const newAudit = {
  973. tender_id: tenderId,
  974. audit_id: data.new_aid,
  975. audit_order: now_audit.audit_order + 1,
  976. audit_type: auditType.key.common,
  977. times,
  978. status: auditConst.status.uncheck,
  979. };
  980. // order+1
  981. await this._syncOrderByDelete(transaction, tenderId, now_audit.audit_order + 1, times, '+');
  982. await transaction.insert(this.tableName, newAudit);
  983. // 更新审批流程页数据,如果存在
  984. } else if (data.operate === 'add-sibling') {
  985. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  986. throw '当前人下无法操作新增';
  987. }
  988. const newAudit = {
  989. tender_id: tenderId,
  990. audit_id: data.new_aid,
  991. audit_order: now_audit.audit_order,
  992. audit_type: now_audit.audit_type,
  993. times: times,
  994. status: auditConst.status.uncheck,
  995. };
  996. await transaction.insert(this.tableName, newAudit);
  997. } else if (data.operate === 'del') {
  998. if (now_audit.status !== auditConst.status.uncheck) {
  999. throw '当前人无法操作删除';
  1000. }
  1001. const flowAuditors = auditors.filter(x => { return x.audit_order === now_audit.audit_order; });
  1002. await transaction.delete(this.tableName, { tender_id: tenderId, times, audit_id: now_audit.audit_id, audit_order: now_audit.audit_order });
  1003. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, tenderId, now_audit.audit_order, times);
  1004. } else if (data.operate === 'change') {
  1005. const nowAudit = await this.getDataByCondition({ tender_id: tenderId, times, audit_id: now_audit.audit_id, audit_order: now_audit.audit_order });
  1006. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1007. throw '当前人无法操作替换';
  1008. }
  1009. nowAudit.audit_id = data.new_aid;
  1010. await transaction.update(this.tableName, nowAudit);
  1011. }
  1012. if (this.ctx.tender.info.shenpi.ledger === shenpiConst.sp_status.gdspl) {
  1013. const newAuditors = await transaction.select(this.tableName, { where: { tender_id: tenderId, times } });
  1014. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'audit_order');
  1015. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1016. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.ledger, shenpiConst.sp_type.ledger, uniqNewAuditorGroup, sp_group);
  1017. } else if (this.ctx.tender.info.shenpi.ledger === shenpiConst.sp_status.gdzs) {
  1018. const newAuditors = await this.getAuditGroupByList(tenderId, times, transaction);
  1019. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.ledger, shenpiConst.sp_type.ledger, this._.map(newAuditors, 'audit_id'));
  1020. }
  1021. // 更新到审批流程方法
  1022. await transaction.commit();
  1023. } catch (err) {
  1024. await transaction.rollback();
  1025. throw err;
  1026. }
  1027. }
  1028. /**
  1029. * 获取审核人已经审核过的审批信息(包括退回,通过,重新审批等)
  1030. *
  1031. * @param auditorId
  1032. * @return {Promise<*>}
  1033. */
  1034. async getDonesByAudit(auditorId, spid = '') {
  1035. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  1036. const status = [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkAgain];
  1037. const sql =
  1038. 'SELECT la.`status`, la.`end_time` as `shenpi_time`, t.`id`, t.`name`, t.`spid` ' +
  1039. ' FROM ?? AS la Left Join ?? AS t ON la.`tender_id` = t.`id` ' +
  1040. ' WHERE la.`audit_id` = ? AND la.`status` in (' + this.ctx.helper.getInArrStrSqlFilter(status) +')' + spSql +
  1041. ' ORDER BY la.`end_time` DESC';
  1042. const sqlParam = [
  1043. this.tableName,
  1044. this.ctx.service.tender.tableName,
  1045. auditorId,
  1046. ];
  1047. return await this.db.query(sql, sqlParam);
  1048. }
  1049. }
  1050. return LedgerAudit;
  1051. };