change_audit.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').flow;
  10. const pushType = require('../const/audit').pushType;
  11. const shenpiConst = require('../const/shenpi');
  12. const smsTypeConst = require('../const/sms_type');
  13. const SMS = require('../lib/sms');
  14. const SmsAliConst = require('../const/sms_alitemplate');
  15. const wxConst = require('../const/wechat_template');
  16. module.exports = app => {
  17. class ChangeAudit extends app.BaseService {
  18. /**
  19. * 构造函数
  20. *
  21. * @param {Object} ctx - egg全局变量
  22. * @return {void}
  23. */
  24. constructor(ctx) {
  25. super(ctx);
  26. this.tableName = 'change_audit';
  27. }
  28. /**
  29. * 获取最后一位审批人
  30. * @param {int} cid - 变更令id
  31. * @param {int} times - 次数
  32. * @param {int} site - 位置
  33. * @param {int} status - 状态
  34. * @return {void}
  35. */
  36. async getLastUser(cid, times, site = 1, status = 1) {
  37. this.initSqlBuilder();
  38. this.sqlBuilder.setAndWhere('cid', {
  39. value: this.db.escape(cid),
  40. operate: '=',
  41. });
  42. this.sqlBuilder.setAndWhere('times', {
  43. value: times,
  44. operate: '=',
  45. });
  46. if (status === 1) {
  47. this.sqlBuilder.setAndWhere('status', {
  48. value: 1,
  49. operate: '!=',
  50. });
  51. }
  52. if (site === 0) {
  53. this.sqlBuilder.setAndWhere('usite', {
  54. value: site,
  55. operate: '=',
  56. });
  57. }
  58. const u_sort = [['usort', 'DESC']];
  59. this.sqlBuilder.orderBy = u_sort;
  60. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  61. const data = await this.db.queryOne(sql, sqlParam);
  62. return data;
  63. }
  64. /**
  65. * 获取最后一位退回的审批人
  66. * @param {int} cid - 变更令id
  67. * @param {int} times - 次数
  68. * @return {void}
  69. */
  70. async getLastBackUser(cid, times) {
  71. this.initSqlBuilder();
  72. this.sqlBuilder.setAndWhere('cid', {
  73. value: this.db.escape(cid),
  74. operate: '=',
  75. });
  76. this.sqlBuilder.setAndWhere('times', {
  77. value: times,
  78. operate: '=',
  79. });
  80. this.sqlBuilder.setAndWhere('status', {
  81. value: 6,
  82. operate: '=',
  83. });
  84. const u_sort = [['usort', 'DESC']];
  85. this.sqlBuilder.orderBy = u_sort;
  86. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  87. const data = await this.db.queryOne(sql, sqlParam);
  88. return data;
  89. }
  90. /**
  91. * 获取当前审批人查看info时的状态
  92. * @param {Object} change - 变更令数据
  93. * @return {void}
  94. */
  95. async getStatusByChange(change) {
  96. const statusConst = auditConst.status;
  97. const auditStatusConst = auditConst.auditStatus;
  98. const uid = this.ctx.session.sessionUser.accountId;
  99. const changeAuditInfo = await this.getAllDataByCondition({ where: { cid: change.cid, times: change.times, uid }, orders: [['usort', 'desc']], limit: 1, offset: 0 });
  100. if (!change.status === statusConst.checked && (changeAuditInfo === null || changeAuditInfo[0] === undefined) && !ctx.tender.isTourist) {
  101. // 无权限查看此变更令
  102. return 0;
  103. }
  104. if (change.status === statusConst.uncheck && uid === change.uid) {
  105. // 待上报
  106. return 1;
  107. } else if (change.status === statusConst.back && uid === change.uid) {
  108. // 待重新上报
  109. return 2;
  110. } else if (change.status === statusConst.revise && uid === change.uid) {
  111. // 修订上报
  112. return 9;
  113. } else if ((change.status === statusConst.back || change.status === statusConst.revise) && uid !== change.uid) {
  114. // 被退回或修订中但你不是原报人
  115. return 3;
  116. } else if (change.status === statusConst.checked) {
  117. // 已完成
  118. return 4;
  119. } else if (change.status === statusConst.checkNo) {
  120. // 已终止
  121. return 5;
  122. } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo.length > 0 && changeAuditInfo[0].status === auditStatusConst.checking) {
  123. // 待你审批
  124. return 6;
  125. } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo.length > 0 && changeAuditInfo[0].status !== auditStatusConst.checking) {
  126. // 审批中但你未到你审批或你已审批
  127. return 7;
  128. } else if (this.ctx.tender.isTourist) {
  129. // 游客模式,只预览不能操作
  130. return 8;
  131. }
  132. // 无权限查看此变更令
  133. return 0;
  134. }
  135. /**
  136. * 根据用户查看此变更状态获取审批人列表
  137. * @param {Object} change - 变更令
  138. * @param {int} status - 状态
  139. * @return {object} list - 列表
  140. */
  141. async getListByStatus(change, status) {
  142. let sql = '';
  143. let sqlParam = '';
  144. switch (status) {
  145. case 1:// 待上报
  146. case 2:// 待重新上报
  147. case 9:// 待修订
  148. sql = 'SELECT * FROM ?? WHERE ' +
  149. 'cid = ? AND times = ? GROUP BY usite ORDER BY usort asc';
  150. sqlParam = [this.tableName, change.cid,
  151. change.times];
  152. break;
  153. case 3: // 被退回但你不是原报人
  154. case 4:// 已完成
  155. case 5:// 已终止
  156. case 7:// 审批中但你未到你审批或你已审批
  157. case 8:// 游客
  158. // 获取完整的审批顺序
  159. sql = 'SELECT * FROM ?? WHERE ' +
  160. 'cid = ? ORDER BY usort';
  161. sqlParam = [this.tableName, change.cid];
  162. break;
  163. case 6: // 审批中
  164. sql = 'SELECT * FROM (SELECT MAX(usort) as ust FROM ?? ' +
  165. 'WHERE cid = ? and times = ? GROUP BY usite ) as b ' +
  166. 'JOIN ?? as a ON a.usort = b.ust WHERE cid = ? and times = ? ORDER BY usite asc';
  167. sqlParam = [this.tableName, change.cid, change.times, this.tableName, change.cid, change.times];
  168. // sql = 'SELECT * FROM ?? WHERE ' +
  169. // 'cid = ? AND times = ? ORDER BY usort';
  170. // sqlParam = [this.tableName, change.cid, change.times];
  171. break;
  172. default:// 无权限查看此变更令
  173. break;
  174. }
  175. const list = await this.db.query(sql, sqlParam);
  176. return list;
  177. }
  178. /**
  179. * 删除变更令审批人列表
  180. * @param {Object} transaction - 事务
  181. * @param {Object} cid - 变更令id
  182. * @param {int} times - 次数
  183. * @return {object} 返回结果
  184. */
  185. async deleteAuditData(transaction, cid, times) {
  186. this.initSqlBuilder();
  187. this.sqlBuilder.setAndWhere('cid', {
  188. value: this.db.escape(cid),
  189. operate: '=',
  190. });
  191. this.sqlBuilder.setAndWhere('times', {
  192. value: times,
  193. operate: '=',
  194. });
  195. this.sqlBuilder.setAndWhere('usite', {
  196. value: 0,
  197. operate: '!=',
  198. });
  199. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'delete');
  200. const result = await transaction.query(sql, sqlParam);
  201. return result;
  202. }
  203. /**
  204. * 获取不重复列表
  205. * @param {Object} cid - 变更令id
  206. * @param {int} times - 次数
  207. * @return {object} 返回结果
  208. */
  209. async getListGroupByTimes(cid, times) {
  210. const sql = 'SELECT * FROM ?? where id in (SELECT MAX(id) FROM ?? WHERE ' +
  211. 'cid = ? AND times = ? GROUP BY usite) ORDER BY usite asc';
  212. const sqlParam = [this.tableName, this.tableName, cid, times];
  213. const list = await this.db.query(sql, sqlParam);
  214. return list;
  215. }
  216. /**
  217. * 获取不重复列表
  218. * @param {Object} cid - 变更令id
  219. * @param {int} times - 次数
  220. * @return {object} 返回结果
  221. */
  222. async getListGroupByWithoutYB(cid, times, transaction = null) {
  223. const sql = 'SELECT * FROM ?? where cid = ? AND times = ? AND usite != 0 AND usort in (SELECT MAX(usort) FROM ?? WHERE ' +
  224. 'cid = ? AND times = ? AND usite != 0 GROUP BY usite) ORDER BY usite asc';
  225. const sqlParam = [this.tableName, cid, times, this.tableName, cid, times];
  226. const list = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  227. return list;
  228. }
  229. async getListOrderByTimes(cid, times) {
  230. const sql = 'SELECT * FROM ?? WHERE ' +
  231. 'cid = ? AND times = ? ORDER BY usort';
  232. const sqlParam = [this.tableName, cid, times];
  233. const list = await this.db.query(sql, sqlParam);
  234. return list;
  235. }
  236. async getListGroupByTimesWithDetail(cid, times) {
  237. const sql = 'SELECT *, pa.name, pa.company, pa.role, pa.mobile, pa.telephone FROM ' + this.tableName + ' ca ' +
  238. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On ca.uid = pa.id' +
  239. ' where id in (SELECT MAX(id) FROM ' + this.tableName +' WHERE cid = ? AND times = ? GROUP BY usite)' +
  240. ' ORDER BY usite asc';
  241. const sqlParam = [cid, times];
  242. const list = await this.db.query(sql, sqlParam);
  243. return list;
  244. }
  245. /**
  246. * 获取比sort大的审批人列表
  247. * @param {Object} cid - 变更令id
  248. * @param {int} usort - 排序
  249. * @return {object} 返回结果
  250. */
  251. async getNextAuditList(cid, usort) {
  252. const sql = 'SELECT * FROM ?? WHERE ' +
  253. 'cid = ? AND usort > ?';
  254. const sqlParam = [this.tableName, cid, usort];
  255. const list = await this.db.query(sql, sqlParam);
  256. return list;
  257. }
  258. /**
  259. * 获取除当前次数的审批人列表
  260. * @param {Object} cid - 变更令id
  261. * @param {int} times - 次数
  262. * @return {object} 返回结果
  263. */
  264. async getListByBack(cid, times) {
  265. this.initSqlBuilder();
  266. this.sqlBuilder.setAndWhere('cid', {
  267. value: this.db.escape(cid),
  268. operate: '=',
  269. });
  270. this.sqlBuilder.setAndWhere('times', {
  271. value: times,
  272. operate: '!=',
  273. });
  274. this.sqlBuilder.orderBy = [['usort', 'ASC']];
  275. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  276. const result = await this.db.query(sql, sqlParam);
  277. return result;
  278. }
  279. /**
  280. * 获取 审核人 待审批的() 变更令列表
  281. * @param uid
  282. * @return {Promise<void>}
  283. */
  284. async getAuditChange(uid) {
  285. const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' +
  286. ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, ' +
  287. ' t.`name`, t.`type`, t.`user_id` ' +
  288. ' FROM ?? AS ca, ?? AS c, ?? As t ' +
  289. ' WHERE ca.`uid` = ? and ca.`status` = ? and c.`status` != ?' +
  290. ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` ORDER BY ca.`sin_time` DESC';
  291. const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, uid, 2, auditConst.status.uncheck];
  292. const changes = await this.db.query(sql, sqlParam);
  293. for (const c of changes) {
  294. if (c.cstatus === auditConst.status.back) {
  295. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  296. ' WHERE ca.cid = ? and ca.times = ? and ca.status = ? and ca.uid = pa.id';
  297. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times - 1, c.cstatus];
  298. c.pre = await this.db.queryOne(preSql, preSqlParam);
  299. } else {
  300. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  301. ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id';
  302. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1];
  303. c.pre = await this.db.queryOne(preSql, preSqlParam);
  304. }
  305. }
  306. return changes;
  307. }
  308. /**
  309. * 获取审核人审核的次数
  310. *
  311. * @param auditorId
  312. * @return {Promise<*>}
  313. */
  314. async getCountByChecked(auditorId) {
  315. const sql = 'Select count(*) as count FROM ?? WHERE uid = ? AND usite != 0 AND status in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.back, auditConst.status.backnew]) +')';
  316. const sqlParam = [this.tableName, auditorId];
  317. const result = await this.db.queryOne(sql, sqlParam);
  318. return result.count ? result.count : 0;
  319. // return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
  320. }
  321. /**
  322. * 获取最近一次审批结束时间
  323. *
  324. * @param auditorId
  325. * @return {Promise<*>}
  326. */
  327. async getLastEndTimeByChecked(auditorId) {
  328. const sql = 'SELECT `sin_time` FROM ?? WHERE `uid` = ? AND usite != 0 ' +
  329. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.back, auditConst.status.backnew]) + ') ORDER BY `sin_time` DESC';
  330. const sqlParam = [this.tableName, auditorId];
  331. const result = await this.db.queryOne(sql, sqlParam);
  332. return result ? result.sin_time : null;
  333. }
  334. /**
  335. * 获取 某时间后 审批进度更新的 变更令
  336. * @param {Number} pid - 查询标段
  337. * @param {Number} uid - 查询人
  338. * @param {Date} time - 查询时间
  339. * @return {Promise<*>}
  340. */
  341. async getNoticeChange(pid, uid, time) {
  342. // const sql = 'SELECT * FROM (SELECT t.`id`, t.`name`, t.`type`, t.`user_id`, ' +
  343. // ' ca.`cid`, c.`code` As `c_code`, c.name As `c_name`, ' +
  344. // ' ca.`uid`, ca.`sin_time` As `cu_time`, ca.`status` As `cu_status`, ca.`name` As `cu_name`, ca.`jobs` As `cu_jobs`, ca.company As `cu_company`' +
  345. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `uid` = ? GROUP BY `tid`)) As t' +
  346. // ' LEFT JOIN ?? As c ON c.`tid` = t.`id`' +
  347. // ' LEFT JOIN ?? As ca ON ca.`cid` = c.`cid`' +
  348. // ' WHERE t.`project_id` = ? and `ca`.`sin_time` > ? and `ca`.`usite` != 0 and `ca`.`status` != ?' +
  349. // ' ORDER By ca.`sin_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
  350. // ' ORDER BY new_t.`cu_time`';
  351. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.change.tableName, this.tableName, pid, time, auditConst.status.checking];
  352. // return await this.db.query(sql, sqlParam);
  353. let notice = await this.db.select('zh_notice', {
  354. where: { pid, type: pushType.change, uid },
  355. orders: [['create_time', 'desc']],
  356. limit: 10, offset: 0,
  357. });
  358. notice = notice.map(v => {
  359. const extra = JSON.parse(v.content);
  360. delete v.content;
  361. return { ...v, ...extra };
  362. });
  363. return notice;
  364. }
  365. async getAllAuditors(tenderId) {
  366. const sql = 'SELECT ca.uid, ca.tid FROM ' + this.tableName + ' ca' +
  367. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ca.tid = t.id' +
  368. ' WHERE t.id = ?' +
  369. ' GROUP BY ca.uid';
  370. const sqlParam = [tenderId];
  371. return this.db.query(sql, sqlParam);
  372. }
  373. /**
  374. * 取待审批变更列表(wap用)
  375. *
  376. * @param auditorId
  377. * @return {Promise<*>}
  378. */
  379. async getAuditChangeByWap(uid) {
  380. const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' +
  381. ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, c.`quality`, c.`total_price`,' +
  382. ' t.`name`, t.`type`, t.`user_id`, ' +
  383. ' ti.`deal_info`, ti.`decimal` ' +
  384. ' FROM ?? AS ca, ?? AS c, ?? As t, ?? AS ti ' +
  385. ' WHERE ca.`uid` = ? and ca.`status` = ? and (c.`status` = ? or c.`status` = ?)' +
  386. ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` and ti.`tid` = t.`id`';
  387. const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, this.ctx.service.tenderInfo.tableName, uid, auditConst.auditStatus.checking, auditConst.status.checking, auditConst.status.backnew];
  388. const changes = await this.db.query(sql, sqlParam);
  389. for (const c of changes) {
  390. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  391. ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id';
  392. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1];
  393. c.pre = await this.db.queryOne(preSql, preSqlParam);
  394. }
  395. return await this.db.query(sql, sqlParam);
  396. }
  397. async updateNewAuditList(change, newIdList) {
  398. const transaction = await this.db.beginTransaction();
  399. try {
  400. // 先删除旧的审批流(除了原报),再添加新的
  401. const sql = 'DELETE FROM ?? WHERE `cid`= ? AND `times` = ? AND `usite` != 0';
  402. const sqlParam = [this.tableName, change.cid, change.times];
  403. await transaction.query(sql, sqlParam);
  404. const newAuditors = [];
  405. let order = 1;
  406. let uSort = await transaction.count(this.tableName, { cid: change.cid });
  407. for (const aid of newIdList) {
  408. const accountInfo = await this.ctx.service.projectAccount.getDataById(aid);
  409. newAuditors.push({
  410. tid: change.tid, cid: change.cid, uid: aid,
  411. name: accountInfo.name, jobs: accountInfo.role, company: accountInfo.company,
  412. times: change.times, usite: order, usort: uSort, status: auditConst.auditStatus.uncheck,
  413. });
  414. order++;
  415. uSort++;
  416. }
  417. if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  418. await transaction.commit();
  419. } catch (err) {
  420. await transaction.rollback();
  421. throw err;
  422. }
  423. }
  424. async updateLastAudit(change, auditList, lastId) {
  425. const transaction = await this.db.beginTransaction();
  426. try {
  427. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  428. const idList = this._.map(auditList, 'uid');
  429. let order = idList.length + 1;
  430. if (idList.indexOf(lastId) !== -1) {
  431. const sql = 'DELETE FROM ?? WHERE `cid`= ? AND `times` = ? AND `uid` = ? AND `usite` != 0';
  432. const sqlParam = [this.tableName, change.cid, change.times, lastId];
  433. await transaction.query(sql, sqlParam);
  434. // await transaction.delete(this.tableName, { cid: change.cid, times: change.times, uid: lastId, usite: 0 });
  435. const user = this._.find(auditList, { 'uid': lastId });
  436. // 顺移之后审核人流程顺序
  437. await this._syncOrderByDelete(transaction, change.cid, user.usite, user.usort, change.times);
  438. order = order - 1;
  439. }
  440. // 添加终审
  441. const userInfo = await this.ctx.service.projectAccount.getDataById(lastId);
  442. let uSort = await transaction.count(this.tableName, { cid: change.cid });
  443. const newAuditor = {
  444. tid: change.tid, cid: change.cid, uid: lastId,
  445. name: userInfo.name, jobs: userInfo.role, company: userInfo.company,
  446. times: change.times, usite: order, usort: uSort, status: auditConst.auditStatus.uncheck,
  447. };
  448. await transaction.insert(this.tableName, newAuditor);
  449. await transaction.commit();
  450. } catch (err) {
  451. await transaction.rollback();
  452. throw err;
  453. }
  454. }
  455. /**
  456. * 移除审核人时,同步其后审核人order
  457. * @param transaction - 事务
  458. * @param {Number} changeId - 变更令id
  459. * @param {Number} usite - 审核人id
  460. * @param {Number} usort - 审核人id
  461. * @param {Number} times - 第几次审批
  462. * @return {Promise<*>}
  463. * @private
  464. */
  465. async _syncOrderByDelete(transaction, changeId, usite, usort, times, selfOperate = '-') {
  466. this.initSqlBuilder();
  467. this.sqlBuilder.setAndWhere('cid', {
  468. value: this.db.escape(changeId),
  469. operate: '=',
  470. });
  471. this.sqlBuilder.setAndWhere('usite', {
  472. value: usite,
  473. operate: '>=',
  474. });
  475. this.sqlBuilder.setAndWhere('times', {
  476. value: times,
  477. operate: '=',
  478. });
  479. this.sqlBuilder.setUpdateData('usite', {
  480. value: 1,
  481. selfOperate: selfOperate,
  482. });
  483. this.sqlBuilder.setUpdateData('usort', {
  484. value: 1,
  485. selfOperate: selfOperate,
  486. });
  487. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  488. const data = await transaction.query(sql, sqlParam);
  489. return data;
  490. }
  491. /**
  492. * 获取 当前审核人
  493. *
  494. * @param {Number} cid - 期id
  495. * @param {Number} times - 第几次审批
  496. * @return {Promise<*>}
  497. */
  498. async getCurAuditor(cid, times = 1) {
  499. const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `status` = ? and `times` = ? and usite != 0';
  500. const sqlParam = [this.tableName, cid, auditConst.status.checking, times];
  501. return await this.db.queryOne(sql, sqlParam);
  502. }
  503. /**
  504. * 获取 审核人列表(除去原报)
  505. *
  506. * @param {Number} cid - 变更id
  507. * @param {Number} times - 第几次审批
  508. * @param {Boolean} includeOR - 是否包含原报
  509. * @return {Promise<*>}
  510. */
  511. async getAuditors(cid, times = 1, includeOR = false) {
  512. const sql = `SELECT * FROM ?? WHERE cid = ? and times = ?${includeOR ? '' : ' and usite != 0'}`;
  513. // const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `times` = ? and usite != 0';
  514. const sqlParam = [this.tableName, cid, times];
  515. return await this.db.query(sql, sqlParam);
  516. }
  517. /**
  518. * 新增审核人
  519. *
  520. * @param {Number} cid - 变更令id
  521. * @param {Number} auditorId - 审核人id
  522. * @param {Number} times - 第几次审批
  523. * @return {Promise<number>}
  524. */
  525. async addAuditor(cid, auditorId, times = 1, is_gdzs = 0) {
  526. const transaction = await this.db.beginTransaction();
  527. try {
  528. let newOrder = await transaction.count(this.tableName, { cid, times });
  529. let uSort = await transaction.count(this.tableName, { cid });
  530. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  531. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  532. uSort = is_gdzs === 1 ? uSort - 1 : uSort;
  533. if (is_gdzs) await this._syncOrderByDelete(transaction, cid, newOrder, uSort, times, '+');
  534. const userInfo = await this.ctx.service.projectAccount.getDataById(auditorId);
  535. const newAuditor = {
  536. tid: this.ctx.tender.id, cid: cid, uid: auditorId,
  537. name: userInfo.name, jobs: userInfo.role, company: userInfo.company,
  538. times: times, usite: newOrder, usort: uSort, status: auditConst.auditStatus.uncheck,
  539. };
  540. const result = await transaction.insert(this.tableName, newAuditor);
  541. await transaction.commit();
  542. return result.effectRows = 1;
  543. } catch (err) {
  544. await transaction.rollback();
  545. throw err;
  546. }
  547. return false;
  548. }
  549. /**
  550. * 移除审核人
  551. *
  552. * @param {Number} cid - 变更令id
  553. * @param {Number} auditorId - 审核人id
  554. * @param {Number} times - 第几次审批
  555. * @return {Promise<boolean>}
  556. */
  557. async deleteAuditor(cid, auditorId, times = 1) {
  558. const transaction = await this.db.beginTransaction();
  559. try {
  560. const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `times` = ? and `uid` = ? and `usite` != 0 ORDER BY `usort` DESC';
  561. const sqlParam = [this.tableName, cid, times, auditorId];
  562. const auditor = await transaction.queryOne(sql, sqlParam);
  563. if (!auditor) {
  564. throw '该审核人不存在';
  565. }
  566. await this._syncOrderByDelete(transaction, cid, auditor.usite, auditor.usort, times);
  567. await transaction.delete(this.tableName, { id: auditor.id });
  568. await transaction.commit();
  569. } catch (err) {
  570. await transaction.rollback();
  571. throw err;
  572. }
  573. return true;
  574. }
  575. /**
  576. * 开始审批
  577. * @param {Number} cid - 变更令id
  578. * @param {Number} times - 第几次审批
  579. * @return {Promise<boolean>}
  580. */
  581. async start(cid, times = 1) {
  582. const audit = await this.getDataByCondition({ cid, times, usite: 1 });
  583. const yBAudit = await this.getDataByCondition({ cid, times, usite: 0 });
  584. if (!audit) {
  585. if(this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  586. throw '请联系管理员添加审批人';
  587. } else {
  588. throw '请先选择审批人,再上报数据';
  589. }
  590. }
  591. const transaction = await this.db.beginTransaction();
  592. try {
  593. await transaction.update(this.tableName, { id: audit.id, status: auditConst.auditStatus.checking, sin_time: new Date() });
  594. // 更新原报人审批状态
  595. await transaction.update(this.tableName, {
  596. id: yBAudit.id,
  597. status: auditConst.auditStatus.checked,
  598. sin_time: new Date(),
  599. });
  600. const changeList = await this.ctx.service.changeAuditList.getList(cid);
  601. let total_price = 0;
  602. // 更新清单spamount的值
  603. const updateListData = [];
  604. for (const cl of changeList) {
  605. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, this.ctx.tender.info.decimal.tp));
  606. if(cl.camount !== cl.spamount) {
  607. const uld = {
  608. id: cl.id,
  609. spamount: cl.camount,
  610. };
  611. updateListData.push(uld);
  612. }
  613. }
  614. if(updateListData.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateListData);
  615. const options = {
  616. where: {
  617. cid: cid,
  618. },
  619. };
  620. const updateData = {
  621. total_price,
  622. tp_decimal: this.ctx.tender.info.decimal.tp,
  623. up_decimal: this.ctx.tender.info.decimal.up,
  624. status: auditConst.status.checking,
  625. };
  626. await transaction.update(this.ctx.service.change.tableName, updateData, options);
  627. // 清空audit_amount
  628. if(times > 1) await transaction.update(this.ctx.service.changeAuditList.tableName, { audit_amount: null }, { where: { cid: cid } });
  629. // 添加短信通知-需要审批提醒功能
  630. const sms = new SMS(this.ctx);
  631. const code = await sms.contentChange(this.ctx.change.code);
  632. const shenpiUrl = await this.ctx.helper.urlToShort(
  633. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.change.tid + '/change/' + cid + '/information#shenpi'
  634. );
  635. await this.ctx.helper.sendAliSms(audit.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  636. biangeng: code,
  637. code: shenpiUrl,
  638. });
  639. // 微信模板通知
  640. const wechatData = {
  641. wap_url: shenpiUrl,
  642. status: wxConst.status.check,
  643. tips: wxConst.tips.check,
  644. code: this.ctx.session.sessionProject.code,
  645. c_name: this.ctx.change.name,
  646. };
  647. await this.ctx.helper.sendWechat(audit.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  648. // 重新发送配置
  649. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  650. pid: this.ctx.session.sessionProject.id,
  651. tid: this.ctx.change.tid,
  652. uid: audit.uid,
  653. sp_type: 'change',
  654. sp_id: audit.id,
  655. table_name: this.tableName,
  656. template: wxConst.template.change,
  657. wx_data: wechatData,
  658. });
  659. await transaction.delete(this.ctx.service.changeHistory.tableName, { cid });
  660. await transaction.commit();
  661. } catch (err) {
  662. await transaction.rollback();
  663. throw err;
  664. }
  665. return true;
  666. }
  667. async getNumByMonth(tid, startMonth, endMonth) {
  668. const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT b.id FROM (SELECT * FROM ?? WHERE tid = ? AND usite != 0 GROUP BY id ORDER BY usort DESC) as b GROUP BY b.cid) AND status = ? AND sin_time between ? and ?';
  669. const sqlParam = [this.tableName, this.tableName, tid, auditConst.auditStatus.checked, startMonth, endMonth];
  670. const result = await this.db.queryOne(sql, sqlParam);
  671. return result ? result.num : 0;
  672. }
  673. /**
  674. * 审批撤回
  675. * @param {Number} stageId - 标段id
  676. * @param {Number} times - 第几次审批
  677. * @return {Promise<void>}
  678. */
  679. async checkCancel(change) {
  680. // 分4种情况,根据ctx.cancancel值判断:
  681. // 1.原报发起撤回,当前流程删除,并回到待上报
  682. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  683. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  684. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  685. switch (change.cancancel) {
  686. case 1: await this._userCheckCancel(change); break;
  687. case 2: await this._auditCheckCancel(change); break;
  688. case 3: await this._auditCheckCancelNoPre(change); break;
  689. case 4: await this._auditCheckCancelNo(change); break;
  690. default: throw '不可撤回,请刷新页面重试';
  691. }
  692. // if (stage.cancancel === 5) {
  693. // await this._auditCheckCancelAnd(stage);
  694. // } else {
  695. // switch (this.ctx.stage.cancancel) {
  696. // case 1: await this._userCheckCancel(stage); break;
  697. // case 2: await this._auditCheckCancel(stage); break;
  698. // case 3: await this._auditCheckCancelNoPre(stage); break;
  699. // case 4: await this._auditCheckCancelNo(stage); break;
  700. // default: throw '不可撤回,请刷新页面重试';
  701. // }
  702. // // 通知发送 - 第三方更新
  703. // if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  704. // const base_data = {
  705. // tid: stage.tid,
  706. // sid: stage.id,
  707. // op: 'update',
  708. // };
  709. // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  710. // base_data.op = 'update';
  711. // base_data.sid = -1;
  712. // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  713. // }
  714. // }
  715. }
  716. /**
  717. * 原报撤回,直接改动审批人状态
  718. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  719. *
  720. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  721. * ...
  722. *
  723. * @param stage
  724. * @returns {Promise<void>}
  725. * @private
  726. */
  727. async _userCheckCancel(change) {
  728. const transaction = await this.db.beginTransaction();
  729. try {
  730. // 整理当前流程审核人状态更新
  731. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.auditStatus.checking });
  732. // // 审批人变成待审批状态
  733. await transaction.update(this.tableName, {
  734. id: curAudit.id,
  735. status: auditConst.auditStatus.uncheck,
  736. sin_time: null,
  737. sdesc: null,
  738. });
  739. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  740. // 原报变成checking状态
  741. const ybAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, usite: 0, status: auditConst.auditStatus.checked });
  742. await transaction.update(this.tableName, {
  743. id: ybAudit.id,
  744. status: auditConst.auditStatus.checking,
  745. sin_time: new Date(),
  746. });
  747. // 变更令变成待上报状态
  748. const options = {
  749. where: {
  750. cid: change.cid,
  751. },
  752. };
  753. await transaction.update(this.ctx.service.change.tableName, {
  754. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back,
  755. }, options);
  756. await transaction.commit();
  757. } catch(err) {
  758. await transaction.rollback();
  759. throw err;
  760. }
  761. }
  762. /**
  763. * 审批人撤回审批通过,插入两条数据
  764. *
  765. * 一审 1 A checked 一审 1 A checked
  766. * 二审 2 B checked pre -> 二审 2 B checked
  767. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  768. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  769. * 三审 5 C uncheck order、status改
  770. * 四审 6 D uncheck order改
  771. *
  772. * @param stage
  773. * @returns {Promise<void>}
  774. * @private
  775. */
  776. async _auditCheckCancel(change) {
  777. const time = new Date();
  778. const transaction = await this.db.beginTransaction();
  779. try {
  780. // 整理当前流程审核人状态更新
  781. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking });
  782. const preAudit = change.preAudit;
  783. if (!curAudit || curAudit.usite <= 1 || !preAudit) {
  784. throw '撤回用户数据错误';
  785. }
  786. // 顺移其后审核人流程顺序
  787. const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?';
  788. await transaction.query(sql, [change.cid, change.times, curAudit.usort]);
  789. // 当前审批人2次添加至流程中
  790. const newAuditors = [];
  791. // 先入撤回记录
  792. newAuditors.push({
  793. tid: change.tid,
  794. cid: change.cid,
  795. uid: preAudit.uid,
  796. name: preAudit.name,
  797. jobs: preAudit.jobs,
  798. company: preAudit.company,
  799. times: change.times,
  800. usite: preAudit.usite,
  801. usort: curAudit.usort,
  802. status: auditConst.auditStatus.checkCancel,
  803. sin_time: time,
  804. sdesc: '',
  805. });
  806. newAuditors.push({
  807. tid: change.tid,
  808. cid: change.cid,
  809. uid: preAudit.uid,
  810. name: preAudit.name,
  811. jobs: preAudit.jobs,
  812. company: preAudit.company,
  813. times: change.times,
  814. usite: preAudit.usite,
  815. usort: curAudit.usort + 1,
  816. status: auditConst.auditStatus.checking,
  817. sin_time: time,
  818. });
  819. await transaction.insert(this.tableName, newAuditors);
  820. // 当前审批人变成待审批
  821. await transaction.update(this.tableName, { id: curAudit.id, usort: curAudit.usort + 2, sin_time: null, status: auditConst.auditStatus.uncheck });
  822. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  823. // 审批列表数据也要回退
  824. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  825. where: { cid: change.cid },
  826. });
  827. let total_price = 0;
  828. const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  829. const updateList = [];
  830. for (const cl of changeList) {
  831. const audit_amount = cl.audit_amount.split(',');
  832. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  833. audit_amount.splice(-1, 1);
  834. const list_update = {
  835. id: cl.id,
  836. audit_amount: audit_amount.join(','),
  837. spamount: parseFloat(last_amount),
  838. };
  839. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  840. updateList.push(list_update)
  841. }
  842. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList);
  843. // 变更令变成待上报状态
  844. const options = {
  845. where: {
  846. cid: change.cid,
  847. },
  848. };
  849. await transaction.update(this.ctx.service.change.tableName, {
  850. status: auditConst.status.checking,
  851. total_price,
  852. cin_time: Date.parse(time) / 1000,
  853. }, options);
  854. await transaction.commit();
  855. } catch(err) {
  856. await transaction.rollback();
  857. throw err;
  858. }
  859. }
  860. /**
  861. * 审批人撤回审批退回上一人,插入两条数据
  862. *
  863. * 一审 1 A checked 一审 1 A checked
  864. * 二审 2 B checked 二审 2 B checked
  865. * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre
  866. * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his
  867. * 三审 5 C uncheck 三审 5 C checking status改 增pay_cur
  868. * 四审 6 D uncheck 四审 6 D uncheck
  869. *
  870. * @param stage
  871. * @returns {Promise<void>}
  872. * @private
  873. */
  874. async _auditCheckCancelNoPre(change) {
  875. const time = new Date();
  876. const transaction = await this.db.beginTransaction();
  877. try {
  878. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking });
  879. const preAudit = change.preAudit;
  880. if (!curAudit || curAudit.order <= 1 || !preAudit) {
  881. throw '撤回用户数据错误';
  882. }
  883. // 整理当前流程审核人状态更新
  884. // 删除当前审批人
  885. await transaction.delete(this.tableName, { id: curAudit.id });
  886. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  887. // 添加撤回人到审批流程中
  888. const newAuditors = [];
  889. newAuditors.push({
  890. tid: change.tid,
  891. cid: change.cid,
  892. uid: preAudit.uid,
  893. name: preAudit.name,
  894. jobs: preAudit.jobs,
  895. company: preAudit.company,
  896. times: change.times,
  897. usite: preAudit.usite,
  898. usort: curAudit.usort,
  899. status: auditConst.auditStatus.checkCancel,
  900. sin_time: time,
  901. sdesc: '',
  902. });
  903. await transaction.insert(this.tableName, newAuditors);
  904. // 更新上一个人,最新审批状态为审批中
  905. await transaction.update(this.tableName, { sin_time: time, status: auditConst.status.checking }, {
  906. where: { cid: change.cid, times: change.times, usort: curAudit.usort + 1 }
  907. });
  908. // 回退spamount值数据
  909. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  910. where: { cid: change.cid },
  911. });
  912. let total_price = 0;
  913. const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  914. const updateList = [];
  915. for (const cl of changeList) {
  916. const audit_amount = cl.audit_amount !== '' ? cl.audit_amount.split(',') : [];
  917. // const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  918. audit_amount.push(cl.spamount);
  919. const list_update = {
  920. id: cl.id,
  921. audit_amount: audit_amount.join(','),
  922. };
  923. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(cl.spamount), tp_decimal));
  924. updateList.push(list_update);
  925. }
  926. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList);
  927. // 变更令变成待上报状态
  928. const options = {
  929. where: {
  930. cid: change.cid,
  931. },
  932. };
  933. await transaction.update(this.ctx.service.change.tableName, {
  934. status: auditConst.status.checking,
  935. total_price,
  936. cin_time: Date.parse(time) / 1000,
  937. }, options);
  938. await transaction.commit();
  939. } catch(err) {
  940. await transaction.rollback();
  941. throw err;
  942. }
  943. }
  944. /**
  945. * 审批人撤回审批退回原报
  946. *
  947. * 1# 一审 1 A checked 1# 一审 1 A checked
  948. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  949. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  950. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  951. * 三审 5 C uncheck order改
  952. *
  953. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  954. * 二审 2 B uncheck
  955. * 三审 3 C uncheck
  956. *
  957. * @param stage
  958. * @returns {Promise<void>}
  959. * @private
  960. */
  961. async _auditCheckCancelNo(change) {
  962. const time = new Date();
  963. const transaction = await this.db.beginTransaction();
  964. try {
  965. // const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times - 1, status: auditConst.auditStatus.back });
  966. const curAudit = await this.getAuditorByStatus(change.cid, change.times - 1, auditConst.auditStatus.back);
  967. // 整理上一个流程审核人状态更新
  968. // 顺移其后审核人流程顺序
  969. const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?';
  970. await transaction.query(sql, [change.cid, change.times - 1, curAudit.usort]);
  971. // 当前审批人2次添加至流程中
  972. const newAuditors = [];
  973. newAuditors.push({
  974. tid: change.tid,
  975. cid: change.cid,
  976. uid: curAudit.uid,
  977. name: curAudit.name,
  978. jobs: curAudit.jobs,
  979. company: curAudit.company,
  980. times: curAudit.times,
  981. usite: curAudit.usite,
  982. usort: curAudit.usort + 1,
  983. status: auditConst.auditStatus.checkCancel,
  984. sin_time: time,
  985. sdesc: '',
  986. });
  987. newAuditors.push({
  988. tid: change.tid,
  989. cid: change.cid,
  990. uid: curAudit.uid,
  991. name: curAudit.name,
  992. jobs: curAudit.jobs,
  993. company: curAudit.company,
  994. times: curAudit.times,
  995. usite: curAudit.usite,
  996. usort: curAudit.usort + 2,
  997. status: auditConst.auditStatus.checking,
  998. sin_time: time,
  999. });
  1000. await transaction.insert(this.tableName, newAuditors);
  1001. // 删除当前次审批流
  1002. await transaction.delete(this.tableName, { cid: change.cid, times: change.times });
  1003. // 回退数据
  1004. await this.ctx.service.changeHistory.returnHistory(transaction, change.cid);
  1005. await transaction.delete(this.ctx.service.changeHistory.tableName, { cid: change.cid });
  1006. await transaction.commit();
  1007. } catch(err) {
  1008. await transaction.rollback();
  1009. throw err;
  1010. }
  1011. }
  1012. async getAuditorByStatus(cid, times, status, transaction= null) {
  1013. const sql = 'SELECT * FROM ?? WHERE `cid` = ? AND `times` = ? AND `status` = ? ORDER BY `usort` DESC';
  1014. const sqlParam = [this.tableName, cid, times, status];
  1015. return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
  1016. }
  1017. async saveAudit(cid, times, data) {
  1018. const transaction = await this.db.beginTransaction();
  1019. try {
  1020. const auditors = await this.getListGroupByWithoutYB(cid, times);
  1021. const now_audit = this._.find(auditors, { uid: data.old_aid });
  1022. if (data.operate === 'add') {
  1023. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1024. throw '当前人下无法操作新增';
  1025. }
  1026. const nowAuditInfo = await this.ctx.service.projectAccount.getDataById(data.new_aid);
  1027. const newAudit = {
  1028. tid: this.ctx.tender.id,
  1029. cid,
  1030. uid: data.new_aid,
  1031. name: nowAuditInfo.name,
  1032. company: nowAuditInfo.company,
  1033. jobs: nowAuditInfo.role,
  1034. usort: now_audit.usort+1,
  1035. usite: now_audit.usite+1,
  1036. times: times,
  1037. status: auditConst.auditStatus.uncheck,
  1038. };
  1039. // order+1
  1040. await this._syncOrderByDelete(transaction, cid, now_audit.usite+1, now_audit.usort+1, times, '+');
  1041. await transaction.insert(this.tableName, newAudit);
  1042. // 更新审批流程页数据,如果存在
  1043. } else if (data.operate === 'del') {
  1044. if (now_audit.status !== auditConst.status.uncheck) {
  1045. throw '当前人无法操作删除';
  1046. }
  1047. await transaction.delete(this.tableName, { cid, times, uid: now_audit.uid, usite: now_audit.usite });
  1048. await this._syncOrderByDelete(transaction, cid, now_audit.usite, now_audit.usort, times);
  1049. // 旧的更新为is_old为1
  1050. // await transaction.update(this.tableName, { is_old: 1 }, {
  1051. // where: {
  1052. // sid: stageId,
  1053. // times,
  1054. // aid: data.old_aid,
  1055. // }
  1056. // });
  1057. } else if (data.operate === 'change') {
  1058. const nowAudit = await this.getDataByCondition({ cid, times, uid: now_audit.uid, usite: now_audit.usite });
  1059. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1060. throw '当前人无法操作替换';
  1061. }
  1062. const nowAuditInfo = await this.ctx.service.projectAccount.getDataById(data.new_aid);
  1063. nowAudit.uid = data.new_aid;
  1064. nowAudit.name = nowAuditInfo.name;
  1065. nowAudit.company = nowAuditInfo.company;
  1066. nowAudit.jobs = nowAuditInfo.role;
  1067. await transaction.update(this.tableName, nowAudit);
  1068. // 旧的更新为is_old为1
  1069. // await transaction.update(this.tableName, { is_old: 1 }, {
  1070. // where: {
  1071. // sid: stageId,
  1072. // times,
  1073. // aid: data.old_aid,
  1074. // }
  1075. // });
  1076. }
  1077. if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl || this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs) {
  1078. const newAuditors = await this.getListGroupByWithoutYB(cid, times, transaction);
  1079. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.change, shenpiConst.sp_type.change, this._.map(newAuditors, 'uid'));
  1080. }
  1081. // 更新到审批流程方法
  1082. await transaction.commit();
  1083. } catch (err) {
  1084. await transaction.rollback();
  1085. throw err;
  1086. }
  1087. }
  1088. }
  1089. return ChangeAudit;
  1090. };