shenpi_audit.js 16 KB

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