cooperation_confirm.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/6/1
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. module.exports = app => {
  11. class CooperationConfirm extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'cooperation_confirm';
  21. }
  22. async save(data) {
  23. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, sid: this.ctx.stage.id, times: this.ctx.stage.times, ledger_id: data.ledger_id, uid: this.ctx.session.sessionUser.accountId });
  24. if (info) {
  25. const updateData = {
  26. id: info.id,
  27. create_time: new Date(),
  28. };
  29. return await this.db.update(this.tableName, updateData);
  30. }
  31. const insertData = {
  32. tid: this.ctx.tender.id,
  33. sid: this.ctx.stage.id,
  34. times: this.ctx.stage.times,
  35. uid: this.ctx.session.sessionUser.accountId,
  36. ledger_id: data.ledger_id,
  37. create_time: new Date(),
  38. };
  39. return await this.db.insert(this.tableName, insertData);
  40. }
  41. async del(data) {
  42. return await this.db.delete(this.tableName, { tid: this.ctx.tender.id, sid: this.ctx.stage.id, times: this.ctx.stage.times, ledger_id: data.ledger_id, uid: this.ctx.session.sessionUser.accountId });
  43. }
  44. async getValidData(tid, sid, times, uid) {
  45. const condition = { where: { tid, sid, times, uid } };
  46. // if (uid) {
  47. // condition.where.uid = uid;
  48. // condition.colums = ['ledger_id', 'pwd'];
  49. // }
  50. return await this.getAllDataByCondition(condition);
  51. }
  52. async delBycheckNoPre(uid, stage, transaction) {
  53. return await transaction.delete(this.tableName, { tid: stage.tid, sid: stage.id, times: stage.times, uid });
  54. }
  55. async lockConfirm4CheckNoPre(stage, uid, pre_uid, transaction) {
  56. if (!transaction) throw '缺少参数';
  57. const locked = await this.getAllDataByCondition({ where: { sid: stage.sid, locked: 1, times: stage.curTimes } });
  58. // 审批人数据锁定
  59. const locking = await this.getAllDataByCondition({ where: { sid: stage.sid, times: stage.curTimes, uid: uid } });
  60. if (locking.length > 0) {
  61. const updateLock = locking.map(x => { return { id: x.id, locked: 1 }; });
  62. await transaction.updateRows(this.tableName, updateLock);
  63. }
  64. const preConfirm = await this.getAllDataByCondition({ where: { sid: stage.sid, times: stage.curTimes, uid: pre_uid } });
  65. // 前审批人数据,与被锁定数据相关数据确认状态保留
  66. locked.push(...locking);
  67. if (preConfirm.length > 0) {
  68. const delConfirm = [];
  69. for (const pc of preConfirm) {
  70. const bills = await this.ctx.service.ledger.getDataByCondition({ tender_id: stage.tid, ledger_id: pc.ledger_id });
  71. const billsOwner = bills.full_path.split('-');
  72. const exist = locked.find(x => { return billsOwner.indexOf(x.ledger_id + '') >= 0; });
  73. if (!exist) delConfirm.push(pc.id);
  74. }
  75. if (delConfirm.length > 0) await transaction.delete(this.tableName, { id: delConfirm });
  76. }
  77. }
  78. async lockComfirm4CheckNo(stage, uid, auditors, transaction) {
  79. if (!transaction) throw '缺少参数';
  80. // 审批人数据锁定
  81. const lockConfirm = await this.getAllDataByCondition({ where: { sid: stage.id, times: stage.curTimes, uid: uid } });
  82. const insertData = [];
  83. lockConfirm.forEach(x => {
  84. delete x.id;
  85. x.locked = 1;
  86. insertData.push(x);
  87. });
  88. // 审批人前所有角色,与锁定数据相关确认状态保留
  89. const keepUid = [ stage.user_id ];
  90. for (const a of auditors) {
  91. if (a.aid === uid) break;
  92. keepUid.push(a.aid);
  93. }
  94. const keepConfirm = await this.getAllDataByCondition({ where: { sid: stage.id, times: stage.curTimes, uid: keepUid } });
  95. for (const kc of keepConfirm) {
  96. const bills = await this.ctx.service.ledger.getDataByCondition({ tender_id: stage.tid, ledger_id: kc.ledger_id });
  97. const billsOwner = bills.full_path.split('-');
  98. const exist = lockConfirm.find(x => { return billsOwner.indexOf(x.ledger_id + '') >= 0; });
  99. if (exist) {
  100. delete kc.id;
  101. kc.locked = 0;
  102. insertData.push(kc);
  103. }
  104. }
  105. await transaction.insert(this.tableName, insertData);
  106. }
  107. async getLockedId(tender, stage) {
  108. // const lockedCoop = await this.getAllDataByCondition({ where: { sid: stage.id, times: stage.curTimes, locked: 1 } });
  109. const sql = `SELECT cc.ledger_id, cc.uid, pa.name FROM ${this.tableName} cc` +
  110. ` LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON cc.uid = pa.id` +
  111. ' WHERE cc.sid = ? AND cc.times = ? AND cc.locked = 1';
  112. return await this.db.query(sql, [stage.id, stage.curTimes]);
  113. }
  114. }
  115. return CooperationConfirm;
  116. };