shenpi_audit.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. };
  208. insertList.push(insertData);
  209. }
  210. }
  211. }
  212. }
  213. await transaction.update(this.ctx.service.tenderInfo.tableName,
  214. {
  215. shenpi: JSON.stringify(shenpiInfo),
  216. },
  217. {
  218. where: {
  219. tid: this.ctx.tender.id,
  220. },
  221. });
  222. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  223. await transaction.commit();
  224. return true;
  225. } catch (err) {
  226. await transaction.rollback();
  227. throw err;
  228. }
  229. }
  230. async setAuditType(data) {
  231. const conn = await this.db.beginTransaction();
  232. try {
  233. const updateData = { audit_type: data.audit_type };
  234. const condition = {
  235. tid: this.ctx.tender.id,
  236. sp_type: data.code,
  237. sp_status: data.status,
  238. audit_id: data.audit_id,
  239. };
  240. await conn.update(this.tableName, updateData, { where: condition });
  241. await conn.commit();
  242. } catch (err) {
  243. await conn.rollback();
  244. throw err;
  245. }
  246. }
  247. // 更新审批流程
  248. async updateAuditList(transaction, tenderId, sp_status, sp_type, ids) {
  249. if (sp_status === shenpiConst.sp_status.gdspl) {
  250. const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
  251. const oldIds = this._.map(auditList, 'audit_id');
  252. if (this._.isEqual(ids, oldIds)) {
  253. return;
  254. }
  255. // 更新固定审批流
  256. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
  257. const insertDatas = [];
  258. for (const [i, id] of ids.entries()) {
  259. insertDatas.push({
  260. tid: tenderId,
  261. sp_type,
  262. sp_status,
  263. audit_id: id,
  264. audit_order: i + 1,
  265. });
  266. }
  267. await transaction.insert(this.tableName, insertDatas);
  268. if (sp_type === shenpiConst.sp_type.stage) {
  269. // 判断哪些audit_id不存在了,哪些audit_为新增
  270. const exist = this._.difference(ids, oldIds);// 判断新增的
  271. const unExist = this._.difference(oldIds, ids);// 判断已删除的
  272. console.log(ids, oldIds, exist, unExist);
  273. if (exist.length > 0) {
  274. const options = {
  275. where: {
  276. tid: this.ctx.tender.id,
  277. user_id: exist,
  278. },
  279. };
  280. const updateData = {
  281. status: 1,
  282. };
  283. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  284. }
  285. if (unExist.length > 0) {
  286. const options2 = {
  287. where: {
  288. tid: this.ctx.tender.id,
  289. user_id: unExist,
  290. },
  291. };
  292. const updateData2 = {
  293. status: 0,
  294. };
  295. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData2, options2);
  296. }
  297. }
  298. } else if (sp_status === shenpiConst.sp_status.gdzs) {
  299. const audit = await this.getAudit(tenderId, sp_type, sp_status);
  300. if (audit && audit.audit_id !== ids[ids.length - 1]) {
  301. // 更换终审
  302. await transaction.update(this.tableName, { audit_id: ids[ids.length - 1] }, { where: { tid: tenderId, sp_type, sp_status } });
  303. } else if (!audit) {
  304. await transaction.insert(this.tableName, { tid: tenderId, sp_type, sp_status, audit_id: ids[ids.length - 1] });
  305. }
  306. }
  307. }
  308. }
  309. return ShenpiAudit;
  310. };