change_audit.js 29 KB

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