ledger_audit.js 53 KB

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