financial_pay_tender_audit.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const shenpiConst = require('../const/shenpi');
  7. const auditType = require('../const/audit').auditType;
  8. module.exports = app => {
  9. class FinancialPayTenderAudit extends BaseService {
  10. /**
  11. * 构造函数
  12. *
  13. * @param {Object} ctx - egg全局变量
  14. * @return {void}
  15. */
  16. constructor(ctx) {
  17. super(ctx);
  18. this.tableName = 'financial_pay_tender_audit';
  19. this.dataId = 'id';
  20. }
  21. async getList(spid, tid, accountList = null) {
  22. if (accountList === null) {
  23. accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  24. where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
  25. columns: ['id', 'account', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  26. });
  27. }
  28. const list = await this.db.select(this.tableName, { where: { spid, tid }, orders: [['id', 'asc']] });
  29. const removeList = [];
  30. for (const l of list) {
  31. const accountInfo = this._.find(accountList, { id: l.uid });
  32. if (!accountInfo) {
  33. removeList.push(l.id);
  34. continue;
  35. }
  36. l.name = accountInfo.name;
  37. l.company = accountInfo.company;
  38. }
  39. if (removeList.length > 0) {
  40. for (const r of removeList) {
  41. list.splice(this._.findIndex(list, { id: r }), 1);
  42. }
  43. }
  44. return list;
  45. }
  46. async saveAudits(spid, tid, accountList) {
  47. const transaction = await this.db.beginTransaction();
  48. try {
  49. // 判断是否已存在该用户,存在则不插入
  50. const pauditList = await this.getAllDataByCondition({ where: { spid, tid } });
  51. const financialAudits = await this.ctx.service.financialAudit.getAllDataByCondition({ where: { spid } });
  52. const pushData = [];
  53. const pushFinancialData = [];
  54. for (const a of this._.uniqBy(accountList, 'id')) {
  55. if (this._.findIndex(pauditList, { uid: a.id }) === -1) {
  56. const data = {
  57. spid,
  58. tid,
  59. uid: a.id,
  60. create_time: new Date(),
  61. };
  62. pushData.push(data);
  63. }
  64. if (this._.findIndex(financialAudits, { uid: a.id }) === -1) {
  65. pushFinancialData.push({
  66. spid,
  67. uid: a.id,
  68. create_time: new Date(),
  69. });
  70. }
  71. }
  72. if (pushData.length > 0) {
  73. await transaction.insert(this.tableName, pushData);
  74. }
  75. if (pushFinancialData.length > 0) {
  76. await transaction.insert(this.ctx.service.financialAudit.tableName, pushFinancialData);
  77. }
  78. transaction.commit();
  79. } catch (error) {
  80. console.log(error);
  81. transaction.rollback();
  82. }
  83. return true;
  84. }
  85. async delAudits(spid, tid, datas) {
  86. // return await this.db.delete(this.tableName, { id });
  87. // 需要同时移除审批流程里的人
  88. const reponseData = {};
  89. const transaction = await this.db.beginTransaction();
  90. try {
  91. await transaction.delete(this.tableName, { id: this._.map(datas, 'id') });
  92. const shenpiAudits = await this.ctx.service.shenpiAudit.getAuditList(tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
  93. const uids = this._.map(datas, 'uid');
  94. const removeData = [];
  95. for (const sp of shenpiAudits) {
  96. if (this._.includes(uids, sp.audit_id)) {
  97. removeData.push(sp.id);
  98. }
  99. }
  100. if (removeData.length > 0) {
  101. await transaction.delete(this.ctx.service.shenpiAudit.tableName, { id: removeData });
  102. // 重新分配order值
  103. const newSpAudits = await transaction.select(this.ctx.service.shenpiAudit.tableName, { where: { tid, sp_type: shenpiConst.sp_other_type.financial, sp_status: shenpiConst.sp_status.gdspl }, orders: [['audit_order', 'asc'], ['id', 'asc']] });
  104. // groupby audit_order
  105. const newSpAuditsGroup = this._.groupBy(newSpAudits, 'audit_order');
  106. const updateData = [];
  107. let order = 1;
  108. for (const a in newSpAuditsGroup) {
  109. const sameOrderAudits = newSpAuditsGroup[a];
  110. for (const sa of sameOrderAudits) {
  111. if (sa.audit_order !== order) {
  112. updateData.push({
  113. id: sa.id,
  114. audit_order: order,
  115. });
  116. }
  117. }
  118. order++;
  119. }
  120. if (updateData.length > 0) {
  121. await transaction.updateRows(this.ctx.service.shenpiAudit.tableName, updateData);
  122. }
  123. }
  124. await transaction.commit();
  125. reponseData.permissionList = await this.getList(spid, tid);
  126. reponseData.auditGroupList = await this.ctx.service.shenpiAudit.getAuditGroupList(tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
  127. } catch (error) {
  128. console.log(error);
  129. await transaction.rollback();
  130. }
  131. return reponseData;
  132. }
  133. async updatePermission(updateData) {
  134. if (!updateData.id) {
  135. return false;
  136. }
  137. return await this.db.update(this.tableName, updateData);
  138. }
  139. async addShenpiAudit(spid, data, tid) {
  140. const reponseData = {};
  141. const info = await this.ctx.service.shenpiAudit.addAudit(data, tid);
  142. reponseData.shenpi = info;
  143. if (info) {
  144. const transaction = await this.db.beginTransaction();
  145. try {
  146. // 判断是否存在并加入到成员表中
  147. const fptAudit = await this.getDataByCondition({ spid, tid, uid: data.audit_id });
  148. if (!fptAudit) {
  149. await transaction.insert(this.tableName, { spid, tid, uid: data.audit_id, create_time: new Date() });
  150. }
  151. // 判断是否存在并加入到成员表中
  152. const fAudit = await this.ctx.service.financialAudit.getDataByCondition({ spid, uid: data.audit_id });
  153. if (!fAudit) {
  154. await transaction.insert(this.ctx.service.financialAudit.tableName, { spid, uid: data.audit_id, create_time: new Date() });
  155. }
  156. await transaction.commit();
  157. reponseData.permissionList = await this.getList(spid, tid);
  158. } catch (error) {
  159. console.log(error);
  160. await transaction.rollback();
  161. }
  162. }
  163. return reponseData;
  164. }
  165. async copyShenpi2otherTender(spid, data, tid) {
  166. const reponseData = {};
  167. const info = await this.ctx.service.shenpiAudit.copyAudit2otherTender(data, tid);
  168. if (info) {
  169. const transaction = await this.db.beginTransaction();
  170. try {
  171. // const permissionList = await this.getAllDataByCondition({ where: { spid, tid } });
  172. const uidList = this._.map(data.auditList, 'audit_id');
  173. const tidList = data.tidList.split(',');
  174. if (tidList.length === 0) {
  175. throw '没有选择要复制的标段';
  176. }
  177. if (uidList.length > 0) {
  178. const pushData = [];
  179. const times = new Date();
  180. for (const t of tidList) {
  181. const tenderAudits = await this.getAllDataByCondition({ where: { spid, tid: t } });
  182. const diffList = this._.difference(uidList, this._.map(tenderAudits, 'uid'));
  183. for (const d of diffList) {
  184. pushData.push({
  185. spid, tid: t, uid: d,
  186. create_time: times,
  187. });
  188. }
  189. }
  190. if (pushData.length > 0) await transaction.insert(this.tableName, pushData);
  191. }
  192. await transaction.commit();
  193. reponseData.otherPermissionList = await this.getList(spid, tidList);
  194. } catch (error) {
  195. console.log(error);
  196. await transaction.rollback();
  197. }
  198. }
  199. return reponseData;
  200. }
  201. async copyAudit2otherTender(spid, data, tid) {
  202. const reponseData = {};
  203. const transaction = await this.db.beginTransaction();
  204. try {
  205. const permissionList = await this.getAllDataByCondition({ where: { spid, tid } });
  206. const uidList = this._.map(permissionList, 'uid');
  207. const tidList = data.tidList.split(',');
  208. if (tidList.length === 0) {
  209. throw '没有选择要复制的标段';
  210. }
  211. if (uidList.length > 0) {
  212. const pushData = [];
  213. const updateData = [];
  214. const times = new Date();
  215. for (const t of tidList) {
  216. const tenderAudits = await this.getAllDataByCondition({ where: { spid, tid: t } });
  217. const diffList = this._.difference(uidList, this._.map(tenderAudits, 'uid'));
  218. for (const d of diffList) {
  219. const p = permissionList.find(item => item.uid === d);
  220. pushData.push({ spid, tid: t, uid: p.uid, is_report: p.is_report, create_time: times });
  221. }
  222. const sameList = this._.intersection(this._.map(tenderAudits, 'uid'), uidList);
  223. for (const s of sameList) {
  224. const p = permissionList.find(item => item.uid === s);
  225. const t = tenderAudits.find(item => item.uid === s);
  226. if (p.is_report !== t.is_report) {
  227. updateData.push({ id: t.id, is_report: p.is_report });
  228. }
  229. }
  230. }
  231. if (pushData.length > 0) await transaction.insert(this.tableName, pushData);
  232. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  233. }
  234. await transaction.commit();
  235. reponseData.otherPermissionList = await this.getList(spid, tidList);
  236. } catch (error) {
  237. console.log(error);
  238. await transaction.rollback();
  239. }
  240. return reponseData;
  241. }
  242. }
  243. return FinancialPayTenderAudit;
  244. };