ledger_audit.js 54 KB

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