ledger_audit.js 59 KB

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