shenpi_audit.js 23 KB

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