shenpi_audit.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. 'use strict';
  2. /**
  3. * 版本数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. const shenpiConst = require('../const/shenpi');
  10. const auditType = require('../const/audit').auditType;
  11. module.exports = app => {
  12. class ShenpiAudit extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'shenpi_audit';
  22. }
  23. async getShenpi(tid, info) {
  24. const spConst = this._.cloneDeep(shenpiConst);
  25. for (const sp of spConst.sp_lc) {
  26. sp.status = info.shenpi && info.shenpi[sp.code] ? info.shenpi[sp.code] : shenpiConst.sp_status.sqspr;
  27. if (sp.status === shenpiConst.sp_status.gdspl) {
  28. sp.groupList = await this.ctx.service.shenpiGroup.getGroupList(tid, sp.type) || [];
  29. if (sp.groupList && sp.groupList.length > 0) {
  30. for (const group of sp.groupList) {
  31. if (group.change_type) group.change_type = JSON.parse(group.change_type);
  32. group.auditGroupList = await this.getAuditGroupList(tid, sp.type, sp.status, group.id);
  33. if (group.is_select) sp.auditGroupList = group.auditGroupList;
  34. }
  35. } else {
  36. sp.auditGroupList = await this.getAuditGroupList(tid, sp.type, sp.status);
  37. }
  38. } else if (sp.status === shenpiConst.sp_status.gdzs) {
  39. sp.audit = await this.getAudit(tid, sp.type, sp.status);
  40. }
  41. }
  42. return spConst;
  43. }
  44. async getAudit(tid, type, status) {
  45. const sql = 'SELECT sp.audit_id, sp.audit_type, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  46. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ?';
  47. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  48. return await this.db.queryOne(sql, sqlParam);
  49. }
  50. async getUnionAudit(tid, type, audit_order) {
  51. const sql = 'SELECT sp.id, sp.audit_id, sp.audit_ledger_id, pa.name, pa.company FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  52. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? AND audit_order = ?';
  53. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, shenpiConst.sp_status.gdspl, audit_order];
  54. return await this.db.query(sql, sqlParam);
  55. }
  56. async getAuditList(tid, type, status, group_id = 0) {
  57. const sql = 'SELECT sp.id, sp.audit_id, sp.audit_order, sp.audit_type, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  58. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? AND sp.sp_group = ? ORDER BY sp.audit_order ASC, sp.id ASC';
  59. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status, group_id];
  60. return await this.db.query(sql, sqlParam);
  61. }
  62. async getAllAuditGroupList(tids, type, status, group_id = 0) {
  63. const sql = 'SELECT sp.id, sp.tid, sp.audit_id, sp.audit_type, sp.audit_order, sp.sp_group, sp.audit_group, sp.audit_group_order, sp.audit_group_limit, sp.audit_checkno_valid, sp.audit_group_need, ' +
  64. ' pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  65. ' WHERE sp.tid in (' + this.ctx.helper.getInArrStrSqlFilter(tids) + ') AND sp.sp_type = ? AND sp.sp_status = ? AND sp.sp_group = ? ORDER BY sp.audit_order ASC, sp.id ASC';
  66. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, type, status, group_id];
  67. const audits = await this.db.query(sql, sqlParam);
  68. const result = [];
  69. for (const t of tids) {
  70. const oneResult = [];
  71. const tidAudits = audits.filter(a => a.tid === t);
  72. for (const a of tidAudits) {
  73. if (a.audit_order > 0) {
  74. if (oneResult[a.audit_order - 1]) {
  75. oneResult[a.audit_order - 1].push(a);
  76. } else {
  77. oneResult.push([a]);
  78. }
  79. } else {
  80. oneResult.push([a]);
  81. }
  82. }
  83. result.push({ tid: t, audits: oneResult });
  84. }
  85. return result;
  86. }
  87. async getAuditGroupList(tid, type, status, group_id = 0) {
  88. const sql = 'SELECT sp.id, sp.audit_id, sp.audit_type, sp.audit_order, sp.sp_group, sp.audit_group, sp.audit_group_order, sp.audit_group_limit, sp.audit_checkno_valid, sp.audit_group_need, ' +
  89. ' pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  90. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? AND sp.sp_group = ? ORDER BY sp.audit_order ASC, sp.id ASC';
  91. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status, group_id];
  92. const audits = await this.db.query(sql, sqlParam);
  93. const result = [];
  94. for (const a of audits) {
  95. if (a.audit_order > 0) {
  96. if (result[a.audit_order - 1]) {
  97. result[a.audit_order - 1].push(a);
  98. } else {
  99. result.push([a]);
  100. }
  101. } else {
  102. result.push([a]);
  103. }
  104. }
  105. return result;
  106. }
  107. async addAudit(data, tid = this.ctx.tender.id) {
  108. let result;
  109. const transaction = await this.db.beginTransaction();
  110. try {
  111. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  112. const options = {
  113. where: {
  114. tid,
  115. user_id: data.audit_id,
  116. },
  117. };
  118. const updateData = {
  119. status: 1,
  120. };
  121. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  122. }
  123. const group = await this.ctx.service.shenpiGroup.getGroupBySelect(tid, data.code);
  124. const insertData = {
  125. tid,
  126. sp_type: data.code,
  127. sp_status: data.status,
  128. audit_id: data.audit_id,
  129. sp_group: group ? group.id : 0,
  130. };
  131. if (data.audit_type) insertData.audit_type = data.audit_type;
  132. if (data.audit_order) insertData.audit_order = data.audit_order;
  133. result = await transaction.insert(this.tableName, insertData);
  134. await transaction.commit();
  135. } catch (err) {
  136. await transaction.rollback();
  137. throw err;
  138. }
  139. if (result.affectedRows !== 1) throw '添加审批人失败';
  140. const sql = 'SELECT sp.id, sp.audit_id, sp.audit_type, sp.audit_order, sp.sp_group, sp.audit_group, sp.audit_group_order, sp.audit_group_limit, sp.audit_checkno_valid, sp.audit_group_need, ' +
  141. ' pa.name, pa.company, pa.role FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  142. ' WHERE sp.id = ?';
  143. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, result.insertId];
  144. return await this.db.queryOne(sql, sqlParam);
  145. }
  146. async removeAudit(data) {
  147. const group = await this.ctx.service.shenpiGroup.getGroupBySelect(this.ctx.tender.id, data.code);
  148. const delData = {
  149. tid: this.ctx.tender.id,
  150. sp_type: data.code,
  151. sp_status: data.status,
  152. audit_id: data.audit_id,
  153. sp_group: group ? group.id : 0,
  154. };
  155. const audit = await this.getDataByCondition(delData);
  156. const conditon = { tid: this.ctx.tender.id, sp_type: data.code, sp_status: data.status };
  157. if (group) conditon.sp_group = group.id;
  158. const allAudit = await this.getAllDataByCondition({ where: conditon });
  159. const sameOrder = allAudit.filter(x => { return x.audit_order === audit.audit_order; });
  160. const updateData = [];
  161. if (sameOrder.length === 1) {
  162. for (const aa of allAudit) {
  163. if (aa.audit_order > audit.audit_order) updateData.push({ id: aa.id, audit_order: aa.audit_order - 1 });
  164. }
  165. }
  166. const transaction = await this.db.beginTransaction();
  167. try {
  168. await transaction.delete(this.tableName, delData);
  169. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  170. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  171. const options = {
  172. where: {
  173. tid: this.ctx.tender.id,
  174. user_id: data.audit_id,
  175. },
  176. };
  177. const updateData = {
  178. status: 0,
  179. };
  180. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  181. }
  182. await transaction.commit();
  183. return true;
  184. } catch (err) {
  185. await transaction.rollback();
  186. throw err;
  187. }
  188. }
  189. async copyAudit2otherTender(data, tid = this.ctx.tender.id) {
  190. const transaction = await this.db.beginTransaction();
  191. try {
  192. const shenpi_status = parseInt(data.status);
  193. // 1.复制修改当前审批到其他的tender_info里
  194. // 2.删除其他的shenpiAudit
  195. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  196. const tenderInfoUpdateList = [];
  197. const tenders = [];
  198. for (const tid of data.tidList.split(',')) {
  199. // 获取原报
  200. const tender = await this.ctx.service.tender.getDataById(tid);
  201. if (tender) {
  202. tenders.push({ id: parseInt(tid), user_id: tender.user_id });
  203. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tid);
  204. // 把当前期状态复制到其他标段里
  205. if (shenpiInfo[data.code] !== shenpi_status) {
  206. shenpiInfo[data.code] = shenpi_status;
  207. tenderInfoUpdateList.push({ row: { shenpi: JSON.stringify(shenpiInfo) }, where: { tid: parseInt(tid) } });
  208. }
  209. }
  210. }
  211. if (tenderInfoUpdateList.length > 0) await transaction.updateRows(this.ctx.service.tenderInfo.tableName, tenderInfoUpdateList);
  212. const insertList = [];
  213. const needYB = ['ledger', 'revise', 'change', 'financial'];
  214. const canYB = needYB.indexOf(data.code) !== -1;
  215. let is_group = false;
  216. let groupList = [];
  217. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  218. groupList = await this.ctx.service.shenpiGroup.getGroupList(tid, shenpiConst.sp_type[data.code]);
  219. if (groupList.length > 0) {
  220. is_group = true;
  221. for (const g of groupList) {
  222. g.auditList = await this.getAllDataByCondition({ where: { tid, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status, sp_group: g.id } });
  223. }
  224. }
  225. }
  226. for (const t of tenders) {
  227. if (shenpi_status !== shenpiConst.sp_status.sqspr) {
  228. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  229. await transaction.delete(this.ctx.service.shenpiGroup.tableName, {
  230. tid: t.id, sp_type: shenpiConst.sp_type[data.code],
  231. });
  232. }
  233. await transaction.delete(this.tableName, { tid: t.id, sp_type: shenpiConst.sp_type[data.code] || shenpiConst.sp_other_type[data.code], sp_status: shenpi_status });
  234. if (is_group) {
  235. for (const g of groupList) {
  236. const result = await transaction.insert(this.ctx.service.shenpiGroup.tableName, {
  237. tid: t.id,
  238. sp_type: shenpiConst.sp_type[data.code],
  239. name: g.name,
  240. is_select: g.is_select,
  241. change_type: g.change_type,
  242. create_time: new Date(),
  243. });
  244. for (const s of g.auditList) {
  245. insertList.push({
  246. tid: t.id,
  247. sp_type: shenpiConst.sp_type[data.code],
  248. sp_status: shenpi_status,
  249. audit_id: s.audit_id,
  250. audit_type: s.audit_type,
  251. audit_order: s.audit_order,
  252. audit_group: s.audit_group || '',
  253. audit_group_order: s.audit_group_order || 0,
  254. audit_group_limit: s.audit_group_limit || 0,
  255. audit_checkno_valid: s.audit_checkno_valid,
  256. audit_group_need: s.audit_group_need || 1,
  257. sp_group: result.insertId,
  258. });
  259. }
  260. }
  261. } else {
  262. const sourceList = data.auditList.filter(x => { return x.audit_id && (x.audit_id !== t.user_id || canYB); });
  263. sourceList.sort((a, b) => { return a.audit_order - b.audit_order; });
  264. let audit_order = 0, curAuditOrder = 0;
  265. for (const s of sourceList) {
  266. if (s.audit_order !== curAuditOrder) {
  267. curAuditOrder = s.audit_order;
  268. audit_order = audit_order + 1;
  269. }
  270. insertList.push({
  271. tid: t.id,
  272. sp_type: shenpiConst.sp_type[data.code] || shenpiConst.sp_other_type[data.code],
  273. sp_status: shenpi_status,
  274. audit_id: s.audit_id,
  275. audit_type: s.audit_type,
  276. audit_order: audit_order,
  277. audit_group: s.audit_group || '',
  278. audit_group_order: s.audit_group_order || 0,
  279. audit_group_limit: s.audit_group_limit || 0,
  280. audit_checkno_valid: s.audit_checkno_valid,
  281. audit_group_need: s.audit_group_need || 1,
  282. sp_group: 0,
  283. });
  284. }
  285. }
  286. }
  287. }
  288. // console.log(tenderInfoUpdateList, insertList);
  289. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  290. await transaction.commit();
  291. return true;
  292. } catch (err) {
  293. await transaction.rollback();
  294. throw err;
  295. }
  296. }
  297. async copyAudit2otherShenpi(data) {
  298. const transaction = await this.db.beginTransaction();
  299. try {
  300. const shenpi_status = parseInt(data.status);
  301. // 1.修改当前审批到它的tender_info里
  302. // 2.删除相同审批状态的shenpiAudit
  303. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  304. const insertList = [];
  305. const needYB = ['ledger', 'revise', 'change'];
  306. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(this.ctx.tender.id);
  307. for (const code of data.shenpiList.split(',')) {
  308. // 把当前审批状态复制到其他标段里
  309. if (shenpiInfo[code] !== shenpi_status) {
  310. shenpiInfo[code] = shenpi_status;
  311. }
  312. if (shenpiInfo[code] !== shenpiConst.sp_status.sqspr) {
  313. if (shenpiInfo[code] === shenpiConst.sp_status.gdspl) {
  314. const group = await this.ctx.service.shenpiGroup.getAllDataByCondition({ where: { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code] } });
  315. if (group && group.length > 0) {
  316. throw '选择同步的流程中存在审批组,无法设置同步';
  317. }
  318. }
  319. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code], sp_status: shenpiInfo[code] });
  320. const flows = data.flowList.split(';');
  321. let audit_order = 1;
  322. for (const f of flows) {
  323. const audit_type = parseInt(f.split(':')[0]);
  324. const auditTypeInfo = auditType.info[audit_type];
  325. const auditTypeValid = !auditTypeInfo.valid || auditTypeInfo.valid.indexOf(code) >= 0;
  326. const auditIds = f.split(':')[1].split(',').map(x => { return parseInt(x)});
  327. for (const aid of auditIds) {
  328. if (aid !== this.ctx.tender.data.user_id || needYB.indexOf(code) >= 0) {
  329. insertList.push({
  330. tid: this.ctx.tender.id,
  331. sp_type: shenpiConst.sp_type[code], sp_status: shenpi_status,
  332. audit_id: aid,
  333. audit_type: auditTypeValid ? audit_type : auditType.key.common,
  334. audit_order: audit_order,
  335. });
  336. if (!auditTypeValid) audit_order++;
  337. }
  338. }
  339. if (auditTypeValid) audit_order++;
  340. }
  341. }
  342. }
  343. await transaction.update(this.ctx.service.tenderInfo.tableName,
  344. {
  345. shenpi: JSON.stringify(shenpiInfo),
  346. },
  347. {
  348. where: {
  349. tid: this.ctx.tender.id,
  350. },
  351. });
  352. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  353. await transaction.commit();
  354. return true;
  355. } catch (err) {
  356. await transaction.rollback();
  357. throw err;
  358. }
  359. }
  360. async setAuditType(data) {
  361. const conn = await this.db.beginTransaction();
  362. try {
  363. const updateData = { audit_type: data.audit_type };
  364. const group = await this.ctx.service.shenpiGroup.getGroupBySelect(this.ctx.tender.id, data.code);
  365. const condition = {
  366. tid: this.ctx.tender.id,
  367. sp_type: data.code,
  368. sp_status: data.status,
  369. audit_id: data.audit_id,
  370. sp_group: group ? group.id : 0,
  371. };
  372. await conn.update(this.tableName, updateData, { where: condition });
  373. await conn.commit();
  374. } catch (err) {
  375. await conn.rollback();
  376. throw err;
  377. }
  378. }
  379. // 更新审批流程
  380. async updateAuditList(transaction, tenderId, sp_status, sp_type, ids) {
  381. if (sp_status === shenpiConst.sp_status.gdspl) {
  382. const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
  383. const oldIds = this._.map(auditList, 'audit_id');
  384. if (this._.isEqual(ids, oldIds)) {
  385. return;
  386. }
  387. // 更新固定审批流
  388. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
  389. const insertDatas = [];
  390. for (const [i, id] of ids.entries()) {
  391. insertDatas.push({
  392. tid: tenderId,
  393. sp_type,
  394. sp_status,
  395. audit_id: id,
  396. audit_order: i + 1,
  397. });
  398. }
  399. await transaction.insert(this.tableName, insertDatas);
  400. if (sp_type === shenpiConst.sp_type.stage) {
  401. // 判断哪些audit_id不存在了,哪些audit_为新增
  402. const exist = this._.difference(ids, oldIds);// 判断新增的
  403. const unExist = this._.difference(oldIds, ids);// 判断已删除的
  404. if (exist.length > 0) {
  405. const options = {
  406. where: {
  407. tid: this.ctx.tender.id,
  408. user_id: exist,
  409. },
  410. };
  411. const updateData = {
  412. status: 1,
  413. };
  414. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  415. }
  416. if (unExist.length > 0) {
  417. const options2 = {
  418. where: {
  419. tid: this.ctx.tender.id,
  420. user_id: unExist,
  421. },
  422. };
  423. const updateData2 = {
  424. status: 0,
  425. };
  426. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData2, options2);
  427. }
  428. }
  429. } else if (sp_status === shenpiConst.sp_status.gdzs) {
  430. const audit = await this.getAudit(tenderId, sp_type, sp_status);
  431. if (audit && audit.audit_id !== ids[ids.length - 1]) {
  432. // 更换终审
  433. await transaction.update(this.tableName, { audit_id: ids[ids.length - 1] }, { where: { tid: tenderId, sp_type, sp_status } });
  434. } else if (!audit) {
  435. await transaction.insert(this.tableName, { tid: tenderId, sp_type, sp_status, audit_id: ids[ids.length - 1] });
  436. }
  437. }
  438. }
  439. async updateAuditListWithAuditType(transaction, tenderId, sp_status, sp_type, auditGroup, sp_group = 0) {
  440. const auditList = await this.getAuditList(tenderId, sp_type, sp_status, sp_group);
  441. const newAuditList = [];
  442. for (const group of auditGroup) {
  443. newAuditList.push(...group);
  444. }
  445. let sameAudit = auditList.length === newAuditList.length;
  446. if (sameAudit) {
  447. for (const audit of auditList) {
  448. const newAudit = newAuditList.find(x => { return x.audit_id === audit.aid; });
  449. if (!newAudit || newAudit.audit_order !== audit.audit_order || newAudit.audit_type !== audit.audit_type) {
  450. sameAudit = false;
  451. break;
  452. }
  453. }
  454. }
  455. if (sameAudit) return;
  456. // 更新固定审批流
  457. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status, sp_group });
  458. const insertDatas = [];
  459. for (const a of newAuditList) {
  460. insertDatas.push({
  461. tid: tenderId,
  462. sp_type,
  463. sp_status,
  464. audit_id: a.aid || a.uid || a.audit_id,
  465. audit_order: a.audit_order,
  466. audit_type: a.audit_type,
  467. audit_group: a.audit_group || '',
  468. audit_group_order: a.audit_group_order || 0,
  469. audit_group_limit: a.audit_group_limit || 0,
  470. audit_checkno_valid: a.audit_checkno_valid,
  471. audit_group_need: a.audit_group_need || 1,
  472. audit_ledger_id: a.audit_ledger_id || '',
  473. sp_group,
  474. });
  475. }
  476. await transaction.insert(this.tableName, insertDatas);
  477. }
  478. async getRemoveTenders(tenders) {
  479. const removeTenders = [];
  480. const shenpiInfos = await this.ctx.service.tenderInfo.getAllTenderShenpiInfo(this._.map(tenders, 'id'));
  481. for (const tender of tenders) {
  482. const tenderInfo = this._.find(shenpiInfos, { tid: tender.id });
  483. if (!tenderInfo) {
  484. removeTenders.push(tender.id);
  485. } else {
  486. tender.shenpiInfo = tenderInfo.shenpiInfo;
  487. // 获取所有的固定审批流或固定终审
  488. const shenpiauditList = {};
  489. for (const shenpi in tender.shenpiInfo) {
  490. if (tender.shenpiInfo[shenpi] === shenpiConst.sp_status.gdspl) {
  491. const group = await this.ctx.service.shenpiGroup.getGroupBySelect(tender.id, shenpiConst.sp_type[shenpi]);
  492. const shenpiList = await this.getAuditList(tender.id, shenpiConst.sp_type[shenpi], tender.shenpiInfo[shenpi], group ? group.id : 0);
  493. const shenpiIdList = this._.map(shenpiList, 'audit_id');
  494. shenpiauditList[shenpi] = shenpiIdList.length ? shenpiIdList : null;
  495. } else if (tender.shenpiInfo[shenpi] === shenpiConst.sp_status.gdzs) {
  496. const shenpiInfo = await this.getDataByCondition({ tid: tender.id, sp_type: shenpiConst.sp_type[shenpi], sp_status: tender.shenpiInfo[shenpi] });
  497. shenpiauditList[shenpi] = shenpiInfo && shenpiInfo.audit_id ? [shenpiInfo.audit_id] : null;
  498. }
  499. }
  500. tender.shenpiauditList = shenpiauditList;
  501. }
  502. }
  503. return removeTenders;
  504. }
  505. async noYbShenpiList(uid, shenpiList) {
  506. // 剔除原报及分析审批流中的审批人
  507. const yBIndex = shenpiList.findIndex(x => { return x.audit_id === uid; });
  508. if (yBIndex !== -1) {
  509. const yB = shenpiList[yBIndex];
  510. if (yB.audit_type !== auditType.key.common) {
  511. // 如果是会签或签且人数只有2人,则audit_type变为common
  512. const curSps = shenpiList.filter(x => { return x.audit_order === yB.audit_order; });
  513. if (curSps.length === 1) {
  514. shenpiList.forEach(x => { if (x.audit_order > yB.audit_order) x.audit_order--; });
  515. } else if (curSps.length === 2) {
  516. curSps.forEach(x => { x.audit_type = auditType.key.common; });
  517. }
  518. } else {
  519. // 比它大的audit_order都要-1;
  520. shenpiList.forEach(x => { if (x.audit_order > yB.audit_order) x.audit_order--; });
  521. }
  522. shenpiList.splice(yBIndex, 1);
  523. }
  524. }
  525. async saveUnionAudit(tid, data) {
  526. const orgData = await this.getAllDataByCondition({ where: { id: data.map(x => { return x.id; }) } });
  527. for (const od of orgData) {
  528. if (od.tid !== tid) throw '保存数据错误,请刷新页面后重试';
  529. }
  530. const updateData = data.map(x => { return { id: x.id, audit_ledger_id: x.audit_ledger_id }; });
  531. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  532. }
  533. async saveMultiAudit(tid, data) {
  534. const orgData = await this.getAllDataByCondition({ where: { id: data.map(x => { return x.id; }) } });
  535. if (orgData.length !== data.length) throw '保存数据错误,请刷新页面后重试';
  536. for (const od of orgData) {
  537. if (od.tid !== tid) throw '保存数据错误,请刷新页面后重试';
  538. }
  539. const updateData = data.map(x => {
  540. return {
  541. id: x.id, audit_group: x.audit_group,
  542. audit_group_order: x.audit_group_order, audit_group_limit: x.audit_group_limit,
  543. audit_checkno_valid: x.audit_checkno_valid, audit_group_need: x.audit_group_need,
  544. };
  545. });
  546. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  547. return updateData;
  548. }
  549. }
  550. return ShenpiAudit;
  551. };