change_plan.js 30 KB

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