intelligentDocking.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. const moment = require('moment');
  8. class IntelligentDockingService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'intelligent_docking');
  11. this.model = this.ctx.model.IntelligentDocking;
  12. this.fmodel = this.ctx.model.IntelligentFollow;
  13. this.tmodel = this.ctx.model.Tfinanceclaims;
  14. }
  15. // 小程序 银企对接接口
  16. async intelligentDocking(data) {
  17. // const { uid, company_name, code, person, opening_bank, orientation, money, claims_min_term, claims_max_term,
  18. // mongey_min_rate, mongey_max_rate, ensure_id, when, additional_information } = data;
  19. const { uid, company_name, person, phone, money, ensure_id, orientation, additional_information } = data;
  20. assert(uid, company_name && person && phone && money && ensure_id && orientation && additional_information, '缺少部分信息项');
  21. // 判断该企业是否有在进行中的需求(一个企业只能有一个进行中的需求)
  22. // const companyList = await this.model.find({ uid, status: '0' });
  23. const companyList = await this.model.aggregate([
  24. { $match: { uid } },
  25. { $project:
  26. {
  27. uid: 1,
  28. xqId: { $toString: '$_id' },
  29. },
  30. },
  31. { $lookup:
  32. {
  33. from: 'intelligent_follow',
  34. localField: 'xqId',
  35. foreignField: 'intelligentId',
  36. as: 'intelligent',
  37. },
  38. },
  39. { $unwind: { path: '$intelligent' } },
  40. {
  41. $project:
  42. {
  43. xqid: 1,
  44. uid: 1,
  45. creditStatus: '$intelligent.creditStatus',
  46. },
  47. },
  48. {
  49. $match:
  50. {
  51. $expr:
  52. {
  53. $and:
  54. [{
  55. $ne: [ '$creditStatus', '1' ],
  56. },
  57. {
  58. $ne: [ '$creditStatus', '3' ],
  59. },
  60. ],
  61. },
  62. },
  63. },
  64. ]);
  65. if (companyList.length > 0) {
  66. return '您有一个正在进行中的对接需求,请完成后再近些申请';
  67. }
  68. // 需求
  69. const condition = { orientation, money, ensure_id };
  70. // 对接产品
  71. const products = await this.findFinanceClaimsList(condition);
  72. const date = new Date();
  73. const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  74. data.create_time = create_time;
  75. let res;
  76. if (products.length > 0) {
  77. console.log(products[0]);
  78. data.cid = products[0]._id;
  79. data.jg_id = products[0].uid;// 金融机构id
  80. try {
  81. res = await this.model.create(data);
  82. } catch (error) {
  83. console.log(error);
  84. res = error;
  85. }
  86. } else {
  87. res = '没有符合条件的对接产品,请修改对接需求';
  88. }
  89. return res;
  90. }
  91. // 根据对接条件查询产品
  92. async findFinanceClaimsList(data) {
  93. // opening_bank 传的是金融机构的id
  94. const { orientation, money, ensure_id } = data;
  95. // 已发布的产品
  96. const match = { status: '1' };
  97. // 担保方式【企业选择】、贷款额度【企业选择】+融资取向对产品进行筛选
  98. // 担保方式
  99. if (ensure_id) {
  100. match.ensure_id = ensure_id;
  101. }
  102. // 贷款额度
  103. if (money) {
  104. match.claims_max_money = { $gte: parseInt(money) };
  105. }
  106. // 融资取向 按第一个取向查询,如果没有结果,按第二个取向查...
  107. const cattributeMatch = { status: '1' };
  108. if (orientation && orientation.length > 0) {
  109. for (const item of orientation) {
  110. cattributeMatch.cattribute = { $in: item };
  111. const res1 = await this.tmodel.find(cattributeMatch);
  112. if (res1.length > 0) {
  113. match.cattribute = { $in: item };
  114. break;
  115. }
  116. }
  117. }
  118. console.log('match', match);
  119. const _sort = {};
  120. // 排序
  121. if (orientation && orientation[0] === '2501') {
  122. _sort.claims_max_term = -1;
  123. }
  124. if (orientation && orientation[0] === '2503') {
  125. _sort.mongey_min_rate = 1;
  126. }
  127. if (orientation && orientation[0] === '2505') {
  128. _sort.claims_max_money = -1;
  129. }
  130. const res = await this.tmodel.find(match).sort(_sort).limit(Number(5));
  131. return res;
  132. }
  133. // 查询对接记录(金融机构,企业)
  134. async dockingFinance(data) {
  135. const query = {};
  136. if (data.id) { // 单查 对接需求id
  137. query._id = ObjectId(data.id);
  138. }
  139. if (data.jg_id) { // 金融机构ID
  140. query.jg_id = data.jg_id;
  141. }
  142. if (data.uid) { // 企业ID
  143. query.uid = data.uid;
  144. }
  145. if (data.cid) { // 产品id
  146. query.cid = data.cid;
  147. }
  148. if (data.status) { // 状态
  149. query.status = data.status;
  150. }
  151. const skip = Number.parseInt(data.skip) || 1;
  152. const limit = Number.parseInt(data.limit) || 10;
  153. // const total = await this.model.countDocuments(query);
  154. const total = await this.model.aggregate([
  155. { $match: query },
  156. { $project:
  157. {
  158. ensure_id: 1,
  159. person: 1,
  160. money: 1,
  161. claims_min_term: 1,
  162. claims_max_term: 1,
  163. mongey_min_rate: 1,
  164. mongey_max_rate: 1,
  165. when: 1,
  166. refuse_times: 1,
  167. additional_information: 1,
  168. status: 1,
  169. jg_id: { $toObjectId: '$jg_id' },
  170. cid: { $toObjectId: '$cid' },
  171. uid: '$uid',
  172. xqId: { $toString: '$_id' },
  173. time: '$meta.createdAt',
  174. },
  175. },
  176. { $lookup:
  177. {
  178. from: 'company_identify',
  179. localField: 'uid',
  180. foreignField: 'uid',
  181. as: 'company',
  182. },
  183. },
  184. { $lookup:
  185. {
  186. from: 'institution',
  187. localField: 'jg_id',
  188. foreignField: '_id',
  189. as: 'institution',
  190. },
  191. },
  192. { $lookup:
  193. {
  194. from: 't_finance_claims',
  195. localField: 'cid',
  196. foreignField: '_id',
  197. as: 'finance_claims',
  198. },
  199. },
  200. { $lookup:
  201. {
  202. from: 'dictionary',
  203. localField: 'ensure_id',
  204. foreignField: 'code',
  205. as: 'dictionary',
  206. },
  207. },
  208. // { $lookup:
  209. // {
  210. // from: 'dictionary',
  211. // localField: 'when',
  212. // foreignField: 'code',
  213. // as: 'when',
  214. // },
  215. // },
  216. { $lookup:
  217. {
  218. from: 'intelligent_follow',
  219. localField: 'xqId',
  220. foreignField: 'intelligentId',
  221. as: 'follow',
  222. },
  223. },
  224. { $unwind: '$company' },
  225. { $unwind: '$institution' },
  226. { $unwind: '$finance_claims' },
  227. { $unwind: '$dictionary' },
  228. { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } },
  229. // { $unwind: { path: '$when', preserveNullAndEmptyArrays: true } },
  230. { $project:
  231. {
  232. ensure_id: 1,
  233. person: 1,
  234. money: 1,
  235. claims_min_term: 1,
  236. claims_max_term: 1,
  237. mongey_min_rate: 1,
  238. mongey_max_rate: 1,
  239. when: 1,
  240. refuse_times: 1,
  241. additional_information: 1,
  242. status: 1,
  243. creditStatus: '$follow.creditStatus',
  244. jg_id: { $toObjectId: '$jg_id' },
  245. cid: { $toObjectId: '$cid' },
  246. uid: '$uid',
  247. xqId: { $toString: '$_id' },
  248. time: '$meta.createdAt',
  249. company: '$company',
  250. institution: '$institution',
  251. finance_claims: '$finance_claims',
  252. dictionary: '$dictionary',
  253. follow: '$follow',
  254. },
  255. },
  256. { $sort: { time: -1 } },
  257. ]);
  258. const result = await this.model.aggregate([
  259. { $match: query },
  260. { $project:
  261. {
  262. ensure_id: 1,
  263. person: 1,
  264. money: 1,
  265. claims_min_term: 1,
  266. claims_max_term: 1,
  267. mongey_min_rate: 1,
  268. mongey_max_rate: 1,
  269. when: 1,
  270. refuse_times: 1,
  271. additional_information: 1,
  272. status: 1,
  273. jg_id: { $toObjectId: '$jg_id' },
  274. cid: { $toObjectId: '$cid' },
  275. uid: '$uid',
  276. xqId: { $toString: '$_id' },
  277. time: '$meta.createdAt',
  278. },
  279. },
  280. { $lookup:
  281. {
  282. from: 'company_identify',
  283. localField: 'uid',
  284. foreignField: 'uid',
  285. as: 'company',
  286. },
  287. },
  288. { $lookup:
  289. {
  290. from: 'institution',
  291. localField: 'jg_id',
  292. foreignField: '_id',
  293. as: 'institution',
  294. },
  295. },
  296. { $lookup:
  297. {
  298. from: 't_finance_claims',
  299. localField: 'cid',
  300. foreignField: '_id',
  301. as: 'finance_claims',
  302. },
  303. },
  304. { $lookup:
  305. {
  306. from: 'dictionary',
  307. localField: 'ensure_id',
  308. foreignField: 'code',
  309. as: 'dictionary',
  310. },
  311. },
  312. // { $lookup:
  313. // {
  314. // from: 'dictionary',
  315. // localField: 'when',
  316. // foreignField: 'code',
  317. // as: 'when',
  318. // },
  319. // },
  320. { $lookup:
  321. {
  322. from: 'intelligent_follow',
  323. localField: 'xqId',
  324. foreignField: 'intelligentId',
  325. as: 'follow',
  326. },
  327. },
  328. { $unwind: '$company' },
  329. { $unwind: '$institution' },
  330. { $unwind: '$finance_claims' },
  331. { $unwind: '$dictionary' },
  332. { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } },
  333. // { $unwind: { path: '$when', preserveNullAndEmptyArrays: true } },
  334. { $project:
  335. {
  336. ensure_id: 1,
  337. person: 1,
  338. money: 1,
  339. claims_min_term: 1,
  340. claims_max_term: 1,
  341. mongey_min_rate: 1,
  342. mongey_max_rate: 1,
  343. when: 1,
  344. refuse_times: 1,
  345. additional_information: 1,
  346. status: 1,
  347. creditStatus: '$follow.creditStatus',
  348. jg_id: { $toObjectId: '$jg_id' },
  349. cid: { $toObjectId: '$cid' },
  350. uid: '$uid',
  351. xqId: { $toString: '$_id' },
  352. time: '$meta.createdAt',
  353. company: '$company',
  354. institution: '$institution',
  355. finance_claims: '$finance_claims',
  356. dictionary: '$dictionary',
  357. follow: '$follow',
  358. },
  359. },
  360. { $skip: (skip - 1) * limit },
  361. { $limit: limit },
  362. { $sort: { time: -1 } },
  363. ]);
  364. return { result, total: total.length };
  365. }
  366. // 拒绝接口
  367. async refuse(data) {
  368. const { intelligentId, guanzhuid, userid, reason } = data;// intelligentId, userid, reason 需求id,银行id,拒绝理由
  369. const intelligent = await this.model.findById(intelligentId);// 对接需求
  370. const guanzhu = await this.fmodel.findById(guanzhuid);
  371. let res;
  372. if (intelligent.refuse_times < 3) {
  373. // 重新对接产品
  374. // 需求
  375. const condition = {
  376. orientation: intelligent.orientation,
  377. money: intelligent.money,
  378. claims_min_term: intelligent.claims_min_term,
  379. claims_max_term: intelligent.claims_max_term,
  380. mongey_min_rate: intelligent.mongey_min_rate,
  381. mongey_max_rate: intelligent.mongey_max_rate,
  382. ensure_id: intelligent.ensure_id,
  383. };
  384. // 对接产品
  385. const products = await this.findFinanceClaimsList(condition);
  386. const ids = intelligent.ids;
  387. if (products.length > 0) {
  388. // console.log('对接的产品', products);
  389. // 拒绝ids 里存在对接的产品id
  390. ids.push({ jr_id: intelligent.jg_id, reason });
  391. const items = products.filter(item => {
  392. if (ids.length === 0) {
  393. return true;
  394. }
  395. return !ids.map(item => {
  396. return item.jr_id;
  397. }).includes(item.uid);
  398. });
  399. console.log('对接的产品items', items);
  400. console.log('ids', ids);
  401. if (items.length > 0) {
  402. intelligent.cid = items[0]._id;
  403. intelligent.jg_id = items[0].uid;// 金融机构id
  404. const date = new Date();
  405. const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  406. intelligent.create_time = create_time;
  407. } else {
  408. // 拒绝后没有匹配的银行
  409. guanzhu.creditStatus = '3';
  410. await guanzhu.save();
  411. // 对接需求表状态
  412. intelligent.status = '1';
  413. await intelligent.save();
  414. this.ctx.service.viewnews.insertViewNews('智能对接', '无银行受理该需求,改需求已自动关闭', intelligent.uid);
  415. return '没有更多对接产品!';
  416. }
  417. // intelligent.ids.push({ jr_id: intelligent.jg_id, reason });
  418. intelligent.ids = ids;
  419. intelligent.refuse_times = intelligent.refuse_times + 1;
  420. if (intelligent.refuse_times === 3) {
  421. guanzhu.creditStatus = '3';
  422. guanzhu.refusemessage = reason;
  423. await guanzhu.save();
  424. // 对接需求表状态
  425. intelligent.status = '1';
  426. this.ctx.service.viewnews.insertViewNews('智能对接', '您已被拒绝3次,不再指派银行受理,请重新提交申请,详情请查看智能对接需求列表', intelligent.uid);
  427. }
  428. res = await intelligent.save();
  429. } else {
  430. return '没有更多对接产品!';
  431. }
  432. } else {
  433. res = '您已被拒绝3次,请重新提交申请';
  434. }
  435. return res;
  436. }
  437. }
  438. module.exports = IntelligentDockingService;