change_audit.js 28 KB

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