change_plan_audit.js 75 KB

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