ledger_audit.js 52 KB

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