change_apply.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit').changeApply;
  10. const auditType = require('../const/audit').auditType;
  11. // const smsTypeConst = require('../const/sms_type');
  12. // const SMS = require('../lib/sms');
  13. // const SmsAliConst = require('../const/sms_alitemplate');
  14. const wxConst = require('../const/wechat_template');
  15. const pushType = require('../const/audit').pushType;
  16. const projectLogConst = require('../const/project_log');
  17. const codeRuleConst = require('../const/code_rule');
  18. const changeConst = require('../const/change');
  19. module.exports = app => {
  20. class ChangeApply extends app.BaseService {
  21. /**
  22. * 构造函数
  23. *
  24. * @param {Object} ctx - egg全局变量
  25. * @return {void}
  26. */
  27. constructor(ctx) {
  28. super(ctx);
  29. this.tableName = 'change_apply';
  30. }
  31. async loadChangeAuditViewData(change) {
  32. const times = change.status === audit.status.checkNo || change.status === audit.status.revise ? change.times - 1 : change.times;
  33. if (!change.user) change.user = await this.ctx.service.projectAccount.getAccountInfoById(change.uid);
  34. change.auditHistory = await this.ctx.service.changeApplyAudit.getAuditorHistory(change.id, times);
  35. // 获取审批流程中左边列表
  36. if ((change.status === audit.status.checkNo || change.status === audit.status.revise) && change.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) {
  37. const auditors = await this.ctx.service.changeApplyAudit.getAuditors(change.id, times); // 全部参与的审批人
  38. const auditorGroups = this.ctx.helper.groupAuditors(auditors, 'order', true);
  39. change.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  40. change.auditors2.unshift([{
  41. aid: change.user.id, order: 0, times: change.times - 1, audit_order: 0, audit_type: auditType.key.common,
  42. name: change.user.name, role: change.user.role, company: change.user.company,
  43. }]);
  44. } else {
  45. change.auditors2 = change.userGroups;
  46. }
  47. if (change.status === audit.status.uncheck || change.status === audit.status.checkNo || change.status === audit.status.revise) {
  48. change.auditorList = await this.ctx.service.changeApplyAudit.getAuditors(change.id, change.times);
  49. }
  50. }
  51. async loadChangeUser(change) {
  52. const status = audit.status;
  53. const accountId = this.ctx.session.sessionUser.accountId;
  54. change.user = await this.ctx.service.projectAccount.getAccountInfoById(change.uid);
  55. change.auditors = await this.ctx.service.changeApplyAudit.getAuditors(change.id, change.times); // 全部参与的审批人
  56. change.auditorIds = this._.map(change.auditors, 'aid');
  57. change.curAuditors = change.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
  58. change.curAuditorIds = this._.map(change.curAuditors, 'aid');
  59. change.flowAuditors = change.curAuditors.length > 0 ? change.auditors.filter(x => { return x.order === change.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  60. change.flowAuditorIds = this._.map(change.flowAuditors, 'aid');
  61. change.nextAuditors = change.curAuditors.length > 0 ? change.auditors.filter(x => { return x.order === change.curAuditors[0].order + 1; }) : [];
  62. change.nextAuditorIds = this._.map(change.nextAuditors, 'aid');
  63. change.auditorGroups = this.ctx.helper.groupAuditors(change.auditors, 'order', true);
  64. change.userGroups = this.ctx.helper.groupAuditorsUniq(change.auditorGroups);
  65. change.userGroups.unshift([{
  66. aid: change.user.id, order: 0, times: change.times, audit_order: 0, audit_type: auditType.key.common,
  67. name: change.user.name, role: change.user.role, company: change.user.company,
  68. }]);
  69. change.finalAuditorIds = change.userGroups[change.userGroups.length - 1].map(x => { return x.aid; });
  70. }
  71. async add(tenderId, userId, code, project_code, name) {
  72. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND `code` = ?';
  73. const sqlParam = [this.tableName, tenderId, code];
  74. const codeCount = await this.db.queryOne(sql, sqlParam);
  75. const count = codeCount.count;
  76. if (count > 0) {
  77. throw '变更申请书编号重复';
  78. }
  79. // 初始化事务
  80. this.transaction = await this.db.beginTransaction();
  81. let result = false;
  82. try {
  83. const change = {
  84. tid: tenderId,
  85. uid: userId,
  86. status: audit.status.uncheck,
  87. times: 1,
  88. in_time: new Date(),
  89. code,
  90. project_code,
  91. name,
  92. quality: changeConst.quality.common.name,
  93. };
  94. if (project_code) {
  95. const projectInfo = await this.ctx.service.changeProject.getDataByCondition({ tid: tenderId, code: project_code });
  96. if (projectInfo) {
  97. change.org_name = projectInfo.org_name;
  98. change.peg = projectInfo.peg;
  99. change.new_code = projectInfo.new_code;
  100. change.class = projectInfo.class;
  101. change.quality = projectInfo.quality;
  102. change.reason = projectInfo.reason;
  103. change.content = projectInfo.content;
  104. change.org_price = projectInfo.org_price;
  105. change.change_price = projectInfo.change_price;
  106. change.crease_price = projectInfo.crease_price;
  107. }
  108. }
  109. const operate = await this.transaction.insert(this.tableName, change);
  110. if (operate.affectedRows <= 0) {
  111. throw '新建变更令数据失败';
  112. }
  113. change.id = operate.insertId;
  114. // 先找出标段最近存在的变更令审批人的变更令info
  115. const preChangeInfo = await this.getHaveAuditLastInfo(tenderId);
  116. if (preChangeInfo) {
  117. // 并把之前存在的变更令审批人添加到zh_change_audit
  118. const auditResult = await this.ctx.service.changeApplyAudit.copyPreChangeApplyAuditors(this.transaction, preChangeInfo, change);
  119. if (!auditResult) {
  120. throw '复制上一次审批流程失败';
  121. }
  122. }
  123. result = change;
  124. this.transaction.commit();
  125. } catch (error) {
  126. console.log(error);
  127. // 回滚
  128. await this.transaction.rollback();
  129. }
  130. return result;
  131. }
  132. async getHaveAuditLastInfo(tenderId) {
  133. const sql = 'SELECT a.* FROM ?? as a LEFT JOIN ?? as b ON a.`id` = b.`caid` WHERE a.`tid` = ? ORDER BY a.`in_time` DESC';
  134. const sqlParam = [this.tableName, this.ctx.service.changeApplyAudit.tableName, tenderId];
  135. return await this.db.queryOne(sql, sqlParam);
  136. }
  137. /**
  138. * 获取变更立项列表
  139. * @param {int} tenderId - 标段id
  140. * @param {int} status - 状态
  141. * @param {int} hadlimit - 分页
  142. * @return {object} list - 列表
  143. */
  144. async getListByStatus(tenderId, status = 0, hadlimit = 1, sortBy = '', orderBy = '') {
  145. let sql = '';
  146. let sqlParam = '';
  147. if (this.ctx.tender.isTourist && status === 0) {
  148. sql = 'SELECT a.*, p.name as account_name FROM ?? As a LEFT JOIN ?? AS p On a.notice_uid = p.id WHERE a.tid = ?';
  149. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId];
  150. } else {
  151. switch (status) {
  152. case 0: // 包含你的所有变更立项
  153. sql =
  154. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.notice_uid = p.id WHERE a.tid = ? AND ' +
  155. '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid)) OR a.status = ?)';
  156. sqlParam = [
  157. this.tableName,
  158. this.ctx.service.projectAccount.tableName,
  159. tenderId,
  160. this.ctx.session.sessionUser.accountId,
  161. audit.status.uncheck,
  162. this.ctx.service.changeApplyAudit.tableName,
  163. this.ctx.session.sessionUser.accountId,
  164. audit.status.checked,
  165. ];
  166. break;
  167. case 1: // 待处理(你的)
  168. sql = 'SELECT a.*, p.name as account_name FROM ?? as a LEFT JOIN ?? AS p On a.notice_uid = p.id WHERE a.tid = ? AND (a.id in(SELECT b.caid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)))';
  169. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, this.ctx.service.changeApplyAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.checkNo, audit.status.revise];
  170. break;
  171. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  172. sql =
  173. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.notice_uid = p.id WHERE ' +
  174. // 'a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid) AND ' +
  175. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  176. sqlParam = [
  177. this.tableName,
  178. this.ctx.service.projectAccount.tableName,
  179. // this.ctx.service.changeApplyAudit.tableName,
  180. this.ctx.session.sessionUser.accountId,
  181. tenderId,
  182. audit.status.uncheck,
  183. audit.status.checkNo,
  184. audit.status.revise,
  185. ];
  186. break;
  187. case 2: // 进行中(所有的)
  188. case 4: // 终止(所有的)
  189. sql =
  190. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.notice_uid = p.id WHERE ' +
  191. 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid))';
  192. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changeApplyAudit.tableName, this.ctx.session.sessionUser.accountId];
  193. break;
  194. case 3: // 已完成(所有的)
  195. sql = 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.notice_uid = p.id WHERE a.status = ? AND a.tid = ?';
  196. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId];
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. if (sortBy && orderBy) {
  203. if (sortBy === 'code') {
  204. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  205. } else {
  206. sql += ' ORDER BY a.in_time ' + orderBy;
  207. }
  208. } else {
  209. sql += ' ORDER BY a.in_time DESC';
  210. }
  211. if (hadlimit) {
  212. const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
  213. const offset = limit * (this.ctx.page - 1);
  214. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  215. sql += ' LIMIT ' + limitString;
  216. }
  217. const list = await this.db.query(sql, sqlParam);
  218. return list;
  219. }
  220. // 获取最后一个变更申请
  221. async getLastChange(tenderId) {
  222. const sql = 'select * from ?? where tid = ? order by in_time desc LIMIT 1';
  223. const sqlParam = [this.tableName, tenderId];
  224. const result = await this.db.queryOne(sql, sqlParam);
  225. return result;
  226. }
  227. /**
  228. * 获取变更令个数
  229. * @param {int} tenderId - 标段id
  230. * @param {int} status - 状态
  231. * @return {void}
  232. */
  233. async getCountByStatus(tenderId, status) {
  234. if (this.ctx.tender.isTourist && status === 0) {
  235. const sql5 = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
  236. const sqlParam5 = [this.tableName, tenderId];
  237. const result5 = await this.db.query(sql5, sqlParam5);
  238. return result5[0].count;
  239. }
  240. switch (status) {
  241. case 0: // 包含你的所有变更令
  242. const sql =
  243. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  244. '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid)) OR a.status = ?)';
  245. const sqlParam = [
  246. this.tableName,
  247. tenderId,
  248. this.ctx.session.sessionUser.accountId,
  249. audit.status.uncheck,
  250. this.ctx.service.changeApplyAudit.tableName,
  251. this.ctx.session.sessionUser.accountId,
  252. audit.status.checked,
  253. ];
  254. const result = await this.db.query(sql, sqlParam);
  255. return result[0].count;
  256. case 1: // 待处理(你的)
  257. // return await this.ctx.service.changeAudit.count({
  258. // tid: tenderId,
  259. // uid: this.ctx.session.sessionUser.accountId,
  260. // status: 2,
  261. // });
  262. const sql6 = 'SELECT count(*) AS count FROM ?? as a WHERE a.tid = ? AND (a.id in(SELECT b.caid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)))';
  263. const sqlParam6 = [this.tableName, tenderId, this.ctx.service.changeApplyAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.checkNo, audit.status.revise];
  264. const result6 = await this.db.query(sql6, sqlParam6);
  265. return result6[0].count;
  266. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  267. const sql2 =
  268. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  269. // 'a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid) ' +
  270. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  271. const sqlParam2 = [
  272. this.tableName,
  273. // this.ctx.service.changeApplyAudit.tableName,
  274. this.ctx.session.sessionUser.accountId,
  275. tenderId,
  276. audit.status.uncheck,
  277. audit.status.checkNo,
  278. audit.status.revise,
  279. ];
  280. const result2 = await this.db.query(sql2, sqlParam2);
  281. return result2[0].count;
  282. case 2: // 进行中(所有的)
  283. case 4: // 终止(所有的)
  284. const sql3 =
  285. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  286. 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid))';
  287. const sqlParam3 = [this.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changeApplyAudit.tableName, this.ctx.session.sessionUser.accountId];
  288. const result3 = await this.db.query(sql3, sqlParam3);
  289. return result3[0].count;
  290. case 3: // 已完成(所有的)
  291. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  292. const sqlParam4 = [this.tableName, status, tenderId];
  293. const result4 = await this.db.query(sql4, sqlParam4);
  294. return result4[0].count;
  295. default:
  296. break;
  297. }
  298. }
  299. /**
  300. * 获取变更方案金额
  301. * @param {int} tenderId - 标段id
  302. * @param {int} status - 状态
  303. * @return {void}
  304. */
  305. async getTp(tenderId, status) {
  306. if (this.ctx.tender.isTourist && status === 0) {
  307. const sql5 = 'SELECT SUM(cast (total_price as decimal(18,6))) AS total_price FROM ?? WHERE tid = ?';
  308. const sqlParam5 = [this.tableName, tenderId];
  309. const result5 = await this.db.query(sql5, sqlParam5);
  310. return result5[0].total_price ? result5[0].total_price : 0;
  311. }
  312. switch (status) {
  313. case 0: // 包含你的所有变更令
  314. const sql =
  315. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE a.tid = ? AND ' +
  316. '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid)) OR a.status = ?)';
  317. const sqlParam = [
  318. this.tableName,
  319. tenderId,
  320. this.ctx.session.sessionUser.accountId,
  321. audit.status.uncheck,
  322. this.ctx.service.changeApplyAudit.tableName,
  323. this.ctx.session.sessionUser.accountId,
  324. audit.status.checked,
  325. ];
  326. const result = await this.db.query(sql, sqlParam);
  327. return result[0].total_price ? result[0].total_price : 0;
  328. case 1: // 待处理(你的)
  329. // return await this.ctx.service.changeAudit.count({
  330. // tid: tenderId,
  331. // uid: this.ctx.session.sessionUser.accountId,
  332. // status: 2,
  333. // });
  334. const sql6 = 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? as a WHERE a.tid = ? AND (a.id in(SELECT b.caid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)))';
  335. const sqlParam6 = [this.tableName, tenderId, this.ctx.service.changeApplyAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.checkNo, audit.status.revise];
  336. const result6 = await this.db.query(sql6, sqlParam6);
  337. return result6[0].total_price ? result6[0].total_price : 0;
  338. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  339. const sql2 =
  340. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE ' +
  341. // 'a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid) ' +
  342. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  343. const sqlParam2 = [
  344. this.tableName,
  345. // this.ctx.service.changePlanAudit.tableName,
  346. this.ctx.session.sessionUser.accountId,
  347. tenderId,
  348. audit.status.uncheck,
  349. audit.status.checkNo,
  350. audit.status.revise,
  351. ];
  352. const result2 = await this.db.query(sql2, sqlParam2);
  353. return result2[0].total_price ? result2[0].total_price : 0;
  354. case 2: // 进行中(所有的)
  355. case 4: // 终止(所有的)
  356. const sql3 =
  357. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE ' +
  358. 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.caid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.caid))';
  359. const sqlParam3 = [this.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changeApplyAudit.tableName, this.ctx.session.sessionUser.accountId];
  360. const result3 = await this.db.query(sql3, sqlParam3);
  361. return result3[0].total_price ? result3[0].total_price : 0;
  362. case 3: // 已完成(所有的)
  363. const sql4 = 'SELECT SUM(cast (total_price as decimal(18,6))) AS total_price FROM ?? WHERE status = ? AND tid = ?';
  364. const sqlParam4 = [this.tableName, status, tenderId];
  365. const result4 = await this.db.query(sql4, sqlParam4);
  366. return result4[0].total_price ? result4[0].total_price : 0;
  367. default:
  368. break;
  369. }
  370. }
  371. /**
  372. * 保存变更信息
  373. * @param {int} postData - 表单提交的数据
  374. * @param {int} tenderId - 标段id
  375. * @return {void}
  376. */
  377. async saveInfo(caId, postData) {
  378. // 初始化事务
  379. const transaction = await this.db.beginTransaction();
  380. let result = false;
  381. try {
  382. const updateData = {
  383. id: caId,
  384. };
  385. updateData[postData.name] = postData.val;
  386. await transaction.update(this.tableName, updateData);
  387. await transaction.commit();
  388. result = true;
  389. } catch (error) {
  390. await transaction.rollback();
  391. result = false;
  392. }
  393. return result;
  394. }
  395. /**
  396. * 判断是否有重名的变更立项
  397. * @param caid
  398. * @param code
  399. * @param tid
  400. * @return {Promise<void>}
  401. */
  402. async isRepeat(caId, code, tid) {
  403. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `code` = ? AND `id` != ? AND `tid` = ?';
  404. const sqlParam = [this.tableName, code, caId, tid];
  405. const result = await this.db.queryOne(sql, sqlParam);
  406. return result.count !== 0;
  407. }
  408. /**
  409. * 查询可用的变更令
  410. * @param { string } cid - 查询的清单
  411. * @return {Promise<*>} - 可用的变更令列表
  412. */
  413. async delete(id) {
  414. // 初始化事务
  415. this.transaction = await this.db.beginTransaction();
  416. let result = false;
  417. try {
  418. const changeInfo = await this.getDataById(id);
  419. // 先删除审批人列表
  420. await this.transaction.delete(this.ctx.service.changeApplyAudit.tableName, { caid: id });
  421. // 再删除附件和附件文件ni zuo
  422. const attList = await this.ctx.service.changeApplyAtt.getAllDataByCondition({ where: { caid: id } });
  423. await this.ctx.helper.delFiles(attList);
  424. await this.transaction.delete(this.ctx.service.changeApplyAtt.tableName, { caid: id });
  425. // 最后删除变更令
  426. await this.transaction.delete(this.tableName, { id });
  427. // 删除history
  428. await this.transaction.delete(this.ctx.service.changeApplyHistory.tableName, { caid: id });
  429. // 记录删除日志
  430. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.changeApply, projectLogConst.status.delete, changeInfo.code);
  431. await this.transaction.commit();
  432. result = true;
  433. } catch (e) {
  434. await this.transaction.rollback();
  435. result = false;
  436. }
  437. return result;
  438. }
  439. async doCheckChangeCanCancel(change) {
  440. // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
  441. const status = audit.status;
  442. const accountId = this.ctx.session.sessionUser.accountId;
  443. const auditors = change.auditors;
  444. change.cancancel = 0;
  445. if (change.status !== status.checked && change.status !== status.uncheck && change.status !== status.revise) {
  446. if (change.status === status.checkNo) {
  447. const lastAuditors = await this.service.changeApplyAudit.getAuditors(change.id, change.times - 1);
  448. const onAuditor = this._.findLast(lastAuditors, { status: status.checkNo });
  449. if (onAuditor && onAuditor.aid === accountId) {
  450. change.cancancel = 4;// 审批人撤回退回原报
  451. change.preAuditors = lastAuditors.filter(x => { return x.order === onAuditor.order; });
  452. }
  453. } else {
  454. if (change.flowAuditors.find(x => { return x.status !== status.checking; }) && change.flowAuditorIds.indexOf(accountId) < 0) return; // 当前流程存在审批人审批通过时,不可撤回
  455. if (change.curAuditorIds.indexOf(accountId) < 0 && change.flowAuditorIds.indexOf(accountId) >= 0) {
  456. change.cancancel = 5; // 会签未全部审批通过时,审批人撤回审批通过
  457. return;
  458. }
  459. const preAuditors = change.curAuditors[0].order !== 1 ? change.auditors.filter(x => { return x.order === change.curAuditors[0].order - 1; }) : [];
  460. const preAuditorCheckAgain = preAuditors.find(pa => { return pa.status === status.checkAgain; });
  461. const preAuditorCheckCancel = preAuditors.find(pa => { return pa.status === status.checkCancel; });
  462. const preAuditorIds = preAuditorCheckAgain ? [] : preAuditors.map(x => { return x.aid; }); // 重审不可撤回
  463. if ((this._.isEqual(change.flowAuditorIds, preAuditorIds) && preAuditorCheckCancel)) {
  464. return; // 不可以多次撤回
  465. }
  466. const preAuditChecked = preAuditors.find(pa => { return pa.status === status.checked && pa.aid === accountId; });
  467. // const preAuditCheckNoPre = preAuditors.find(pa => { return pa.status === status.checkNoPre && pa.uid === accountId; });
  468. if (preAuditorIds.indexOf(accountId) >= 0) {
  469. if (preAuditChecked && change.status === status.checking) {
  470. change.cancancel = 2;// 审批人撤回审批通过
  471. }
  472. // else if (preAuditCheckNoPre && change.status === status.checkNoPre) {
  473. // change.cancancel = 3;// 审批人撤回审批退回上一人
  474. // }
  475. change.preAuditors = preAuditors;
  476. } else if (preAuditors.length === 0 && accountId === change.uid) {
  477. change.cancancel = 1;// 原报撤回
  478. }
  479. }
  480. }
  481. }
  482. async getListByArchives(tid, ids) {
  483. if (ids.length === 0) return [];
  484. const sql = 'SELECT c.* FROM ?? as c LEFT JOIN (SELECT caid, MAX(end_time) as end_time FROM ?? WHERE ' +
  485. 'tid = ? AND caid in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY caid) as ca ON c.id = ca.caid WHERE' +
  486. ' c.tid = ? AND c.id in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') AND c.status = ? ORDER BY ca.end_time DESC';
  487. const params = [this.tableName, this.ctx.service.changeApplyAudit.tableName, tid, tid, audit.status.checked];
  488. const list = await this.db.query(sql, params);
  489. return list;
  490. }
  491. }
  492. return ChangeApply;
  493. };