shenpi_audit.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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, 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, 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.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 addAudit(data) {
  35. const transaction = await this.db.beginTransaction();
  36. try {
  37. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  38. const options = {
  39. where: {
  40. tid: this.ctx.tender.id,
  41. user_id: data.audit_id,
  42. },
  43. };
  44. const updateData = {
  45. status: 1,
  46. };
  47. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  48. }
  49. const insertData = {
  50. tid: this.ctx.tender.id,
  51. sp_type: data.code,
  52. sp_status: data.status,
  53. audit_id: data.audit_id,
  54. };
  55. const result = await transaction.insert(this.tableName, insertData);
  56. await transaction.commit();
  57. return result.effectRows === 1;
  58. } catch (err) {
  59. await transaction.rollback();
  60. throw err;
  61. }
  62. }
  63. async removeAudit(data) {
  64. const transaction = await this.db.beginTransaction();
  65. try {
  66. const delData = {
  67. tid: this.ctx.tender.id,
  68. sp_type: data.code,
  69. sp_status: data.status,
  70. audit_id: data.audit_id,
  71. };
  72. await transaction.delete(this.tableName, delData);
  73. if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {
  74. const options = {
  75. where: {
  76. tid: this.ctx.tender.id,
  77. user_id: data.audit_id,
  78. },
  79. };
  80. const updateData = {
  81. status: 0,
  82. };
  83. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  84. }
  85. await transaction.commit();
  86. return true;
  87. } catch (err) {
  88. await transaction.rollback();
  89. throw err;
  90. }
  91. }
  92. async copyAudit2otherTender(data) {
  93. const transaction = await this.db.beginTransaction();
  94. try {
  95. const shenpi_status = parseInt(data.status);
  96. // 1.复制修改当前审批到其他的tender_info里
  97. // 2.删除其他的shenpiAudit
  98. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  99. const tenderInfoUpdateList = [];
  100. const tenders = [];
  101. for (const tid of data.tidList.split(',')) {
  102. // 获取原报
  103. const tender = await this.ctx.service.tender.getDataById(tid);
  104. if (tender) {
  105. tenders.push({ id: parseInt(tid), user_id: tender.user_id });
  106. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tid);
  107. // 把当前期状态复制到其他标段里
  108. if (shenpiInfo[data.code] !== shenpi_status) {
  109. shenpiInfo[data.code] = shenpi_status;
  110. tenderInfoUpdateList.push({ row: { shenpi: JSON.stringify(shenpiInfo) }, where: { tid: parseInt(tid) } });
  111. }
  112. }
  113. }
  114. if (tenderInfoUpdateList.length > 0) await transaction.updateRows(this.ctx.service.tenderInfo.tableName, tenderInfoUpdateList);
  115. const insertList = [];
  116. const needYB = ['ledger', 'revise', 'change'];
  117. const canYB = needYB.indexOf(data.code) !== -1;
  118. for (const t of tenders) {
  119. if (shenpi_status !== shenpiConst.sp_status.sqspr) {
  120. await transaction.delete(this.tableName, { tid: t.id, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status });
  121. for (const aid of data.aidList.split(',')) {
  122. if (aid && parseInt(aid) !== t.user_id || (parseInt(aid) === t.user_id && canYB)) {
  123. const insertData = {
  124. tid: t.id,
  125. sp_type: shenpiConst.sp_type[data.code],
  126. sp_status: shenpi_status,
  127. audit_id: parseInt(aid),
  128. };
  129. insertList.push(insertData);
  130. }
  131. }
  132. }
  133. }
  134. // console.log(tenderInfoUpdateList, insertList);
  135. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  136. await transaction.commit();
  137. return true;
  138. } catch (err) {
  139. await transaction.rollback();
  140. throw err;
  141. }
  142. }
  143. async copyAudit2otherShenpi(data) {
  144. const transaction = await this.db.beginTransaction();
  145. try {
  146. const shenpi_status = parseInt(data.status);
  147. // 1.修改当前审批到它的tender_info里
  148. // 2.删除相同审批状态的shenpiAudit
  149. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  150. const insertList = [];
  151. const needYB = ['ledger', 'revise', 'change'];
  152. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(this.ctx.tender.id);
  153. for (const code of data.shenpiList.split(',')) {
  154. // 把当前审批状态复制到其他标段里
  155. if (shenpiInfo[code] !== shenpi_status) {
  156. shenpiInfo[code] = shenpi_status;
  157. }
  158. if (shenpiInfo[code] !== shenpiConst.sp_status.sqspr) {
  159. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code], sp_status: shenpiInfo[code] });
  160. for (const aid of data.aidList.split(',')) {
  161. if (aid && parseInt(aid) !== this.ctx.tender.data.user_id || (parseInt(aid) === this.ctx.tender.data.user_id && needYB.indexOf(code) !== -1)) {
  162. const insertData = {
  163. tid: this.ctx.tender.id,
  164. sp_type: shenpiConst.sp_type[code],
  165. sp_status: shenpi_status,
  166. audit_id: parseInt(aid),
  167. };
  168. insertList.push(insertData);
  169. }
  170. }
  171. }
  172. }
  173. await transaction.update(this.ctx.service.tenderInfo.tableName,
  174. {
  175. shenpi: JSON.stringify(shenpiInfo),
  176. },
  177. {
  178. where: {
  179. tid: this.ctx.tender.id,
  180. },
  181. });
  182. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  183. await transaction.commit();
  184. return true;
  185. } catch (err) {
  186. await transaction.rollback();
  187. throw err;
  188. }
  189. }
  190. // 更新审批流程
  191. async updateAuditList(transaction, tenderId, sp_status, sp_type, ids) {
  192. if (sp_status === shenpiConst.sp_status.gdspl) {
  193. const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
  194. const oldIds = this._.map(auditList, 'audit_id');
  195. if (this._.isEqual(ids, oldIds)) {
  196. return;
  197. }
  198. // 更新固定审批流
  199. await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
  200. const insertDatas = [];
  201. for (const id of ids) {
  202. insertDatas.push({
  203. tid: tenderId,
  204. sp_type,
  205. sp_status,
  206. audit_id: id,
  207. });
  208. }
  209. await transaction.insert(this.tableName, insertDatas);
  210. if (sp_type === shenpiConst.sp_type.stage) {
  211. // 判断哪些audit_id不存在了,哪些audit_为新增
  212. const exist = this._.difference(ids, oldIds);// 判断新增的
  213. const unExist = this._.difference(oldIds, ids);// 判断已删除的
  214. console.log(ids, oldIds, exist, unExist);
  215. if (exist.length > 0) {
  216. const options = {
  217. where: {
  218. tid: this.ctx.tender.id,
  219. user_id: exist,
  220. },
  221. };
  222. const updateData = {
  223. status: 1,
  224. };
  225. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
  226. }
  227. if (unExist.length > 0) {
  228. const options2 = {
  229. where: {
  230. tid: this.ctx.tender.id,
  231. user_id: unExist,
  232. },
  233. };
  234. const updateData2 = {
  235. status: 0,
  236. };
  237. await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData2, options2);
  238. }
  239. }
  240. } else if (sp_status === shenpiConst.sp_status.gdzs) {
  241. const audit = await this.getAudit(tenderId, sp_type, sp_status);
  242. if (audit && audit.audit_id !== ids[ids.length - 1]) {
  243. // 更换终审
  244. await transaction.update(this.tableName, { audit_id: ids[ids.length - 1] }, { where: { tid: tenderId, sp_type, sp_status } });
  245. } else if (!audit) {
  246. await transaction.insert(this.tableName, { tid: tenderId, sp_type, sp_status, audit_id: ids[ids.length - 1] });
  247. }
  248. }
  249. }
  250. }
  251. return ShenpiAudit;
  252. };