change_audit.js 29 KB

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