shenpi_audit.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 updateData = [];
  97. for (const aa of allAudit) {
  98. if (aa.audit_order > audit.audit_order) updateData.push({ id: aa.id, audit_order: aa.audit_order - 1});
  99. }
  100. const transaction = await this.db.beginTransaction();
  101. try {
  102. await transaction.delete(this.tableName, delData);
  103. if (updateData.length > 0) await transaction.update(updateData);
  104. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  105. const options = {
  106. where: {
  107. tid: this.ctx.tender.id,
  108. user_id: data.audit_id,
  109. },
  110. };
  111. const updateData = {
  112. status: 0,
  113. };
  114. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  115. }
  116. await transaction.commit();
  117. return true;
  118. } catch (err) {
  119. await transaction.rollback();
  120. throw err;
  121. }
  122. }
  123. async copyAudit2otherTender(data) {
  124. const transaction = await this.db.beginTransaction();
  125. try {
  126. const shenpi_status = parseInt(data.status);
  127. // 1.复制修改当前审批到其他的tender_info里
  128. // 2.删除其他的shenpiAudit
  129. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  130. const tenderInfoUpdateList = [];
  131. const tenders = [];
  132. for (const tid of data.tidList.split(',')) {
  133. // 获取原报
  134. const tender = await this.ctx.service.tender.getDataById(tid);
  135. if (tender) {
  136. tenders.push({ id: parseInt(tid), user_id: tender.user_id });
  137. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tid);
  138. // 把当前期状态复制到其他标段里
  139. if (shenpiInfo[data.code] !== shenpi_status) {
  140. shenpiInfo[data.code] = shenpi_status;
  141. tenderInfoUpdateList.push({ row: { shenpi: JSON.stringify(shenpiInfo) }, where: { tid: parseInt(tid) } });
  142. }
  143. }
  144. }
  145. if (tenderInfoUpdateList.length > 0) await transaction.updateRows(this.ctx.service.tenderInfo.tableName, tenderInfoUpdateList);
  146. const insertList = [];
  147. const needYB = ['ledger', 'revise', 'change'];
  148. const canYB = needYB.indexOf(data.code) !== -1;
  149. for (const t of tenders) {
  150. if (shenpi_status !== shenpiConst.sp_status.sqspr) {
  151. await transaction.delete(this.tableName, { tid: t.id, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status });
  152. const sourceList = data.auditList.filter(x => { return x.audit_id && (x.audit_id !== t.user_id || canYB) });
  153. sourceList.sort((a, b) => { return a.audit_order - b.audit_order; });
  154. let audit_order = 0, curAuditOrder = 0;
  155. for (const s of sourceList) {
  156. if (s.audit_order !== curAuditOrder) {
  157. curAuditOrder = s.audit_order;
  158. audit_order = audit_order + 1;
  159. }
  160. insertList.push({
  161. tid: t.id,
  162. sp_type: shenpiConst.sp_type[data.code],
  163. sp_status: shenpi_status,
  164. audit_id: s.audit_id,
  165. audit_type: s.audit_type,
  166. audit_order: audit_order,
  167. });
  168. }
  169. }
  170. }
  171. // console.log(tenderInfoUpdateList, insertList);
  172. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  173. await transaction.commit();
  174. return true;
  175. } catch (err) {
  176. await transaction.rollback();
  177. throw err;
  178. }
  179. }
  180. async copyAudit2otherShenpi(data) {
  181. const transaction = await this.db.beginTransaction();
  182. try {
  183. const shenpi_status = parseInt(data.status);
  184. // 1.修改当前审批到它的tender_info里
  185. // 2.删除相同审批状态的shenpiAudit
  186. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  187. const insertList = [];
  188. const needYB = ['ledger', 'revise', 'change'];
  189. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(this.ctx.tender.id);
  190. for (const code of data.shenpiList.split(',')) {
  191. // 把当前审批状态复制到其他标段里
  192. if (shenpiInfo[code] !== shenpi_status) {
  193. shenpiInfo[code] = shenpi_status;
  194. }
  195. if (shenpiInfo[code] !== shenpiConst.sp_status.sqspr) {
  196. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code], sp_status: shenpiInfo[code] });
  197. for (const aid of data.aidList.split(',')) {
  198. if (aid && parseInt(aid) !== this.ctx.tender.data.user_id || (parseInt(aid) === this.ctx.tender.data.user_id && needYB.indexOf(code) !== -1)) {
  199. const insertData = {
  200. tid: this.ctx.tender.id,
  201. sp_type: shenpiConst.sp_type[code],
  202. sp_status: shenpi_status,
  203. audit_id: parseInt(aid),
  204. };
  205. insertList.push(insertData);
  206. }
  207. }
  208. }
  209. }
  210. await transaction.update(this.ctx.service.tenderInfo.tableName,
  211. {
  212. shenpi: JSON.stringify(shenpiInfo),
  213. },
  214. {
  215. where: {
  216. tid: this.ctx.tender.id,
  217. },
  218. });
  219. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  220. await transaction.commit();
  221. return true;
  222. } catch (err) {
  223. await transaction.rollback();
  224. throw err;
  225. }
  226. }
  227. async setAuditType(data) {
  228. const conn = await this.db.beginTransaction();
  229. try {
  230. const updateData = { audit_type: data.audit_type };
  231. const condition = {
  232. tid: this.ctx.tender.id,
  233. sp_type: data.code,
  234. sp_status: data.status,
  235. audit_id: data.audit_id,
  236. };
  237. await conn.update(this.tableName, updateData, { where: condition });
  238. await conn.commit();
  239. } catch (err) {
  240. await conn.rollback();
  241. throw err;
  242. }
  243. }
  244. // 更新审批流程
  245. async updateAuditList(transaction, tenderId, sp_status, sp_type, ids) {
  246. if (sp_status === shenpiConst.sp_status.gdspl) {
  247. const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
  248. const oldIds = this._.map(auditList, 'audit_id');
  249. if (this._.isEqual(ids, oldIds)) {
  250. return;
  251. }
  252. // 更新固定审批流
  253. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
  254. const insertDatas = [];
  255. for (const [i, id] of ids.entries()) {
  256. insertDatas.push({
  257. tid: tenderId,
  258. sp_type,
  259. sp_status,
  260. audit_id: id,
  261. audit_order: i + 1,
  262. });
  263. }
  264. await transaction.insert(this.tableName, insertDatas);
  265. if (sp_type === shenpiConst.sp_type.stage) {
  266. // 判断哪些audit_id不存在了,哪些audit_为新增
  267. const exist = this._.difference(ids, oldIds);// 判断新增的
  268. const unExist = this._.difference(oldIds, ids);// 判断已删除的
  269. console.log(ids, oldIds, exist, unExist);
  270. if (exist.length > 0) {
  271. const options = {
  272. where: {
  273. tid: this.ctx.tender.id,
  274. user_id: exist,
  275. },
  276. };
  277. const updateData = {
  278. status: 1,
  279. };
  280. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  281. }
  282. if (unExist.length > 0) {
  283. const options2 = {
  284. where: {
  285. tid: this.ctx.tender.id,
  286. user_id: unExist,
  287. },
  288. };
  289. const updateData2 = {
  290. status: 0,
  291. };
  292. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData2, options2);
  293. }
  294. }
  295. } else if (sp_status === shenpiConst.sp_status.gdzs) {
  296. const audit = await this.getAudit(tenderId, sp_type, sp_status);
  297. if (audit && audit.audit_id !== ids[ids.length - 1]) {
  298. // 更换终审
  299. await transaction.update(this.tableName, { audit_id: ids[ids.length - 1] }, { where: { tid: tenderId, sp_type, sp_status } });
  300. } else if (!audit) {
  301. await transaction.insert(this.tableName, { tid: tenderId, sp_type, sp_status, audit_id: ids[ids.length - 1] });
  302. }
  303. }
  304. }
  305. }
  306. return ShenpiAudit;
  307. };