change_audit.js 53 KB

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