shenpi_audit.js 31 KB

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