financial_pay_tender_audit.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 getAllList(spid, tids, 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: tids }, 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 getList(spid, tid, accountList = null) {
  47. if (accountList === null) {
  48. accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  49. where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
  50. columns: ['id', 'account', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  51. });
  52. }
  53. const list = await this.db.select(this.tableName, { where: { spid, tid }, orders: [['id', 'asc']] });
  54. const removeList = [];
  55. for (const l of list) {
  56. const accountInfo = this._.find(accountList, { id: l.uid });
  57. if (!accountInfo) {
  58. removeList.push(l.id);
  59. continue;
  60. }
  61. l.name = accountInfo.name;
  62. l.company = accountInfo.company;
  63. }
  64. if (removeList.length > 0) {
  65. for (const r of removeList) {
  66. list.splice(this._.findIndex(list, { id: r }), 1);
  67. }
  68. }
  69. return list;
  70. }
  71. async saveAudits(spid, tid, accountList) {
  72. const transaction = await this.db.beginTransaction();
  73. try {
  74. // 判断是否已存在该用户,存在则不插入
  75. const pauditList = await this.getAllDataByCondition({ where: { spid, tid } });
  76. const financialAudits = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid } });
  77. const pushData = [];
  78. const pushSubProjPermissionData = [];
  79. const updateSubProjPermissionData = [];
  80. for (const a of this._.uniqBy(accountList, 'id')) {
  81. if (this._.findIndex(pauditList, { uid: a.id }) === -1) {
  82. const data = {
  83. spid,
  84. tid,
  85. uid: a.id,
  86. create_time: new Date(),
  87. };
  88. pushData.push(data);
  89. }
  90. const subProjPermission = this._.find(financialAudits, { uid: a.id });
  91. if (!subProjPermission) {
  92. pushSubProjPermissionData.push({
  93. id: this.uuid.v4(),
  94. spid,
  95. pid: this.ctx.session.sessionProject.id,
  96. uid: a.id,
  97. fund_trans_permission: 1,
  98. fund_pay_permission: 1,
  99. });
  100. } else {
  101. subProjPermission.fund_trans_permission = subProjPermission.fund_trans_permission ? this._.map(subProjPermission.fund_trans_permission.split(','), this._.toInteger) : [];
  102. subProjPermission.fund_pay_permission = subProjPermission.fund_pay_permission ? this._.map(subProjPermission.fund_pay_permission.split(','), this._.toInteger) : [];
  103. const financialPermission = await this.ctx.service.subProjPermission.getFinancailPermission(subProjPermission.fund_trans_permission, subProjPermission.fund_pay_permission);
  104. if (!financialPermission.pay_show) {
  105. subProjPermission.fund_pay_permission.push(1);
  106. const upPermission = {
  107. id: subProjPermission.id,
  108. fund_pay_permission: subProjPermission.fund_pay_permission.join(','),
  109. };
  110. if (!financialPermission.transfer_show) {
  111. subProjPermission.fund_trans_permission.push(1);
  112. upPermission.fund_trans_permission = subProjPermission.fund_trans_permission.join(',');
  113. }
  114. updateSubProjPermissionData.push(upPermission);
  115. }
  116. }
  117. }
  118. if (pushData.length > 0) {
  119. await transaction.insert(this.tableName, pushData);
  120. }
  121. if (pushSubProjPermissionData.length > 0) {
  122. await transaction.insert(this.ctx.service.subProjPermission.tableName, pushSubProjPermissionData);
  123. }
  124. if (updateSubProjPermissionData.length > 0) {
  125. await transaction.updateRows(this.ctx.service.subProjPermission.tableName, updateSubProjPermissionData);
  126. }
  127. transaction.commit();
  128. } catch (error) {
  129. console.log(error);
  130. transaction.rollback();
  131. }
  132. return true;
  133. }
  134. async delAudits(spid, tid, datas) {
  135. // return await this.db.delete(this.tableName, { id });
  136. // 需要同时移除审批流程里的人
  137. const reponseData = {};
  138. const transaction = await this.db.beginTransaction();
  139. try {
  140. await transaction.delete(this.tableName, { id: this._.map(datas, 'id') });
  141. const shenpiAudits = await this.ctx.service.shenpiAudit.getAuditList(tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
  142. const uids = this._.map(datas, 'uid');
  143. const removeData = [];
  144. for (const sp of shenpiAudits) {
  145. if (this._.includes(uids, sp.audit_id)) {
  146. removeData.push(sp.id);
  147. }
  148. }
  149. if (removeData.length > 0) {
  150. await transaction.delete(this.ctx.service.shenpiAudit.tableName, { id: removeData });
  151. // 重新分配order值
  152. 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']] });
  153. // groupby audit_order
  154. const newSpAuditsGroup = this._.groupBy(newSpAudits, 'audit_order');
  155. const updateData = [];
  156. let order = 1;
  157. for (const a in newSpAuditsGroup) {
  158. const sameOrderAudits = newSpAuditsGroup[a];
  159. for (const sa of sameOrderAudits) {
  160. if (sa.audit_order !== order) {
  161. updateData.push({
  162. id: sa.id,
  163. audit_order: order,
  164. });
  165. }
  166. }
  167. order++;
  168. }
  169. if (updateData.length > 0) {
  170. await transaction.updateRows(this.ctx.service.shenpiAudit.tableName, updateData);
  171. }
  172. }
  173. await transaction.commit();
  174. reponseData.permissionList = await this.getList(spid, tid);
  175. reponseData.auditGroupList = await this.ctx.service.shenpiAudit.getAuditGroupList(tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
  176. } catch (error) {
  177. console.log(error);
  178. await transaction.rollback();
  179. }
  180. return reponseData;
  181. }
  182. async updatePermission(updateData) {
  183. if (!updateData.id) {
  184. return false;
  185. }
  186. return await this.db.update(this.tableName, updateData);
  187. }
  188. async addShenpiAudit(spid, data, tid) {
  189. const reponseData = {};
  190. const info = await this.ctx.service.shenpiAudit.addAudit(data, tid);
  191. reponseData.shenpi = info;
  192. if (info) {
  193. const transaction = await this.db.beginTransaction();
  194. try {
  195. // 判断是否存在并加入到成员表中
  196. const fptAudit = await this.getDataByCondition({ spid, tid, uid: data.audit_id });
  197. if (!fptAudit) {
  198. await transaction.insert(this.tableName, { spid, tid, uid: data.audit_id, create_time: new Date() });
  199. }
  200. // 判断是否存在并加入到成员表中
  201. const fAudit = await this.ctx.service.subProjPermission.getDataByCondition({ spid, uid: data.audit_id });
  202. if (!fAudit) {
  203. await transaction.insert(this.ctx.service.subProjPermission.tableName, {
  204. id: this.uuid.v4(),
  205. spid,
  206. pid: this.ctx.session.sessionProject.id,
  207. uid: data.audit_id,
  208. fund_trans_permission: 1,
  209. fund_pay_permission: 1,
  210. });
  211. } else {
  212. fAudit.fund_trans_permission = fAudit.fund_trans_permission ? this._.map(fAudit.fund_trans_permission.split(','), this._.toInteger) : [];
  213. fAudit.fund_pay_permission = fAudit.fund_pay_permission ? this._.map(fAudit.fund_pay_permission.split(','), this._.toInteger) : [];
  214. const financialPermission = await this.ctx.service.subProjPermission.getFinancailPermission(fAudit.fund_trans_permission, fAudit.fund_pay_permission);
  215. if (!financialPermission.pay_show) {
  216. fAudit.fund_pay_permission.push(1);
  217. const upPermission = {
  218. id: fAudit.id,
  219. fund_pay_permission: fAudit.fund_pay_permission.join(','),
  220. };
  221. if (!financialPermission.transfer_show) {
  222. fAudit.fund_trans_permission.push(1);
  223. upPermission.fund_trans_permission = fAudit.fund_trans_permission.join(',');
  224. }
  225. await transaction.update(this.ctx.service.subProjPermission.tableName, upPermission);
  226. }
  227. }
  228. await transaction.commit();
  229. reponseData.permissionList = await this.getList(spid, tid);
  230. } catch (error) {
  231. console.log(error);
  232. await transaction.rollback();
  233. }
  234. }
  235. return reponseData;
  236. }
  237. async copyShenpi2otherTender(spid, data, tid) {
  238. const reponseData = {};
  239. const info = await this.ctx.service.shenpiAudit.copyAudit2otherTender(data, tid);
  240. if (info) {
  241. const transaction = await this.db.beginTransaction();
  242. try {
  243. // const permissionList = await this.getAllDataByCondition({ where: { spid, tid } });
  244. const uidList = this._.map(data.auditList, 'audit_id');
  245. const tidList = data.tidList.split(',');
  246. if (tidList.length === 0) {
  247. throw '没有选择要复制的标段';
  248. }
  249. if (uidList.length > 0) {
  250. const pushData = [];
  251. const times = new Date();
  252. for (const t of tidList) {
  253. const tenderAudits = await this.getAllDataByCondition({ where: { spid, tid: t } });
  254. const diffList = this._.difference(uidList, this._.map(tenderAudits, 'uid'));
  255. for (const d of diffList) {
  256. pushData.push({
  257. spid, tid: t, uid: d,
  258. create_time: times,
  259. });
  260. }
  261. }
  262. if (pushData.length > 0) await transaction.insert(this.tableName, pushData);
  263. }
  264. await transaction.commit();
  265. reponseData.otherPermissionList = await this.getList(spid, tidList);
  266. } catch (error) {
  267. console.log(error);
  268. await transaction.rollback();
  269. }
  270. }
  271. return reponseData;
  272. }
  273. async copyAudit2otherTender(spid, data, tid) {
  274. const reponseData = {};
  275. const transaction = await this.db.beginTransaction();
  276. try {
  277. const permissionList = await this.getAllDataByCondition({ where: { spid, tid } });
  278. const uidList = this._.map(permissionList, 'uid');
  279. const tidList = data.tidList.split(',');
  280. if (tidList.length === 0) {
  281. throw '没有选择要复制的标段';
  282. }
  283. if (uidList.length > 0) {
  284. const pushData = [];
  285. const updateData = [];
  286. const times = new Date();
  287. for (const t of tidList) {
  288. const tenderAudits = await this.getAllDataByCondition({ where: { spid, tid: t } });
  289. const diffList = this._.difference(uidList, this._.map(tenderAudits, 'uid'));
  290. for (const d of diffList) {
  291. const p = permissionList.find(item => item.uid === d);
  292. pushData.push({ spid, tid: t, uid: p.uid, is_report: p.is_report, create_time: times });
  293. }
  294. const sameList = this._.intersection(this._.map(tenderAudits, 'uid'), uidList);
  295. for (const s of sameList) {
  296. const p = permissionList.find(item => item.uid === s);
  297. const t = tenderAudits.find(item => item.uid === s);
  298. if (p.is_report !== t.is_report) {
  299. updateData.push({ id: t.id, is_report: p.is_report });
  300. }
  301. }
  302. }
  303. if (pushData.length > 0) await transaction.insert(this.tableName, pushData);
  304. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  305. }
  306. await transaction.commit();
  307. reponseData.otherPermissionList = await this.getList(spid, tidList);
  308. } catch (error) {
  309. console.log(error);
  310. await transaction.rollback();
  311. }
  312. return reponseData;
  313. }
  314. }
  315. return FinancialPayTenderAudit;
  316. };