change_audit.js 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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: [['usort', '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. * 审批撤回
  649. * @param {Number} stageId - 标段id
  650. * @param {Number} times - 第几次审批
  651. * @return {Promise<void>}
  652. */
  653. async checkCancel(change) {
  654. // 分4种情况,根据ctx.cancancel值判断:
  655. // 1.原报发起撤回,当前流程删除,并回到待上报
  656. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  657. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  658. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  659. switch (change.cancancel) {
  660. case 1: await this._userCheckCancel(change); break;
  661. case 2: await this._auditCheckCancel(change); break;
  662. case 3: await this._auditCheckCancelNoPre(change); break;
  663. case 4: await this._auditCheckCancelNo(change); break;
  664. default: throw '不可撤回,请刷新页面重试';
  665. }
  666. // if (stage.cancancel === 5) {
  667. // await this._auditCheckCancelAnd(stage);
  668. // } else {
  669. // switch (this.ctx.stage.cancancel) {
  670. // case 1: await this._userCheckCancel(stage); break;
  671. // case 2: await this._auditCheckCancel(stage); break;
  672. // case 3: await this._auditCheckCancelNoPre(stage); break;
  673. // case 4: await this._auditCheckCancelNo(stage); break;
  674. // default: throw '不可撤回,请刷新页面重试';
  675. // }
  676. // // 通知发送 - 第三方更新
  677. // if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  678. // const base_data = {
  679. // tid: stage.tid,
  680. // sid: stage.id,
  681. // op: 'update',
  682. // };
  683. // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  684. // base_data.op = 'update';
  685. // base_data.sid = -1;
  686. // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  687. // }
  688. // }
  689. }
  690. /**
  691. * 原报撤回,直接改动审批人状态
  692. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  693. *
  694. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  695. * ...
  696. *
  697. * @param stage
  698. * @returns {Promise<void>}
  699. * @private
  700. */
  701. async _userCheckCancel(change) {
  702. const transaction = await this.db.beginTransaction();
  703. try {
  704. // 整理当前流程审核人状态更新
  705. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.auditStatus.checking });
  706. // // 审批人变成待审批状态
  707. await transaction.update(this.tableName, {
  708. id: curAudit.id,
  709. status: auditConst.auditStatus.uncheck,
  710. sin_time: null,
  711. sdesc: null,
  712. });
  713. // 原报变成checking状态
  714. const ybAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, usite: 0, status: auditConst.auditStatus.checked });
  715. await transaction.update(this.tableName, {
  716. id: ybAudit.id,
  717. status: auditConst.auditStatus.checking,
  718. sin_time: new Date(),
  719. });
  720. // 变更令变成待上报状态
  721. const options = {
  722. where: {
  723. cid: change.cid,
  724. },
  725. };
  726. await transaction.update(this.ctx.service.change.tableName, {
  727. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back,
  728. }, options);
  729. await transaction.commit();
  730. } catch(err) {
  731. await transaction.rollback();
  732. throw err;
  733. }
  734. }
  735. /**
  736. * 审批人撤回审批通过,插入两条数据
  737. *
  738. * 一审 1 A checked 一审 1 A checked
  739. * 二审 2 B checked pre -> 二审 2 B checked
  740. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  741. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  742. * 三审 5 C uncheck order、status改
  743. * 四审 6 D uncheck order改
  744. *
  745. * @param stage
  746. * @returns {Promise<void>}
  747. * @private
  748. */
  749. async _auditCheckCancel(change) {
  750. const time = new Date();
  751. const transaction = await this.db.beginTransaction();
  752. try {
  753. // 整理当前流程审核人状态更新
  754. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking });
  755. const preAudit = change.preAudit;
  756. if (!curAudit || curAudit.usite <= 1 || !preAudit) {
  757. throw '撤回用户数据错误';
  758. }
  759. // 顺移其后审核人流程顺序
  760. const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?';
  761. await transaction.query(sql, [change.cid, change.times, curAudit.usort]);
  762. // 当前审批人2次添加至流程中
  763. const newAuditors = [];
  764. // 先入撤回记录
  765. newAuditors.push({
  766. tid: change.tid,
  767. cid: change.cid,
  768. uid: preAudit.uid,
  769. name: preAudit.name,
  770. jobs: preAudit.jobs,
  771. company: preAudit.company,
  772. times: change.times,
  773. usite: preAudit.usite,
  774. usort: curAudit.usort,
  775. status: auditConst.auditStatus.checkCancel,
  776. sin_time: time,
  777. sdesc: '',
  778. });
  779. newAuditors.push({
  780. tid: change.tid,
  781. cid: change.cid,
  782. uid: preAudit.uid,
  783. name: preAudit.name,
  784. jobs: preAudit.jobs,
  785. company: preAudit.company,
  786. times: change.times,
  787. usite: preAudit.usite,
  788. usort: curAudit.usort + 1,
  789. status: auditConst.auditStatus.checking,
  790. sin_time: time,
  791. });
  792. await transaction.insert(this.tableName, newAuditors);
  793. // 当前审批人变成待审批
  794. await transaction.update(this.tableName, { id: curAudit.id, usort: curAudit.usort + 2, sin_time: null, status: auditConst.auditStatus.uncheck });
  795. // 审批列表数据也要回退
  796. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  797. where: { cid: change.cid },
  798. });
  799. let total_price = 0;
  800. const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  801. const updateList = [];
  802. for (const cl of changeList) {
  803. const audit_amount = cl.audit_amount.split(',');
  804. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  805. audit_amount.splice(-1, 1);
  806. const list_update = {
  807. id: cl.id,
  808. audit_amount: audit_amount.join(','),
  809. spamount: parseFloat(last_amount),
  810. };
  811. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  812. updateList.push(list_update)
  813. }
  814. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList);
  815. // 变更令变成待上报状态
  816. const options = {
  817. where: {
  818. cid: change.cid,
  819. },
  820. };
  821. await transaction.update(this.ctx.service.change.tableName, {
  822. status: auditConst.status.checking,
  823. total_price,
  824. cin_time: Date.parse(time) / 1000,
  825. }, options);
  826. await transaction.commit();
  827. } catch(err) {
  828. await transaction.rollback();
  829. throw err;
  830. }
  831. }
  832. /**
  833. * 审批人撤回审批退回上一人,插入两条数据
  834. *
  835. * 一审 1 A checked 一审 1 A checked
  836. * 二审 2 B checked 二审 2 B checked
  837. * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre
  838. * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his
  839. * 三审 5 C uncheck 三审 5 C checking status改 增pay_cur
  840. * 四审 6 D uncheck 四审 6 D uncheck
  841. *
  842. * @param stage
  843. * @returns {Promise<void>}
  844. * @private
  845. */
  846. async _auditCheckCancelNoPre(change) {
  847. const time = new Date();
  848. const transaction = await this.db.beginTransaction();
  849. try {
  850. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking });
  851. const preAudit = change.preAudit;
  852. if (!curAudit || curAudit.order <= 1 || !preAudit) {
  853. throw '撤回用户数据错误';
  854. }
  855. // 整理当前流程审核人状态更新
  856. // 删除当前审批人
  857. await transaction.delete(this.tableName, { id: curAudit.id });
  858. // 添加撤回人到审批流程中
  859. const newAuditors = [];
  860. newAuditors.push({
  861. tid: change.tid,
  862. cid: change.cid,
  863. uid: preAudit.uid,
  864. name: preAudit.name,
  865. jobs: preAudit.jobs,
  866. company: preAudit.company,
  867. times: change.times,
  868. usite: preAudit.usite,
  869. usort: curAudit.usort,
  870. status: auditConst.auditStatus.checkCancel,
  871. sin_time: time,
  872. sdesc: '',
  873. });
  874. await transaction.insert(this.tableName, newAuditors);
  875. // 更新上一个人,最新审批状态为审批中
  876. await transaction.update(this.tableName, { sin_time: time, status: auditConst.status.checking }, {
  877. where: { cid: change.cid, times: change.times, usort: curAudit.usort + 1 }
  878. });
  879. // 回退spamount值数据
  880. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  881. where: { cid: change.cid },
  882. });
  883. let total_price = 0;
  884. const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  885. const updateList = [];
  886. for (const cl of changeList) {
  887. const audit_amount = cl.audit_amount.split(',');
  888. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  889. const list_update = {
  890. id: cl.id,
  891. spamount: parseFloat(last_amount),
  892. };
  893. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  894. updateList.push(list_update);
  895. }
  896. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList);
  897. // 变更令变成待上报状态
  898. const options = {
  899. where: {
  900. cid: change.cid,
  901. },
  902. };
  903. await transaction.update(this.ctx.service.change.tableName, {
  904. status: auditConst.status.checking,
  905. total_price,
  906. cin_time: Date.parse(time) / 1000,
  907. }, options);
  908. await transaction.commit();
  909. } catch(err) {
  910. await transaction.rollback();
  911. throw err;
  912. }
  913. }
  914. /**
  915. * 审批人撤回审批退回原报
  916. *
  917. * 1# 一审 1 A checked 1# 一审 1 A checked
  918. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  919. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  920. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  921. * 三审 5 C uncheck order改
  922. *
  923. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  924. * 二审 2 B uncheck
  925. * 三审 3 C uncheck
  926. *
  927. * @param stage
  928. * @returns {Promise<void>}
  929. * @private
  930. */
  931. async _auditCheckCancelNo(change) {
  932. const time = new Date();
  933. const transaction = await this.db.beginTransaction();
  934. try {
  935. const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times - 1, status: auditConst.auditStatus.back });
  936. // 整理上一个流程审核人状态更新
  937. // 顺移其后审核人流程顺序
  938. const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?';
  939. await transaction.query(sql, [change.cid, change.times -1, curAudit.usort]);
  940. // 当前审批人2次添加至流程中
  941. const newAuditors = [];
  942. newAuditors.push({
  943. tid: change.tid,
  944. cid: change.cid,
  945. uid: curAudit.uid,
  946. name: curAudit.name,
  947. jobs: curAudit.jobs,
  948. company: curAudit.company,
  949. times: curAudit.times,
  950. usite: curAudit.usite,
  951. usort: curAudit.usort + 1,
  952. status: auditConst.auditStatus.checkCancel,
  953. sin_time: time,
  954. sdesc: '',
  955. });
  956. newAuditors.push({
  957. tid: change.tid,
  958. cid: change.cid,
  959. uid: curAudit.uid,
  960. name: curAudit.name,
  961. jobs: curAudit.jobs,
  962. company: curAudit.company,
  963. times: curAudit.times,
  964. usite: curAudit.usite,
  965. usort: curAudit.usort + 2,
  966. status: auditConst.auditStatus.checking,
  967. sin_time: time,
  968. });
  969. await transaction.insert(this.tableName, newAuditors);
  970. // 删除当前次审批流
  971. await transaction.delete(this.tableName, { cid: change.cid, times: change.times });
  972. // 回退数据
  973. await this.ctx.service.changeHistory.returnHistory(transaction, change.cid);
  974. await transaction.delete(this.ctx.service.changeHistory.tableName, { cid: change.cid });
  975. await transaction.commit();
  976. } catch(err) {
  977. await transaction.rollback();
  978. throw err;
  979. }
  980. }
  981. }
  982. return ChangeAudit;
  983. };