change_plan.js 30 KB

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