change_audit.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. const pushType = require('../const/audit').pushType;
  11. module.exports = app => {
  12. class ChangeAudit extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'change_audit';
  22. }
  23. /**
  24. * 获取最后一位审批人
  25. * @param {int} cid - 变更令id
  26. * @param {int} times - 次数
  27. * @param {int} site - 位置
  28. * @param {int} status - 状态
  29. * @return {void}
  30. */
  31. async getLastUser(cid, times, site = 1, status = 1) {
  32. this.initSqlBuilder();
  33. this.sqlBuilder.setAndWhere('cid', {
  34. value: this.db.escape(cid),
  35. operate: '=',
  36. });
  37. this.sqlBuilder.setAndWhere('times', {
  38. value: times,
  39. operate: '=',
  40. });
  41. if (status === 1) {
  42. this.sqlBuilder.setAndWhere('status', {
  43. value: 1,
  44. operate: '!=',
  45. });
  46. }
  47. if (site === 0) {
  48. this.sqlBuilder.setAndWhere('usite', {
  49. value: site,
  50. operate: '=',
  51. });
  52. }
  53. const u_sort = [['usort', 'DESC']];
  54. this.sqlBuilder.orderBy = u_sort;
  55. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  56. const data = await this.db.queryOne(sql, sqlParam);
  57. return data;
  58. }
  59. /**
  60. * 获取最后一位退回的审批人
  61. * @param {int} cid - 变更令id
  62. * @param {int} times - 次数
  63. * @return {void}
  64. */
  65. async getLastBackUser(cid, times) {
  66. this.initSqlBuilder();
  67. this.sqlBuilder.setAndWhere('cid', {
  68. value: this.db.escape(cid),
  69. operate: '=',
  70. });
  71. this.sqlBuilder.setAndWhere('times', {
  72. value: times,
  73. operate: '=',
  74. });
  75. this.sqlBuilder.setAndWhere('status', {
  76. value: 6,
  77. operate: '=',
  78. });
  79. const u_sort = [['usort', 'DESC']];
  80. this.sqlBuilder.orderBy = u_sort;
  81. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  82. const data = await this.db.queryOne(sql, sqlParam);
  83. return data;
  84. }
  85. /**
  86. * 获取当前审批人查看info时的状态
  87. * @param {Object} change - 变更令数据
  88. * @return {void}
  89. */
  90. async getStatusByChange(change) {
  91. const statusConst = audit.flow.status;
  92. const auditStatusConst = audit.flow.auditStatus;
  93. const uid = this.ctx.session.sessionUser.accountId;
  94. const changeAuditInfo = await this.getAllDataByCondition({ where: { cid: change.cid, times: change.times, uid }, orders: [['id', 'desc']], limit: 1, offset: 0 });
  95. if (!change.status === statusConst.checked && (changeAuditInfo === null || changeAuditInfo[0] === undefined)) {
  96. // 无权限查看此变更令
  97. return 0;
  98. }
  99. if (change.status === statusConst.uncheck && uid === change.uid) {
  100. // 待上报
  101. return 1;
  102. } else if (change.status === statusConst.back && uid === change.uid) {
  103. // 待重新上报
  104. return 2;
  105. } else if (change.status === statusConst.back && uid !== change.uid) {
  106. // 被退回但你不是原报人
  107. return 3;
  108. } else if (change.status === statusConst.checked) {
  109. // 已完成
  110. return 4;
  111. } else if (change.status === statusConst.checkNo) {
  112. // 已终止
  113. return 5;
  114. } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo[0].status === auditStatusConst.checking) {
  115. // 待你审批
  116. return 6;
  117. } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo[0].status !== auditStatusConst.checking) {
  118. // 审批中但你未到你审批或你已审批
  119. return 7;
  120. }
  121. // 无权限查看此变更令
  122. return 0;
  123. }
  124. /**
  125. * 根据用户查看此变更状态获取审批人列表
  126. * @param {Object} change - 变更令
  127. * @param {int} status - 状态
  128. * @return {object} list - 列表
  129. */
  130. async getListByStatus(change, status) {
  131. let sql = '';
  132. let sqlParam = '';
  133. switch (status) {
  134. case 1:// 待上报
  135. case 2:// 待重新上报
  136. sql = 'SELECT * FROM ?? WHERE ' +
  137. 'cid = ? AND times = ? GROUP BY usite';
  138. sqlParam = [this.tableName, change.cid,
  139. change.times];
  140. break;
  141. case 3: // 被退回但你不是原报人
  142. case 4:// 已完成
  143. case 5:// 已终止
  144. case 7:// 审批中但你未到你审批或你已审批
  145. // 获取完整的审批顺序
  146. sql = 'SELECT * FROM ?? WHERE ' +
  147. 'cid = ? ORDER BY usort';
  148. sqlParam = [this.tableName, change.cid];
  149. break;
  150. case 6: // 审批中
  151. sql = 'SELECT * FROM (SELECT MAX(usort) as ust FROM ?? ' +
  152. 'WHERE cid = ? and times = ? GROUP BY usite ) as b ' +
  153. 'JOIN ?? as a ON a.usort = b.ust WHERE cid = ? and times = ? ORDER BY usite';
  154. sqlParam = [this.tableName, change.cid, change.times, this.tableName, change.cid, change.times];
  155. // sql = 'SELECT * FROM ?? WHERE ' +
  156. // 'cid = ? AND times = ? ORDER BY usort';
  157. // sqlParam = [this.tableName, change.cid, change.times];
  158. break;
  159. default:// 无权限查看此变更令
  160. break;
  161. }
  162. const list = await this.db.query(sql, sqlParam);
  163. return list;
  164. }
  165. /**
  166. * 删除变更令审批人列表
  167. * @param {Object} transaction - 事务
  168. * @param {Object} cid - 变更令id
  169. * @param {int} times - 次数
  170. * @return {object} 返回结果
  171. */
  172. async deleteAuditData(transaction, cid, times) {
  173. this.initSqlBuilder();
  174. this.sqlBuilder.setAndWhere('cid', {
  175. value: this.db.escape(cid),
  176. operate: '=',
  177. });
  178. this.sqlBuilder.setAndWhere('times', {
  179. value: times,
  180. operate: '=',
  181. });
  182. this.sqlBuilder.setAndWhere('usite', {
  183. value: 0,
  184. operate: '!=',
  185. });
  186. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'delete');
  187. const result = await transaction.query(sql, sqlParam);
  188. return result;
  189. }
  190. /**
  191. * 获取不重复列表
  192. * @param {Object} cid - 变更令id
  193. * @param {int} times - 次数
  194. * @return {object} 返回结果
  195. */
  196. async getListGroupByTimes(cid, times) {
  197. const sql = 'SELECT * FROM ?? where id in (SELECT MAX(id) FROM ?? WHERE ' +
  198. 'cid = ? AND times = ? GROUP BY usite)';
  199. const sqlParam = [this.tableName, this.tableName, cid, times];
  200. const list = await this.db.query(sql, sqlParam);
  201. return list;
  202. }
  203. async getListOrderByTimes(cid, times) {
  204. const sql = 'SELECT * FROM ?? WHERE ' +
  205. 'cid = ? AND times = ? ORDER BY usort';
  206. const sqlParam = [this.tableName, cid, times];
  207. const list = await this.db.query(sql, sqlParam);
  208. return list;
  209. }
  210. /**
  211. * 获取比sort大的审批人列表
  212. * @param {Object} cid - 变更令id
  213. * @param {int} usort - 排序
  214. * @return {object} 返回结果
  215. */
  216. async getNextAuditList(cid, usort) {
  217. const sql = 'SELECT * FROM ?? WHERE ' +
  218. 'cid = ? AND usort > ?';
  219. const sqlParam = [this.tableName, cid, usort];
  220. const list = await this.db.query(sql, sqlParam);
  221. return list;
  222. }
  223. /**
  224. * 获取除当前次数的审批人列表
  225. * @param {Object} cid - 变更令id
  226. * @param {int} times - 次数
  227. * @return {object} 返回结果
  228. */
  229. async getListByBack(cid, times) {
  230. this.initSqlBuilder();
  231. this.sqlBuilder.setAndWhere('cid', {
  232. value: this.db.escape(cid),
  233. operate: '=',
  234. });
  235. this.sqlBuilder.setAndWhere('times', {
  236. value: times,
  237. operate: '!=',
  238. });
  239. this.sqlBuilder.orderBy = [['usort', 'ASC']];
  240. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  241. const result = await this.db.query(sql, sqlParam);
  242. return result;
  243. }
  244. /**
  245. * 获取 审核人 待审批的() 变更令列表
  246. * @param uid
  247. * @return {Promise<void>}
  248. */
  249. async getAuditChange(uid) {
  250. const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' +
  251. ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, ' +
  252. ' t.`name`, t.`type`, t.`user_id` ' +
  253. ' FROM ?? AS ca, ?? AS c, ?? As t ' +
  254. ' WHERE ca.`uid` = ? and ca.`status` = ? and c.`status` != ?' +
  255. ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` ORDER BY ca.`sin_time` DESC';
  256. const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, uid, 2, audit.flow.status.uncheck];
  257. const changes = await this.db.query(sql, sqlParam);
  258. for (const c of changes) {
  259. if (c.cstatus === audit.flow.status.back) {
  260. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  261. ' WHERE ca.cid = ? and ca.times = ? and ca.status = ? and ca.uid = pa.id';
  262. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times - 1, c.cstatus];
  263. c.pre = await this.db.queryOne(preSql, preSqlParam);
  264. } else {
  265. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  266. ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id';
  267. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1];
  268. c.pre = await this.db.queryOne(preSql, preSqlParam);
  269. }
  270. }
  271. return changes;
  272. }
  273. /**
  274. * 获取 某时间后 审批进度更新的 变更令
  275. * @param {Number} pid - 查询标段
  276. * @param {Number} uid - 查询人
  277. * @param {Date} time - 查询时间
  278. * @return {Promise<*>}
  279. */
  280. async getNoticeChange(pid, uid, time) {
  281. // const sql = 'SELECT * FROM (SELECT t.`id`, t.`name`, t.`type`, t.`user_id`, ' +
  282. // ' ca.`cid`, c.`code` As `c_code`, c.name As `c_name`, ' +
  283. // ' 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`' +
  284. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `uid` = ? GROUP BY `tid`)) As t' +
  285. // ' LEFT JOIN ?? As c ON c.`tid` = t.`id`' +
  286. // ' LEFT JOIN ?? As ca ON ca.`cid` = c.`cid`' +
  287. // ' WHERE t.`project_id` = ? and `ca`.`sin_time` > ? and `ca`.`usite` != 0 and `ca`.`status` != ?' +
  288. // ' ORDER By ca.`sin_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
  289. // ' ORDER BY new_t.`cu_time`';
  290. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.change.tableName, this.tableName, pid, time, audit.flow.status.checking];
  291. // return await this.db.query(sql, sqlParam);
  292. let notice = await this.db.select('zh_notice', {
  293. where: { pid, type: pushType.change, uid },
  294. orders: [['create_time', 'desc']],
  295. limit: 10, offset: 0,
  296. });
  297. notice = notice.map(v => {
  298. const extra = JSON.parse(v.content);
  299. delete v.content;
  300. return { ...v, ...extra };
  301. });
  302. return notice;
  303. }
  304. async getAllAuditors(tenderId) {
  305. const sql = 'SELECT ca.uid, ca.tid FROM ' + this.tableName + ' ca' +
  306. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ca.tid = t.id' +
  307. ' WHERE t.id = ?' +
  308. ' GROUP BY ca.uid';
  309. const sqlParam = [tenderId];
  310. return this.db.query(sql, sqlParam);
  311. }
  312. /**
  313. * 取待审批变更列表(wap用)
  314. *
  315. * @param auditorId
  316. * @return {Promise<*>}
  317. */
  318. async getAuditChangeByWap(uid) {
  319. const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' +
  320. ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, c.`quality`, c.`total_price`,' +
  321. ' t.`name`, t.`type`, t.`user_id`, ' +
  322. ' ti.`deal_info`, ti.`decimal` ' +
  323. ' FROM ?? AS ca, ?? AS c, ?? As t, ?? AS ti ' +
  324. ' WHERE ca.`uid` = ? and ca.`status` = ? and c.`status` != ? and c.`status` != ?' +
  325. ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` and ti.`tid` = t.`id`';
  326. const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, this.ctx.service.tenderInfo.tableName, uid, audit.flow.auditStatus.checking, audit.flow.status.uncheck, audit.flow.status.back];
  327. const changes = await this.db.query(sql, sqlParam);
  328. for (const c of changes) {
  329. const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
  330. ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id';
  331. const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1];
  332. c.pre = await this.db.queryOne(preSql, preSqlParam);
  333. }
  334. return await this.db.query(sql, sqlParam);
  335. }
  336. async updateNewAuditList(change, newIdList) {
  337. const transaction = await this.db.beginTransaction();
  338. try {
  339. // 先删除旧的审批流(除了原报),再添加新的
  340. const sql = 'DELETE FROM ?? WHERE `cid`= ? AND `times` = ? AND `usite` != 0';
  341. const sqlParam = [this.tableName, change.cid, change.times];
  342. await transaction.query(sql, sqlParam);
  343. const newAuditors = [];
  344. let order = 1;
  345. let uSort = await transaction.count(this.tableName, { cid: change.cid });
  346. for (const aid of newIdList) {
  347. const accountInfo = await this.ctx.service.projectAccount.getDataById(aid);
  348. newAuditors.push({
  349. tid: change.tid, cid: change.cid, uid: aid,
  350. name: accountInfo.name, jobs: accountInfo.role, company: accountInfo.company,
  351. times: change.times, usite: order, usort: uSort, status: audit.flow.auditStatus.uncheck,
  352. });
  353. order++;
  354. uSort++;
  355. }
  356. await transaction.insert(this.tableName, newAuditors);
  357. await transaction.commit();
  358. } catch (err) {
  359. await transaction.rollback();
  360. throw err;
  361. }
  362. }
  363. async updateLastAudit(change, auditList, lastId) {
  364. const transaction = await this.db.beginTransaction();
  365. try {
  366. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  367. const idList = this._.map(auditList, 'uid');
  368. let order = idList.length + 1;
  369. if (idList.indexOf(lastId) !== -1) {
  370. const sql = 'DELETE FROM ?? WHERE `cid`= ? AND `times` = ? AND `uid` = ? AND `usite` != 0';
  371. const sqlParam = [this.tableName, change.cid, change.times, lastId];
  372. await transaction.query(sql, sqlParam);
  373. // await transaction.delete(this.tableName, { cid: change.cid, times: change.times, uid: lastId, usite: 0 });
  374. const user = this._.find(auditList, { 'uid': lastId });
  375. // 顺移之后审核人流程顺序
  376. await this._syncOrderByDelete(transaction, change.cid, user.usite, user.usort, change.times);
  377. order = order - 1;
  378. }
  379. // 添加终审
  380. const userInfo = await this.ctx.service.projectAccount.getDataById(lastId);
  381. let uSort = await transaction.count(this.tableName, { cid: change.cid });
  382. const newAuditor = {
  383. tid: change.tid, cid: change.cid, uid: lastId,
  384. name: userInfo.name, jobs: userInfo.role, company: userInfo.company,
  385. times: change.times, usite: order, usort: uSort, status: audit.flow.auditStatus.uncheck,
  386. };
  387. await transaction.insert(this.tableName, newAuditor);
  388. await transaction.commit();
  389. } catch (err) {
  390. await transaction.rollback();
  391. throw err;
  392. }
  393. }
  394. /**
  395. * 移除审核人时,同步其后审核人order
  396. * @param transaction - 事务
  397. * @param {Number} changeId - 变更令id
  398. * @param {Number} usite - 审核人id
  399. * @param {Number} usort - 审核人id
  400. * @param {Number} times - 第几次审批
  401. * @return {Promise<*>}
  402. * @private
  403. */
  404. async _syncOrderByDelete(transaction, changeId, usite, usort, times, selfOperate = '-') {
  405. this.initSqlBuilder();
  406. this.sqlBuilder.setAndWhere('cid', {
  407. value: this.db.escape(changeId),
  408. operate: '=',
  409. });
  410. this.sqlBuilder.setAndWhere('usite', {
  411. value: usite,
  412. operate: '>=',
  413. });
  414. this.sqlBuilder.setAndWhere('times', {
  415. value: times,
  416. operate: '=',
  417. });
  418. this.sqlBuilder.setUpdateData('usite', {
  419. value: 1,
  420. selfOperate: selfOperate,
  421. });
  422. this.sqlBuilder.setUpdateData('usort', {
  423. value: 1,
  424. selfOperate: selfOperate,
  425. });
  426. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  427. const data = await transaction.query(sql, sqlParam);
  428. return data;
  429. }
  430. }
  431. return ChangeAudit;
  432. };