ledger_audit.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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, ` + '`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, ` + '`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.`sid` = ? 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 = orgAuditors.map(x => {
  573. return { ...x, times: times + 1, status: auditConst.status.uncheck };
  574. });
  575. await transaction.insert(this.tableName, insertAuditors);
  576. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
  577. status: SmsAliConst.status.back,
  578. });
  579. // 微信模板通知
  580. const wechatData = {
  581. status: wxConst.status.back,
  582. tips: wxConst.tips.back,
  583. begin_time: Date.parse(beginAuditors[0].begin_time),
  584. };
  585. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
  586. await transaction.commit();
  587. } catch (err) {
  588. await transaction.rollback();
  589. throw err;
  590. }
  591. }
  592. async _checked(tenderId, opinion, times) {
  593. const accountId = this.ctx.session.sessionUser.accountId;
  594. const pid = this.ctx.session.sessionProject.id;
  595. const auditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, times, status: auditConst.status.checking } });
  596. if (auditors.length === 0) throw '审核数据错误';
  597. const selfAuditor = auditors.find(x => { return x.audit_id === accountId; });
  598. if (!selfAuditor) throw '当前标段您无权审批';
  599. const flowAuditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, times, audit_order: selfAuditor.audit_order } });
  600. const nextAuditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, times, audit_order: selfAuditor.audit_order + 1 } });
  601. const beginAuditors = await this.getAllDataByCondition({ where: { tender_id: tenderId, audit_order: 1, times } });
  602. const time = new Date();
  603. const noticeContent = await this.getNoticeContent(selfAuditor.tender_id, pid, accountId, opinion);
  604. const defaultNoticeRecord = { pid, type: pushType.ledger, status: auditConst.status.checked, content: noticeContent };
  605. const transaction = await this.db.beginTransaction();
  606. try {
  607. // 获取审核人列表,添加提醒
  608. const records = [{ uid: this.ctx.tender.data.user_id, ...defaultNoticeRecord } ];
  609. const auditList = await this.getAuditors(tenderId, times);
  610. auditList.forEach(audit => {
  611. records.push({ uid: audit.audit_id, ...defaultNoticeRecord});
  612. });
  613. await transaction.insert('zh_notice', records);
  614. const users = this._.uniq(this._.concat(this._.map(auditList, 'audit_id'), this.ctx.tender.data.user_id));
  615. // 更新当前审核流程
  616. await transaction.update(this.tableName, { id: selfAuditor.id, status: auditConst.status.checked, opinion, end_time: time });
  617. // 审批提醒
  618. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(auditors, 'id'));
  619. if (auditors.length === 1 || selfAuditor.audit_type === auditType.key.or) {
  620. // 或签更新他人审批状态
  621. if (selfAuditor.audit_type === auditType.key.or) {
  622. const updateOther = [];
  623. for (const audit of auditors) {
  624. if (audit.audit_id === selfAuditor.audit_id) continue;
  625. updateOther.push({ id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: time });
  626. }
  627. if (updateOther.length > 0) {
  628. await transaction.updateRows(this.tableName, updateOther);
  629. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, updateOther.map(x => { return x.id}));
  630. }
  631. }
  632. // 无下一审核人表示,审核结束
  633. if (nextAuditors.length > 0) {
  634. const nextAuditUpdateData = nextAuditors.map(x => { return {id: x.id, status: auditConst.status.checking, begin_time: time }; });
  635. await transaction.updateRows(this.tableName, nextAuditUpdateData);
  636. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checking, auditConst.status.checked, nextAuditors);
  637. const nextAuditorIds = nextAuditors.map(x => { return x.audit_id; });
  638. await this.ctx.helper.sendAliSms(nextAuditorIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  639. // 微信模板通知
  640. const wechatData = {
  641. status: wxConst.status.check,
  642. tips: wxConst.tips.check,
  643. begin_time: Date.parse(beginAuditors[0].begin_time),
  644. };
  645. await this.ctx.helper.sendWechat(nextAuditorIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  646. for (const audit of nextAuditors) {
  647. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
  648. pid: this.ctx.session.sessionProject.id,
  649. tid: tenderId,
  650. uid: audit.audit_id,
  651. sp_type: 'ledger',
  652. sp_id: audit.id,
  653. table_name: this.tableName,
  654. template: wxConst.template.ledger,
  655. wx_data: wechatData,
  656. });
  657. }
  658. } else {
  659. // 同步标段信息
  660. await transaction.update(this.ctx.service.tender.tableName, {
  661. id: tenderId, ledger_status: auditConst.status.checked,
  662. });
  663. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checked, auditConst.status.checked);
  664. // 审批通过,添加标记
  665. await this.ctx.service.tenderTag.saveTenderTag(tenderId, { ledger_time: time }, transaction);
  666. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
  667. status: SmsAliConst.status.success,
  668. });
  669. // 微信模板通知
  670. const wechatData = {
  671. status: wxConst.status.success,
  672. tips: wxConst.tips.success,
  673. begin_time: Date.parse(beginAuditors[0].begin_time),
  674. };
  675. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
  676. // 审批通过 - 检查三方特殊推送
  677. await this.ctx.service.specMsg.addLedgerMsg(transaction, pid, this.ctx.tender, pushOperate.ledger.checked);
  678. }
  679. }
  680. await transaction.commit();
  681. } catch (err) {
  682. await transaction.rollback();
  683. throw err;
  684. }
  685. }
  686. /**
  687. * 审批
  688. * @param {Number} tenderId - 标段id
  689. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  690. * @param {String} opinion 审批意见
  691. * @param {Number} times - 第几次审批
  692. * @return {Promise<void>}
  693. */
  694. async check(tenderId, checkType, opinion, times = 1) {
  695. switch (checkType) {
  696. case auditConst.status.checked: await this._checked(tenderId, opinion, times); break;
  697. case auditConst.status.checkNo: await this._checkNo(tenderId, opinion, times); break;
  698. default: throw '提交数据错误';
  699. }
  700. }
  701. /**
  702. * 重新审批
  703. * @param {Number} tenderId - 标段id
  704. * @param {Number} times - 第几次审批
  705. * @return {Promise<void>}
  706. */
  707. async checkAgain(tenderId, times = 1) {
  708. const time = new Date();
  709. const accountId = this.ctx.session.sessionUser.accountId;
  710. // 整理当前流程审核人状态更新
  711. const auditors = await this.getAllDataByCondition({
  712. where: { tender_id: tenderId, times },
  713. orders: [['audit_order', 'desc']],
  714. });
  715. if (auditors.length <= 0) throw '台账审核数据错误';
  716. const flowAuditors = auditors.filter(x => { return x.audit_order === auditors[0].audit_order; });
  717. const selfAudit = flowAuditors.find(x => { return x.audit_id === accountId; });
  718. if (!selfAudit) throw '当前台账您无权重审';
  719. const tender = this.ctx.service.tender.getDataById(tenderId);
  720. if (!tender) throw '标段数据错误';
  721. let otherAuditIds = [tender.user_id, ...auditors.map(x => { return x.audit_id })];
  722. otherAuditIds = this._.uniq(otherAuditIds).filter(x => { return x !== selfAudit.audit_id; });
  723. const checkAgainAuditor = flowAuditors.map(x => {
  724. return {
  725. tender_id: tenderId, times, audit_order: x.audit_order + 1, audit_id: x.audit_id,
  726. audit_type: x.audit_type, audit_ledger_id: x.audit_ledger_id,
  727. begin_time: time, end_time: time, opinion: '',
  728. status: x.audit_id === accountId ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  729. }
  730. });
  731. const checkingAuditor = flowAuditors.map(x => {
  732. return {
  733. tender_id: tenderId, times, audit_order: x.audit_order + 2, audit_id: x.audit_id,
  734. audit_type: x.audit_type, audit_ledger_id: x.audit_ledger_id,
  735. begin_time: time, end_time: null, opinion: '', status: auditConst.status.checking,
  736. };
  737. });
  738. const transaction = await this.db.beginTransaction();
  739. try {
  740. // 当前审批人2次添加至流程中
  741. await transaction.insert(this.tableName, checkAgainAuditor);
  742. const checking_result = await transaction.insert(this.tableName, checkingAuditor);
  743. // 同步标段信息
  744. await transaction.update(this.ctx.service.tender.tableName, {
  745. id: tenderId,
  746. ledger_status: auditConst.status.checking,
  747. });
  748. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checking, auditConst.status.checkAgain, checkingAuditor);
  749. if (otherAuditIds.length > 0) {
  750. await this.ctx.helper.sendAliSms(otherAuditIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  751. // 微信模板通知
  752. const wechatData = {
  753. status: wxConst.status.check,
  754. tips: wxConst.tips.check,
  755. begin_time: Date.parse(time),
  756. };
  757. await this.ctx.helper.sendWechat(selfAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  758. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
  759. pid: this.ctx.session.sessionProject.id,
  760. tid: this.ctx.tender.id,
  761. uid: selfAudit.audit_id,
  762. sp_type: 'ledger',
  763. sp_id: checking_result.insertId,
  764. table_name: this.tableName,
  765. template: wxConst.template.ledger,
  766. wx_data: wechatData,
  767. });
  768. }
  769. await transaction.commit();
  770. } catch (err) {
  771. await transaction.rollback();
  772. throw err;
  773. }
  774. }
  775. /**
  776. * 获取审核人需要审核的标段列表
  777. *
  778. * @param auditorId
  779. * @return {Promise<*>}
  780. */
  781. async getAuditTender(auditorId) {
  782. const sql =
  783. '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` ' +
  784. ' FROM ?? AS la Left Join ?? AS t ON la.`tender_id` = t.`id` ' +
  785. ' 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)))' +
  786. ' ORDER BY la.`begin_time` DESC';
  787. const sqlParam = [
  788. this.tableName,
  789. this.ctx.service.tender.tableName,
  790. auditorId,
  791. auditConst.status.checking,
  792. auditorId,
  793. auditConst.status.checkNo,
  794. auditConst.status.checkNo,
  795. ];
  796. return await this.db.query(sql, sqlParam);
  797. }
  798. /**
  799. * 获取审核人审核的次数
  800. *
  801. * @param auditorId
  802. * @return {Promise<*>}
  803. */
  804. async getCountByChecked(auditorId) {
  805. return await this.db.count(this.tableName, { audit_id: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
  806. }
  807. /**
  808. * 获取最近一次审批结束时间
  809. *
  810. * @param auditorId
  811. * @return {Promise<*>}
  812. */
  813. async getLastEndTimeByChecked(auditorId) {
  814. const sql = 'SELECT `end_time` FROM ?? WHERE `audit_id` = ? ' +
  815. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  816. const sqlParam = [this.tableName, auditorId];
  817. const result = await this.db.queryOne(sql, sqlParam);
  818. return result ? result.end_time : null;
  819. }
  820. /**
  821. * 用于添加推送所需的content内容
  822. * @param {Number} id 标段id
  823. * @param {Number} pid 项目id
  824. * @param {Number} uid 审核人id
  825. */
  826. async getNoticeContent(id, pid, uid, opinion = '') {
  827. const noticeSql =
  828. 'SELECT * FROM (SELECT ' +
  829. ' t.`id` As `tid`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  830. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  831. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  832. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  833. const noticeSqlParam = [this.ctx.service.tender.tableName, id, this.ctx.service.projectAccount.tableName, uid, pid];
  834. const content = await this.db.query(noticeSql, noticeSqlParam);
  835. if (content.length) {
  836. content[0].opinion = opinion;
  837. }
  838. return content.length ? JSON.stringify(content[0]) : '';
  839. }
  840. /**
  841. * 获取审核人流程列表
  842. * @param {Number} tender_id - 标段id
  843. * @param {Number} times 审核次数
  844. * @return {Promise<Array>} 查询结果集
  845. */
  846. async getAuditGroupByList(tender_id, times = 1, transaction = null) {
  847. // const sql =
  848. // 'SELECT la.`audit_id`, la.`status`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.`audit_order` ' +
  849. // ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  850. // ' WHERE la.`tender_id` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  851. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tender_id, times];
  852. // return transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  853. const sql =
  854. 'SELECT la.`audit_id`, la.`status`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.`audit_order` ' +
  855. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  856. ' WHERE la.`tender_id` = ? and la.`times` = ? ORDER BY la.`audit_order` DESC';
  857. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tender_id, times];
  858. const result = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  859. const audits = [];
  860. for (const r of result) {
  861. if (audits.findIndex(a => a.audit_id === r.audit_id) === -1) {
  862. audits.push(r);
  863. }
  864. }
  865. return audits.reverse();
  866. }
  867. /**
  868. * 获取审核人流程列表(包括原报)
  869. * @param {Number} tender_id - 标段id
  870. * @param {Number} times 审核次数
  871. * @return {Promise<Array>} 查询结果集(包括原报)
  872. */
  873. async getAuditorsWithOwner(tender_id, times = 1) {
  874. const result = await this.getAuditGroupByList(tender_id, times);
  875. const sql =
  876. 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As tender_id, 0 As `audit_order`' +
  877. ' FROM ' + this.ctx.service.tender.tableName + ' As s' +
  878. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  879. ' ON s.user_id = pa.id' +
  880. ' WHERE s.id = ?';
  881. const sqlParam = [times, tender_id, tender_id];
  882. const user = await this.db.queryOne(sql, sqlParam);
  883. result.unshift(user);
  884. return result;
  885. }
  886. async updateNewAuditList(tender, newList) {
  887. const transaction = await this.db.beginTransaction();
  888. try {
  889. // 先删除旧的审批流,再添加新的
  890. await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times });
  891. const newAuditors = [];
  892. for (const auditor of newList) {
  893. newAuditors.push({
  894. tender_id: tender.id, audit_id: auditor.audit_id,
  895. times: tender.ledger_times, audit_order: auditor.audit_order, status: auditConst.status.uncheck,
  896. audit_type: auditor.audit_type, audit_ledger_id: auditor.audit_type === auditType.key.union ? auditor.audit_ledger_id : '',
  897. })
  898. }
  899. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  900. await transaction.commit();
  901. } catch (err) {
  902. await transaction.rollback();
  903. throw err;
  904. }
  905. }
  906. async updateLastAudit(tender, auditList, lastId) {
  907. const transaction = await this.db.beginTransaction();
  908. try {
  909. // 先判断auditList里的audit_id是否与lastId相同,相同则删除并重新更新order
  910. const existAudit = auditList.find(x => { return x.audit_id === lastId });
  911. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.audit_order)}, 0) + 1 : 1; // 最大值 + 1
  912. if (existAudit) {
  913. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, audit_id: lastId });
  914. const sameOrder = auditList.filter(x => { return x.audit_order === existAudit.audit_order });
  915. if (sameOrder.length === 1) {
  916. const updateData = [];
  917. auditList.forEach(x => {
  918. if (x.audit_order <= existAudit.audit_order) return;
  919. updateData.push({id: x.id, audit_order: x.audit_order - 1});
  920. });
  921. if (updateData.length > 0) {
  922. await transaction.updateRows(updateData);
  923. }
  924. order = order - 1;
  925. }
  926. }
  927. // 添加终审
  928. const newAuditor = {
  929. tender_id: tender.id, audit_id: lastId,
  930. times: tender.ledger_times, audit_order: order, status: auditConst.status.uncheck,
  931. };
  932. await transaction.insert(this.tableName, newAuditor);
  933. await transaction.commit();
  934. } catch (err) {
  935. await transaction.rollback();
  936. throw err;
  937. }
  938. }
  939. /**
  940. * 删除本次审批流程
  941. * @param {Number} tenderId - 标段id
  942. * @param {Number} times - 第几次审批
  943. * @param {Object} data - 更改参数
  944. * @return {Promise<void>}
  945. */
  946. async saveAudit(tenderId, times, data) {
  947. const transaction = await this.db.beginTransaction();
  948. try {
  949. const auditors = await this.getAuditGroupByList(tenderId, times);
  950. const now_audit = this._.find(auditors, { audit_id: data.old_aid });
  951. if (data.operate === 'add') {
  952. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  953. throw '当前人下无法操作新增';
  954. }
  955. const newAudit = {
  956. tender_id: tenderId,
  957. audit_id: data.new_aid,
  958. audit_order: now_audit.audit_order + 1,
  959. times,
  960. status: auditConst.status.uncheck,
  961. };
  962. // order+1
  963. await this._syncOrderByDelete(transaction, tenderId, now_audit.audit_order + 1, times, '+');
  964. await transaction.insert(this.tableName, newAudit);
  965. // 更新审批流程页数据,如果存在
  966. } else if (data.operate === 'del') {
  967. if (now_audit.status !== auditConst.status.uncheck) {
  968. throw '当前人无法操作删除';
  969. }
  970. await transaction.delete(this.tableName, { tender_id: tenderId, times, audit_id: now_audit.audit_id, audit_order: now_audit.audit_order });
  971. await this._syncOrderByDelete(transaction, tenderId, now_audit.audit_order, times);
  972. } else if (data.operate === 'change') {
  973. const nowAudit = await this.getDataByCondition({ tender_id: tenderId, times, audit_id: now_audit.audit_id, audit_order: now_audit.audit_order });
  974. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  975. throw '当前人无法操作替换';
  976. }
  977. nowAudit.audit_id = data.new_aid;
  978. await transaction.update(this.tableName, nowAudit);
  979. }
  980. if (this.ctx.tender.info.shenpi.ledger === shenpiConst.sp_status.gdspl || this.ctx.tender.info.shenpi.ledger === shenpiConst.sp_status.gdzs) {
  981. const newAuditors = await this.getAuditGroupByList(tenderId, times, transaction);
  982. 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'));
  983. }
  984. // 更新到审批流程方法
  985. await transaction.commit();
  986. } catch (err) {
  987. await transaction.rollback();
  988. throw err;
  989. }
  990. }
  991. }
  992. return LedgerAudit;
  993. };