intelligentFollow.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class IntelligentFollowService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'intelligent_follow');
  10. this.model = this.ctx.model.IntelligentFollow;
  11. this.dmodel = this.ctx.model.IntelligentDocking;
  12. }
  13. // 已完成列表
  14. async getFinishList(data) {
  15. const match = {};
  16. if (data.userid) { // 金融机构ID---管理员
  17. match.uid = data.userid;
  18. } else {
  19. if (data.uid) { // 金融机构ID---客户经理
  20. match.uuid = data.uid;
  21. }
  22. }
  23. match.creditStatus = '1';
  24. const skip = Number.parseInt(data.skip) || 1;
  25. const limit = Number.parseInt(data.limit) || 10;
  26. const total = await this.model.countDocuments(match);
  27. const res = await this.model.aggregate([
  28. { $match: match },
  29. { $project: { intelligentId: { $toObjectId: '$intelligentId' }, creditStatus: 1, savetime: '$meta.createdAt' } },
  30. { $lookup: {
  31. from: 'intelligent_docking',
  32. localField: 'intelligentId',
  33. foreignField: '_id',
  34. as: 'intelligent' } },
  35. { $unwind: '$intelligent' },
  36. { $skip: (skip - 1) * limit },
  37. { $limit: limit },
  38. { $sort: { savetime: -1 } },
  39. ]);
  40. const newres = { res, total };
  41. return newres;
  42. }
  43. // 关注列表
  44. async getFollowList(data) {
  45. const match = {};
  46. if (data.userid) { // 金融机构ID---客户经理
  47. match.uuid = data.userid;
  48. }
  49. if (data.creditStatus) {
  50. match.creditStatus = data.creditStatus;
  51. }
  52. // else {
  53. // match.creditStatus = { $ne: '1' };
  54. // }
  55. const skip = Number.parseInt(data.skip) || 1;
  56. const limit = Number.parseInt(data.limit) || 10;
  57. const total = await this.model.countDocuments(match);
  58. const res = await this.model.aggregate([
  59. { $match: match },
  60. { $project: { intelligentId: { $toObjectId: '$intelligentId' }, creditStatus: 1, savetime: '$meta.createdAt' } },
  61. { $lookup: {
  62. from: 'intelligent_docking',
  63. localField: 'intelligentId',
  64. foreignField: '_id',
  65. as: 'intelligent' } },
  66. { $unwind: '$intelligent' },
  67. { $skip: (skip - 1) * limit },
  68. { $limit: limit },
  69. { $sort: { savetime: -1 } },
  70. ]);
  71. const newres = { res, total };
  72. return newres;
  73. }
  74. // 授信接口
  75. async getCredit(data) {
  76. const { id, money, creditStatus, senhemessage, jindiaomessage, sxcpname, sxhowlong, sxcplilue } = data;// id:关注ID money:授信额度 orcredit:状态,senhemessage:审核备注,jindiaomessage:尽调备注
  77. const now = new Date();
  78. const nowtime = now.getTime();// 当前时间戳(授信时间)
  79. const intelligentFollow = await this.model.findById(id);
  80. if (creditStatus == '1' || creditStatus == '3') {
  81. const xuqiuId = intelligentFollow.intelligentId;
  82. const xuqiuData = await this.dmodel.findById(xuqiuId);
  83. xuqiuData.status = '1';
  84. await xuqiuData.save();
  85. }
  86. if (senhemessage) {
  87. intelligentFollow.senhemessage = senhemessage;
  88. }
  89. if (jindiaomessage) {
  90. intelligentFollow.jindiaomessage = jindiaomessage;
  91. }
  92. if (money) {
  93. intelligentFollow.credit_money = parseInt(money);
  94. }
  95. if (sxcpname) {
  96. intelligentFollow.sxcpname = sxcpname;
  97. }
  98. if (sxhowlong) {
  99. intelligentFollow.sxhowlong = sxhowlong;
  100. }
  101. if (sxcplilue) {
  102. intelligentFollow.sxcplilue = sxcplilue;
  103. }
  104. // 状态
  105. intelligentFollow.creditStatus = creditStatus;
  106. const creattime = new Date(intelligentFollow.meta.createdAt).getTime();// 创建时间时的时间戳
  107. const accept_time = parseInt(nowtime) - parseInt(creattime);// 授理时间(授信时间-创建时间)
  108. intelligentFollow.accept_time = parseInt(accept_time);
  109. intelligentFollow.credit_time = parseInt(nowtime);
  110. const res = await intelligentFollow.save();
  111. return res;
  112. }
  113. // 关注前接口
  114. async getBeforFollow(data) {
  115. const { xqid, userid } = data;// 需求ID,金融机构ID
  116. const match = {};
  117. if (userid) { // 金融机构ID
  118. match.uid = userid;
  119. }
  120. if (xqid) { // 需求ID
  121. match.intelligentId = xqid;
  122. }
  123. const result = {};
  124. const res = await this.model.find(match);
  125. if (res.length > 0) {
  126. result.finstatus = 'SUCCESS';
  127. } else {
  128. result.finstatus = 'ERROR';
  129. }
  130. return result;
  131. }
  132. // 金融机构关注需求
  133. async getFollow(data) {
  134. const { xqid, cid, userid, uuid } = data;// 需求ID,金融机构ID,客户经理id
  135. const newdata = {};
  136. newdata.intelligentId = xqid;
  137. newdata.cid = cid;
  138. newdata.uid = userid;
  139. newdata.uuid = uuid;
  140. newdata.creditStatus = '0';
  141. newdata.credit_money = 0;
  142. newdata.accept_time = 0;
  143. newdata.credit_time = 0;
  144. // console.log('new =============', newdata);
  145. const res = await this.model.create(newdata);
  146. console.dir(this.dmodel);
  147. const qy = await this.dmodel.findById(xqid);
  148. this.ctx.service.viewnews.insertViewNews('银企对接需求信息', '您有新的银企对接需求被关注,请查看需求列表', qy.uid);
  149. return res;
  150. }
  151. }
  152. module.exports = IntelligentFollowService;