change_project_audit.js 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').changeProject;
  10. const pushType = require('../const/audit').pushType;
  11. const pushOperate = require('../const/spec_3f').pushOperate;
  12. const shenpiConst = require('../const/shenpi');
  13. const smsTypeConst = require('../const/sms_type');
  14. const SMS = require('../lib/sms');
  15. const SmsAliConst = require('../const/sms_alitemplate');
  16. const wxConst = require('../const/wechat_template');
  17. const auditType = require('../const/audit').auditType;
  18. module.exports = app => {
  19. class ChangeProjectAudit extends app.BaseService {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局变量
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. this.tableName = 'change_project_audit';
  29. }
  30. /**
  31. * 获取 审核列表信息
  32. *
  33. * @param {Number} cpId - 变更立项id
  34. * @param {Number} times - 第几次审批
  35. * @return {Promise<*>}
  36. */
  37. async getAuditors(cpId, times = 1, order_sort = 'asc', noYB = false) {
  38. // const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
  39. // 'FROM ?? AS la, ?? AS pa, (SELECT t1.`aid`,(@i:=@i+1) as `sort` FROM (SELECT t.`aid`, t.`order` FROM (select `aid`, `order` from ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` LIMIT 200) t GROUP BY t.`aid` ORDER BY t.`order`) t1, (select @i:=0) as it) as g ' +
  40. // 'WHERE la.`cpid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
  41. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, cpId, times, cpId, times];
  42. // const result = await this.db.query(sql, sqlParam);
  43. // const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `cpid` = ? AND `times` = ? GROUP BY `aid`) as a';
  44. // const sqlParam2 = [this.tableName, cpId, times];
  45. // const count = await this.db.queryOne(sql2, sqlParam2);
  46. // for (const i in result) {
  47. // result[i].max_sort = count.num;
  48. // }
  49. const ybSql = noYB ? ' AND la.audit_order != 0' : '';
  50. const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
  51. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone' +
  52. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
  53. ' WHERE la.cpid = ? AND la.times = ?' + ybSql +
  54. ' ORDER BY la.order ' + order_sort;
  55. const sqlParam = [cpId, times];
  56. const result = await this.db.query(sql, sqlParam);
  57. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  58. for (const i in result) {
  59. result[i].max_sort = max_sort;
  60. }
  61. return result;
  62. }
  63. /**
  64. * 获取 当前审核人
  65. *
  66. * @param {Number} cpId - 变更立项id
  67. * @param {Number} times - 第几次审批
  68. * @return {Promise<*>}
  69. */
  70. async getCurAuditor(cpId, times = 1) {
  71. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  72. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  73. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?';
  74. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checking, times];
  75. return await this.db.queryOne(sql, sqlParam);
  76. }
  77. async getCurAuditors(cpId, times = 1) {
  78. const sql =
  79. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  80. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  81. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?';
  82. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checking, times];
  83. return await this.db.query(sql, sqlParam);
  84. }
  85. /**
  86. * 获取审核人流程列表
  87. *
  88. * @param auditorId
  89. * @return {Promise<*>}
  90. */
  91. async getAuditGroupByList(changeId, times, transaction = false) {
  92. const sql = 'SELECT pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  93. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  94. ' WHERE la.`cpid` = ? and la.`times` = ? and la.`status` != ? and la.`status` != ? GROUP BY la.`aid` ORDER BY la.`order`';
  95. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, changeId, times, auditConst.status.revise, auditConst.status.cancelRevise];
  96. // const sql =
  97. // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.audit_type, la.audit_order' +
  98. // ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `cpid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  99. // ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  100. // ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`cpid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  101. // const sqlParam = [this.tableName, changeId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, changeId, times, 0];
  102. return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  103. }
  104. /**
  105. * 移除审核人
  106. *
  107. * @param {Number} materialId - 材料调差期id
  108. * @param {Number} status - 期状态
  109. * @param {Number} status - 期次数
  110. * @return {Promise<boolean>}
  111. */
  112. async getAuditorByStatus(cpId, status, times = 1) {
  113. let auditor = null;
  114. let sql = '';
  115. let sqlParam = '';
  116. switch (status) {
  117. case auditConst.status.checking :
  118. case auditConst.status.checked :
  119. case auditConst.status.checkNo :
  120. case auditConst.status.revise :
  121. case auditConst.status.cancelRevise :
  122. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order` ' +
  123. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  124. ' WHERE la.`cpid` = ? and la.`status` = ? ' +
  125. ' ORDER BY la.`times` desc, la.`order` desc';
  126. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, status];
  127. auditor = await this.db.queryOne(sql, sqlParam);
  128. break;
  129. case auditConst.status.back :
  130. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order` ' +
  131. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  132. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?' +
  133. ' ORDER BY la.`times` desc, la.`order` desc';
  134. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.back, parseInt(times) - 1];
  135. auditor = await this.db.queryOne(sql, sqlParam);
  136. break;
  137. case auditConst.status.uncheck :
  138. break;
  139. default:break;
  140. }
  141. return auditor;
  142. }
  143. async getAuditorsByStatus(cpId, status, times = 1) {
  144. let auditor = [];
  145. let sql = '';
  146. let sqlParam = '';
  147. let cur;
  148. switch (status) {
  149. case auditConst.status.checking :
  150. case auditConst.status.checked :
  151. case auditConst.status.checkNo :
  152. case auditConst.status.revise :
  153. case auditConst.status.cancelRevise :
  154. cur = await this.db.queryOne('SELECT * From ?? where cpid = ? AND times = ? AND status = ? ORDER By times DESC, `order` DESC', [this.tableName, cpId, times, status]);
  155. if (!cur) return [];
  156. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.`audit_order`, la.`audit_type` ' +
  157. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  158. ' WHERE la.`cpid` = ? and la.`order` = ? and times = ?';
  159. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, cur.order, times];
  160. auditor = await this.db.query(sql, sqlParam);
  161. break;
  162. case auditConst.status.back :
  163. cur = await this.db.queryOne('SELECT * From ?? where cpid = ? AND times = ? AND status = ? ORDER By times DESC, `order` DESC', [this.tableName, cpId, parseInt(times) - 1, status]);
  164. if (!cur) return [];
  165. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.`audit_order`, la.`audit_type` ' +
  166. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  167. ' WHERE la.`cpid` = ? and la.`order` = ? and la.`times` = ?';
  168. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, cur.order, parseInt(times) - 1];
  169. auditor = await this.db.query(sql, sqlParam);
  170. break;
  171. case auditConst.status.uncheck:
  172. default:
  173. break;
  174. }
  175. return auditor;
  176. }
  177. async getLastAudit(cpid, times, transaction = null) {
  178. const sql = 'SELECT * FROM ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` DESC';
  179. const sqlParam = [this.tableName, cpid, times];
  180. return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
  181. }
  182. /**
  183. * 获取审核人流程列表(包括原报)
  184. * @param {Number} materialId 调差id
  185. * @param {Number} times 审核次数
  186. * @return {Promise<Array>} 查询结果集(包括原报)
  187. */
  188. async getAuditorsWithOwner(cpId, times = 1) {
  189. const result = await this.getAuditGroupByList(cpId, times);
  190. const sql =
  191. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`' +
  192. ' FROM ' +
  193. this.ctx.service.changeProject.tableName +
  194. ' As s' +
  195. ' LEFT JOIN ' +
  196. this.ctx.service.projectAccount.tableName +
  197. ' As pa' +
  198. ' ON s.uid = pa.id' +
  199. ' WHERE s.id = ?';
  200. const sqlParam = [times, cpId, cpId];
  201. const user = await this.db.queryOne(sql, sqlParam);
  202. result.unshift(user);
  203. return result;
  204. }
  205. /**
  206. * 新增审核人
  207. *
  208. * @param {Number} cpId - 立项书id
  209. * @param {Number} auditorId - 审核人id
  210. * @param {Number} times - 第几次审批
  211. * @return {Promise<number>}
  212. */
  213. async addAuditor(cpId, auditorId, times = 1, is_gdzs = 0) {
  214. const transaction = await this.db.beginTransaction();
  215. let flag = false;
  216. try {
  217. let [newOrder, newAuditOrder] = await this.getNewOrder(cpId, times);
  218. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  219. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  220. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  221. if (is_gdzs) await this._syncOrderByDelete(transaction, cpId, newOrder, times, '+');
  222. const data = {
  223. tid: this.ctx.tender.id,
  224. cpid: cpId,
  225. aid: auditorId,
  226. times,
  227. order: newOrder,
  228. status: auditConst.status.uncheck,
  229. audit_order: newAuditOrder,
  230. };
  231. const result = await transaction.insert(this.tableName, data);
  232. await transaction.commit();
  233. flag = result.effectRows = 1;
  234. } catch (err) {
  235. await transaction.rollback();
  236. throw err;
  237. }
  238. return flag;
  239. }
  240. /**
  241. * 获取 最新审核顺序
  242. *
  243. * @param {Number} cpId - 立项书id
  244. * @param {Number} times - 第几次审批
  245. * @return {Promise<number>}
  246. */
  247. async getNewOrder(cpId, times = 1) {
  248. const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ?? Where `cpid` = ? and `times` = ?';
  249. const sqlParam = [this.tableName, cpId, times];
  250. const result = await this.db.queryOne(sql, sqlParam);
  251. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  252. }
  253. /**
  254. * 移除审核人
  255. *
  256. * @param {Number} cpId - 变更立项书id
  257. * @param {Number} auditorId - 审核人id
  258. * @param {Number} times - 第几次审批
  259. * @return {Promise<boolean>}
  260. */
  261. async deleteAuditor(cpId, auditorId, times = 1) {
  262. const transaction = await this.db.beginTransaction();
  263. try {
  264. const condition = { cpid: cpId, aid: auditorId, times };
  265. const auditor = await this.getDataByCondition(condition);
  266. if (!auditor) {
  267. throw '该审核人不存在';
  268. }
  269. // 移除整个流程的人
  270. await transaction.delete(this.tableName, { cpid: cpId, order: auditor.order, times });
  271. await this._syncOrderByDelete(transaction, cpId, auditor.order, times);
  272. await transaction.commit();
  273. } catch (err) {
  274. await transaction.rollback();
  275. throw err;
  276. }
  277. return true;
  278. }
  279. /**
  280. * 移除审核人时,同步其后审核人order
  281. * @param transaction - 事务
  282. * @param {Number} cpId - 变更立项书id
  283. * @param {Number} auditorId - 审核人id
  284. * @param {Number} times - 第几次审批
  285. * @return {Promise<*>}
  286. * @private
  287. */
  288. async _syncOrderByDelete(transaction, cpId, order, times, selfOperate = '-') {
  289. this.initSqlBuilder();
  290. this.sqlBuilder.setAndWhere('cpid', {
  291. value: cpId,
  292. operate: '=',
  293. });
  294. this.sqlBuilder.setAndWhere('order', {
  295. value: order,
  296. operate: '>=',
  297. });
  298. this.sqlBuilder.setAndWhere('times', {
  299. value: times,
  300. operate: '=',
  301. });
  302. this.sqlBuilder.setUpdateData('order', {
  303. value: 1,
  304. selfOperate,
  305. });
  306. this.sqlBuilder.setUpdateData('audit_order', {
  307. value: 1,
  308. selfOperate,
  309. });
  310. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  311. const data = await transaction.query(sql, sqlParam);
  312. return data;
  313. }
  314. /**
  315. * 开始审批
  316. * @param {Number} cpId - 立项书id
  317. * @param {Number} times - 第几次审批
  318. * @return {Promise<boolean>}
  319. */
  320. async start(cpId, times = 1) {
  321. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: 1 } });
  322. if (audits.length === 0) {
  323. if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  324. throw '请联系管理员添加审批人';
  325. } else {
  326. throw '请先选择审批人,再上报数据';
  327. }
  328. }
  329. const transaction = await this.db.beginTransaction();
  330. try {
  331. const begin_time = new Date();
  332. const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time }; });
  333. await transaction.updateRows(this.tableName, updateData);
  334. await transaction.update(this.ctx.service.changeProject.tableName, {
  335. id: cpId, status: auditConst.status.checking,
  336. });
  337. // 微信模板通知
  338. const wechatData = {
  339. type: 'project',
  340. status: wxConst.status.check,
  341. tips: wxConst.tips.check,
  342. code: this.ctx.session.sessionProject.code,
  343. c_name: this.ctx.change.name,
  344. };
  345. await this.ctx.helper.sendWechat(this._.map(audits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  346. for (const audit of audits) {
  347. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  348. pid: this.ctx.session.sessionProject.id,
  349. tid: this.ctx.tender.id,
  350. uid: audit.aid,
  351. sp_type: 'change',
  352. sp_id: audit.id,
  353. table_name: this.tableName,
  354. template: wxConst.template.change,
  355. wx_data: wechatData,
  356. });
  357. }
  358. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: cpId });
  359. // todo 更新标段tender状态 ?
  360. // 检查三方特殊推送
  361. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_project.flow);
  362. await transaction.commit();
  363. } catch (err) {
  364. await transaction.rollback();
  365. throw err;
  366. }
  367. return true;
  368. }
  369. /**
  370. * 获取审核人需要审核的期列表
  371. *
  372. * @param auditorId
  373. * @return {Promise<*>}
  374. */
  375. async getAuditChangeProject(auditorId) {
  376. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`cpid`,' +
  377. ' m.`status` As `mstatus`, m.`code` As `mcode`,' +
  378. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  379. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  380. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  381. ' and ma.`cpid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  382. const sqlParam = [this.tableName, this.ctx.service.changeProject.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.back, auditConst.status.back];
  383. const result = await this.db.query(sql, sqlParam);
  384. // 过滤result中存在重复cpid的值, 保留最新的一条
  385. const filterResult = [];
  386. const cpidArr = [];
  387. for (const r of result) {
  388. if (cpidArr.indexOf(r.cpid) === -1) {
  389. filterResult.push(r);
  390. cpidArr.push(r.cpid);
  391. }
  392. }
  393. return filterResult;
  394. }
  395. /**
  396. * 获取审核人审核的次数
  397. *
  398. * @param auditorId
  399. * @return {Promise<*>}
  400. */
  401. async getCountByChecked(auditorId) {
  402. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.back, auditConst.status.checkNo] });
  403. }
  404. /**
  405. * 获取最近一次审批结束时间
  406. *
  407. * @param auditorId
  408. * @return {Promise<*>}
  409. */
  410. async getLastEndTimeByChecked(auditorId) {
  411. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  412. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.back, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  413. const sqlParam = [this.tableName, auditorId];
  414. const result = await this.db.queryOne(sql, sqlParam);
  415. return result ? result.end_time : null;
  416. }
  417. /**
  418. * 用于添加推送所需的content内容
  419. * @param {Number} pid 项目id
  420. * @param {Number} tid 台账id
  421. * @param {Number} cpId 立项书id
  422. * @param {Number} uid 审批人id
  423. */
  424. async getNoticeContent(pid, tid, cpId, uid, opinion = '') {
  425. const noticeSql = 'SELECT * FROM (SELECT ' +
  426. ' t.`id` As `tid`, ma.`cpid`, m.`code` as `c_code`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  427. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  428. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  429. ' LEFT JOIN ?? As ma ON m.`id` = ma.`cpid`' +
  430. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  431. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  432. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.changeProject.tableName, cpId, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  433. const content = await this.db.query(noticeSql, noticeSqlParam);
  434. if (content.length) {
  435. content[0].opinion = opinion;
  436. }
  437. return content.length ? JSON.stringify(content[0]) : '';
  438. }
  439. /**
  440. * 审批
  441. * @param {Number} cpId - 立项书id
  442. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  443. * @param {Number} times - 第几次审批
  444. * @return {Promise<void>}
  445. */
  446. async check(cpId, checkData, times = 1) {
  447. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.back) {
  448. throw '提交数据错误';
  449. }
  450. const pid = this.ctx.session.sessionProject.id;
  451. switch (checkData.checkType) {
  452. case auditConst.status.checked:
  453. await this._checked(pid, cpId, checkData, times);
  454. break;
  455. case auditConst.status.back:
  456. await this._back(pid, cpId, checkData, times);
  457. break;
  458. case auditConst.status.checkNo:
  459. await this._checkNo(pid, cpId, checkData, times);
  460. break;
  461. default:
  462. throw '无效审批操作';
  463. }
  464. }
  465. async _checked(pid, cpId, checkData, times) {
  466. const accountId = this.ctx.session.sessionUser.accountId;
  467. const time = new Date();
  468. // 整理当前流程审核人状态更新
  469. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  470. if (audits.length === 0) throw '审核数据错误';
  471. const selfAudit = audits.find(x => { return x.aid === accountId; });
  472. if (!selfAudit) throw '当前标段您无权审批';
  473. const flowAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: selfAudit.order } });
  474. const nextAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: selfAudit.order + 1 } });
  475. // 获取审核人列表
  476. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  477. // const sqlParam = [this.tableName, cpId, times];
  478. // const auditors = await this.db.query(sql, sqlParam);
  479. //
  480. // const nextAudit = await this.getDataByCondition({ cpid: cpId, times, order: audit.order + 1 });
  481. const transaction = await this.db.beginTransaction();
  482. try {
  483. // 更新本人审批状态
  484. await transaction.update(this.tableName, {
  485. id: selfAudit.id,
  486. status: checkData.checkType,
  487. opinion: checkData.opinion,
  488. end_time: time,
  489. });
  490. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  491. // 获取推送必要信息
  492. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  493. // 添加推送
  494. // const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }];
  495. const records = [];
  496. const auditors = await this.getAuditorsWithOwner(cpId, times);
  497. auditors.forEach(audit => {
  498. records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  499. });
  500. await transaction.insert('zh_notice', records);
  501. if (audits.length === 1 || selfAudit.audit_type !== auditType.key.and) {
  502. // 或签更新他人审批状态
  503. if (selfAudit.audit_type === auditType.key.or) {
  504. const updateOther = [];
  505. for (const audit of audits) {
  506. if (audit.aid === selfAudit.aid) continue;
  507. updateOther.push({
  508. id: audit.id,
  509. status: auditConst.status.checkSkip,
  510. opinion: '',
  511. end_time: time,
  512. });
  513. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  514. }
  515. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  516. }
  517. // 无下一审核人表示,审核结束
  518. if (nextAudits.length > 0) {
  519. // 流程至下一审批人
  520. const updateData = nextAudits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time: time }; });
  521. await transaction.updateRows(this.tableName, updateData);
  522. // 同步 期信息
  523. await transaction.update(this.ctx.service.changeProject.tableName, {
  524. id: cpId, status: auditConst.status.checking,
  525. });
  526. // 微信模板通知
  527. const wechatData = {
  528. type: 'project',
  529. status: wxConst.status.check,
  530. tips: wxConst.tips.check,
  531. code: this.ctx.session.sessionProject.code,
  532. c_name: this.ctx.change.name,
  533. };
  534. await this.ctx.helper.sendWechat(this._.map(nextAudits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  535. for (const nextAudit of nextAudits) {
  536. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  537. pid: this.ctx.session.sessionProject.id,
  538. tid: this.ctx.tender.id,
  539. uid: nextAudit.aid,
  540. sp_type: 'change',
  541. sp_id: nextAudit.id,
  542. table_name: this.tableName,
  543. template: wxConst.template.change,
  544. wx_data: wechatData,
  545. });
  546. }
  547. // 检查三方特殊推送
  548. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  549. } else {
  550. // 本期结束
  551. // 生成截止本期数据 final数据
  552. // 同步 期信息
  553. await transaction.update(this.ctx.service.changeProject.tableName, {
  554. id: cpId, status: checkData.checkType,
  555. });
  556. // 微信模板通知
  557. const users = this._.uniq(this._.map(auditors, 'aid'));
  558. const wechatData = {
  559. type: 'project',
  560. status: wxConst.status.success,
  561. tips: wxConst.tips.success,
  562. code: this.ctx.session.sessionProject.code,
  563. c_name: this.ctx.change.name,
  564. };
  565. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  566. // 检查三方特殊推送
  567. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  568. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.checked);
  569. }
  570. } else {
  571. // 同步 期信息
  572. await transaction.update(this.ctx.service.changeProject.tableName, {
  573. id: cpId,
  574. status: auditConst.status.checking,
  575. });
  576. }
  577. await transaction.commit();
  578. } catch (err) {
  579. await transaction.rollback();
  580. throw err;
  581. }
  582. }
  583. async _back(pid, cpId, checkData, times) {
  584. const accountId = this.ctx.session.sessionUser.accountId;
  585. const time = new Date();
  586. const changeData = await this.ctx.service.changeProject.getDataById(cpId);
  587. // 整理当前流程审核人状态更新
  588. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  589. if (!audits) throw '审核数据错误';
  590. const selfAudit = audits.find(x => { return x.aid === accountId; });
  591. if (!selfAudit) throw '当前标段您无权审批';
  592. // const flowAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times: selfAudit.times, order: selfAudit.order } });
  593. const auditors = await this.getUniqAuditor(cpId, times); // 全部参与的审批人
  594. const newAuditors = auditors.map(x => {
  595. return {
  596. aid: x.aid, tid: selfAudit.tid, cpid: selfAudit.cpid,
  597. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  598. audit_type: x.audit_type, audit_order: x.audit_order,
  599. };
  600. });
  601. const transaction = await this.db.beginTransaction();
  602. try {
  603. const updateData = [];
  604. audits.forEach(x => {
  605. updateData.push({
  606. id: x.id,
  607. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  608. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  609. end_time: x.aid === selfAudit.aid ? time : null,
  610. });
  611. });
  612. await transaction.updateRows(this.tableName, updateData);
  613. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  614. // 添加到消息推送表
  615. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  616. const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.back, content: noticeContent }];
  617. auditors.forEach(audit => {
  618. records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.back, content: noticeContent });
  619. });
  620. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  621. // 同步期信息
  622. await transaction.update(this.ctx.service.changeProject.tableName, {
  623. id: cpId, status: checkData.checkType,
  624. times: times + 1,
  625. });
  626. // 拷贝新一次审核流程列表
  627. await transaction.insert(this.tableName, newAuditors);
  628. // 微信模板通知
  629. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  630. const wechatData = {
  631. type: 'project',
  632. status: wxConst.status.back,
  633. tips: wxConst.tips.back,
  634. code: this.ctx.session.sessionProject.code,
  635. c_name: this.ctx.change.name,
  636. };
  637. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  638. // 生成内容保存表至zh_change_project_history中,用于撤回
  639. await this.ctx.service.changeProjectHistory.saveHistory(transaction, changeData);
  640. // 检查三方特殊推送
  641. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  642. await transaction.commit();
  643. } catch (err) {
  644. await transaction.rollback();
  645. throw err;
  646. }
  647. }
  648. async _checkNo(pid, cpId, checkData, times) {
  649. const accountId = this.ctx.session.sessionUser.accountId;
  650. const time = new Date();
  651. // 整理当前流程审核人状态更新
  652. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  653. if (!audits) throw '审核数据错误';
  654. const selfAudit = audits.find(x => { return x.aid === accountId; });
  655. if (!selfAudit) throw '当前标段您无权终止';
  656. const auditors = await this.getUniqAuditor(cpId, times); // 全部参与的审批人
  657. // 获取审核人列表
  658. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  659. // const sqlParam = [this.tableName, cpId, times];
  660. // const auditors = await this.db.query(sql, sqlParam);
  661. const transaction = await this.db.beginTransaction();
  662. try {
  663. const updateData = [];
  664. audits.forEach(x => {
  665. updateData.push({
  666. id: x.id,
  667. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  668. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  669. end_time: x.aid === selfAudit.aid ? time : null,
  670. });
  671. });
  672. await transaction.updateRows(this.tableName, updateData);
  673. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  674. // 获取推送必要信息
  675. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  676. // 添加推送
  677. const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
  678. auditors.forEach(audit => {
  679. records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  680. });
  681. await transaction.insert('zh_notice', records);
  682. // 本期结束
  683. // 生成截止本期数据 final数据
  684. // 同步 期信息
  685. await transaction.update(this.ctx.service.changeProject.tableName, {
  686. id: cpId, status: checkData.checkType,
  687. });
  688. // 微信模板通知
  689. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  690. const wechatData = {
  691. type: 'project',
  692. status: wxConst.status.stop,
  693. tips: wxConst.tips.stop,
  694. code: this.ctx.session.sessionProject.code,
  695. c_name: this.ctx.change.name,
  696. };
  697. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  698. // 检查三方特殊推送
  699. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  700. await transaction.commit();
  701. } catch (err) {
  702. await transaction.rollback();
  703. throw err;
  704. }
  705. }
  706. /**
  707. * 复制上一期的审批人列表给最新一期
  708. *
  709. * @param transaction - 新增一期的事务
  710. * @param {Object} preMaterial - 上一期
  711. * @param {Object} newaMaterial - 最新一期
  712. * @return {Promise<*>}
  713. */
  714. async copyPreChangeProjectAuditors(transaction, preChange, newChange) {
  715. // 再获取非原报审批人
  716. const auditList = await this.getUniqAuditor(preChange.id, preChange.times); // 全部参与的审批人
  717. const newAuditors = [];
  718. for (const x of auditList) {
  719. if (x.audit_order !== 0) {
  720. newAuditors.push({
  721. tid: preChange.tid, cpid: newChange.id, aid: x.aid,
  722. times: 1, order: x.audit_order, status: auditConst.status.uncheck,
  723. audit_type: x.audit_type, audit_order: x.audit_order,
  724. });
  725. }
  726. }
  727. const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null;
  728. return result ? result.affectedRows === newAuditors.length : true;
  729. }
  730. async getAllAuditors(tenderId) {
  731. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  732. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  733. ' WHERE t.id = ?' +
  734. ' GROUP BY ma.aid';
  735. const sqlParam = [tenderId];
  736. return this.db.query(sql, sqlParam);
  737. }
  738. /**
  739. * 审批撤回
  740. * @param {Number} stageId - 标段id
  741. * @param {Number} times - 第几次审批
  742. * @return {Promise<void>}
  743. */
  744. async checkCancel(change) {
  745. // 分4种情况,根据ctx.cancancel值判断:
  746. // 1.原报发起撤回,当前流程删除,并回到待上报
  747. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  748. // 3.审批人撤回审批终止,增加流程,并回到它审批中,并更新立项状态为审批中
  749. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  750. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  751. switch (change.cancancel) {
  752. case 1: await this._userCheckCancel(change); break;
  753. case 2: await this._auditCheckCancel(change); break;
  754. case 3: await this._auditCheckCancelStop(change); break;
  755. case 4: await this._auditCheckCancelNo(change); break;
  756. case 5: await this._auditCheckCancelAnd(change); break;
  757. default: throw '不可撤回,请刷新页面重试';
  758. }
  759. }
  760. /**
  761. * 原报撤回,直接改动审批人状态
  762. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  763. *
  764. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  765. * ...
  766. *
  767. * @param stage
  768. * @returns {Promise<void>}
  769. * @private
  770. */
  771. async _userCheckCancel(change) {
  772. const transaction = await this.db.beginTransaction();
  773. try {
  774. // 整理当前流程审核人状态更新
  775. // 审批人变成待审批状态
  776. const updateData = change.curAuditors.map(x => {
  777. return {
  778. id: x.id,
  779. status: auditConst.status.uncheck,
  780. begin_time: null, opinion: null,
  781. };
  782. });
  783. await transaction.updateRows(this.tableName, updateData);
  784. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  785. // 变成待上报状态
  786. await transaction.update(this.ctx.service.changeProject.tableName, {
  787. id: change.id,
  788. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back,
  789. });
  790. await transaction.commit();
  791. } catch (err) {
  792. await transaction.rollback();
  793. throw err;
  794. }
  795. }
  796. /**
  797. * 审批人撤回审批通过,插入两条数据
  798. *
  799. * 一审 1 A checked 一审 1 A checked
  800. * 二审 2 B checked pre -> 二审 2 B checked
  801. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  802. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  803. * 三审 5 C uncheck order、status改
  804. * 四审 6 D uncheck order改
  805. *
  806. * @param stage
  807. * @returns {Promise<void>}
  808. * @private
  809. */
  810. async _auditCheckCancel(change) {
  811. if (change.curAuditors.length === 0 || change.curAuditors[0].order <= 1) {
  812. throw '撤回用户数据错误';
  813. }
  814. const accountId = this.ctx.session.sessionUser.accountId;
  815. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId; });
  816. if (!selfAuditor) throw '撤回用户数据错误';
  817. const time = new Date();
  818. const transaction = await this.db.beginTransaction();
  819. try {
  820. // 顺移其后审核人流程顺序
  821. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  822. await transaction.query(sql, [change.id, selfAuditor.times, change.curAuditors[0].order]);
  823. // 当前审批人2次添加至流程中
  824. const checkCancelAuditors = [],
  825. checkingAuditors = [];
  826. change.preAuditors.forEach(x => {
  827. checkCancelAuditors.push({
  828. tid: change.tid, cpid: change.id, aid: x.aid,
  829. times: x.times, order: x.order + 1,
  830. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  831. begin_time: time, end_time: time, opinion: '',
  832. audit_type: x.audit_type, audit_order: x.audit_order,
  833. });
  834. });
  835. change.preAuditors.forEach(x => {
  836. checkingAuditors.push({
  837. tid: change.tid, cpid: change.id, aid: x.aid,
  838. times: x.times, order: x.order + 2,
  839. status: auditConst.status.checking,
  840. begin_time: time, end_time: time, opinion: '',
  841. audit_type: x.audit_type, audit_order: x.audit_order,
  842. });
  843. });
  844. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  845. // 当前审批人变成待审批
  846. await transaction.updateRows(this.tableName, change.curAuditors.map(x => {
  847. return { id: x.id, begin_time: null, status: auditConst.status.uncheck, order: x.order + 2 };
  848. }));
  849. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(change.curAuditors, 'id'));
  850. await transaction.commit();
  851. } catch (err) {
  852. await transaction.rollback();
  853. throw err;
  854. }
  855. }
  856. /**
  857. * 审批人撤回审批终止,插入两条数据
  858. *
  859. * @param stage
  860. * @returns {Promise<void>}
  861. * @private
  862. */
  863. async _auditCheckCancelStop(change) {
  864. const accountId = this.ctx.session.sessionUser.accountId;
  865. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
  866. if (!selfAuditor) throw '该变更立项由他人审批终止,您不可撤回';
  867. const time = new Date();
  868. const transaction = await this.db.beginTransaction();
  869. try {
  870. // 顺移其后审核人流程顺序
  871. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  872. await transaction.query(sql, [change.id, change.times, selfAuditor.order]);
  873. // 当前审批人2次添加至流程中
  874. const checkCancelAuditors = [],
  875. checkingAuditors = [];
  876. change.preAuditors.forEach(x => {
  877. checkCancelAuditors.push({
  878. tid: change.tid, cpid: change.id, aid: x.aid,
  879. times: x.times, order: x.order + 1,
  880. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  881. begin_time: time, end_time: time, opinion: '',
  882. audit_type: x.audit_type, audit_order: x.audit_order,
  883. });
  884. });
  885. change.preAuditors.forEach(x => {
  886. checkingAuditors.push({
  887. tid: change.tid, cpid: change.id, aid: x.aid,
  888. times: x.times, order: x.order + 2,
  889. status: auditConst.status.checking,
  890. begin_time: time, end_time: time, opinion: '',
  891. audit_type: x.audit_type, audit_order: x.audit_order,
  892. });
  893. });
  894. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  895. await transaction.update(this.ctx.service.changeProject.tableName, {
  896. id: change.id,
  897. status: auditConst.status.checking,
  898. });
  899. await transaction.commit();
  900. } catch (err) {
  901. await transaction.rollback();
  902. throw err;
  903. }
  904. }
  905. /**
  906. * 审批人撤回审批退回原报
  907. *
  908. * 1# 一审 1 A checked 1# 一审 1 A checked
  909. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  910. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  911. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  912. * 三审 5 C uncheck order改
  913. *
  914. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  915. * 二审 2 B uncheck
  916. * 三审 3 C uncheck
  917. *
  918. * @param stage
  919. * @returns {Promise<void>}
  920. * @private
  921. */
  922. async _auditCheckCancelNo(change) {
  923. const accountId = this.ctx.session.sessionUser.accountId;
  924. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.back; });
  925. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  926. const time = new Date();
  927. const transaction = await this.db.beginTransaction();
  928. try {
  929. // 整理上一个流程审核人状态更新
  930. // 顺移其后审核人流程顺序
  931. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  932. await transaction.query(sql, [change.id, selfAuditor.times, selfAuditor.order]);
  933. // 当前审批人2次添加至流程中
  934. const checkCancelAuditors = [],
  935. checkingAuditors = [];
  936. change.preAuditors.forEach(x => {
  937. checkCancelAuditors.push({
  938. tid: change.tid, cpid: change.id, aid: x.aid,
  939. times: x.times, order: x.order + 1,
  940. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  941. begin_time: time, end_time: time, opinion: '',
  942. audit_type: x.audit_type, audit_order: x.audit_order,
  943. });
  944. });
  945. change.preAuditors.forEach(x => {
  946. checkingAuditors.push({
  947. tid: change.tid, cpid: change.id, aid: x.aid,
  948. times: x.times, order: x.order + 2,
  949. status: auditConst.status.checking,
  950. begin_time: time, end_time: time, opinion: '',
  951. audit_type: x.audit_type, audit_order: x.audit_order,
  952. });
  953. });
  954. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  955. // 删除当前次审批流
  956. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  957. // 回退数据
  958. await this.ctx.service.changeProjectHistory.returnHistory(transaction, change.id);
  959. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: change.id });
  960. // // 设置变更立项为审批中
  961. // await transaction.update(this.ctx.service.changeProject.tableName, {
  962. // id: change.id,
  963. // time: change.times - 1,
  964. // status: auditConst.status.checking,
  965. // });
  966. await transaction.commit();
  967. } catch (err) {
  968. await transaction.rollback();
  969. throw err;
  970. }
  971. }
  972. /**
  973. * 会签未全部审批通过时,撤回仅修改本人状态
  974. *
  975. * @param stage
  976. * @returns {Promise<void>}
  977. * @private
  978. */
  979. async _auditCheckCancelAnd(change) {
  980. const accountId = this.ctx.session.sessionUser.accountId;
  981. const selfAuditor = change.flowAuditors.find(x => { return x.aid === accountId; });
  982. if (!selfAuditor || selfAuditor.status !== auditConst.status.checked) throw '不可撤回';
  983. const transaction = await this.db.beginTransaction();
  984. try {
  985. await transaction.update(this.tableName, {
  986. id: selfAuditor.id, status: auditConst.status.checking, opinion: '', end_time: null,
  987. });
  988. await transaction.commit();
  989. } catch (err) {
  990. await transaction.rollback();
  991. throw err;
  992. }
  993. }
  994. /**
  995. * 重新审批变更令
  996. * @param { string } cid - 查询的清单
  997. * @return {Promise<*>} - 可用的变更令列表
  998. */
  999. async checkRevise(change) {
  1000. const accountId = this.ctx.session.sessionUser.accountId;
  1001. const time = new Date();
  1002. // 初始化事务
  1003. const transaction = await this.db.beginTransaction();
  1004. let result = false;
  1005. try {
  1006. const pid = this.ctx.session.sessionProject.id;
  1007. // 获取审核人列表
  1008. const auditors = await this.getUniqAuditor(change.id, change.times); // 全部参与的审批人
  1009. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1010. // const sqlParam = [this.tableName, change.id, change.times];
  1011. // const auditors = await this.db.query(sql, sqlParam);
  1012. // 添加到消息推送表
  1013. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '发起修订');
  1014. const records = [{ pid, type: pushType.changeProject, uid: change.uid, status: auditConst.status.revise, content: noticeContent }];
  1015. auditors.forEach(auditor => {
  1016. records.push({
  1017. pid,
  1018. type: pushType.changeProject,
  1019. uid: auditor.aid,
  1020. status: auditConst.status.revise,
  1021. content: noticeContent,
  1022. });
  1023. });
  1024. await transaction.insert('zh_notice', records);
  1025. const order = change.auditors.length > 0 ? change.auditors.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
  1026. // 当前审批人添加至流程中
  1027. const checkReviseAuditor = {
  1028. tid: change.tid, cpid: change.id, aid: change.uid,
  1029. times: change.times, order,
  1030. status: auditConst.status.revise,
  1031. begin_time: time, end_time: time, opinion: '',
  1032. audit_type: auditType.key.common, audit_order: 0,
  1033. };
  1034. await transaction.insert(this.tableName, checkReviseAuditor);
  1035. // 修订人加入审批流程
  1036. const newAuditors = auditors.map(x => {
  1037. return {
  1038. aid: x.aid, tid: change.tid, cpid: change.id,
  1039. times: change.times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  1040. audit_type: x.audit_type, audit_order: x.audit_order,
  1041. };
  1042. });
  1043. await transaction.insert(this.tableName, newAuditors);
  1044. // 生成内容保存表至zh_change_history中,用于撤销修订回退
  1045. const changeData = await transaction.get(this.ctx.service.changeProject.tableName, { id: change.id });
  1046. await this.ctx.service.changeProjectHistory.saveHistory(transaction, changeData);
  1047. // 设置变更立项修订状态
  1048. await transaction.update(this.ctx.service.changeProject.tableName, {
  1049. id: change.id,
  1050. status: auditConst.status.revise,
  1051. times: change.times + 1,
  1052. });
  1053. await transaction.commit();
  1054. result = true;
  1055. } catch (error) {
  1056. console.log(error);
  1057. await transaction.rollback();
  1058. result = false;
  1059. }
  1060. return result;
  1061. }
  1062. /**
  1063. * 撤销修订变更令
  1064. * @param { string } cid - 查询的清单
  1065. * @return {Promise<*>} - 可用的变更令列表
  1066. */
  1067. async cancelRevise(change) {
  1068. const time = new Date();
  1069. // 初始化事务
  1070. const transaction = await this.db.beginTransaction();
  1071. let result = false;
  1072. try {
  1073. const pid = this.ctx.session.sessionProject.id;
  1074. // 获取审核人列表
  1075. const auditors = await this.getUniqAuditor(change.id, change.times - 1); // 全部参与的审批人
  1076. // 添加到消息推送表
  1077. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '撤销修订');
  1078. const records = [{ pid, type: pushType.changeProject, uid: change.uid, status: auditConst.status.cancelRevise, content: noticeContent }];
  1079. auditors.forEach(auditor => {
  1080. records.push({
  1081. pid,
  1082. type: pushType.changeProject,
  1083. uid: auditor.aid,
  1084. status: auditConst.status.cancelRevise,
  1085. content: noticeContent,
  1086. });
  1087. });
  1088. await transaction.insert('zh_notice', records);
  1089. const lastAudit = await this.getLastAudit(change.id, change.times - 1);
  1090. // 新增一个撤销修订状态到审批流程中
  1091. const revise_audit = {
  1092. tid: change.tid, cpid: change.id, aid: this.ctx.session.sessionUser.accountId,
  1093. times: change.times - 1, order: lastAudit.order + 1, status: auditConst.status.cancelRevise,
  1094. begin_time: time, end_time: time, opinion: '',
  1095. audit_type: lastAudit.audit_type, audit_order: lastAudit.audit_order,
  1096. };
  1097. await transaction.insert(this.tableName, revise_audit);
  1098. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  1099. await this.ctx.service.changeProjectHistory.returnHistory(transaction, change.id);
  1100. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: change.id });
  1101. await transaction.commit();
  1102. result = true;
  1103. } catch (error) {
  1104. console.log(error);
  1105. await transaction.rollback();
  1106. result = false;
  1107. }
  1108. return result;
  1109. }
  1110. /**
  1111. * 重新审批变更立项
  1112. * @param { string } cid - 查询的清单
  1113. * @return {Promise<*>} - 可用的变更令列表
  1114. */
  1115. async checkAgain(change) {
  1116. const accountId = this.ctx.session.sessionUser.accountId;
  1117. // 初始化事务
  1118. const time = new Date();
  1119. const transaction = await this.db.beginTransaction();
  1120. let result = false;
  1121. try {
  1122. const noYbAuditors = change.auditors.filter(x => { return x.audit_order !== 0; });
  1123. const noYbMaxOrder = noYbAuditors[noYbAuditors.length - 1].order;
  1124. const maxOrder = change.auditors[change.auditors.length - 1].order;
  1125. const audits = change.auditors.filter(x => { return x.order === noYbMaxOrder; });
  1126. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  1127. const selfAudit = audits.find(x => { return x.aid === accountId; });
  1128. if (!selfAudit) throw '当前标段您无权审批';
  1129. // 当前审批人2次添加至流程中
  1130. const checkAgainAuditors = [];
  1131. audits.forEach(x => {
  1132. checkAgainAuditors.push({
  1133. tid: change.tid, cpid: change.id, aid: x.aid,
  1134. times: x.times, order: maxOrder + 1,
  1135. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  1136. begin_time: time, end_time: time, opinion: '',
  1137. audit_type: x.audit_type, audit_order: x.audit_order,
  1138. });
  1139. });
  1140. const checkingAuditors = [];
  1141. audits.forEach(x => {
  1142. checkingAuditors.push({
  1143. tid: change.tid, cpid: change.id, aid: x.aid,
  1144. times: x.times, order: maxOrder + 2,
  1145. status: auditConst.status.checking,
  1146. begin_time: time,
  1147. audit_type: x.audit_type, audit_order: x.audit_order,
  1148. });
  1149. });
  1150. await transaction.insert(this.tableName, checkAgainAuditors);
  1151. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  1152. // 获取刚批量添加的所有list
  1153. // for (let j = 0; j < checkingAuditors.length; j++) {
  1154. // checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  1155. // }
  1156. // 设置变更令审批中
  1157. await transaction.update(this.ctx.service.changeProject.tableName, {
  1158. id: change.id,
  1159. status: auditConst.status.checking,
  1160. });
  1161. // 检查三方特殊推送
  1162. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_project.flow);
  1163. await transaction.commit();
  1164. result = true;
  1165. } catch (error) {
  1166. console.log(error);
  1167. await transaction.rollback();
  1168. result = false;
  1169. }
  1170. return result;
  1171. }
  1172. async getAuditorGroup(cpId, times) {
  1173. const auditors = await this.getAuditors(cpId, times); // 全部参与的审批人
  1174. return this.ctx.helper.groupAuditors(auditors, 'order', true);
  1175. }
  1176. async getUserGroup(cpId, times) {
  1177. const group = await this.getAuditorGroup(cpId, times);
  1178. const sql =
  1179. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  1180. ' FROM ' + this.ctx.service.changeProject.tableName + ' As s' +
  1181. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1182. ' ON s.uid = pa.id' +
  1183. ' WHERE s.id = ?';
  1184. const sqlParam = [times, cpId, cpId];
  1185. const user = await this.db.queryOne(sql, sqlParam);
  1186. user.audit_order = 0;
  1187. group.unshift([ user ]);
  1188. return group;
  1189. }
  1190. async getUniqUserGroup(cpId, times) {
  1191. const group = await this.getAuditorGroup(cpId, times);
  1192. const sql =
  1193. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  1194. ' FROM ' + this.ctx.service.changeProject.tableName + ' As s' +
  1195. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1196. ' ON s.uid = pa.id' +
  1197. ' WHERE s.id = ?';
  1198. const sqlParam = [times, cpId, cpId];
  1199. const user = await this.db.queryOne(sql, sqlParam);
  1200. user.audit_order = 0;
  1201. group.unshift([ user ]);
  1202. return this.ctx.helper.groupAuditorsUniq(group);
  1203. }
  1204. async getAuditorHistory(cpId, times, reverse = false) {
  1205. const history = [];
  1206. if (times >= 1) {
  1207. for (let i = 1; i <= times; i++) {
  1208. const auditors = await this.getAuditors(cpId, i);
  1209. const group = this.ctx.helper.groupAuditors(auditors);
  1210. const historyGroup = [];
  1211. // 找出group里audit_order最大值
  1212. const max_info = group.length > 0 ? this._.maxBy(group, function(item) {
  1213. return item && item[0] && item[0].audit_order;
  1214. }) : null;
  1215. const max_order = max_info ? max_info[0].audit_order : -1;
  1216. for (const g of group) {
  1217. const his = {
  1218. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  1219. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  1220. auditors: g,
  1221. };
  1222. if (his.audit_type === auditType.key.common) {
  1223. his.name = g[0].name;
  1224. } else {
  1225. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  1226. }
  1227. his.is_final = his.audit_order === max_order;
  1228. if (g[0].begin_time) {
  1229. his.begin_time = g[0].begin_time;
  1230. const beginTime = this.ctx.moment(g[0].begin_time);
  1231. his.beginYear = beginTime.format('YYYY');
  1232. his.beginDate = beginTime.format('MM-DD');
  1233. his.beginTime = beginTime.format('HH:mm:ss');
  1234. }
  1235. let end_time;
  1236. g.forEach(x => {
  1237. if (x.status === auditConst.status.checkSkip) return;
  1238. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  1239. if (x.end_time && (!end_time || x.end_time > end_time)) {
  1240. end_time = x.end_time;
  1241. if (his.status !== auditConst.status.checking) his.status = x.status;
  1242. }
  1243. });
  1244. if (end_time) {
  1245. his.end_time = end_time;
  1246. const endTime = this.ctx.moment(end_time);
  1247. his.endYear = endTime.format('YYYY');
  1248. his.endDate = endTime.format('MM-DD');
  1249. his.endTime = endTime.format('HH:mm:ss');
  1250. }
  1251. historyGroup.push(his);
  1252. }
  1253. if (reverse) {
  1254. history.push(historyGroup.reverse());
  1255. } else {
  1256. history.push(historyGroup);
  1257. }
  1258. }
  1259. }
  1260. return history;
  1261. }
  1262. async getUniqAuditor(cpId, times) {
  1263. const auditors = await this.getAuditors(cpId, times); // 全部参与的审批人
  1264. const result = [];
  1265. auditors.forEach(x => {
  1266. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  1267. result.push(x);
  1268. }
  1269. });
  1270. return result;
  1271. }
  1272. async saveAudit(cpId, times, sp_group, data) {
  1273. const transaction = await this.db.beginTransaction();
  1274. try {
  1275. const auditors = await this.getAuditGroupByList(cpId, times);
  1276. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1277. if (data.operate !== 'del') {
  1278. const exist = await this.getDataByCondition({ cpid: cpId, times, aid: data.new_aid });
  1279. if (exist) throw '该审核人已存在,请勿重复添加';
  1280. }
  1281. if (data.operate === 'add') {
  1282. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1283. throw '当前人下无法操作新增';
  1284. }
  1285. const newAudit = {
  1286. tid: this.ctx.tender.id, cpid: cpId, aid: data.new_aid,
  1287. order: now_audit.order + 1,
  1288. audit_order: now_audit.audit_order + 1, audit_type: auditType.key.common,
  1289. times, status: auditConst.status.uncheck,
  1290. };
  1291. // order+1
  1292. await this._syncOrderByDelete(transaction, cpId, now_audit.order + 1, times, '+');
  1293. await transaction.insert(this.tableName, newAudit);
  1294. // 更新审批流程页数据,如果存在
  1295. } else if (data.operate === 'add-sibling') {
  1296. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1297. throw '当前人下无法操作新增';
  1298. }
  1299. const newAudit = {
  1300. tid: this.ctx.tender.id, cpid: cpId, aid: data.new_aid,
  1301. order: now_audit.order,
  1302. audit_order: now_audit.audit_order, audit_type: now_audit.audit_type,
  1303. times, status: auditConst.status.uncheck,
  1304. };
  1305. await transaction.insert(this.tableName, newAudit);
  1306. } else if (data.operate === 'del') {
  1307. if (now_audit.status !== auditConst.status.uncheck) {
  1308. throw '当前人无法操作删除';
  1309. }
  1310. const flowAuditors = auditors.filter(x => { return x.audit_order === now_audit.audit_order; });
  1311. await transaction.delete(this.tableName, { cpid: cpId, times, aid: now_audit.aid, order: now_audit.order });
  1312. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, cpId, now_audit.order, times);
  1313. // 旧的更新为is_old为1
  1314. // await transaction.update(this.tableName, { is_old: 1 }, {
  1315. // where: {
  1316. // sid: stageId,
  1317. // times,
  1318. // aid: data.old_aid,
  1319. // }
  1320. // });
  1321. } else if (data.operate === 'change') {
  1322. const nowAudit = await this.getDataByCondition({ cpid: cpId, times, aid: now_audit.aid, order: now_audit.order });
  1323. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1324. throw '当前人无法操作替换';
  1325. }
  1326. nowAudit.aid = data.new_aid;
  1327. await transaction.update(this.tableName, nowAudit);
  1328. // 旧的更新为is_old为1
  1329. // await transaction.update(this.tableName, { is_old: 1 }, {
  1330. // where: {
  1331. // sid: stageId,
  1332. // times,
  1333. // aid: data.old_aid,
  1334. // }
  1335. // });
  1336. }
  1337. if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  1338. const newAuditors = await transaction.select(this.tableName, { where: { cpid: cpId, times }, orders: [['order', 'asc']] });
  1339. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'order', true);
  1340. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1341. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.change, shenpiConst.sp_type.change, uniqNewAuditorGroup, sp_group);
  1342. } else if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs) {
  1343. const newAuditors = await this.getAuditGroupByList(cpId, times, transaction);
  1344. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.change, shenpiConst.sp_type.change, this._.map(newAuditors, 'aid'));
  1345. }
  1346. // 更新到审批流程方法
  1347. await transaction.commit();
  1348. } catch (err) {
  1349. await transaction.rollback();
  1350. throw err;
  1351. }
  1352. }
  1353. async changeSpGroup(change, sp_group) {
  1354. const transaction = await this.db.beginTransaction();
  1355. try {
  1356. const group = await this.ctx.service.shenpiGroup.getDataById(sp_group);
  1357. if (!group) {
  1358. throw '该固定审批组不存在,请刷新页面重新获取';
  1359. }
  1360. const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpiConst.sp_status.gdspl, sp_group: group.id } });
  1361. await this.ctx.service.shenpiAudit.noYbShenpiList(change.uid, shenpiList);
  1362. await this.updateNewAuditors(change, shenpiList, transaction);
  1363. await transaction.update(this.ctx.service.changeProject.tableName, { id: change.id, sp_group: group.id });
  1364. await transaction.commit();
  1365. } catch (err) {
  1366. await transaction.rollback();
  1367. throw err;
  1368. }
  1369. return true;
  1370. }
  1371. async updateNewAuditList(change, newList) {
  1372. const transaction = await this.db.beginTransaction();
  1373. try {
  1374. await this.updateNewAuditors(change, newList, transaction);
  1375. await transaction.commit();
  1376. } catch (err) {
  1377. await transaction.rollback();
  1378. throw err;
  1379. }
  1380. }
  1381. async updateNewAuditors(change, newList, transaction) {
  1382. // 先删除旧的审批流,再添加新的
  1383. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  1384. const newAuditors = [];
  1385. for (const auditor of newList) {
  1386. newAuditors.push({
  1387. tid: change.tid, cpid: change.id, aid: auditor.audit_id,
  1388. times: change.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1389. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1390. });
  1391. }
  1392. if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1393. }
  1394. async updateLastAudit(change, auditList, lastId) {
  1395. const transaction = await this.db.beginTransaction();
  1396. try {
  1397. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1398. const existAudit = auditList.find(x => { return x.aid === lastId; });
  1399. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
  1400. if (existAudit) {
  1401. await transaction.delete(this.tableName, { cpid: change.id, times: change.times, aid: lastId });
  1402. const sameOrder = auditList.filter(x => { return x.order === existAudit.order; });
  1403. if (sameOrder.length === 1) {
  1404. const updateData = [];
  1405. auditList.forEach(x => {
  1406. if (x.order <= existAudit.order) return;
  1407. updateData.push({ id: x.id, order: x.order - 1, audit_order: x.audit_order - 1 });
  1408. });
  1409. if (updateData.length > 0) {
  1410. await transaction.updateRows(updateData);
  1411. }
  1412. order = order - 1;
  1413. }
  1414. }
  1415. // 添加终审
  1416. const newAuditor = {
  1417. tid: change.tid, cpid: change.id, aid: lastId,
  1418. times: change.times, order, status: auditConst.status.uncheck,
  1419. audit_type: auditType.key.common, audit_order: order,
  1420. };
  1421. await transaction.insert(this.tableName, newAuditor);
  1422. await transaction.commit();
  1423. } catch (err) {
  1424. await transaction.rollback();
  1425. throw err;
  1426. }
  1427. }
  1428. }
  1429. return ChangeProjectAudit;
  1430. };