change_plan.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit').changePlan;
  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 ChangePlan 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_plan';
  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.changePlanAudit.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.changePlanAudit.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.changePlanAudit.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.changePlanAudit.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, apply_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. apply_code,
  91. name,
  92. quality: changeConst.quality.common.name,
  93. };
  94. let caid = null;
  95. if (apply_code) {
  96. const applyInfo = await this.ctx.service.changeApply.getDataByCondition({ tid: tenderId, code: apply_code });
  97. if (applyInfo) {
  98. caid = applyInfo.id;
  99. change.org_name = applyInfo.org_name;
  100. change.peg = applyInfo.peg;
  101. change.new_code = applyInfo.new_code;
  102. change.c_new_code = applyInfo.c_new_code;
  103. change.design_name = applyInfo.design_name;
  104. change.class = applyInfo.class;
  105. change.quality = applyInfo.quality;
  106. change.reason = applyInfo.reason;
  107. change.content = applyInfo.content;
  108. change.total_price = applyInfo.total_price;
  109. }
  110. }
  111. const operate = await this.transaction.insert(this.tableName, change);
  112. if (operate.affectedRows <= 0) {
  113. throw '新建变更令数据失败';
  114. }
  115. change.id = operate.insertId;
  116. // 清单也要同步
  117. if (caid) {
  118. const changeList = await this.ctx.service.changeApplyList.getList(caid);
  119. const insertArray = [];
  120. for (const c of changeList) {
  121. insertArray.push({
  122. tid: this.ctx.tender.id,
  123. cpid: operate.insertId,
  124. code: c.code,
  125. name: c.name,
  126. unit: c.unit,
  127. unit_price: c.unit_price,
  128. oamount: c.oamount,
  129. camount: c.camount,
  130. spamount: c.camount,
  131. new_up: c.new_up,
  132. ex_memo1: c.ex_memo1,
  133. ex_memo2: c.ex_memo2,
  134. });
  135. }
  136. if (insertArray.length > 0) await this.transaction.insert(this.ctx.service.changePlanList.tableName, insertArray);
  137. }
  138. // 先找出标段最近存在的变更令审批人的变更令info
  139. const preChangeInfo = await this.getHaveAuditLastInfo(tenderId);
  140. if (preChangeInfo) {
  141. // 并把之前存在的变更令审批人添加到zh_change_audit
  142. const auditResult = await this.ctx.service.changePlanAudit.copyPreChangePlanAuditors(this.transaction, preChangeInfo, change);
  143. if (!auditResult) {
  144. throw '复制上一次审批流程失败';
  145. }
  146. }
  147. result = change;
  148. await this.transaction.commit();
  149. } catch (error) {
  150. console.log(error);
  151. // 回滚
  152. await this.transaction.rollback();
  153. }
  154. return result;
  155. }
  156. async getHaveAuditLastInfo(tenderId) {
  157. const sql = 'SELECT a.* FROM ?? as a LEFT JOIN ?? as b ON a.`id` = b.`cpid` WHERE a.`tid` = ? ORDER BY a.`in_time` DESC';
  158. const sqlParam = [this.tableName, this.ctx.service.changePlanAudit.tableName, tenderId];
  159. return await this.db.queryOne(sql, sqlParam);
  160. }
  161. /**
  162. * 获取变更方案列表
  163. * @param {int} tenderId - 标段id
  164. * @param {int} status - 状态
  165. * @param {int} hadlimit - 分页
  166. * @return {object} list - 列表
  167. */
  168. async getListByStatus(tenderId, status = 0, hadlimit = 1, sortBy = '', orderBy = '') {
  169. let sql = '';
  170. let sqlParam = '';
  171. if ((this.ctx.tender.isTourist || this.ctx.session.sessionUser.is_admin) && status === 0) {
  172. sql = 'SELECT a.*, p.name as account_name FROM ?? As a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ?';
  173. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId];
  174. } else {
  175. switch (status) {
  176. case 0: // 包含你的所有变更方案
  177. sql =
  178. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ? AND ' +
  179. '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.cpid)) OR a.status = ?)';
  180. sqlParam = [
  181. this.tableName,
  182. this.ctx.service.projectAccount.tableName,
  183. tenderId,
  184. this.ctx.session.sessionUser.accountId,
  185. audit.status.uncheck,
  186. this.ctx.service.changePlanAudit.tableName,
  187. this.ctx.session.sessionUser.accountId,
  188. audit.status.checked,
  189. ];
  190. break;
  191. case 1: // 待处理(你的)
  192. sql = 'SELECT a.*, p.name as account_name FROM ?? as a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ? AND (a.id in(SELECT b.cpid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)))';
  193. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, this.ctx.service.changePlanAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.checkNo, audit.status.revise];
  194. break;
  195. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  196. sql =
  197. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
  198. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  199. sqlParam = [
  200. this.tableName,
  201. this.ctx.service.projectAccount.tableName,
  202. // this.ctx.service.changePlanAudit.tableName,
  203. this.ctx.session.sessionUser.accountId,
  204. tenderId,
  205. audit.status.uncheck,
  206. audit.status.checkNo,
  207. audit.status.revise,
  208. ];
  209. break;
  210. case 2: // 进行中(所有的)
  211. case 4: // 终止(所有的)
  212. sql =
  213. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
  214. 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.cpid))';
  215. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changePlanAudit.tableName, this.ctx.session.sessionUser.accountId];
  216. break;
  217. case 3: // 已完成(所有的)
  218. sql = 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.status = ? AND a.tid = ?';
  219. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId];
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. if (sortBy && orderBy) {
  226. if (sortBy === 'code') {
  227. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  228. } else {
  229. sql += ' ORDER BY a.in_time ' + orderBy;
  230. }
  231. } else {
  232. sql += ' ORDER BY a.in_time DESC';
  233. }
  234. if (hadlimit) {
  235. const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
  236. const offset = limit * (this.ctx.page - 1);
  237. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  238. sql += ' LIMIT ' + limitString;
  239. }
  240. const list = await this.db.query(sql, sqlParam);
  241. return list;
  242. }
  243. // 获取最后一个变更方案
  244. async getLastChange(tenderId) {
  245. const sql = 'select * from ?? where tid = ? order by in_time desc LIMIT 1';
  246. const sqlParam = [this.tableName, tenderId];
  247. const result = await this.db.queryOne(sql, sqlParam);
  248. return result;
  249. }
  250. /**
  251. * 获取变更令个数
  252. * @param {int} tenderId - 标段id
  253. * @param {int} status - 状态
  254. * @return {void}
  255. */
  256. async getCountByStatus(tenderId, status) {
  257. if ((this.ctx.tender.isTourist || this.ctx.session.sessionUser.is_admin) && status === 0) {
  258. const sql5 = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
  259. const sqlParam5 = [this.tableName, tenderId];
  260. const result5 = await this.db.query(sql5, sqlParam5);
  261. return result5[0].count;
  262. }
  263. switch (status) {
  264. case 0: // 包含你的所有变更令
  265. const sql =
  266. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  267. '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.cpid)) OR a.status = ?)';
  268. const sqlParam = [
  269. this.tableName,
  270. tenderId,
  271. this.ctx.session.sessionUser.accountId,
  272. audit.status.uncheck,
  273. this.ctx.service.changePlanAudit.tableName,
  274. this.ctx.session.sessionUser.accountId,
  275. audit.status.checked,
  276. ];
  277. const result = await this.db.query(sql, sqlParam);
  278. return result[0].count;
  279. case 1: // 待处理(你的)
  280. // return await this.ctx.service.changeAudit.count({
  281. // tid: tenderId,
  282. // uid: this.ctx.session.sessionUser.accountId,
  283. // status: 2,
  284. // });
  285. const sql6 = 'SELECT count(*) AS count FROM ?? as a WHERE a.tid = ? AND (a.id in(SELECT b.cpid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)))';
  286. const sqlParam6 = [this.tableName, tenderId, this.ctx.service.changePlanAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.checkNo, audit.status.revise];
  287. const result6 = await this.db.query(sql6, sqlParam6);
  288. return result6[0].count;
  289. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  290. const sql2 =
  291. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  292. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  293. const sqlParam2 = [
  294. this.tableName,
  295. // this.ctx.service.changePlanAudit.tableName,
  296. this.ctx.session.sessionUser.accountId,
  297. tenderId,
  298. audit.status.uncheck,
  299. audit.status.checkNo,
  300. audit.status.revise,
  301. ];
  302. const result2 = await this.db.query(sql2, sqlParam2);
  303. return result2[0].count;
  304. case 2: // 进行中(所有的)
  305. case 4: // 终止(所有的)
  306. const sql3 =
  307. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  308. 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.cpid))';
  309. const sqlParam3 = [this.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changePlanAudit.tableName, this.ctx.session.sessionUser.accountId];
  310. const result3 = await this.db.query(sql3, sqlParam3);
  311. return result3[0].count;
  312. case 3: // 已完成(所有的)
  313. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  314. const sqlParam4 = [this.tableName, status, tenderId];
  315. const result4 = await this.db.query(sql4, sqlParam4);
  316. return result4[0].count;
  317. default:
  318. break;
  319. }
  320. }
  321. /**
  322. * 获取变更方案金额
  323. * @param {int} tenderId - 标段id
  324. * @param {int} status - 状态
  325. * @return {void}
  326. */
  327. async getTp(tenderId, status) {
  328. if ((this.ctx.tender.isTourist || this.ctx.session.sessionUser.is_admin) && status === 0) {
  329. const sql5 = 'SELECT SUM(cast (total_price as decimal(18,6))) AS total_price FROM ?? WHERE tid = ?';
  330. const sqlParam5 = [this.tableName, tenderId];
  331. const result5 = await this.db.query(sql5, sqlParam5);
  332. return result5[0].total_price ? result5[0].total_price : 0;
  333. }
  334. switch (status) {
  335. case 0: // 包含你的所有变更令
  336. const sql =
  337. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE a.tid = ? AND ' +
  338. '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.cpid)) OR a.status = ?)';
  339. const sqlParam = [
  340. this.tableName,
  341. tenderId,
  342. this.ctx.session.sessionUser.accountId,
  343. audit.status.uncheck,
  344. this.ctx.service.changePlanAudit.tableName,
  345. this.ctx.session.sessionUser.accountId,
  346. audit.status.checked,
  347. ];
  348. const result = await this.db.query(sql, sqlParam);
  349. return result[0].total_price ? result[0].total_price : 0;
  350. case 1: // 待处理(你的)
  351. // return await this.ctx.service.changeAudit.count({
  352. // tid: tenderId,
  353. // uid: this.ctx.session.sessionUser.accountId,
  354. // status: 2,
  355. // });
  356. 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.cpid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)))';
  357. const sqlParam6 = [this.tableName, tenderId, this.ctx.service.changePlanAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.checkNo, audit.status.revise];
  358. const result6 = await this.db.query(sql6, sqlParam6);
  359. return result6[0].total_price ? result6[0].total_price : 0;
  360. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  361. const sql2 =
  362. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE ' +
  363. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) ' +
  364. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  365. const sqlParam2 = [
  366. this.tableName,
  367. // this.ctx.service.changePlanAudit.tableName,
  368. this.ctx.session.sessionUser.accountId,
  369. tenderId,
  370. audit.status.uncheck,
  371. audit.status.checkNo,
  372. audit.status.revise,
  373. ];
  374. const result2 = await this.db.query(sql2, sqlParam2);
  375. return result2[0].total_price ? result2[0].total_price : 0;
  376. case 2: // 进行中(所有的)
  377. case 4: // 终止(所有的)
  378. const sql3 =
  379. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE ' +
  380. 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? GROUP BY b.cpid))';
  381. const sqlParam3 = [this.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changePlanAudit.tableName, this.ctx.session.sessionUser.accountId];
  382. const result3 = await this.db.query(sql3, sqlParam3);
  383. return result3[0].total_price ? result3[0].total_price : 0;
  384. case 3: // 已完成(所有的)
  385. const sql4 = 'SELECT SUM(cast (total_price as decimal(18,6))) AS total_price FROM ?? WHERE status = ? AND tid = ?';
  386. const sqlParam4 = [this.tableName, status, tenderId];
  387. const result4 = await this.db.query(sql4, sqlParam4);
  388. return result4[0].total_price ? result4[0].total_price : 0;
  389. default:
  390. break;
  391. }
  392. }
  393. /**
  394. * 保存变更信息
  395. * @param {int} postData - 表单提交的数据
  396. * @param {int} tenderId - 标段id
  397. * @return {void}
  398. */
  399. async saveInfo(cpid, postData) {
  400. // 初始化事务
  401. const transaction = await this.db.beginTransaction();
  402. let result = false;
  403. try {
  404. const updateData = {
  405. id: cpid,
  406. };
  407. updateData[postData.name] = postData.val;
  408. await transaction.update(this.tableName, updateData);
  409. await transaction.commit();
  410. result = true;
  411. } catch (error) {
  412. await transaction.rollback();
  413. result = false;
  414. }
  415. return result;
  416. }
  417. /**
  418. * 判断是否有重名的变更立项
  419. * @param cpid
  420. * @param code
  421. * @param tid
  422. * @return {Promise<void>}
  423. */
  424. async isRepeat(cpid, code, tid) {
  425. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `code` = ? AND `id` != ? AND `tid` = ?';
  426. const sqlParam = [this.tableName, code, cpid, tid];
  427. const result = await this.db.queryOne(sql, sqlParam);
  428. return result.count !== 0;
  429. }
  430. /**
  431. * 查询可用的变更令
  432. * @param { string } cid - 查询的清单
  433. * @return {Promise<*>} - 可用的变更令列表
  434. */
  435. async delete(id) {
  436. // 初始化事务
  437. this.transaction = await this.db.beginTransaction();
  438. let result = false;
  439. try {
  440. const changeInfo = await this.getDataById(id);
  441. // 先删除审批人列表
  442. await this.transaction.delete(this.ctx.service.changePlanAudit.tableName, { cpid: id });
  443. // 删除清单
  444. await this.transaction.delete(this.ctx.service.changePlanList.tableName, { cpid: id });
  445. // 再删除附件和附件文件ni zuo
  446. const attList = await this.ctx.service.changePlanAtt.getAllDataByCondition({ where: { cpid: id } });
  447. await this.ctx.helper.delFiles(attList);
  448. await this.transaction.delete(this.ctx.service.changePlanAtt.tableName, { cpid: id });
  449. // 最后删除变更令
  450. await this.transaction.delete(this.tableName, { id });
  451. // 删除history
  452. await this.transaction.delete(this.ctx.service.changePlanHistory.tableName, { cpid: id });
  453. // 记录删除日志
  454. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.changePlan, projectLogConst.status.delete, changeInfo.code);
  455. await this.transaction.commit();
  456. result = true;
  457. } catch (e) {
  458. await this.transaction.rollback();
  459. result = false;
  460. }
  461. return result;
  462. }
  463. async doCheckChangeCanCancel(change) {
  464. // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
  465. const status = audit.status;
  466. const accountId = this.ctx.session.sessionUser.accountId;
  467. const auditors = change.auditors;
  468. change.cancancel = 0;
  469. if (change.status !== status.checked && change.status !== status.uncheck && change.status !== status.revise) {
  470. if (change.status === status.checkNo) {
  471. const lastAuditors = await this.service.changePlanAudit.getAuditors(change.id, change.times - 1);
  472. const onAuditor = this._.findLast(lastAuditors, { status: status.checkNo });
  473. if (onAuditor && onAuditor.aid === accountId) {
  474. change.cancancel = 4;// 审批人撤回退回原报
  475. change.preAuditors = lastAuditors.filter(x => { return x.order === onAuditor.order; });
  476. }
  477. } else {
  478. if (change.flowAuditors.find(x => { return x.status !== status.checking; }) && change.flowAuditorIds.indexOf(accountId) < 0) return; // 当前流程存在审批人审批通过时,不可撤回
  479. if (change.curAuditorIds.indexOf(accountId) < 0 && change.flowAuditorIds.indexOf(accountId) >= 0) {
  480. change.cancancel = 5; // 会签未全部审批通过时,审批人撤回审批通过
  481. return;
  482. }
  483. const preAuditors = change.curAuditors[0].order !== 1 ? change.auditors.filter(x => { return x.order === change.curAuditors[0].order - 1; }) : [];
  484. const preAuditorCheckAgain = preAuditors.find(pa => { return pa.status === status.checkAgain; });
  485. const preAuditorCheckCancel = preAuditors.find(pa => { return pa.status === status.checkCancel; });
  486. const preAuditorIds = preAuditorCheckAgain ? [] : preAuditors.map(x => { return x.aid; }); // 重审不可撤回
  487. if ((this._.isEqual(change.flowAuditorIds, preAuditorIds) && preAuditorCheckCancel)) {
  488. return; // 不可以多次撤回
  489. }
  490. const preAuditChecked = preAuditors.find(pa => { return pa.status === status.checked && pa.aid === accountId; });
  491. // const preAuditCheckNoPre = preAuditors.find(pa => { return pa.status === status.checkNoPre && pa.uid === accountId; });
  492. if (preAuditorIds.indexOf(accountId) >= 0) {
  493. if (preAuditChecked && change.status === status.checking) {
  494. change.cancancel = 2;// 审批人撤回审批通过
  495. }
  496. // else if (preAuditCheckNoPre && change.status === status.checkNoPre) {
  497. // change.cancancel = 3;// 审批人撤回审批退回上一人
  498. // }
  499. change.preAuditors = preAuditors;
  500. } else if (preAuditors.length === 0 && accountId === change.uid) {
  501. change.cancancel = 1;// 原报撤回
  502. }
  503. }
  504. }
  505. }
  506. async getListByArchives(tid, ids) {
  507. if (ids.length === 0) return [];
  508. const sql = 'SELECT c.* FROM ?? as c LEFT JOIN (SELECT cpid, MAX(end_time) as end_time FROM ?? WHERE ' +
  509. 'tid = ? AND cpid in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY cpid) as ca ON c.id = ca.cpid WHERE' +
  510. ' c.tid = ? AND c.id in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') AND c.status = ? ORDER BY ca.end_time DESC';
  511. const params = [this.tableName, this.ctx.service.changePlanAudit.tableName, tid, tid, audit.status.checked];
  512. const list = await this.db.query(sql, params);
  513. return list;
  514. }
  515. }
  516. return ChangePlan;
  517. };