financial_pay_tender_audit.js 15 KB

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