intelligentDocking.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 result = await this.model.aggregate([
  155. // { $match: query },
  156. { $project:
  157. {
  158. ensure_id: 1,
  159. person: 1,
  160. phone: 1,
  161. money: 1,
  162. claims_min_term: 1,
  163. claims_max_term: 1,
  164. mongey_min_rate: 1,
  165. mongey_max_rate: 1,
  166. when: 1,
  167. refuse_times: 1,
  168. additional_information: 1,
  169. status: 1,
  170. jg_id: { $toObjectId: '$jg_id' },
  171. cid: { $toObjectId: '$cid' },
  172. uid: '$uid',
  173. xqId: { $toString: '$_id' },
  174. time: '$meta.createdAt',
  175. },
  176. },
  177. { $lookup:
  178. {
  179. from: 'company_identify',
  180. localField: 'uid',
  181. foreignField: 'uid',
  182. as: 'company',
  183. },
  184. },
  185. { $lookup:
  186. {
  187. from: 'institution',
  188. localField: 'jg_id',
  189. foreignField: '_id',
  190. as: 'institution',
  191. },
  192. },
  193. { $lookup:
  194. {
  195. from: 't_finance_claims',
  196. localField: 'cid',
  197. foreignField: '_id',
  198. as: 'finance_claims',
  199. },
  200. },
  201. { $lookup:
  202. {
  203. from: 'dictionary',
  204. localField: 'ensure_id',
  205. foreignField: 'code',
  206. as: 'dictionary',
  207. },
  208. },
  209. { $lookup:
  210. {
  211. from: 'intelligent_follow',
  212. localField: 'xqId',
  213. foreignField: 'intelligentId',
  214. as: 'follow',
  215. },
  216. },
  217. { $unwind: { path: '$company', preserveNullAndEmptyArrays: true } },
  218. { $unwind: { path: '$institution', preserveNullAndEmptyArrays: true } },
  219. { $unwind: { path: '$finance_claims', preserveNullAndEmptyArrays: true } },
  220. { $unwind: { path: '$dictionary', preserveNullAndEmptyArrays: true } },
  221. // { $unwind: '$company', preserveNullAndEmptyArrays: true },
  222. // { $unwind: '$institution', preserveNullAndEmptyArrays: true },
  223. // { $unwind: '$finance_claims', preserveNullAndEmptyArrays: true },
  224. // { $unwind: '$dictionary', preserveNullAndEmptyArrays: true },
  225. { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } },
  226. { $project:
  227. {
  228. ensure_id: 1,
  229. person: 1,
  230. phone: 1,
  231. money: 1,
  232. claims_min_term: 1,
  233. claims_max_term: 1,
  234. mongey_min_rate: 1,
  235. mongey_max_rate: 1,
  236. when: 1,
  237. refuse_times: 1,
  238. additional_information: 1,
  239. status: 1,
  240. creditStatus: '$follow.creditStatus',
  241. jg_id: { $toObjectId: '$jg_id' },
  242. cid: { $toObjectId: '$cid' },
  243. uid: '$uid',
  244. xqId: { $toString: '$_id' },
  245. time: '$meta.createdAt',
  246. company: '$company',
  247. institution: '$institution',
  248. finance_claims: '$finance_claims',
  249. dictionary: '$dictionary',
  250. follow: '$follow',
  251. uuid: { $toObjectId: '$follow.uuid' },
  252. },
  253. },
  254. { $lookup:
  255. {
  256. from: 'other_user',
  257. localField: 'uuid',
  258. foreignField: '_id',
  259. as: 'otherUser',
  260. },
  261. },
  262. { $unwind: { path: '$otherUser', preserveNullAndEmptyArrays: true } },
  263. { $skip: (skip - 1) * limit },
  264. { $limit: limit },
  265. { $sort: { time: -1 } },
  266. ]);
  267. return { result, total };
  268. }
  269. // 金控端查询列表
  270. async jinkongList(data) {
  271. const query = {};
  272. if (data.id) { // 单查 对接需求id
  273. query._id = ObjectId(data.id);
  274. }
  275. if (data.jg_id) { // 金融机构ID
  276. query.jg_id = data.jg_id;
  277. }
  278. if (data.uid) { // 企业ID
  279. query.uid = data.uid;
  280. }
  281. if (data.cid) { // 产品id
  282. query.cid = data.cid;
  283. }
  284. if (data.status) { // 状态
  285. query.status = data.status;
  286. }
  287. const skip = Number.parseInt(data.skip) || 1;
  288. const limit = Number.parseInt(data.limit) || 10;
  289. // const total = await this.model.countDocuments(query);
  290. const total = await this.model.aggregate([
  291. { $match: query },
  292. { $project:
  293. {
  294. ensure_id: 1,
  295. person: 1,
  296. money: 1,
  297. claims_min_term: 1,
  298. claims_max_term: 1,
  299. mongey_min_rate: 1,
  300. mongey_max_rate: 1,
  301. when: 1,
  302. refuse_times: 1,
  303. additional_information: 1,
  304. status: 1,
  305. jg_id: { $toObjectId: '$jg_id' },
  306. cid: { $toObjectId: '$cid' },
  307. uid: '$uid',
  308. xqId: { $toString: '$_id' },
  309. time: '$meta.createdAt',
  310. },
  311. },
  312. { $lookup:
  313. {
  314. from: 'institution',
  315. localField: 'jg_id',
  316. foreignField: '_id',
  317. as: 'institution',
  318. },
  319. },
  320. { $lookup:
  321. {
  322. from: 't_finance_claims',
  323. localField: 'cid',
  324. foreignField: '_id',
  325. as: 'finance_claims',
  326. },
  327. },
  328. { $lookup:
  329. {
  330. from: 'dictionary',
  331. localField: 'ensure_id',
  332. foreignField: 'code',
  333. as: 'dictionary',
  334. },
  335. },
  336. { $lookup:
  337. {
  338. from: 'intelligent_follow',
  339. localField: 'xqId',
  340. foreignField: 'intelligentId',
  341. as: 'follow',
  342. },
  343. },
  344. { $unwind: '$institution' },
  345. { $unwind: '$finance_claims' },
  346. { $unwind: '$dictionary' },
  347. { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } },
  348. { $project:
  349. {
  350. ensure_id: 1,
  351. person: 1,
  352. money: 1,
  353. claims_min_term: 1,
  354. claims_max_term: 1,
  355. mongey_min_rate: 1,
  356. mongey_max_rate: 1,
  357. when: 1,
  358. refuse_times: 1,
  359. additional_information: 1,
  360. status: 1,
  361. creditStatus: '$follow.creditStatus',
  362. jg_id: { $toObjectId: '$jg_id' },
  363. cid: { $toObjectId: '$cid' },
  364. uid: '$uid',
  365. xqId: { $toString: '$_id' },
  366. time: '$meta.createdAt',
  367. company: '$company',
  368. institution: '$institution',
  369. finance_claims: '$finance_claims',
  370. dictionary: '$dictionary',
  371. follow: '$follow',
  372. },
  373. },
  374. { $sort: { time: -1 } },
  375. ]);
  376. const result = await this.model.aggregate([
  377. { $match: query },
  378. { $project:
  379. {
  380. ensure_id: 1,
  381. person: 1,
  382. money: 1,
  383. claims_min_term: 1,
  384. claims_max_term: 1,
  385. mongey_min_rate: 1,
  386. mongey_max_rate: 1,
  387. when: 1,
  388. refuse_times: 1,
  389. additional_information: 1,
  390. status: 1,
  391. jg_id: { $toObjectId: '$jg_id' },
  392. cid: { $toObjectId: '$cid' },
  393. uid: '$uid',
  394. xqId: { $toString: '$_id' },
  395. time: '$meta.createdAt',
  396. },
  397. },
  398. { $lookup:
  399. {
  400. from: 'company_identify',
  401. localField: 'uid',
  402. foreignField: 'uid',
  403. as: 'company',
  404. },
  405. },
  406. { $lookup:
  407. {
  408. from: 'institution',
  409. localField: 'jg_id',
  410. foreignField: '_id',
  411. as: 'institution',
  412. },
  413. },
  414. { $lookup:
  415. {
  416. from: 't_finance_claims',
  417. localField: 'cid',
  418. foreignField: '_id',
  419. as: 'finance_claims',
  420. },
  421. },
  422. { $lookup:
  423. {
  424. from: 'dictionary',
  425. localField: 'ensure_id',
  426. foreignField: 'code',
  427. as: 'dictionary',
  428. },
  429. },
  430. // { $lookup:
  431. // {
  432. // from: 'dictionary',
  433. // localField: 'when',
  434. // foreignField: 'code',
  435. // as: 'when',
  436. // },
  437. // },
  438. { $lookup:
  439. {
  440. from: 'intelligent_follow',
  441. localField: 'xqId',
  442. foreignField: 'intelligentId',
  443. as: 'follow',
  444. },
  445. },
  446. { $unwind: '$company' },
  447. { $unwind: '$institution' },
  448. { $unwind: '$finance_claims' },
  449. { $unwind: '$dictionary' },
  450. { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } },
  451. // { $unwind: { path: '$when', preserveNullAndEmptyArrays: true } },
  452. { $project:
  453. {
  454. ensure_id: 1,
  455. person: 1,
  456. money: 1,
  457. claims_min_term: 1,
  458. claims_max_term: 1,
  459. mongey_min_rate: 1,
  460. mongey_max_rate: 1,
  461. when: 1,
  462. refuse_times: 1,
  463. additional_information: 1,
  464. status: 1,
  465. creditStatus: '$follow.creditStatus',
  466. jg_id: { $toObjectId: '$jg_id' },
  467. cid: { $toObjectId: '$cid' },
  468. uid: '$uid',
  469. xqId: { $toString: '$_id' },
  470. time: '$meta.createdAt',
  471. company: '$company',
  472. institution: '$institution',
  473. finance_claims: '$finance_claims',
  474. dictionary: '$dictionary',
  475. follow: '$follow',
  476. },
  477. },
  478. { $skip: (skip - 1) * limit },
  479. { $limit: limit },
  480. { $sort: { time: -1 } },
  481. ]);
  482. return { result, total: total.length };
  483. }
  484. // 拒绝接口
  485. async refuse(data) {
  486. const { intelligentId, guanzhuid, userid, reason } = data;// intelligentId, userid, reason 需求id,银行id,拒绝理由
  487. const intelligent = await this.model.findById(intelligentId);// 对接需求
  488. const guanzhu = await this.fmodel.findById(guanzhuid);
  489. let res;
  490. if (intelligent.refuse_times < 3) {
  491. // 重新对接产品
  492. // 需求
  493. const condition = {
  494. orientation: intelligent.orientation,
  495. money: intelligent.money,
  496. claims_min_term: intelligent.claims_min_term,
  497. claims_max_term: intelligent.claims_max_term,
  498. mongey_min_rate: intelligent.mongey_min_rate,
  499. mongey_max_rate: intelligent.mongey_max_rate,
  500. ensure_id: intelligent.ensure_id,
  501. };
  502. // 对接产品
  503. const products = await this.findFinanceClaimsList(condition);
  504. const ids = intelligent.ids;
  505. if (products.length > 0) {
  506. // console.log('对接的产品', products);
  507. // 拒绝ids 里存在对接的产品id
  508. ids.push({ jr_id: intelligent.jg_id, reason });
  509. const items = products.filter(item => {
  510. if (ids.length === 0) {
  511. return true;
  512. }
  513. return !ids.map(item => {
  514. return item.jr_id;
  515. }).includes(item.uid);
  516. });
  517. console.log('对接的产品items', items);
  518. console.log('ids', ids);
  519. if (items.length > 0) {
  520. intelligent.cid = items[0]._id;
  521. intelligent.jg_id = items[0].uid;// 金融机构id
  522. const date = new Date();
  523. const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  524. intelligent.create_time = create_time;
  525. } else {
  526. // 拒绝后没有匹配的银行
  527. guanzhu.creditStatus = '3';
  528. await guanzhu.save();
  529. // 对接需求表状态
  530. intelligent.status = '1';
  531. await intelligent.save();
  532. this.ctx.service.viewnews.insertViewNews('智能对接', '无银行受理该需求,改需求已自动关闭', intelligent.uid);
  533. return '没有更多对接产品!';
  534. }
  535. // intelligent.ids.push({ jr_id: intelligent.jg_id, reason });
  536. intelligent.ids = ids;
  537. intelligent.refuse_times = intelligent.refuse_times + 1;
  538. if (intelligent.refuse_times === 3) {
  539. guanzhu.creditStatus = '3';
  540. guanzhu.refusemessage = reason;
  541. await guanzhu.save();
  542. // 对接需求表状态
  543. intelligent.status = '1';
  544. this.ctx.service.viewnews.insertViewNews('智能对接', '您已被拒绝3次,不再指派银行受理,请重新提交申请,详情请查看智能对接需求列表', intelligent.uid);
  545. }
  546. res = await intelligent.save();
  547. } else {
  548. return '没有更多对接产品!';
  549. }
  550. } else {
  551. res = '您已被拒绝3次,请重新提交申请';
  552. }
  553. return res;
  554. }
  555. }
  556. module.exports = IntelligentDockingService;