shenpi_audit.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. for (const aid of data.aidList.split(',')) {
  153. if (aid && parseInt(aid) !== t.user_id || (parseInt(aid) === t.user_id && canYB)) {
  154. const insertData = {
  155. tid: t.id,
  156. sp_type: shenpiConst.sp_type[data.code],
  157. sp_status: shenpi_status,
  158. audit_id: parseInt(aid),
  159. };
  160. insertList.push(insertData);
  161. }
  162. }
  163. }
  164. }
  165. // console.log(tenderInfoUpdateList, insertList);
  166. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  167. await transaction.commit();
  168. return true;
  169. } catch (err) {
  170. await transaction.rollback();
  171. throw err;
  172. }
  173. }
  174. async copyAudit2otherShenpi(data) {
  175. const transaction = await this.db.beginTransaction();
  176. try {
  177. const shenpi_status = parseInt(data.status);
  178. // 1.修改当前审批到它的tender_info里
  179. // 2.删除相同审批状态的shenpiAudit
  180. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  181. const insertList = [];
  182. const needYB = ['ledger', 'revise', 'change'];
  183. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(this.ctx.tender.id);
  184. for (const code of data.shenpiList.split(',')) {
  185. // 把当前审批状态复制到其他标段里
  186. if (shenpiInfo[code] !== shenpi_status) {
  187. shenpiInfo[code] = shenpi_status;
  188. }
  189. if (shenpiInfo[code] !== shenpiConst.sp_status.sqspr) {
  190. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code], sp_status: shenpiInfo[code] });
  191. for (const aid of data.aidList.split(',')) {
  192. if (aid && parseInt(aid) !== this.ctx.tender.data.user_id || (parseInt(aid) === this.ctx.tender.data.user_id && needYB.indexOf(code) !== -1)) {
  193. const insertData = {
  194. tid: this.ctx.tender.id,
  195. sp_type: shenpiConst.sp_type[code],
  196. sp_status: shenpi_status,
  197. audit_id: parseInt(aid),
  198. };
  199. insertList.push(insertData);
  200. }
  201. }
  202. }
  203. }
  204. await transaction.update(this.ctx.service.tenderInfo.tableName,
  205. {
  206. shenpi: JSON.stringify(shenpiInfo),
  207. },
  208. {
  209. where: {
  210. tid: this.ctx.tender.id,
  211. },
  212. });
  213. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  214. await transaction.commit();
  215. return true;
  216. } catch (err) {
  217. await transaction.rollback();
  218. throw err;
  219. }
  220. }
  221. async setAuditType(data) {
  222. const conn = await this.db.beginTransaction();
  223. try {
  224. const updateData = { audit_type: data.audit_type };
  225. const condition = {
  226. tid: this.ctx.tender.id,
  227. sp_type: data.code,
  228. sp_status: data.status,
  229. audit_id: data.audit_id,
  230. };
  231. await conn.update(this.tableName, updateData, { where: condition });
  232. await conn.commit();
  233. } catch (err) {
  234. await conn.rollback();
  235. throw err;
  236. }
  237. }
  238. // 更新审批流程
  239. async updateAuditList(transaction, tenderId, sp_status, sp_type, ids) {
  240. if (sp_status === shenpiConst.sp_status.gdspl) {
  241. const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
  242. const oldIds = this._.map(auditList, 'audit_id');
  243. if (this._.isEqual(ids, oldIds)) {
  244. return;
  245. }
  246. // 更新固定审批流
  247. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
  248. const insertDatas = [];
  249. for (const [i, id] of ids.entries()) {
  250. insertDatas.push({
  251. tid: tenderId,
  252. sp_type,
  253. sp_status,
  254. audit_id: id,
  255. audit_order: i + 1,
  256. });
  257. }
  258. await transaction.insert(this.tableName, insertDatas);
  259. if (sp_type === shenpiConst.sp_type.stage) {
  260. // 判断哪些audit_id不存在了,哪些audit_为新增
  261. const exist = this._.difference(ids, oldIds);// 判断新增的
  262. const unExist = this._.difference(oldIds, ids);// 判断已删除的
  263. console.log(ids, oldIds, exist, unExist);
  264. if (exist.length > 0) {
  265. const options = {
  266. where: {
  267. tid: this.ctx.tender.id,
  268. user_id: exist,
  269. },
  270. };
  271. const updateData = {
  272. status: 1,
  273. };
  274. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  275. }
  276. if (unExist.length > 0) {
  277. const options2 = {
  278. where: {
  279. tid: this.ctx.tender.id,
  280. user_id: unExist,
  281. },
  282. };
  283. const updateData2 = {
  284. status: 0,
  285. };
  286. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData2, options2);
  287. }
  288. }
  289. } else if (sp_status === shenpiConst.sp_status.gdzs) {
  290. const audit = await this.getAudit(tenderId, sp_type, sp_status);
  291. if (audit && audit.audit_id !== ids[ids.length - 1]) {
  292. // 更换终审
  293. await transaction.update(this.tableName, { audit_id: ids[ids.length - 1] }, { where: { tid: tenderId, sp_type, sp_status } });
  294. } else if (!audit) {
  295. await transaction.insert(this.tableName, { tid: tenderId, sp_type, sp_status, audit_id: ids[ids.length - 1] });
  296. }
  297. }
  298. }
  299. }
  300. return ShenpiAudit;
  301. };