shenpi_audit.js 24 KB

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