change_plan.js 26 KB

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