shenpi_audit.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.auditGroupList = await this.getAuditGroupList(tid, sp.type, sp.status);
  27. } else if (sp.status === shenpiConst.sp_status.gdzs) {
  28. sp.audit = await this.getAudit(tid, sp.type, sp.status);
  29. }
  30. }
  31. return shenpiConst;
  32. }
  33. async getAudit(tid, type, status) {
  34. const sql = 'SELECT sp.audit_id, sp.audit_type, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  35. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ?';
  36. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  37. return await this.db.queryOne(sql, sqlParam);
  38. }
  39. async getAuditList(tid, type, status) {
  40. const sql = 'SELECT sp.audit_id, sp.audit_type, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  41. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? ORDER BY sp.audit_order ASC, sp.id ASC';
  42. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  43. return await this.db.query(sql, sqlParam);
  44. }
  45. async getAuditGroupList(tid, type, status) {
  46. const sql = 'SELECT sp.audit_id, sp.audit_type, sp.audit_order, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  47. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? ORDER BY sp.audit_order ASC, sp.id ASC';
  48. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  49. const audits = await this.db.query(sql, sqlParam);
  50. const result = [];
  51. for (const a of audits) {
  52. if (a.audit_order > 0) {
  53. if (result[a.audit_order - 1]) {
  54. result[a.audit_order - 1].push(a);
  55. } else {
  56. result.push([a]);
  57. }
  58. } else {
  59. result.push([a]);
  60. }
  61. }
  62. return result;
  63. }
  64. async addAudit(data) {
  65. let result;
  66. const transaction = await this.db.beginTransaction();
  67. try {
  68. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  69. const options = {
  70. where: {
  71. tid: this.ctx.tender.id,
  72. user_id: data.audit_id,
  73. },
  74. };
  75. const updateData = {
  76. status: 1,
  77. };
  78. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  79. }
  80. const insertData = {
  81. tid: this.ctx.tender.id,
  82. sp_type: data.code,
  83. sp_status: data.status,
  84. audit_id: data.audit_id,
  85. };
  86. if (data.audit_type) insertData.audit_type = data.audit_type;
  87. if (data.audit_order) insertData.audit_order = data.audit_order;
  88. result = await transaction.insert(this.tableName, insertData);
  89. await transaction.commit();
  90. } catch (err) {
  91. await transaction.rollback();
  92. throw err;
  93. }
  94. if (result.affectedRows !== 1) throw '添加审批人失败';
  95. data.id = result.insertId;
  96. return data;
  97. }
  98. async removeAudit(data) {
  99. const delData = {
  100. tid: this.ctx.tender.id,
  101. sp_type: data.code,
  102. sp_status: data.status,
  103. audit_id: data.audit_id,
  104. };
  105. const audit = await this.getDataByCondition(delData);
  106. const allAudit = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, sp_type: data.code, sp_status: data.status } });
  107. const sameOrder = allAudit.filter(x => { return x.audit_order === audit.audit_order });
  108. const updateData = [];
  109. if (sameOrder.length === 1) {
  110. for (const aa of allAudit) {
  111. if (aa.audit_order > audit.audit_order) updateData.push({ id: aa.id, audit_order: aa.audit_order - 1});
  112. }
  113. }
  114. const transaction = await this.db.beginTransaction();
  115. try {
  116. await transaction.delete(this.tableName, delData);
  117. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  118. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  119. const options = {
  120. where: {
  121. tid: this.ctx.tender.id,
  122. user_id: data.audit_id,
  123. },
  124. };
  125. const updateData = {
  126. status: 0,
  127. };
  128. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  129. }
  130. await transaction.commit();
  131. return true;
  132. } catch (err) {
  133. await transaction.rollback();
  134. throw err;
  135. }
  136. }
  137. async copyAudit2otherTender(data) {
  138. const transaction = await this.db.beginTransaction();
  139. try {
  140. const shenpi_status = parseInt(data.status);
  141. // 1.复制修改当前审批到其他的tender_info里
  142. // 2.删除其他的shenpiAudit
  143. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  144. const tenderInfoUpdateList = [];
  145. const tenders = [];
  146. for (const tid of data.tidList.split(',')) {
  147. // 获取原报
  148. const tender = await this.ctx.service.tender.getDataById(tid);
  149. if (tender) {
  150. tenders.push({ id: parseInt(tid), user_id: tender.user_id });
  151. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tid);
  152. // 把当前期状态复制到其他标段里
  153. if (shenpiInfo[data.code] !== shenpi_status) {
  154. shenpiInfo[data.code] = shenpi_status;
  155. tenderInfoUpdateList.push({ row: { shenpi: JSON.stringify(shenpiInfo) }, where: { tid: parseInt(tid) } });
  156. }
  157. }
  158. }
  159. if (tenderInfoUpdateList.length > 0) await transaction.updateRows(this.ctx.service.tenderInfo.tableName, tenderInfoUpdateList);
  160. const insertList = [];
  161. const needYB = ['ledger', 'revise', 'change'];
  162. const canYB = needYB.indexOf(data.code) !== -1;
  163. for (const t of tenders) {
  164. if (shenpi_status !== shenpiConst.sp_status.sqspr) {
  165. await transaction.delete(this.tableName, { tid: t.id, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status });
  166. const sourceList = data.auditList.filter(x => { return x.audit_id && (x.audit_id !== t.user_id || canYB) });
  167. sourceList.sort((a, b) => { return a.audit_order - b.audit_order; });
  168. let audit_order = 0, curAuditOrder = 0;
  169. for (const s of sourceList) {
  170. if (s.audit_order !== curAuditOrder) {
  171. curAuditOrder = s.audit_order;
  172. audit_order = audit_order + 1;
  173. }
  174. insertList.push({
  175. tid: t.id,
  176. sp_type: shenpiConst.sp_type[data.code],
  177. sp_status: shenpi_status,
  178. audit_id: s.audit_id,
  179. audit_type: s.audit_type,
  180. audit_order: audit_order,
  181. });
  182. }
  183. }
  184. }
  185. // console.log(tenderInfoUpdateList, insertList);
  186. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  187. await transaction.commit();
  188. return true;
  189. } catch (err) {
  190. await transaction.rollback();
  191. throw err;
  192. }
  193. }
  194. async copyAudit2otherShenpi(data) {
  195. const transaction = await this.db.beginTransaction();
  196. try {
  197. const shenpi_status = parseInt(data.status);
  198. // 1.修改当前审批到它的tender_info里
  199. // 2.删除相同审批状态的shenpiAudit
  200. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  201. const insertList = [];
  202. const needYB = ['ledger', 'revise', 'change'];
  203. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(this.ctx.tender.id);
  204. for (const code of data.shenpiList.split(',')) {
  205. // 把当前审批状态复制到其他标段里
  206. if (shenpiInfo[code] !== shenpi_status) {
  207. shenpiInfo[code] = shenpi_status;
  208. }
  209. if (shenpiInfo[code] !== shenpiConst.sp_status.sqspr) {
  210. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code], sp_status: shenpiInfo[code] });
  211. for (const aid of data.aidList.split(',')) {
  212. if (aid && parseInt(aid) !== this.ctx.tender.data.user_id || (parseInt(aid) === this.ctx.tender.data.user_id && needYB.indexOf(code) !== -1)) {
  213. const insertData = {
  214. tid: this.ctx.tender.id,
  215. sp_type: shenpiConst.sp_type[code],
  216. sp_status: shenpi_status,
  217. audit_id: parseInt(aid),
  218. audit_order: insertList.length + 1,
  219. };
  220. insertList.push(insertData);
  221. }
  222. }
  223. }
  224. }
  225. await transaction.update(this.ctx.service.tenderInfo.tableName,
  226. {
  227. shenpi: JSON.stringify(shenpiInfo),
  228. },
  229. {
  230. where: {
  231. tid: this.ctx.tender.id,
  232. },
  233. });
  234. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  235. await transaction.commit();
  236. return true;
  237. } catch (err) {
  238. await transaction.rollback();
  239. throw err;
  240. }
  241. }
  242. async setAuditType(data) {
  243. const conn = await this.db.beginTransaction();
  244. try {
  245. const updateData = { audit_type: data.audit_type };
  246. const condition = {
  247. tid: this.ctx.tender.id,
  248. sp_type: data.code,
  249. sp_status: data.status,
  250. audit_id: data.audit_id,
  251. };
  252. await conn.update(this.tableName, updateData, { where: condition });
  253. await conn.commit();
  254. } catch (err) {
  255. await conn.rollback();
  256. throw err;
  257. }
  258. }
  259. // 更新审批流程
  260. async updateAuditList(transaction, tenderId, sp_status, sp_type, ids) {
  261. if (sp_status === shenpiConst.sp_status.gdspl) {
  262. const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
  263. const oldIds = this._.map(auditList, 'audit_id');
  264. if (this._.isEqual(ids, oldIds)) {
  265. return;
  266. }
  267. // 更新固定审批流
  268. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
  269. const insertDatas = [];
  270. for (const [i, id] of ids.entries()) {
  271. insertDatas.push({
  272. tid: tenderId,
  273. sp_type,
  274. sp_status,
  275. audit_id: id,
  276. audit_order: i + 1,
  277. });
  278. }
  279. await transaction.insert(this.tableName, insertDatas);
  280. if (sp_type === shenpiConst.sp_type.stage) {
  281. // 判断哪些audit_id不存在了,哪些audit_为新增
  282. const exist = this._.difference(ids, oldIds);// 判断新增的
  283. const unExist = this._.difference(oldIds, ids);// 判断已删除的
  284. console.log(ids, oldIds, exist, unExist);
  285. if (exist.length > 0) {
  286. const options = {
  287. where: {
  288. tid: this.ctx.tender.id,
  289. user_id: exist,
  290. },
  291. };
  292. const updateData = {
  293. status: 1,
  294. };
  295. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  296. }
  297. if (unExist.length > 0) {
  298. const options2 = {
  299. where: {
  300. tid: this.ctx.tender.id,
  301. user_id: unExist,
  302. },
  303. };
  304. const updateData2 = {
  305. status: 0,
  306. };
  307. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData2, options2);
  308. }
  309. }
  310. } else if (sp_status === shenpiConst.sp_status.gdzs) {
  311. const audit = await this.getAudit(tenderId, sp_type, sp_status);
  312. if (audit && audit.audit_id !== ids[ids.length - 1]) {
  313. // 更换终审
  314. await transaction.update(this.tableName, { audit_id: ids[ids.length - 1] }, { where: { tid: tenderId, sp_type, sp_status } });
  315. } else if (!audit) {
  316. await transaction.insert(this.tableName, { tid: tenderId, sp_type, sp_status, audit_id: ids[ids.length - 1] });
  317. }
  318. }
  319. }
  320. async updateAuditListWithAuditType(transaction, tenderId, sp_status, sp_type, auditGroup) {
  321. const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
  322. const newAuditList = [];
  323. for (const group of auditGroup) {
  324. newAuditList.push(...group);
  325. }
  326. let sameAudit = auditList.length === newAuditList.length;
  327. if (sameAudit) {
  328. for (const audit of auditList) {
  329. const newAudit = newAuditList.find(x => { return x.audit_id === audit.aid; });
  330. if (!newAudit || newAudit.audit_order !== audit.audit_order || newAudit.audit_type !== audit.audit_type) {
  331. sameAudit = false;
  332. break;
  333. }
  334. }
  335. }
  336. if (sameAudit) return;
  337. // 更新固定审批流
  338. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
  339. const insertDatas = [];
  340. for (const a of newAuditList) {
  341. insertDatas.push({
  342. tid: tenderId,
  343. sp_type,
  344. sp_status,
  345. audit_id: a.aid,
  346. audit_order: a.audit_order,
  347. audit_type: a.audit_type
  348. });
  349. }
  350. await transaction.insert(this.tableName, insertDatas);
  351. }
  352. async getRemoveTenders(tenders) {
  353. const removeTenders = [];
  354. for (const tender of tenders) {
  355. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tender.id);
  356. if (!shenpiInfo) {
  357. removeTenders.push(tender.id);
  358. } else {
  359. tender.shenpiInfo = shenpiInfo;
  360. // 获取所有的固定审批流或固定终审
  361. const shenpiauditList = {};
  362. for (const shenpi in tender.shenpiInfo) {
  363. if (tender.shenpiInfo[shenpi] === shenpiConst.sp_status.gdspl) {
  364. const shenpiList = await this.getAllDataByCondition({ where: { tid: tender.id, sp_type: shenpiConst.sp_type[shenpi], sp_status: tender.shenpiInfo[shenpi] } });
  365. const shenpiIdList = this._.map(shenpiList, 'audit_id');
  366. shenpiauditList[shenpi] = shenpiIdList.length ? shenpiIdList : null;
  367. } else if (tender.shenpiInfo[shenpi] === shenpiConst.sp_status.gdzs) {
  368. const shenpiInfo = await this.getDataByCondition({ tid: tender.id, sp_type: shenpiConst.sp_type[shenpi], sp_status: tender.shenpiInfo[shenpi] });
  369. shenpiauditList[shenpi] = shenpiInfo && shenpiInfo.audit_id ? [shenpiInfo.audit_id] : null;
  370. }
  371. }
  372. tender.shenpiauditList = shenpiauditList;
  373. }
  374. }
  375. return removeTenders;
  376. }
  377. }
  378. return ShenpiAudit;
  379. };