shenpi_audit.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. for (const sp of shenpiConst.sp_lc) {
  25. sp.status = info.shenpi ? info.shenpi[sp.code] : shenpiConst.sp_status.sqspr;
  26. if (sp.status === shenpiConst.sp_status.gdspl) {
  27. sp.groupList = await this.ctx.service.shenpiGroup.getGroupList(tid, sp.type) || [];
  28. if (sp.groupList && sp.groupList.length > 0) {
  29. for (const group of sp.groupList) {
  30. if (group.change_type) group.change_type = JSON.parse(group.change_type);
  31. group.auditGroupList = await this.getAuditGroupList(tid, sp.type, sp.status, group.id);
  32. if (group.is_select) sp.auditGroupList = group.auditGroupList;
  33. }
  34. } else {
  35. sp.auditGroupList = await this.getAuditGroupList(tid, sp.type, sp.status);
  36. }
  37. } else if (sp.status === shenpiConst.sp_status.gdzs) {
  38. sp.audit = await this.getAudit(tid, sp.type, sp.status);
  39. }
  40. }
  41. return shenpiConst;
  42. }
  43. async getAudit(tid, type, status) {
  44. const sql = 'SELECT sp.audit_id, sp.audit_type, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  45. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ?';
  46. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  47. return await this.db.queryOne(sql, sqlParam);
  48. }
  49. async getAuditList(tid, type, status, group_id = 0) {
  50. const sql = 'SELECT sp.id, sp.audit_id, sp.audit_type, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  51. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? AND sp.sp_group = ? ORDER BY sp.audit_order ASC, sp.id ASC';
  52. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status, group_id];
  53. return await this.db.query(sql, sqlParam);
  54. }
  55. async getAuditGroupList(tid, type, status, group_id = 0) {
  56. const sql = 'SELECT sp.audit_id, sp.audit_type, sp.audit_order, sp.sp_group, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  57. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? AND sp.sp_group = ? ORDER BY sp.audit_order ASC, sp.id ASC';
  58. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status, group_id];
  59. const audits = await this.db.query(sql, sqlParam);
  60. const result = [];
  61. for (const a of audits) {
  62. if (a.audit_order > 0) {
  63. if (result[a.audit_order - 1]) {
  64. result[a.audit_order - 1].push(a);
  65. } else {
  66. result.push([a]);
  67. }
  68. } else {
  69. result.push([a]);
  70. }
  71. }
  72. return result;
  73. }
  74. async addAudit(data) {
  75. let result;
  76. const transaction = await this.db.beginTransaction();
  77. try {
  78. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  79. const options = {
  80. where: {
  81. tid: this.ctx.tender.id,
  82. user_id: data.audit_id,
  83. },
  84. };
  85. const updateData = {
  86. status: 1,
  87. };
  88. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  89. }
  90. const group = await this.ctx.service.shenpiGroup.getGroupBySelect(this.ctx.tender.id, data.code);
  91. const insertData = {
  92. tid: this.ctx.tender.id,
  93. sp_type: data.code,
  94. sp_status: data.status,
  95. audit_id: data.audit_id,
  96. sp_group: group ? group.id : 0,
  97. };
  98. if (data.audit_type) insertData.audit_type = data.audit_type;
  99. if (data.audit_order) insertData.audit_order = data.audit_order;
  100. result = await transaction.insert(this.tableName, insertData);
  101. await transaction.commit();
  102. } catch (err) {
  103. await transaction.rollback();
  104. throw err;
  105. }
  106. if (result.affectedRows !== 1) throw '添加审批人失败';
  107. data.id = result.insertId;
  108. return data;
  109. }
  110. async removeAudit(data) {
  111. const group = await this.ctx.service.shenpiGroup.getGroupBySelect(this.ctx.tender.id, data.code);
  112. const delData = {
  113. tid: this.ctx.tender.id,
  114. sp_type: data.code,
  115. sp_status: data.status,
  116. audit_id: data.audit_id,
  117. sp_group: group ? group.id : 0,
  118. };
  119. const audit = await this.getDataByCondition(delData);
  120. const conditon = { tid: this.ctx.tender.id, sp_type: data.code, sp_status: data.status };
  121. if (group) conditon.sp_group = group.id;
  122. const allAudit = await this.getAllDataByCondition({ where: conditon });
  123. const sameOrder = allAudit.filter(x => { return x.audit_order === audit.audit_order; });
  124. const updateData = [];
  125. if (sameOrder.length === 1) {
  126. for (const aa of allAudit) {
  127. if (aa.audit_order > audit.audit_order) updateData.push({ id: aa.id, audit_order: aa.audit_order - 1 });
  128. }
  129. }
  130. const transaction = await this.db.beginTransaction();
  131. try {
  132. await transaction.delete(this.tableName, delData);
  133. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  134. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  135. const options = {
  136. where: {
  137. tid: this.ctx.tender.id,
  138. user_id: data.audit_id,
  139. },
  140. };
  141. const updateData = {
  142. status: 0,
  143. };
  144. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  145. }
  146. await transaction.commit();
  147. return true;
  148. } catch (err) {
  149. await transaction.rollback();
  150. throw err;
  151. }
  152. }
  153. async copyAudit2otherTender(data) {
  154. const transaction = await this.db.beginTransaction();
  155. try {
  156. const shenpi_status = parseInt(data.status);
  157. // 1.复制修改当前审批到其他的tender_info里
  158. // 2.删除其他的shenpiAudit
  159. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  160. const tenderInfoUpdateList = [];
  161. const tenders = [];
  162. for (const tid of data.tidList.split(',')) {
  163. // 获取原报
  164. const tender = await this.ctx.service.tender.getDataById(tid);
  165. if (tender) {
  166. tenders.push({ id: parseInt(tid), user_id: tender.user_id });
  167. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tid);
  168. // 把当前期状态复制到其他标段里
  169. if (shenpiInfo[data.code] !== shenpi_status) {
  170. shenpiInfo[data.code] = shenpi_status;
  171. tenderInfoUpdateList.push({ row: { shenpi: JSON.stringify(shenpiInfo) }, where: { tid: parseInt(tid) } });
  172. }
  173. }
  174. }
  175. if (tenderInfoUpdateList.length > 0) await transaction.updateRows(this.ctx.service.tenderInfo.tableName, tenderInfoUpdateList);
  176. const insertList = [];
  177. const needYB = ['ledger', 'revise', 'change'];
  178. const canYB = needYB.indexOf(data.code) !== -1;
  179. let is_group = false;
  180. let groupList = [];
  181. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  182. groupList = await this.ctx.service.shenpiGroup.getGroupList(this.ctx.tender.id, shenpiConst.sp_type[data.code]);
  183. if (groupList.length > 0) {
  184. is_group = true;
  185. for (const g of groupList) {
  186. g.auditList = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status, sp_group: g.id } });
  187. }
  188. }
  189. }
  190. for (const t of tenders) {
  191. if (shenpi_status !== shenpiConst.sp_status.sqspr) {
  192. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  193. await transaction.delete(this.ctx.service.shenpiGroup.tableName, {
  194. tid: t.id, sp_type: shenpiConst.sp_type[data.code],
  195. });
  196. }
  197. await transaction.delete(this.tableName, { tid: t.id, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status });
  198. if (is_group) {
  199. for (const g of groupList) {
  200. const result = await transaction.insert(this.ctx.service.shenpiGroup.tableName, {
  201. tid: t.id,
  202. sp_type: shenpiConst.sp_type[data.code],
  203. name: g.name,
  204. is_select: g.is_select,
  205. change_type: g.change_type,
  206. create_time: new Date(),
  207. });
  208. for (const s of g.auditList) {
  209. insertList.push({
  210. tid: t.id,
  211. sp_type: shenpiConst.sp_type[data.code],
  212. sp_status: shenpi_status,
  213. audit_id: s.audit_id,
  214. audit_type: s.audit_type,
  215. audit_order: s.audit_order,
  216. sp_group: result.insertId,
  217. });
  218. }
  219. }
  220. } else {
  221. const sourceList = data.auditList.filter(x => { return x.audit_id && (x.audit_id !== t.user_id || canYB); });
  222. sourceList.sort((a, b) => { return a.audit_order - b.audit_order; });
  223. let audit_order = 0, curAuditOrder = 0;
  224. for (const s of sourceList) {
  225. if (s.audit_order !== curAuditOrder) {
  226. curAuditOrder = s.audit_order;
  227. audit_order = audit_order + 1;
  228. }
  229. insertList.push({
  230. tid: t.id,
  231. sp_type: shenpiConst.sp_type[data.code],
  232. sp_status: shenpi_status,
  233. audit_id: s.audit_id,
  234. audit_type: s.audit_type,
  235. audit_order: audit_order,
  236. sp_group: 0,
  237. });
  238. }
  239. }
  240. }
  241. }
  242. // console.log(tenderInfoUpdateList, insertList);
  243. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  244. await transaction.commit();
  245. return true;
  246. } catch (err) {
  247. await transaction.rollback();
  248. throw err;
  249. }
  250. }
  251. async copyAudit2otherShenpi(data) {
  252. const transaction = await this.db.beginTransaction();
  253. try {
  254. const shenpi_status = parseInt(data.status);
  255. // 1.修改当前审批到它的tender_info里
  256. // 2.删除相同审批状态的shenpiAudit
  257. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  258. const insertList = [];
  259. const needYB = ['ledger', 'revise', 'change'];
  260. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(this.ctx.tender.id);
  261. for (const code of data.shenpiList.split(',')) {
  262. // 把当前审批状态复制到其他标段里
  263. if (shenpiInfo[code] !== shenpi_status) {
  264. shenpiInfo[code] = shenpi_status;
  265. }
  266. if (shenpiInfo[code] !== shenpiConst.sp_status.sqspr) {
  267. if (shenpiInfo[code] === shenpiConst.sp_status.gdspl) {
  268. const group = await this.ctx.service.shenpiGroup.getAllDataByCondition({ where: { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code] } });
  269. if (group && group.length > 0) {
  270. throw '选择同步的流程中存在审批组,无法设置同步';
  271. }
  272. }
  273. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code], sp_status: shenpiInfo[code] });
  274. for (const aid of data.aidList.split(',')) {
  275. if (aid && parseInt(aid) !== this.ctx.tender.data.user_id || (parseInt(aid) === this.ctx.tender.data.user_id && needYB.indexOf(code) !== -1)) {
  276. const insertData = {
  277. tid: this.ctx.tender.id,
  278. sp_type: shenpiConst.sp_type[code],
  279. sp_status: shenpi_status,
  280. audit_id: parseInt(aid),
  281. audit_order: insertList.length + 1,
  282. };
  283. insertList.push(insertData);
  284. }
  285. }
  286. }
  287. }
  288. await transaction.update(this.ctx.service.tenderInfo.tableName,
  289. {
  290. shenpi: JSON.stringify(shenpiInfo),
  291. },
  292. {
  293. where: {
  294. tid: this.ctx.tender.id,
  295. },
  296. });
  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 setAuditType(data) {
  306. const conn = await this.db.beginTransaction();
  307. try {
  308. const updateData = { audit_type: data.audit_type };
  309. const group = await this.ctx.service.shenpiGroup.getGroupBySelect(this.ctx.tender.id, data.code);
  310. const condition = {
  311. tid: this.ctx.tender.id,
  312. sp_type: data.code,
  313. sp_status: data.status,
  314. audit_id: data.audit_id,
  315. sp_group: group ? group.id : 0,
  316. };
  317. await conn.update(this.tableName, updateData, { where: condition });
  318. await conn.commit();
  319. } catch (err) {
  320. await conn.rollback();
  321. throw err;
  322. }
  323. }
  324. // 更新审批流程
  325. async updateAuditList(transaction, tenderId, sp_status, sp_type, ids) {
  326. if (sp_status === shenpiConst.sp_status.gdspl) {
  327. const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
  328. const oldIds = this._.map(auditList, 'audit_id');
  329. if (this._.isEqual(ids, oldIds)) {
  330. return;
  331. }
  332. // 更新固定审批流
  333. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
  334. const insertDatas = [];
  335. for (const [i, id] of ids.entries()) {
  336. insertDatas.push({
  337. tid: tenderId,
  338. sp_type,
  339. sp_status,
  340. audit_id: id,
  341. audit_order: i + 1,
  342. });
  343. }
  344. await transaction.insert(this.tableName, insertDatas);
  345. if (sp_type === shenpiConst.sp_type.stage) {
  346. // 判断哪些audit_id不存在了,哪些audit_为新增
  347. const exist = this._.difference(ids, oldIds);// 判断新增的
  348. const unExist = this._.difference(oldIds, ids);// 判断已删除的
  349. console.log(ids, oldIds, exist, unExist);
  350. if (exist.length > 0) {
  351. const options = {
  352. where: {
  353. tid: this.ctx.tender.id,
  354. user_id: exist,
  355. },
  356. };
  357. const updateData = {
  358. status: 1,
  359. };
  360. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  361. }
  362. if (unExist.length > 0) {
  363. const options2 = {
  364. where: {
  365. tid: this.ctx.tender.id,
  366. user_id: unExist,
  367. },
  368. };
  369. const updateData2 = {
  370. status: 0,
  371. };
  372. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData2, options2);
  373. }
  374. }
  375. } else if (sp_status === shenpiConst.sp_status.gdzs) {
  376. const audit = await this.getAudit(tenderId, sp_type, sp_status);
  377. if (audit && audit.audit_id !== ids[ids.length - 1]) {
  378. // 更换终审
  379. await transaction.update(this.tableName, { audit_id: ids[ids.length - 1] }, { where: { tid: tenderId, sp_type, sp_status } });
  380. } else if (!audit) {
  381. await transaction.insert(this.tableName, { tid: tenderId, sp_type, sp_status, audit_id: ids[ids.length - 1] });
  382. }
  383. }
  384. }
  385. async updateAuditListWithAuditType(transaction, tenderId, sp_status, sp_type, auditGroup, sp_group = 0) {
  386. const auditList = await this.getAuditList(tenderId, sp_type, sp_status, sp_group);
  387. const newAuditList = [];
  388. for (const group of auditGroup) {
  389. newAuditList.push(...group);
  390. }
  391. let sameAudit = auditList.length === newAuditList.length;
  392. if (sameAudit) {
  393. for (const audit of auditList) {
  394. const newAudit = newAuditList.find(x => { return x.audit_id === audit.aid; });
  395. if (!newAudit || newAudit.audit_order !== audit.audit_order || newAudit.audit_type !== audit.audit_type) {
  396. sameAudit = false;
  397. break;
  398. }
  399. }
  400. }
  401. if (sameAudit) return;
  402. // 更新固定审批流
  403. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status, sp_group });
  404. const insertDatas = [];
  405. for (const a of newAuditList) {
  406. insertDatas.push({
  407. tid: tenderId,
  408. sp_type,
  409. sp_status,
  410. audit_id: a.aid || a.uid,
  411. audit_order: a.audit_order,
  412. audit_type: a.audit_type,
  413. sp_group,
  414. });
  415. }
  416. await transaction.insert(this.tableName, insertDatas);
  417. }
  418. async getRemoveTenders(tenders) {
  419. const removeTenders = [];
  420. for (const tender of tenders) {
  421. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tender.id);
  422. if (!shenpiInfo) {
  423. removeTenders.push(tender.id);
  424. } else {
  425. tender.shenpiInfo = shenpiInfo;
  426. // 获取所有的固定审批流或固定终审
  427. const shenpiauditList = {};
  428. for (const shenpi in tender.shenpiInfo) {
  429. if (tender.shenpiInfo[shenpi] === shenpiConst.sp_status.gdspl) {
  430. const group = await this.ctx.service.shenpiGroup.getGroupBySelect(tender.id, shenpiConst.sp_type[shenpi]);
  431. const shenpiList = await this.getAuditList(tender.id, shenpiConst.sp_type[shenpi], tender.shenpiInfo[shenpi], group ? group.id : 0);
  432. const shenpiIdList = this._.map(shenpiList, 'audit_id');
  433. shenpiauditList[shenpi] = shenpiIdList.length ? shenpiIdList : null;
  434. } else if (tender.shenpiInfo[shenpi] === shenpiConst.sp_status.gdzs) {
  435. const shenpiInfo = await this.getDataByCondition({ tid: tender.id, sp_type: shenpiConst.sp_type[shenpi], sp_status: tender.shenpiInfo[shenpi] });
  436. shenpiauditList[shenpi] = shenpiInfo && shenpiInfo.audit_id ? [shenpiInfo.audit_id] : null;
  437. }
  438. }
  439. tender.shenpiauditList = shenpiauditList;
  440. }
  441. }
  442. return removeTenders;
  443. }
  444. async noYbShenpiList(uid, shenpiList) {
  445. // 剔除原报及分析审批流中的审批人
  446. const yBIndex = shenpiList.findIndex(x => { return x.audit_id === uid; });
  447. if (yBIndex !== -1) {
  448. const yB = shenpiList[yBIndex];
  449. if (yB.audit_type !== auditType.key.common) {
  450. // 如果是会签或签且人数只有2人,则audit_type变为common
  451. const curSps = shenpiList.filter(x => { return x.audit_order === yB.audit_order; });
  452. if (curSps.length === 1) {
  453. shenpiList.forEach(x => { if (x.audit_order > yB.audit_order) x.audit_order--; });
  454. } else if (curSps.length === 2) {
  455. curSps.forEach(x => { x.audit_type = auditType.key.common; });
  456. }
  457. } else {
  458. // 比它大的audit_order都要-1;
  459. shenpiList.forEach(x => { if (x.audit_order > yB.audit_order) x.audit_order--; });
  460. }
  461. shenpiList.splice(yBIndex, 1);
  462. }
  463. }
  464. }
  465. return ShenpiAudit;
  466. };