settle_audit_ass.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. 'use strict';
  2. /**
  3. * 奖罚金
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. module.exports = app => {
  11. class SettleAuditAss extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'settle_audit_ass';
  21. }
  22. async getData(settle) {
  23. const settleData = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.audit_times } });
  24. if (settle.status === auditConst.settle.status.checked) return settleData;
  25. const result = [];
  26. const data = await this.ctx.service.auditAss.getData(settle.tid);
  27. for (const sd of settleData) {
  28. const index = data.findIndex(x => { return x.user_id === sd.user_id && x.ass_user_id === sd.ass_user_id; });
  29. if (index >= 0) {
  30. data.splice(index, 1);
  31. result.push(sd);
  32. }
  33. }
  34. result.push(...data);
  35. return result;
  36. }
  37. async getUserAssist(settle, user_id) {
  38. const settleData = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.audit_times, user_id } });
  39. if (settle.status === auditConst.settle.status.checked) return settleData;
  40. const result = [];
  41. const data = await this.ctx.service.auditAss.getUserData(settle.tid, user_id);
  42. for (const sd of settleData) {
  43. const index = data.findIndex(x => { return x.user_id === sd.user_id && x.ass_user_id === sd.ass_user_id; });
  44. if (index >= 0) {
  45. data.splice(index, 1);
  46. result.push(sd);
  47. }
  48. }
  49. result.push(...data);
  50. return result;
  51. }
  52. async updateData(data) {
  53. if (!data.user_id || !data.ass_user_id || data.confirm === undefined) throw '缺少参数';
  54. const settleData = await this.getDataByCondition({ settle_id: this.ctx.settle.id, audit_times: this.ctx.settle.audit_times, user_id: data.user_id, ass_user_id: data.ass_user_id });
  55. if (data.confirm) {
  56. if (settleData && settleData.confirm) throw '数据错误';
  57. const auditAss = await this.ctx.service.auditAss.getDataByCondition({ tid: this.ctx.tender.id, user_id: data.user_id, ass_user_id: data.ass_user_id });
  58. if (settleData) {
  59. await this.defaultUpdate({ id: settleData.id, confirm: 1, ass_ledger_id: auditAss.ass_ledger_id });
  60. settleData.confirm = 1;
  61. return settleData;
  62. } else {
  63. const assInfo = await this.ctx.service.projectAccount.getDataById(data.ass_user_id);
  64. const insertData = {
  65. pid: this.ctx.session.sessionProject.id, tid: this.ctx.tender.id,
  66. settle_id: this.ctx.settle.id, audit_times: this.ctx.settle.audit_times,
  67. user_id: data.user_id, ass_user_id: data.ass_user_id, ass_ledger_id: auditAss.ass_ledger_id,
  68. name: assInfo.name, company: assInfo.company, role: assInfo.role,
  69. confirm: 1,
  70. };
  71. const result = await this.db.insert(this.tableName, insertData);
  72. insertData.id = result.insertId;
  73. return insertData;
  74. }
  75. } else {
  76. if (!settleData && !settle.confirm) throw '数据错误';
  77. await this.defaultUpdate({ id: settleData.id, confirm: 0, locked: 0 });
  78. settleData.confirm = 0;
  79. settleData.locked = 0;
  80. return settleData;
  81. }
  82. }
  83. async lockConfirm4CheckNoPre(settle, uid, pre_uid, transaction) {
  84. if (!transaction) throw '缺少参数';
  85. const locked = await this.getAllDataByCondition({ where: { settle_id: settle.id, locked: 1, audit_times: settle.curTimes } });
  86. locked.forEach(x => {
  87. x.locked_ledger_id = x.ass_ledger_id.split(',');
  88. });
  89. // 审批人数据锁定
  90. const locking = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.curTimes, user_id: uid, confirm: 1 } });
  91. locking.forEach(x => {
  92. x.locked_ledger_id = x.ass_ledger_id.split(',');
  93. });
  94. if (locking.length > 0) {
  95. const updateLock = locking.map(x => { return { id: x.id, locked: 1 }; });
  96. await transaction.updateRows(this.tableName, updateLock);
  97. }
  98. const preConfirm = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.curTimes, user_id: pre_uid, confirm: 1 } });
  99. // 前审批人数据,与被锁定数据相关数据确认状态保留
  100. locked.push(...locking);
  101. if (preConfirm.length > 0) {
  102. const delConfirm = [];
  103. for (const pc of preConfirm) {
  104. const bills = await this.ctx.service.ledger.getDataByCondition({ tender_id: settle.tid, ledger_id: pc.ledger_id });
  105. const billsOwner = bills ? bills.full_path.split('-') : [];
  106. const exist = locked.find(x => {
  107. for (const id of x.locked_ledger_id) {
  108. if (billsOwner.indexOf(id) >= 0) return true;
  109. }
  110. return false;
  111. });
  112. if (!exist) delConfirm.push(pc.id);
  113. }
  114. if (delConfirm.length > 0) await transaction.delete(this.tableName, { id: delConfirm });
  115. }
  116. }
  117. async lockConfirm4CheckNo(settle, uid, auditors, transaction) {
  118. if (!transaction) throw '缺少参数';
  119. // 审批人数据锁定
  120. const lockConfirm = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.curTimes, user_id: uid, confirm: 1 } });
  121. if (lockConfirm.length === 0) return;
  122. const insertData = [];
  123. lockConfirm.forEach(x => {
  124. delete x.id;
  125. x.locked = 1;
  126. x.audit_times = x.audit_times + 1;
  127. insertData.push(x);
  128. });
  129. // 审批人前所有角色,与锁定数据相关确认状态保留
  130. const keepUid = [ settle.user_id ];
  131. for (const a of auditors) {
  132. if (a.aid === uid) break;
  133. keepUid.push(a.aid);
  134. }
  135. const keepConfirm = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.curTimes, user_id: keepUid, confirm: 1 } });
  136. for (const kc of keepConfirm) {
  137. const bills = await this.ctx.service.ledger.getDataByCondition({ tender_id: settle.tid, ledger_id: kc.ledger_id });
  138. const billsOwner = bills ? bills.full_path.split('-') : [];
  139. const exist = lockConfirm.find(x => {
  140. const lid = x.ass_ledger_id.split(',');
  141. for (const id of lid) {
  142. if (billsOwner.indexOf(id) >= 0) return true;
  143. }
  144. return false;
  145. });
  146. if (exist) {
  147. delete kc.id;
  148. kc.locked = 0;
  149. kc.audit_times = kc.audit_times + 1;
  150. insertData.push(kc);
  151. }
  152. }
  153. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  154. }
  155. async cancelLock(settle, uid, transaction) {
  156. await transaction.update(this.tableName, { locked: 0 }, { where: { settle_id: settle.id, audit_times: settle.audit_times, user_id: uid } });
  157. }
  158. async getLockedId(settle) {
  159. return await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.audit_times, locked: 1 } });
  160. }
  161. async getReportData(settle_id, audit_times) {
  162. const sql = 'SELECT saa.tid, saa.settle_id, saa.audit_times, saa.user_id, saa.ass_ledger_id, saa.ass_user_id, saa.name, saa.company, saa.role, pa.sign_path, pa.stamp_path' +
  163. ` FROM ${this.tableName} saa LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON saa.ass_user_id = pa.id` +
  164. ` WHERE saa.settle_id = ? AND saa.audit_times = ?`;
  165. const result = await this.db.query(sql, [settle_id, audit_times]);
  166. result.forEach(x => { x.ass_ledger_id = x.ass_ledger_id ? x.ass_ledger_id.split(',') : []});
  167. return result;
  168. }
  169. }
  170. return SettleAuditAss;
  171. };