change_audit.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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. * @return {Promise<*>}
  509. */
  510. async getAuditors(cid, times = 1) {
  511. const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `times` = ? and usite != 0';
  512. const sqlParam = [this.tableName, cid, times];
  513. return await this.db.query(sql, sqlParam);
  514. }
  515. /**
  516. * 新增审核人
  517. *
  518. * @param {Number} cid - 变更令id
  519. * @param {Number} auditorId - 审核人id
  520. * @param {Number} times - 第几次审批
  521. * @return {Promise<number>}
  522. */
  523. async addAuditor(cid, auditorId, times = 1, is_gdzs = 0) {
  524. const transaction = await this.db.beginTransaction();
  525. try {
  526. let newOrder = await transaction.count(this.tableName, { cid, times });
  527. let uSort = await transaction.count(this.tableName, { cid });
  528. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  529. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  530. uSort = is_gdzs === 1 ? uSort - 1 : uSort;
  531. if (is_gdzs) await this._syncOrderByDelete(transaction, cid, newOrder, uSort, times, '+');
  532. const userInfo = await this.ctx.service.projectAccount.getDataById(auditorId);
  533. const newAuditor = {
  534. tid: this.ctx.tender.id, cid: cid, uid: auditorId,
  535. name: userInfo.name, jobs: userInfo.role, company: userInfo.company,
  536. times: times, usite: newOrder, usort: uSort, status: auditConst.auditStatus.uncheck,
  537. };
  538. const result = await transaction.insert(this.tableName, newAuditor);
  539. await transaction.commit();
  540. return result.effectRows = 1;
  541. } catch (err) {
  542. await transaction.rollback();
  543. throw err;
  544. }
  545. return false;
  546. }
  547. /**
  548. * 移除审核人
  549. *
  550. * @param {Number} cid - 变更令id
  551. * @param {Number} auditorId - 审核人id
  552. * @param {Number} times - 第几次审批
  553. * @return {Promise<boolean>}
  554. */
  555. async deleteAuditor(cid, auditorId, times = 1) {
  556. const transaction = await this.db.beginTransaction();
  557. try {
  558. const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `times` = ? and `uid` = ? and `usite` != 0 ORDER BY `usort` DESC';
  559. const sqlParam = [this.tableName, cid, times, auditorId];
  560. const auditor = await transaction.queryOne(sql, sqlParam);
  561. if (!auditor) {
  562. throw '该审核人不存在';
  563. }
  564. await this._syncOrderByDelete(transaction, cid, auditor.usite, auditor.usort, times);
  565. await transaction.delete(this.tableName, { id: auditor.id });
  566. await transaction.commit();
  567. } catch (err) {
  568. await transaction.rollback();
  569. throw err;
  570. }
  571. return true;
  572. }
  573. /**
  574. * 开始审批
  575. * @param {Number} cid - 变更令id
  576. * @param {Number} times - 第几次审批
  577. * @return {Promise<boolean>}
  578. */
  579. async start(cid, times = 1) {
  580. const audit = await this.getDataByCondition({ cid, times, usite: 1 });
  581. const yBAudit = await this.getDataByCondition({ cid, times, usite: 0 });
  582. if (!audit) {
  583. if(this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  584. throw '请联系管理员添加审批人';
  585. } else {
  586. throw '请先选择审批人,再上报数据';
  587. }
  588. }
  589. const transaction = await this.db.beginTransaction();
  590. try {
  591. await transaction.update(this.tableName, { id: audit.id, status: auditConst.auditStatus.checking, sin_time: new Date() });
  592. // 更新原报人审批状态
  593. await transaction.update(this.tableName, {
  594. id: yBAudit.id,
  595. status: auditConst.auditStatus.checked,
  596. sin_time: new Date(),
  597. });
  598. const changeList = await this.ctx.service.changeAuditList.getList(cid);
  599. let total_price = 0;
  600. // 更新清单spamount的值
  601. const updateListData = [];
  602. for (const cl of changeList) {
  603. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, this.ctx.tender.info.decimal.tp));
  604. if(cl.camount !== cl.spamount) {
  605. const uld = {
  606. id: cl.id,
  607. spamount: cl.camount,
  608. };
  609. updateListData.push(uld);
  610. }
  611. }
  612. if(updateListData.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateListData);
  613. const options = {
  614. where: {
  615. cid: cid,
  616. },
  617. };
  618. const updateData = {
  619. total_price,
  620. tp_decimal: this.ctx.tender.info.decimal.tp,
  621. up_decimal: this.ctx.tender.info.decimal.up,
  622. status: auditConst.status.checking,
  623. };
  624. await transaction.update(this.ctx.service.change.tableName, updateData, options);
  625. // 清空audit_amount
  626. if(times > 1) await transaction.update(this.ctx.service.changeAuditList.tableName, { audit_amount: null }, { where: { cid: cid } });
  627. // 添加短信通知-需要审批提醒功能
  628. const sms = new SMS(this.ctx);
  629. const code = await sms.contentChange(this.ctx.change.code);
  630. const shenpiUrl = await this.ctx.helper.urlToShort(
  631. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.change.tid + '/change/' + cid + '/information#shenpi'
  632. );
  633. await this.ctx.helper.sendAliSms(audit.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  634. biangeng: code,
  635. code: shenpiUrl,
  636. });
  637. // 微信模板通知
  638. const wechatData = {
  639. wap_url: shenpiUrl,
  640. status: wxConst.status.check,
  641. tips: wxConst.tips.check,
  642. code: this.ctx.session.sessionProject.code,
  643. c_name: this.ctx.change.name,
  644. };
  645. await this.ctx.helper.sendWechat(audit.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  646. // 重新发送配置
  647. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  648. pid: this.ctx.session.sessionProject.id,
  649. tid: this.ctx.change.tid,
  650. uid: audit.uid,
  651. sp_type: 'change',
  652. sp_id: audit.id,
  653. table_name: this.tableName,
  654. template: wxConst.template.change,
  655. wx_data: wechatData,
  656. });
  657. await transaction.delete(this.ctx.service.changeHistory.tableName, { cid });
  658. await transaction.commit();
  659. } catch (err) {
  660. await transaction.rollback();
  661. throw err;
  662. }
  663. return true;
  664. }
  665. async getNumByMonth(tid, startMonth, endMonth) {
  666. 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 ?';
  667. const sqlParam = [this.tableName, this.tableName, tid, auditConst.auditStatus.checked, startMonth, endMonth];
  668. const result = await this.db.queryOne(sql, sqlParam);
  669. return result ? result.num : 0;
  670. }
  671. /**
  672. * 审批撤回
  673. * @param {Number} stageId - 标段id
  674. * @param {Number} times - 第几次审批
  675. * @return {Promise<void>}
  676. */
  677. async checkCancel(change) {
  678. // 分4种情况,根据ctx.cancancel值判断:
  679. // 1.原报发起撤回,当前流程删除,并回到待上报
  680. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  681. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  682. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  683. switch (change.cancancel) {
  684. case 1: await this._userCheckCancel(change); break;
  685. case 2: await this._auditCheckCancel(change); break;
  686. case 3: await this._auditCheckCancelNoPre(change); break;
  687. case 4: await this._auditCheckCancelNo(change); break;
  688. default: throw '不可撤回,请刷新页面重试';
  689. }
  690. // if (stage.cancancel === 5) {
  691. // await this._auditCheckCancelAnd(stage);
  692. // } else {
  693. // switch (this.ctx.stage.cancancel) {
  694. // case 1: await this._userCheckCancel(stage); break;
  695. // case 2: await this._auditCheckCancel(stage); break;
  696. // case 3: await this._auditCheckCancelNoPre(stage); break;
  697. // case 4: await this._auditCheckCancelNo(stage); break;
  698. // default: throw '不可撤回,请刷新页面重试';
  699. // }
  700. // // 通知发送 - 第三方更新
  701. // if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  702. // const base_data = {
  703. // tid: stage.tid,
  704. // sid: stage.id,
  705. // op: 'update',
  706. // };
  707. // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  708. // base_data.op = 'update';
  709. // base_data.sid = -1;
  710. // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  711. // }
  712. // }
  713. }
  714. /**
  715. * 原报撤回,直接改动审批人状态
  716. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  717. *
  718. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  719. * ...
  720. *
  721. * @param stage
  722. * @returns {Promise<void>}
  723. * @private
  724. */
  725. async _userCheckCancel(change) {
  726. const transaction = await this.db.beginTransaction();
  727. try {
  728. // 整理当前流程审核人状态更新
  729. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.auditStatus.checking });
  730. // // 审批人变成待审批状态
  731. await transaction.update(this.tableName, {
  732. id: curAudit.id,
  733. status: auditConst.auditStatus.uncheck,
  734. sin_time: null,
  735. sdesc: null,
  736. });
  737. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  738. // 原报变成checking状态
  739. const ybAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, usite: 0, status: auditConst.auditStatus.checked });
  740. await transaction.update(this.tableName, {
  741. id: ybAudit.id,
  742. status: auditConst.auditStatus.checking,
  743. sin_time: new Date(),
  744. });
  745. // 变更令变成待上报状态
  746. const options = {
  747. where: {
  748. cid: change.cid,
  749. },
  750. };
  751. await transaction.update(this.ctx.service.change.tableName, {
  752. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back,
  753. }, options);
  754. await transaction.commit();
  755. } catch(err) {
  756. await transaction.rollback();
  757. throw err;
  758. }
  759. }
  760. /**
  761. * 审批人撤回审批通过,插入两条数据
  762. *
  763. * 一审 1 A checked 一审 1 A checked
  764. * 二审 2 B checked pre -> 二审 2 B checked
  765. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  766. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  767. * 三审 5 C uncheck order、status改
  768. * 四审 6 D uncheck order改
  769. *
  770. * @param stage
  771. * @returns {Promise<void>}
  772. * @private
  773. */
  774. async _auditCheckCancel(change) {
  775. const time = new Date();
  776. const transaction = await this.db.beginTransaction();
  777. try {
  778. // 整理当前流程审核人状态更新
  779. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking });
  780. const preAudit = change.preAudit;
  781. if (!curAudit || curAudit.usite <= 1 || !preAudit) {
  782. throw '撤回用户数据错误';
  783. }
  784. // 顺移其后审核人流程顺序
  785. const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?';
  786. await transaction.query(sql, [change.cid, change.times, curAudit.usort]);
  787. // 当前审批人2次添加至流程中
  788. const newAuditors = [];
  789. // 先入撤回记录
  790. newAuditors.push({
  791. tid: change.tid,
  792. cid: change.cid,
  793. uid: preAudit.uid,
  794. name: preAudit.name,
  795. jobs: preAudit.jobs,
  796. company: preAudit.company,
  797. times: change.times,
  798. usite: preAudit.usite,
  799. usort: curAudit.usort,
  800. status: auditConst.auditStatus.checkCancel,
  801. sin_time: time,
  802. sdesc: '',
  803. });
  804. newAuditors.push({
  805. tid: change.tid,
  806. cid: change.cid,
  807. uid: preAudit.uid,
  808. name: preAudit.name,
  809. jobs: preAudit.jobs,
  810. company: preAudit.company,
  811. times: change.times,
  812. usite: preAudit.usite,
  813. usort: curAudit.usort + 1,
  814. status: auditConst.auditStatus.checking,
  815. sin_time: time,
  816. });
  817. await transaction.insert(this.tableName, newAuditors);
  818. // 当前审批人变成待审批
  819. await transaction.update(this.tableName, { id: curAudit.id, usort: curAudit.usort + 2, sin_time: null, status: auditConst.auditStatus.uncheck });
  820. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  821. // 审批列表数据也要回退
  822. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  823. where: { cid: change.cid },
  824. });
  825. let total_price = 0;
  826. const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  827. const updateList = [];
  828. for (const cl of changeList) {
  829. const audit_amount = cl.audit_amount.split(',');
  830. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  831. audit_amount.splice(-1, 1);
  832. const list_update = {
  833. id: cl.id,
  834. audit_amount: audit_amount.join(','),
  835. spamount: parseFloat(last_amount),
  836. };
  837. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  838. updateList.push(list_update)
  839. }
  840. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList);
  841. // 变更令变成待上报状态
  842. const options = {
  843. where: {
  844. cid: change.cid,
  845. },
  846. };
  847. await transaction.update(this.ctx.service.change.tableName, {
  848. status: auditConst.status.checking,
  849. total_price,
  850. cin_time: Date.parse(time) / 1000,
  851. }, options);
  852. await transaction.commit();
  853. } catch(err) {
  854. await transaction.rollback();
  855. throw err;
  856. }
  857. }
  858. /**
  859. * 审批人撤回审批退回上一人,插入两条数据
  860. *
  861. * 一审 1 A checked 一审 1 A checked
  862. * 二审 2 B checked 二审 2 B checked
  863. * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre
  864. * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his
  865. * 三审 5 C uncheck 三审 5 C checking status改 增pay_cur
  866. * 四审 6 D uncheck 四审 6 D uncheck
  867. *
  868. * @param stage
  869. * @returns {Promise<void>}
  870. * @private
  871. */
  872. async _auditCheckCancelNoPre(change) {
  873. const time = new Date();
  874. const transaction = await this.db.beginTransaction();
  875. try {
  876. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking });
  877. const preAudit = change.preAudit;
  878. if (!curAudit || curAudit.order <= 1 || !preAudit) {
  879. throw '撤回用户数据错误';
  880. }
  881. // 整理当前流程审核人状态更新
  882. // 删除当前审批人
  883. await transaction.delete(this.tableName, { id: curAudit.id });
  884. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  885. // 添加撤回人到审批流程中
  886. const newAuditors = [];
  887. newAuditors.push({
  888. tid: change.tid,
  889. cid: change.cid,
  890. uid: preAudit.uid,
  891. name: preAudit.name,
  892. jobs: preAudit.jobs,
  893. company: preAudit.company,
  894. times: change.times,
  895. usite: preAudit.usite,
  896. usort: curAudit.usort,
  897. status: auditConst.auditStatus.checkCancel,
  898. sin_time: time,
  899. sdesc: '',
  900. });
  901. await transaction.insert(this.tableName, newAuditors);
  902. // 更新上一个人,最新审批状态为审批中
  903. await transaction.update(this.tableName, { sin_time: time, status: auditConst.status.checking }, {
  904. where: { cid: change.cid, times: change.times, usort: curAudit.usort + 1 }
  905. });
  906. // 回退spamount值数据
  907. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  908. where: { cid: change.cid },
  909. });
  910. let total_price = 0;
  911. const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  912. const updateList = [];
  913. for (const cl of changeList) {
  914. const audit_amount = cl.audit_amount !== '' ? cl.audit_amount.split(',') : [];
  915. // const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  916. audit_amount.push(cl.spamount);
  917. const list_update = {
  918. id: cl.id,
  919. audit_amount: audit_amount.join(','),
  920. };
  921. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(cl.spamount), tp_decimal));
  922. updateList.push(list_update);
  923. }
  924. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList);
  925. // 变更令变成待上报状态
  926. const options = {
  927. where: {
  928. cid: change.cid,
  929. },
  930. };
  931. await transaction.update(this.ctx.service.change.tableName, {
  932. status: auditConst.status.checking,
  933. total_price,
  934. cin_time: Date.parse(time) / 1000,
  935. }, options);
  936. await transaction.commit();
  937. } catch(err) {
  938. await transaction.rollback();
  939. throw err;
  940. }
  941. }
  942. /**
  943. * 审批人撤回审批退回原报
  944. *
  945. * 1# 一审 1 A checked 1# 一审 1 A checked
  946. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  947. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  948. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  949. * 三审 5 C uncheck order改
  950. *
  951. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  952. * 二审 2 B uncheck
  953. * 三审 3 C uncheck
  954. *
  955. * @param stage
  956. * @returns {Promise<void>}
  957. * @private
  958. */
  959. async _auditCheckCancelNo(change) {
  960. const time = new Date();
  961. const transaction = await this.db.beginTransaction();
  962. try {
  963. // const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times - 1, status: auditConst.auditStatus.back });
  964. const curAudit = await this.getAuditorByStatus(change.cid, change.times - 1, auditConst.auditStatus.back);
  965. // 整理上一个流程审核人状态更新
  966. // 顺移其后审核人流程顺序
  967. const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?';
  968. await transaction.query(sql, [change.cid, change.times - 1, curAudit.usort]);
  969. // 当前审批人2次添加至流程中
  970. const newAuditors = [];
  971. newAuditors.push({
  972. tid: change.tid,
  973. cid: change.cid,
  974. uid: curAudit.uid,
  975. name: curAudit.name,
  976. jobs: curAudit.jobs,
  977. company: curAudit.company,
  978. times: curAudit.times,
  979. usite: curAudit.usite,
  980. usort: curAudit.usort + 1,
  981. status: auditConst.auditStatus.checkCancel,
  982. sin_time: time,
  983. sdesc: '',
  984. });
  985. newAuditors.push({
  986. tid: change.tid,
  987. cid: change.cid,
  988. uid: curAudit.uid,
  989. name: curAudit.name,
  990. jobs: curAudit.jobs,
  991. company: curAudit.company,
  992. times: curAudit.times,
  993. usite: curAudit.usite,
  994. usort: curAudit.usort + 2,
  995. status: auditConst.auditStatus.checking,
  996. sin_time: time,
  997. });
  998. await transaction.insert(this.tableName, newAuditors);
  999. // 删除当前次审批流
  1000. await transaction.delete(this.tableName, { cid: change.cid, times: change.times });
  1001. // 回退数据
  1002. await this.ctx.service.changeHistory.returnHistory(transaction, change.cid);
  1003. await transaction.delete(this.ctx.service.changeHistory.tableName, { cid: change.cid });
  1004. await transaction.commit();
  1005. } catch(err) {
  1006. await transaction.rollback();
  1007. throw err;
  1008. }
  1009. }
  1010. async getAuditorByStatus(cid, times, status, transaction= null) {
  1011. const sql = 'SELECT * FROM ?? WHERE `cid` = ? AND `times` = ? AND `status` = ? ORDER BY `usort` DESC';
  1012. const sqlParam = [this.tableName, cid, times, status];
  1013. return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
  1014. }
  1015. async saveAudit(cid, times, data) {
  1016. const transaction = await this.db.beginTransaction();
  1017. try {
  1018. const auditors = await this.getListGroupByWithoutYB(cid, times);
  1019. const now_audit = this._.find(auditors, { uid: data.old_aid });
  1020. if (data.operate === 'add') {
  1021. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1022. throw '当前人下无法操作新增';
  1023. }
  1024. const nowAuditInfo = await this.ctx.service.projectAccount.getDataById(data.new_aid);
  1025. const newAudit = {
  1026. tid: this.ctx.tender.id,
  1027. cid,
  1028. uid: data.new_aid,
  1029. name: nowAuditInfo.name,
  1030. company: nowAuditInfo.company,
  1031. jobs: nowAuditInfo.role,
  1032. usort: now_audit.usort+1,
  1033. usite: now_audit.usite+1,
  1034. times: times,
  1035. status: auditConst.auditStatus.uncheck,
  1036. };
  1037. // order+1
  1038. await this._syncOrderByDelete(transaction, cid, now_audit.usite+1, now_audit.usort+1, times, '+');
  1039. await transaction.insert(this.tableName, newAudit);
  1040. // 更新审批流程页数据,如果存在
  1041. } else if (data.operate === 'del') {
  1042. if (now_audit.status !== auditConst.status.uncheck) {
  1043. throw '当前人无法操作删除';
  1044. }
  1045. await transaction.delete(this.tableName, { cid, times, uid: now_audit.uid, usite: now_audit.usite });
  1046. await this._syncOrderByDelete(transaction, cid, now_audit.usite, now_audit.usort, times);
  1047. // 旧的更新为is_old为1
  1048. // await transaction.update(this.tableName, { is_old: 1 }, {
  1049. // where: {
  1050. // sid: stageId,
  1051. // times,
  1052. // aid: data.old_aid,
  1053. // }
  1054. // });
  1055. } else if (data.operate === 'change') {
  1056. const nowAudit = await this.getDataByCondition({ cid, times, uid: now_audit.uid, usite: now_audit.usite });
  1057. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1058. throw '当前人无法操作替换';
  1059. }
  1060. const nowAuditInfo = await this.ctx.service.projectAccount.getDataById(data.new_aid);
  1061. nowAudit.uid = data.new_aid;
  1062. nowAudit.name = nowAuditInfo.name;
  1063. nowAudit.company = nowAuditInfo.company;
  1064. nowAudit.jobs = nowAuditInfo.role;
  1065. await transaction.update(this.tableName, nowAudit);
  1066. // 旧的更新为is_old为1
  1067. // await transaction.update(this.tableName, { is_old: 1 }, {
  1068. // where: {
  1069. // sid: stageId,
  1070. // times,
  1071. // aid: data.old_aid,
  1072. // }
  1073. // });
  1074. }
  1075. if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl || this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs) {
  1076. const newAuditors = await this.getListGroupByWithoutYB(cid, times, transaction);
  1077. 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'));
  1078. }
  1079. // 更新到审批流程方法
  1080. await transaction.commit();
  1081. } catch (err) {
  1082. await transaction.rollback();
  1083. throw err;
  1084. }
  1085. }
  1086. }
  1087. return ChangeAudit;
  1088. };