change_audit.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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 ?? t1 JOIN (SELECT MAX(id) as max_id FROM ?? WHERE tid = ? AND usite != 0 GROUP BY id ORDER BY usort DESC) t2 ON t1.id = t2.max_id WHERE t1.status = ? AND t1.sin_time between ? and ?';
  669. // 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 ?';
  670. const sqlParam = [this.tableName, this.tableName, tid, auditConst.auditStatus.checked, startMonth, endMonth];
  671. const result = await this.db.queryOne(sql, sqlParam);
  672. return result ? result.num : 0;
  673. }
  674. /**
  675. * 审批撤回
  676. * @param {Number} stageId - 标段id
  677. * @param {Number} times - 第几次审批
  678. * @return {Promise<void>}
  679. */
  680. async checkCancel(change) {
  681. // 分4种情况,根据ctx.cancancel值判断:
  682. // 1.原报发起撤回,当前流程删除,并回到待上报
  683. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  684. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  685. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  686. switch (change.cancancel) {
  687. case 1: await this._userCheckCancel(change); break;
  688. case 2: await this._auditCheckCancel(change); break;
  689. case 3: await this._auditCheckCancelNoPre(change); break;
  690. case 4: await this._auditCheckCancelNo(change); break;
  691. default: throw '不可撤回,请刷新页面重试';
  692. }
  693. // if (stage.cancancel === 5) {
  694. // await this._auditCheckCancelAnd(stage);
  695. // } else {
  696. // switch (this.ctx.stage.cancancel) {
  697. // case 1: await this._userCheckCancel(stage); break;
  698. // case 2: await this._auditCheckCancel(stage); break;
  699. // case 3: await this._auditCheckCancelNoPre(stage); break;
  700. // case 4: await this._auditCheckCancelNo(stage); break;
  701. // default: throw '不可撤回,请刷新页面重试';
  702. // }
  703. // // 通知发送 - 第三方更新
  704. // if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  705. // const base_data = {
  706. // tid: stage.tid,
  707. // sid: stage.id,
  708. // op: 'update',
  709. // };
  710. // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  711. // base_data.op = 'update';
  712. // base_data.sid = -1;
  713. // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  714. // }
  715. // }
  716. }
  717. /**
  718. * 原报撤回,直接改动审批人状态
  719. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  720. *
  721. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  722. * ...
  723. *
  724. * @param stage
  725. * @returns {Promise<void>}
  726. * @private
  727. */
  728. async _userCheckCancel(change) {
  729. const transaction = await this.db.beginTransaction();
  730. try {
  731. // 整理当前流程审核人状态更新
  732. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.auditStatus.checking });
  733. // // 审批人变成待审批状态
  734. await transaction.update(this.tableName, {
  735. id: curAudit.id,
  736. status: auditConst.auditStatus.uncheck,
  737. sin_time: null,
  738. sdesc: null,
  739. });
  740. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  741. // 原报变成checking状态
  742. const ybAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, usite: 0, status: auditConst.auditStatus.checked });
  743. await transaction.update(this.tableName, {
  744. id: ybAudit.id,
  745. status: auditConst.auditStatus.checking,
  746. sin_time: new Date(),
  747. });
  748. // 变更令变成待上报状态
  749. const options = {
  750. where: {
  751. cid: change.cid,
  752. },
  753. };
  754. await transaction.update(this.ctx.service.change.tableName, {
  755. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back,
  756. }, options);
  757. await transaction.commit();
  758. } catch(err) {
  759. await transaction.rollback();
  760. throw err;
  761. }
  762. }
  763. /**
  764. * 审批人撤回审批通过,插入两条数据
  765. *
  766. * 一审 1 A checked 一审 1 A checked
  767. * 二审 2 B checked pre -> 二审 2 B checked
  768. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  769. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  770. * 三审 5 C uncheck order、status改
  771. * 四审 6 D uncheck order改
  772. *
  773. * @param stage
  774. * @returns {Promise<void>}
  775. * @private
  776. */
  777. async _auditCheckCancel(change) {
  778. const time = new Date();
  779. const transaction = await this.db.beginTransaction();
  780. try {
  781. // 整理当前流程审核人状态更新
  782. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking });
  783. const preAudit = change.preAudit;
  784. if (!curAudit || curAudit.usite <= 1 || !preAudit) {
  785. throw '撤回用户数据错误';
  786. }
  787. // 顺移其后审核人流程顺序
  788. const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?';
  789. await transaction.query(sql, [change.cid, change.times, curAudit.usort]);
  790. // 当前审批人2次添加至流程中
  791. const newAuditors = [];
  792. // 先入撤回记录
  793. newAuditors.push({
  794. tid: change.tid,
  795. cid: change.cid,
  796. uid: preAudit.uid,
  797. name: preAudit.name,
  798. jobs: preAudit.jobs,
  799. company: preAudit.company,
  800. times: change.times,
  801. usite: preAudit.usite,
  802. usort: curAudit.usort,
  803. status: auditConst.auditStatus.checkCancel,
  804. sin_time: time,
  805. sdesc: '',
  806. });
  807. newAuditors.push({
  808. tid: change.tid,
  809. cid: change.cid,
  810. uid: preAudit.uid,
  811. name: preAudit.name,
  812. jobs: preAudit.jobs,
  813. company: preAudit.company,
  814. times: change.times,
  815. usite: preAudit.usite,
  816. usort: curAudit.usort + 1,
  817. status: auditConst.auditStatus.checking,
  818. sin_time: time,
  819. });
  820. await transaction.insert(this.tableName, newAuditors);
  821. // 当前审批人变成待审批
  822. await transaction.update(this.tableName, { id: curAudit.id, usort: curAudit.usort + 2, sin_time: null, status: auditConst.auditStatus.uncheck });
  823. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  824. // 审批列表数据也要回退
  825. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  826. where: { cid: change.cid },
  827. });
  828. let total_price = 0;
  829. const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  830. const updateList = [];
  831. for (const cl of changeList) {
  832. const audit_amount = cl.audit_amount.split(',');
  833. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  834. audit_amount.splice(-1, 1);
  835. const list_update = {
  836. id: cl.id,
  837. audit_amount: audit_amount.join(','),
  838. spamount: parseFloat(last_amount),
  839. };
  840. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  841. updateList.push(list_update)
  842. }
  843. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList);
  844. // 变更令变成待上报状态
  845. const options = {
  846. where: {
  847. cid: change.cid,
  848. },
  849. };
  850. await transaction.update(this.ctx.service.change.tableName, {
  851. status: auditConst.status.checking,
  852. total_price,
  853. cin_time: Date.parse(time) / 1000,
  854. }, options);
  855. await transaction.commit();
  856. } catch(err) {
  857. await transaction.rollback();
  858. throw err;
  859. }
  860. }
  861. /**
  862. * 审批人撤回审批退回上一人,插入两条数据
  863. *
  864. * 一审 1 A checked 一审 1 A checked
  865. * 二审 2 B checked 二审 2 B checked
  866. * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre
  867. * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his
  868. * 三审 5 C uncheck 三审 5 C checking status改 增pay_cur
  869. * 四审 6 D uncheck 四审 6 D uncheck
  870. *
  871. * @param stage
  872. * @returns {Promise<void>}
  873. * @private
  874. */
  875. async _auditCheckCancelNoPre(change) {
  876. const time = new Date();
  877. const transaction = await this.db.beginTransaction();
  878. try {
  879. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking });
  880. const preAudit = change.preAudit;
  881. if (!curAudit || curAudit.order <= 1 || !preAudit) {
  882. throw '撤回用户数据错误';
  883. }
  884. // 整理当前流程审核人状态更新
  885. // 删除当前审批人
  886. await transaction.delete(this.tableName, { id: curAudit.id });
  887. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  888. // 添加撤回人到审批流程中
  889. const newAuditors = [];
  890. newAuditors.push({
  891. tid: change.tid,
  892. cid: change.cid,
  893. uid: preAudit.uid,
  894. name: preAudit.name,
  895. jobs: preAudit.jobs,
  896. company: preAudit.company,
  897. times: change.times,
  898. usite: preAudit.usite,
  899. usort: curAudit.usort,
  900. status: auditConst.auditStatus.checkCancel,
  901. sin_time: time,
  902. sdesc: '',
  903. });
  904. await transaction.insert(this.tableName, newAuditors);
  905. // 更新上一个人,最新审批状态为审批中
  906. await transaction.update(this.tableName, { sin_time: time, status: auditConst.status.checking }, {
  907. where: { cid: change.cid, times: change.times, usort: curAudit.usort + 1 }
  908. });
  909. // 回退spamount值数据
  910. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  911. where: { cid: change.cid },
  912. });
  913. let total_price = 0;
  914. const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  915. const updateList = [];
  916. for (const cl of changeList) {
  917. const audit_amount = cl.audit_amount !== '' ? cl.audit_amount.split(',') : [];
  918. // const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  919. audit_amount.push(cl.spamount);
  920. const list_update = {
  921. id: cl.id,
  922. audit_amount: audit_amount.join(','),
  923. };
  924. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(cl.spamount), tp_decimal));
  925. updateList.push(list_update);
  926. }
  927. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList);
  928. // 变更令变成待上报状态
  929. const options = {
  930. where: {
  931. cid: change.cid,
  932. },
  933. };
  934. await transaction.update(this.ctx.service.change.tableName, {
  935. status: auditConst.status.checking,
  936. total_price,
  937. cin_time: Date.parse(time) / 1000,
  938. }, options);
  939. await transaction.commit();
  940. } catch(err) {
  941. await transaction.rollback();
  942. throw err;
  943. }
  944. }
  945. /**
  946. * 审批人撤回审批退回原报
  947. *
  948. * 1# 一审 1 A checked 1# 一审 1 A checked
  949. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  950. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  951. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  952. * 三审 5 C uncheck order改
  953. *
  954. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  955. * 二审 2 B uncheck
  956. * 三审 3 C uncheck
  957. *
  958. * @param stage
  959. * @returns {Promise<void>}
  960. * @private
  961. */
  962. async _auditCheckCancelNo(change) {
  963. const time = new Date();
  964. const transaction = await this.db.beginTransaction();
  965. try {
  966. // const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times - 1, status: auditConst.auditStatus.back });
  967. const curAudit = await this.getAuditorByStatus(change.cid, change.times - 1, auditConst.auditStatus.back);
  968. // 整理上一个流程审核人状态更新
  969. // 顺移其后审核人流程顺序
  970. const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?';
  971. await transaction.query(sql, [change.cid, change.times - 1, curAudit.usort]);
  972. // 当前审批人2次添加至流程中
  973. const newAuditors = [];
  974. newAuditors.push({
  975. tid: change.tid,
  976. cid: change.cid,
  977. uid: curAudit.uid,
  978. name: curAudit.name,
  979. jobs: curAudit.jobs,
  980. company: curAudit.company,
  981. times: curAudit.times,
  982. usite: curAudit.usite,
  983. usort: curAudit.usort + 1,
  984. status: auditConst.auditStatus.checkCancel,
  985. sin_time: time,
  986. sdesc: '',
  987. });
  988. newAuditors.push({
  989. tid: change.tid,
  990. cid: change.cid,
  991. uid: curAudit.uid,
  992. name: curAudit.name,
  993. jobs: curAudit.jobs,
  994. company: curAudit.company,
  995. times: curAudit.times,
  996. usite: curAudit.usite,
  997. usort: curAudit.usort + 2,
  998. status: auditConst.auditStatus.checking,
  999. sin_time: time,
  1000. });
  1001. await transaction.insert(this.tableName, newAuditors);
  1002. // 删除当前次审批流
  1003. await transaction.delete(this.tableName, { cid: change.cid, times: change.times });
  1004. // 回退数据
  1005. await this.ctx.service.changeHistory.returnHistory(transaction, change.cid);
  1006. await transaction.delete(this.ctx.service.changeHistory.tableName, { cid: change.cid });
  1007. await transaction.commit();
  1008. } catch(err) {
  1009. await transaction.rollback();
  1010. throw err;
  1011. }
  1012. }
  1013. async getAuditorByStatus(cid, times, status, transaction= null) {
  1014. const sql = 'SELECT * FROM ?? WHERE `cid` = ? AND `times` = ? AND `status` = ? ORDER BY `usort` DESC';
  1015. const sqlParam = [this.tableName, cid, times, status];
  1016. return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
  1017. }
  1018. async saveAudit(cid, times, data) {
  1019. const transaction = await this.db.beginTransaction();
  1020. try {
  1021. const auditors = await this.getListGroupByWithoutYB(cid, times);
  1022. const now_audit = this._.find(auditors, { uid: data.old_aid });
  1023. if (data.operate === 'add') {
  1024. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1025. throw '当前人下无法操作新增';
  1026. }
  1027. const nowAuditInfo = await this.ctx.service.projectAccount.getDataById(data.new_aid);
  1028. const newAudit = {
  1029. tid: this.ctx.tender.id,
  1030. cid,
  1031. uid: data.new_aid,
  1032. name: nowAuditInfo.name,
  1033. company: nowAuditInfo.company,
  1034. jobs: nowAuditInfo.role,
  1035. usort: now_audit.usort+1,
  1036. usite: now_audit.usite+1,
  1037. times: times,
  1038. status: auditConst.auditStatus.uncheck,
  1039. };
  1040. // order+1
  1041. await this._syncOrderByDelete(transaction, cid, now_audit.usite+1, now_audit.usort+1, times, '+');
  1042. await transaction.insert(this.tableName, newAudit);
  1043. // 更新审批流程页数据,如果存在
  1044. } else if (data.operate === 'del') {
  1045. if (now_audit.status !== auditConst.status.uncheck) {
  1046. throw '当前人无法操作删除';
  1047. }
  1048. await transaction.delete(this.tableName, { cid, times, uid: now_audit.uid, usite: now_audit.usite });
  1049. await this._syncOrderByDelete(transaction, cid, now_audit.usite, now_audit.usort, times);
  1050. // 旧的更新为is_old为1
  1051. // await transaction.update(this.tableName, { is_old: 1 }, {
  1052. // where: {
  1053. // sid: stageId,
  1054. // times,
  1055. // aid: data.old_aid,
  1056. // }
  1057. // });
  1058. } else if (data.operate === 'change') {
  1059. const nowAudit = await this.getDataByCondition({ cid, times, uid: now_audit.uid, usite: now_audit.usite });
  1060. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1061. throw '当前人无法操作替换';
  1062. }
  1063. const nowAuditInfo = await this.ctx.service.projectAccount.getDataById(data.new_aid);
  1064. nowAudit.uid = data.new_aid;
  1065. nowAudit.name = nowAuditInfo.name;
  1066. nowAudit.company = nowAuditInfo.company;
  1067. nowAudit.jobs = nowAuditInfo.role;
  1068. await transaction.update(this.tableName, nowAudit);
  1069. // 旧的更新为is_old为1
  1070. // await transaction.update(this.tableName, { is_old: 1 }, {
  1071. // where: {
  1072. // sid: stageId,
  1073. // times,
  1074. // aid: data.old_aid,
  1075. // }
  1076. // });
  1077. }
  1078. if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl || this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs) {
  1079. const newAuditors = await this.getListGroupByWithoutYB(cid, times, transaction);
  1080. 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'));
  1081. }
  1082. // 更新到审批流程方法
  1083. await transaction.commit();
  1084. } catch (err) {
  1085. await transaction.rollback();
  1086. throw err;
  1087. }
  1088. }
  1089. }
  1090. return ChangeAudit;
  1091. };