change_audit.js 31 KB

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