intelligentFollow.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. } else {
  52. match.creditStatus = { $ne: '1' };
  53. }
  54. const skip = Number.parseInt(data.skip) || 1;
  55. const limit = Number.parseInt(data.limit) || 10;
  56. const total = await this.model.countDocuments(match);
  57. const res = await this.model.aggregate([
  58. { $match: match },
  59. { $project: { intelligentId: { $toObjectId: '$intelligentId' }, creditStatus: 1, savetime: '$meta.createdAt' } },
  60. { $lookup: {
  61. from: 'intelligent_docking',
  62. localField: 'intelligentId',
  63. foreignField: '_id',
  64. as: 'intelligent' } },
  65. { $unwind: '$intelligent' },
  66. { $skip: (skip - 1) * limit },
  67. { $limit: limit },
  68. { $sort: { savetime: -1 } },
  69. ]);
  70. const newres = { res, total };
  71. return newres;
  72. }
  73. // 授信接口
  74. async getCredit(data) {
  75. const { id, money, creditStatus, senhemessage, jindiaomessage, sxcpname, sxhowlong, sxcplilue } = data;// id:关注ID money:授信额度 orcredit:状态,senhemessage:审核备注,jindiaomessage:尽调备注
  76. const now = new Date();
  77. const nowtime = now.getTime();// 当前时间戳(授信时间)
  78. const intelligentFollow = await this.model.findById(id);
  79. if (creditStatus == '1' || creditStatus == '3') {
  80. const xuqiuId = intelligentFollow.intelligentId;
  81. const xuqiuData = await this.dmodel.findById(xuqiuId);
  82. xuqiuData.status = '1';
  83. await xuqiuData.save();
  84. }
  85. if (senhemessage) {
  86. intelligentFollow.senhemessage = senhemessage;
  87. }
  88. if (jindiaomessage) {
  89. intelligentFollow.jindiaomessage = jindiaomessage;
  90. }
  91. if (money) {
  92. intelligentFollow.credit_money = parseInt(money);
  93. }
  94. if (sxcpname) {
  95. intelligentFollow.sxcpname = sxcpname;
  96. }
  97. if (sxhowlong) {
  98. intelligentFollow.sxhowlong = sxhowlong;
  99. }
  100. if (sxcplilue) {
  101. intelligentFollow.sxcplilue = sxcplilue;
  102. }
  103. // 状态
  104. intelligentFollow.creditStatus = creditStatus;
  105. const creattime = new Date(intelligentFollow.meta.createdAt).getTime();// 创建时间时的时间戳
  106. const accept_time = parseInt(nowtime) - parseInt(creattime);// 授理时间(授信时间-创建时间)
  107. intelligentFollow.accept_time = parseInt(accept_time);
  108. intelligentFollow.credit_time = parseInt(nowtime);
  109. const res = await intelligentFollow.save();
  110. return res;
  111. }
  112. // 关注前接口
  113. async getBeforFollow(data) {
  114. const { xqid, userid } = data;// 需求ID,金融机构ID
  115. const match = {};
  116. if (userid) { // 金融机构ID
  117. match.uid = userid;
  118. }
  119. if (xqid) { // 需求ID
  120. match.intelligentId = xqid;
  121. }
  122. const result = {};
  123. const res = await this.model.find(match);
  124. if (res.length > 0) {
  125. result.finstatus = 'SUCCESS';
  126. } else {
  127. result.finstatus = 'ERROR';
  128. }
  129. return result;
  130. }
  131. // 金融机构关注需求
  132. async getFollow(data) {
  133. const { xqid, cid, userid, uuid } = data;// 需求ID,金融机构ID,客户经理id
  134. const newdata = {};
  135. newdata.intelligentId = xqid;
  136. newdata.cid = cid;
  137. newdata.uid = userid;
  138. newdata.uuid = uuid;
  139. newdata.creditStatus = '0';
  140. newdata.credit_money = 0;
  141. newdata.accept_time = 0;
  142. newdata.credit_time = 0;
  143. console.log('new =============', newdata);
  144. const res = await this.model.create(newdata);
  145. console.dir(this.dmodel);
  146. const qy = await this.dmodel.findById(xqid);
  147. this.ctx.service.viewnews.insertViewNews('银企对接需求信息', '您有新的银企对接需求被关注,请查看需求列表', qy.uid);
  148. return res;
  149. }
  150. }
  151. module.exports = IntelligentFollowService;