change_audit.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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: [['id', '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.back && uid !== change.uid) {
  111. // 被退回但你不是原报人
  112. return 3;
  113. } else if (change.status === statusConst.checked) {
  114. // 已完成
  115. return 4;
  116. } else if (change.status === statusConst.checkNo) {
  117. // 已终止
  118. return 5;
  119. } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo.length > 0 && changeAuditInfo[0].status === auditStatusConst.checking) {
  120. // 待你审批
  121. return 6;
  122. } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo.length > 0 && changeAuditInfo[0].status !== auditStatusConst.checking) {
  123. // 审批中但你未到你审批或你已审批
  124. return 7;
  125. } else if (this.ctx.tender.isTourist) {
  126. // 游客模式,只预览不能操作
  127. return 8;
  128. }
  129. // 无权限查看此变更令
  130. return 0;
  131. }
  132. /**
  133. * 根据用户查看此变更状态获取审批人列表
  134. * @param {Object} change - 变更令
  135. * @param {int} status - 状态
  136. * @return {object} list - 列表
  137. */
  138. async getListByStatus(change, status) {
  139. let sql = '';
  140. let sqlParam = '';
  141. switch (status) {
  142. case 1:// 待上报
  143. case 2:// 待重新上报
  144. sql = 'SELECT * FROM ?? WHERE ' +
  145. 'cid = ? AND times = ? GROUP BY usite';
  146. sqlParam = [this.tableName, change.cid,
  147. change.times];
  148. break;
  149. case 3: // 被退回但你不是原报人
  150. case 4:// 已完成
  151. case 5:// 已终止
  152. case 7:// 审批中但你未到你审批或你已审批
  153. case 8:// 游客
  154. // 获取完整的审批顺序
  155. sql = 'SELECT * FROM ?? WHERE ' +
  156. 'cid = ? ORDER BY usort';
  157. sqlParam = [this.tableName, change.cid];
  158. break;
  159. case 6: // 审批中
  160. sql = 'SELECT * FROM (SELECT MAX(usort) as ust FROM ?? ' +
  161. 'WHERE cid = ? and times = ? GROUP BY usite ) as b ' +
  162. 'JOIN ?? as a ON a.usort = b.ust WHERE cid = ? and times = ? ORDER BY usite';
  163. sqlParam = [this.tableName, change.cid, change.times, this.tableName, change.cid, change.times];
  164. // sql = 'SELECT * FROM ?? WHERE ' +
  165. // 'cid = ? AND times = ? ORDER BY usort';
  166. // sqlParam = [this.tableName, change.cid, change.times];
  167. break;
  168. default:// 无权限查看此变更令
  169. break;
  170. }
  171. const list = await this.db.query(sql, sqlParam);
  172. return list;
  173. }
  174. /**
  175. * 删除变更令审批人列表
  176. * @param {Object} transaction - 事务
  177. * @param {Object} cid - 变更令id
  178. * @param {int} times - 次数
  179. * @return {object} 返回结果
  180. */
  181. async deleteAuditData(transaction, cid, times) {
  182. this.initSqlBuilder();
  183. this.sqlBuilder.setAndWhere('cid', {
  184. value: this.db.escape(cid),
  185. operate: '=',
  186. });
  187. this.sqlBuilder.setAndWhere('times', {
  188. value: times,
  189. operate: '=',
  190. });
  191. this.sqlBuilder.setAndWhere('usite', {
  192. value: 0,
  193. operate: '!=',
  194. });
  195. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'delete');
  196. const result = await transaction.query(sql, sqlParam);
  197. return result;
  198. }
  199. /**
  200. * 获取不重复列表
  201. * @param {Object} cid - 变更令id
  202. * @param {int} times - 次数
  203. * @return {object} 返回结果
  204. */
  205. async getListGroupByTimes(cid, times) {
  206. const sql = 'SELECT * FROM ?? where id in (SELECT MAX(id) FROM ?? WHERE ' +
  207. 'cid = ? AND times = ? GROUP BY usite) ORDER BY usite asc';
  208. const sqlParam = [this.tableName, this.tableName, cid, times];
  209. const list = await this.db.query(sql, sqlParam);
  210. return list;
  211. }
  212. async getListOrderByTimes(cid, times) {
  213. const sql = 'SELECT * FROM ?? WHERE ' +
  214. 'cid = ? AND times = ? ORDER BY usort';
  215. const sqlParam = [this.tableName, cid, times];
  216. const list = await this.db.query(sql, sqlParam);
  217. return list;
  218. }
  219. /**
  220. * 获取比sort大的审批人列表
  221. * @param {Object} cid - 变更令id
  222. * @param {int} usort - 排序
  223. * @return {object} 返回结果
  224. */
  225. async getNextAuditList(cid, usort) {
  226. const sql = 'SELECT * FROM ?? WHERE ' +
  227. 'cid = ? AND usort > ?';
  228. const sqlParam = [this.tableName, cid, usort];
  229. const list = await this.db.query(sql, sqlParam);
  230. return list;
  231. }
  232. /**
  233. * 获取除当前次数的审批人列表
  234. * @param {Object} cid - 变更令id
  235. * @param {int} times - 次数
  236. * @return {object} 返回结果
  237. */
  238. async getListByBack(cid, times) {
  239. this.initSqlBuilder();
  240. this.sqlBuilder.setAndWhere('cid', {
  241. value: this.db.escape(cid),
  242. operate: '=',
  243. });
  244. this.sqlBuilder.setAndWhere('times', {
  245. value: times,
  246. operate: '!=',
  247. });
  248. this.sqlBuilder.orderBy = [['usort', 'ASC']];
  249. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  250. const result = await this.db.query(sql, sqlParam);
  251. return result;
  252. }
  253. /**
  254. * 获取 审核人 待审批的() 变更令列表
  255. * @param uid
  256. * @return {Promise<void>}
  257. */
  258. async getAuditChange(uid) {
  259. const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' +
  260. ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, ' +
  261. ' t.`name`, t.`type`, t.`user_id` ' +
  262. ' FROM ?? AS ca, ?? AS c, ?? As t ' +
  263. ' WHERE ca.`uid` = ? and ca.`status` = ? and c.`status` != ?' +
  264. ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` ORDER BY ca.`sin_time` DESC';
  265. const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, uid, 2, auditConst.status.uncheck];
  266. const changes = await this.db.query(sql, sqlParam);
  267. for (const c of changes) {
  268. if (c.cstatus === auditConst.status.back) {
  269. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  270. ' WHERE ca.cid = ? and ca.times = ? and ca.status = ? and ca.uid = pa.id';
  271. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times - 1, c.cstatus];
  272. c.pre = await this.db.queryOne(preSql, preSqlParam);
  273. } else {
  274. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  275. ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id';
  276. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1];
  277. c.pre = await this.db.queryOne(preSql, preSqlParam);
  278. }
  279. }
  280. return changes;
  281. }
  282. /**
  283. * 获取 某时间后 审批进度更新的 变更令
  284. * @param {Number} pid - 查询标段
  285. * @param {Number} uid - 查询人
  286. * @param {Date} time - 查询时间
  287. * @return {Promise<*>}
  288. */
  289. async getNoticeChange(pid, uid, time) {
  290. // const sql = 'SELECT * FROM (SELECT t.`id`, t.`name`, t.`type`, t.`user_id`, ' +
  291. // ' ca.`cid`, c.`code` As `c_code`, c.name As `c_name`, ' +
  292. // ' 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`' +
  293. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `uid` = ? GROUP BY `tid`)) As t' +
  294. // ' LEFT JOIN ?? As c ON c.`tid` = t.`id`' +
  295. // ' LEFT JOIN ?? As ca ON ca.`cid` = c.`cid`' +
  296. // ' WHERE t.`project_id` = ? and `ca`.`sin_time` > ? and `ca`.`usite` != 0 and `ca`.`status` != ?' +
  297. // ' ORDER By ca.`sin_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
  298. // ' ORDER BY new_t.`cu_time`';
  299. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.change.tableName, this.tableName, pid, time, auditConst.status.checking];
  300. // return await this.db.query(sql, sqlParam);
  301. let notice = await this.db.select('zh_notice', {
  302. where: { pid, type: pushType.change, uid },
  303. orders: [['create_time', 'desc']],
  304. limit: 10, offset: 0,
  305. });
  306. notice = notice.map(v => {
  307. const extra = JSON.parse(v.content);
  308. delete v.content;
  309. return { ...v, ...extra };
  310. });
  311. return notice;
  312. }
  313. async getAllAuditors(tenderId) {
  314. const sql = 'SELECT ca.uid, ca.tid FROM ' + this.tableName + ' ca' +
  315. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ca.tid = t.id' +
  316. ' WHERE t.id = ?' +
  317. ' GROUP BY ca.uid';
  318. const sqlParam = [tenderId];
  319. return this.db.query(sql, sqlParam);
  320. }
  321. /**
  322. * 取待审批变更列表(wap用)
  323. *
  324. * @param auditorId
  325. * @return {Promise<*>}
  326. */
  327. async getAuditChangeByWap(uid) {
  328. const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' +
  329. ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, c.`quality`, c.`total_price`,' +
  330. ' t.`name`, t.`type`, t.`user_id`, ' +
  331. ' ti.`deal_info`, ti.`decimal` ' +
  332. ' FROM ?? AS ca, ?? AS c, ?? As t, ?? AS ti ' +
  333. ' WHERE ca.`uid` = ? and ca.`status` = ? and c.`status` != ? and c.`status` != ?' +
  334. ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` and ti.`tid` = t.`id`';
  335. 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.uncheck, auditConst.status.back];
  336. const changes = await this.db.query(sql, sqlParam);
  337. for (const c of changes) {
  338. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  339. ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id';
  340. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1];
  341. c.pre = await this.db.queryOne(preSql, preSqlParam);
  342. }
  343. return await this.db.query(sql, sqlParam);
  344. }
  345. async updateNewAuditList(change, newIdList) {
  346. const transaction = await this.db.beginTransaction();
  347. try {
  348. // 先删除旧的审批流(除了原报),再添加新的
  349. const sql = 'DELETE FROM ?? WHERE `cid`= ? AND `times` = ? AND `usite` != 0';
  350. const sqlParam = [this.tableName, change.cid, change.times];
  351. await transaction.query(sql, sqlParam);
  352. const newAuditors = [];
  353. let order = 1;
  354. let uSort = await transaction.count(this.tableName, { cid: change.cid });
  355. for (const aid of newIdList) {
  356. const accountInfo = await this.ctx.service.projectAccount.getDataById(aid);
  357. newAuditors.push({
  358. tid: change.tid, cid: change.cid, uid: aid,
  359. name: accountInfo.name, jobs: accountInfo.role, company: accountInfo.company,
  360. times: change.times, usite: order, usort: uSort, status: auditConst.auditStatus.uncheck,
  361. });
  362. order++;
  363. uSort++;
  364. }
  365. if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  366. await transaction.commit();
  367. } catch (err) {
  368. await transaction.rollback();
  369. throw err;
  370. }
  371. }
  372. async updateLastAudit(change, auditList, lastId) {
  373. const transaction = await this.db.beginTransaction();
  374. try {
  375. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  376. const idList = this._.map(auditList, 'uid');
  377. let order = idList.length + 1;
  378. if (idList.indexOf(lastId) !== -1) {
  379. const sql = 'DELETE FROM ?? WHERE `cid`= ? AND `times` = ? AND `uid` = ? AND `usite` != 0';
  380. const sqlParam = [this.tableName, change.cid, change.times, lastId];
  381. await transaction.query(sql, sqlParam);
  382. // await transaction.delete(this.tableName, { cid: change.cid, times: change.times, uid: lastId, usite: 0 });
  383. const user = this._.find(auditList, { 'uid': lastId });
  384. // 顺移之后审核人流程顺序
  385. await this._syncOrderByDelete(transaction, change.cid, user.usite, user.usort, change.times);
  386. order = order - 1;
  387. }
  388. // 添加终审
  389. const userInfo = await this.ctx.service.projectAccount.getDataById(lastId);
  390. let uSort = await transaction.count(this.tableName, { cid: change.cid });
  391. const newAuditor = {
  392. tid: change.tid, cid: change.cid, uid: lastId,
  393. name: userInfo.name, jobs: userInfo.role, company: userInfo.company,
  394. times: change.times, usite: order, usort: uSort, status: auditConst.auditStatus.uncheck,
  395. };
  396. await transaction.insert(this.tableName, newAuditor);
  397. await transaction.commit();
  398. } catch (err) {
  399. await transaction.rollback();
  400. throw err;
  401. }
  402. }
  403. /**
  404. * 移除审核人时,同步其后审核人order
  405. * @param transaction - 事务
  406. * @param {Number} changeId - 变更令id
  407. * @param {Number} usite - 审核人id
  408. * @param {Number} usort - 审核人id
  409. * @param {Number} times - 第几次审批
  410. * @return {Promise<*>}
  411. * @private
  412. */
  413. async _syncOrderByDelete(transaction, changeId, usite, usort, times, selfOperate = '-') {
  414. this.initSqlBuilder();
  415. this.sqlBuilder.setAndWhere('cid', {
  416. value: this.db.escape(changeId),
  417. operate: '=',
  418. });
  419. this.sqlBuilder.setAndWhere('usite', {
  420. value: usite,
  421. operate: '>=',
  422. });
  423. this.sqlBuilder.setAndWhere('times', {
  424. value: times,
  425. operate: '=',
  426. });
  427. this.sqlBuilder.setUpdateData('usite', {
  428. value: 1,
  429. selfOperate: selfOperate,
  430. });
  431. this.sqlBuilder.setUpdateData('usort', {
  432. value: 1,
  433. selfOperate: selfOperate,
  434. });
  435. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  436. const data = await transaction.query(sql, sqlParam);
  437. return data;
  438. }
  439. /**
  440. * 获取 当前审核人
  441. *
  442. * @param {Number} cid - 期id
  443. * @param {Number} times - 第几次审批
  444. * @return {Promise<*>}
  445. */
  446. async getCurAuditor(cid, times = 1) {
  447. const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `status` = ? and `times` = ? and usite != 0';
  448. const sqlParam = [this.tableName, cid, auditConst.status.checking, times];
  449. return await this.db.queryOne(sql, sqlParam);
  450. }
  451. /**
  452. * 获取 审核人列表(除去原报)
  453. *
  454. * @param {Number} cid - 变更id
  455. * @param {Number} times - 第几次审批
  456. * @return {Promise<*>}
  457. */
  458. async getAuditors(cid, times = 1) {
  459. const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `times` = ? and usite != 0';
  460. const sqlParam = [this.tableName, cid, times];
  461. return await this.db.query(sql, sqlParam);
  462. }
  463. /**
  464. * 新增审核人
  465. *
  466. * @param {Number} cid - 变更令id
  467. * @param {Number} auditorId - 审核人id
  468. * @param {Number} times - 第几次审批
  469. * @return {Promise<number>}
  470. */
  471. async addAuditor(cid, auditorId, times = 1, is_gdzs = 0) {
  472. const transaction = await this.db.beginTransaction();
  473. try {
  474. let newOrder = await transaction.count(this.tableName, { cid, times });
  475. let uSort = await transaction.count(this.tableName, { cid });
  476. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  477. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  478. uSort = is_gdzs === 1 ? uSort - 1 : uSort;
  479. if (is_gdzs) await this._syncOrderByDelete(transaction, cid, newOrder, uSort, times, '+');
  480. const userInfo = await this.ctx.service.projectAccount.getDataById(auditorId);
  481. const newAuditor = {
  482. tid: this.ctx.tender.id, cid: cid, uid: auditorId,
  483. name: userInfo.name, jobs: userInfo.role, company: userInfo.company,
  484. times: times, usite: newOrder, usort: uSort, status: auditConst.auditStatus.uncheck,
  485. };
  486. const result = await transaction.insert(this.tableName, newAuditor);
  487. await transaction.commit();
  488. return result.effectRows = 1;
  489. } catch (err) {
  490. await transaction.rollback();
  491. throw err;
  492. }
  493. return false;
  494. }
  495. /**
  496. * 移除审核人
  497. *
  498. * @param {Number} cid - 变更令id
  499. * @param {Number} auditorId - 审核人id
  500. * @param {Number} times - 第几次审批
  501. * @return {Promise<boolean>}
  502. */
  503. async deleteAuditor(cid, auditorId, times = 1) {
  504. const transaction = await this.db.beginTransaction();
  505. try {
  506. const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `times` = ? and `uid` = ? and `usite` != 0 ORDER BY `usort` DESC';
  507. const sqlParam = [this.tableName, cid, times, auditorId];
  508. const auditor = await transaction.queryOne(sql, sqlParam);
  509. if (!auditor) {
  510. throw '该审核人不存在';
  511. }
  512. await this._syncOrderByDelete(transaction, cid, auditor.usite, auditor.usort, times);
  513. await transaction.delete(this.tableName, { id: auditor.id });
  514. await transaction.commit();
  515. } catch (err) {
  516. await transaction.rollback();
  517. throw err;
  518. }
  519. return true;
  520. }
  521. /**
  522. * 开始审批
  523. * @param {Number} cid - 变更令id
  524. * @param {Number} times - 第几次审批
  525. * @return {Promise<boolean>}
  526. */
  527. async start(cid, times = 1) {
  528. const audit = await this.getDataByCondition({ cid, times, usite: 1 });
  529. const yBAudit = await this.getDataByCondition({ cid, times, usite: 0 });
  530. if (!audit) {
  531. if(this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  532. throw '请联系管理员添加审批人';
  533. } else {
  534. throw '请先选择审批人,再上报数据';
  535. }
  536. }
  537. const transaction = await this.db.beginTransaction();
  538. try {
  539. await transaction.update(this.tableName, { id: audit.id, status: auditConst.auditStatus.checking, sin_time: new Date() });
  540. // 更新原报人审批状态
  541. await transaction.update(this.tableName, {
  542. id: yBAudit.id,
  543. status: auditConst.auditStatus.checked,
  544. sin_time: new Date(),
  545. });
  546. const changeList = await this.ctx.service.changeAuditList.getList(cid);
  547. let total_price = 0;
  548. // 更新清单spamount的值
  549. const updateListData = [];
  550. for (const cl of changeList) {
  551. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, this.ctx.tender.info.decimal.tp));
  552. if(cl.camount !== cl.spamount) {
  553. const uld = {
  554. id: cl.id,
  555. spamount: cl.camount,
  556. };
  557. updateListData.push(uld);
  558. }
  559. }
  560. if(updateListData.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateListData);
  561. const options = {
  562. where: {
  563. cid: cid,
  564. },
  565. };
  566. const updateData = {
  567. total_price,
  568. tp_decimal: this.ctx.tender.info.decimal.tp,
  569. status: auditConst.status.checking,
  570. };
  571. await transaction.update(this.ctx.service.change.tableName, updateData, options);
  572. // 添加短信通知-需要审批提醒功能
  573. const sms = new SMS(this.ctx);
  574. const code = await sms.contentChange(this.ctx.change.code);
  575. const shenpiUrl = await this.ctx.helper.urlToShort(
  576. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.change.tid + '/change/' + cid + '/info#shenpi'
  577. );
  578. await this.ctx.helper.sendAliSms(audit.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  579. biangeng: code,
  580. code: shenpiUrl,
  581. });
  582. // 微信模板通知
  583. const wechatData = {
  584. wap_url: shenpiUrl,
  585. status: wxConst.status.check,
  586. tips: wxConst.tips.check,
  587. code: this.ctx.session.sessionProject.code,
  588. c_name: this.ctx.change.name,
  589. };
  590. await this.ctx.helper.sendWechat(audit.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  591. await transaction.commit();
  592. } catch (err) {
  593. await transaction.rollback();
  594. throw err;
  595. }
  596. return true;
  597. }
  598. }
  599. return ChangeAudit;
  600. };