intelligentFollow.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.followId) { // 关注表id
  47. match._id = ObjectId(data.followId);
  48. }
  49. if (data.userid) { // 金融机构ID---客户经理
  50. match.uuid = data.userid;
  51. }
  52. if (data.creditStatus) {
  53. match.creditStatus = data.creditStatus;
  54. }
  55. // else {
  56. // match.creditStatus = { $ne: '1' };
  57. // }
  58. const skip = Number.parseInt(data.skip) || 1;
  59. const limit = Number.parseInt(data.limit) || 10;
  60. const total = await this.model.countDocuments(match);
  61. const res = await this.model.aggregate([
  62. { $match: match },
  63. { $project:
  64. {
  65. intelligentId: { $toObjectId: '$intelligentId' },
  66. uuid: 1,
  67. creditStatus: 1,
  68. credit_money: 1,
  69. sxcpname: 1,
  70. sxhowlong: 1,
  71. sxcplilue: 1,
  72. savetime: '$meta.createdAt',
  73. time: '$meta.createdAt',
  74. },
  75. },
  76. { $lookup: {
  77. from: 'intelligent_docking',
  78. localField: 'intelligentId',
  79. foreignField: '_id',
  80. as: 'intelligent' } },
  81. // { $unwind: '$intelligent' },
  82. { $unwind: { path: '$intelligent', preserveNullAndEmptyArrays: true } },
  83. { $project:
  84. {
  85. intelligentId: 1,
  86. uuid: 1,
  87. creditStatus: 1,
  88. credit_money: 1,
  89. sxcpname: 1,
  90. sxhowlong: 1,
  91. sxcplilue: 1,
  92. time: 1,
  93. savetime: '$meta.createdAt',
  94. intelligent: '$intelligent',
  95. jg_id: { $toObjectId: '$intelligent.jg_id' },
  96. cid: { $toObjectId: '$intelligent.cid' },
  97. uid: '$intelligent.uid',
  98. ensure_id: '$intelligent.ensure_id',
  99. },
  100. },
  101. { $lookup:
  102. {
  103. from: 'company_identify',
  104. localField: 'uid',
  105. foreignField: 'uid',
  106. as: 'company',
  107. },
  108. },
  109. { $lookup:
  110. {
  111. from: 'institution',
  112. localField: 'jg_id',
  113. foreignField: '_id',
  114. as: 'institution',
  115. },
  116. },
  117. { $lookup:
  118. {
  119. from: 't_finance_claims',
  120. localField: 'cid',
  121. foreignField: '_id',
  122. as: 'finance_claims',
  123. },
  124. },
  125. { $lookup:
  126. {
  127. from: 'dictionary',
  128. localField: 'ensure_id',
  129. foreignField: 'code',
  130. as: 'dictionary',
  131. },
  132. },
  133. { $unwind: { path: '$company', preserveNullAndEmptyArrays: true } },
  134. { $unwind: { path: '$institution', preserveNullAndEmptyArrays: true } },
  135. { $unwind: { path: '$finance_claims', preserveNullAndEmptyArrays: true } },
  136. { $unwind: { path: '$dictionary', preserveNullAndEmptyArrays: true } },
  137. { $skip: (skip - 1) * limit },
  138. { $limit: limit },
  139. { $sort: { time: -1 } },
  140. ]);
  141. const newres = { res, total };
  142. return newres;
  143. }
  144. // 授信接口
  145. async getCredit(data) {
  146. const { id, money, creditStatus, senhemessage, jindiaomessage, sxcpname, sxhowlong, sxcplilue } = data;// id:关注ID money:授信额度 orcredit:状态,senhemessage:审核备注,jindiaomessage:尽调备注
  147. const now = new Date();
  148. const nowtime = now.getTime();// 当前时间戳(授信时间)
  149. const intelligentFollow = await this.model.findById(id);
  150. if (creditStatus == '1' || creditStatus == '3') {
  151. const xuqiuId = intelligentFollow.intelligentId;
  152. const xuqiuData = await this.dmodel.findById(xuqiuId);
  153. xuqiuData.status = '1';
  154. await xuqiuData.save();
  155. }
  156. if (senhemessage) {
  157. intelligentFollow.senhemessage = senhemessage;
  158. }
  159. if (jindiaomessage) {
  160. intelligentFollow.jindiaomessage = jindiaomessage;
  161. }
  162. if (money) {
  163. intelligentFollow.credit_money = parseInt(money);
  164. }
  165. if (sxcpname) {
  166. intelligentFollow.sxcpname = sxcpname;
  167. }
  168. if (sxhowlong) {
  169. intelligentFollow.sxhowlong = sxhowlong;
  170. }
  171. if (sxcplilue) {
  172. intelligentFollow.sxcplilue = sxcplilue;
  173. }
  174. // 状态
  175. intelligentFollow.creditStatus = creditStatus;
  176. const creattime = new Date(intelligentFollow.meta.createdAt).getTime();// 创建时间时的时间戳
  177. const accept_time = parseInt(nowtime) - parseInt(creattime);// 授理时间(授信时间-创建时间)
  178. intelligentFollow.accept_time = parseInt(accept_time);
  179. intelligentFollow.credit_time = parseInt(nowtime);
  180. const res = await intelligentFollow.save();
  181. return res;
  182. }
  183. // 关注前接口
  184. async getBeforFollow(data) {
  185. const { xqid, userid } = data;// 需求ID,金融机构ID
  186. const match = {};
  187. if (userid) { // 金融机构ID
  188. match.uid = userid;
  189. }
  190. if (xqid) { // 需求ID
  191. match.intelligentId = xqid;
  192. }
  193. const result = {};
  194. const res = await this.model.find(match);
  195. if (res.length > 0) {
  196. result.finstatus = 'SUCCESS';
  197. } else {
  198. result.finstatus = 'ERROR';
  199. }
  200. return result;
  201. }
  202. // 金融机构关注需求
  203. async getFollow(data) {
  204. const { xqid, cid, userid, uuid } = data;// 需求ID,金融机构ID,客户经理id
  205. const newdata = {};
  206. newdata.intelligentId = xqid;
  207. newdata.cid = cid;
  208. newdata.uid = userid;
  209. newdata.uuid = uuid;
  210. newdata.creditStatus = '0';
  211. newdata.credit_money = 0;
  212. newdata.accept_time = 0;
  213. newdata.credit_time = 0;
  214. // console.log('new =============', newdata);
  215. const res = await this.model.create(newdata);
  216. console.dir(this.dmodel);
  217. const qy = await this.dmodel.findById(xqid);
  218. this.ctx.service.viewnews.insertViewNews('银企对接需求信息', '您有新的银企对接需求被关注,请查看需求列表', qy.uid);
  219. return res;
  220. }
  221. }
  222. module.exports = IntelligentFollowService;