ledger_audit.js 57 KB

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