intelligentDocking.js 17 KB

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