smartdocking.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 SmartdockingService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'smartdocking');
  11. this.model = this.ctx.model.Smartdocking;
  12. }
  13. // 门户网站银企对接方法
  14. async docking(data) {
  15. const { company_name, phone, passwd, type, claims_money, claims_term, ensure_id } = data;
  16. let company = '';
  17. // 如果企业未注册,创建企业用户
  18. if (type === '0') {
  19. company = await this.ctx.service.companyuser.create({ company_name, phone, passwd });
  20. }
  21. // 如果企业已注册,登录企业用户
  22. else if (type === '1') {
  23. company = await this.ctx.service.companyuser.loginss({ phone, passwd });
  24. // 将企业用户的企业名称放入数据中
  25. data.company_name = company.company_name;
  26. }
  27. // 将企业用户的id放入数据中
  28. const uid = company._id;
  29. data.uid = uid;
  30. // 将贷款期限转为数字类型并与相应的数比较得出传入查询债权产品方法的参数
  31. let claims_max_term;
  32. const _claims_term = Number(claims_term);
  33. if (_claims_term <= 3) {
  34. claims_max_term = '3';
  35. } else if (_claims_term <= 6) {
  36. claims_max_term = '6';
  37. } else if (_claims_term <= 9) {
  38. claims_max_term = '9';
  39. } else if (_claims_term <= 12) {
  40. claims_max_term = '12';
  41. } else if (_claims_term > 12) {
  42. claims_max_term = '13';
  43. }
  44. // 将贷款金额转为数字类型并与相应的数比较得出传入查询债权产品方法的参数
  45. let claims_max_money;
  46. const _claims_money = Number(claims_money);
  47. if (_claims_money <= 100) {
  48. claims_max_money = '100';
  49. } else if (_claims_money <= 200) {
  50. claims_max_money = '200';
  51. } else if (_claims_money <= 500) {
  52. claims_max_money = '500';
  53. } else if (_claims_money <= 1000) {
  54. claims_max_money = '1000';
  55. } else if (_claims_money > 1000) {
  56. claims_max_money = '1001';
  57. }
  58. // 通过查询债权产品接口用条件查询出相应的债权产品
  59. const condition = { userid: uid, claims_max_term, claims_max_money, ensure_id };
  60. const products = await this.ctx.service.searchauto.financeclaimssearch(condition);
  61. const result_info = [];
  62. // 遍历查询的到的债权产品
  63. for (const product of products.data) {
  64. const productid = product.id;
  65. // 根据担保方式code查询担保方式名称
  66. const ensure_name = await this.ctx.service.dictionary.query({ code: product.ensure_id, type: 'db' });
  67. if (ensure_name) {
  68. product.ensure_id = ensure_name[0].name;
  69. }
  70. // 取得查询到的债权产品的最大贷款额度与传入的贷款额度条件相减取绝对值算出贷款金额的匹配度
  71. let match_money = 0;
  72. const minus_money = Math.abs(Number(product.claims_max_money) - _claims_money);
  73. if (minus_money <= 100) {
  74. match_money = 100;
  75. } else if (minus_money <= 300) {
  76. match_money = 80;
  77. } else if (minus_money <= 500) {
  78. match_money = 60;
  79. } else {
  80. match_money = 40;
  81. }
  82. // 取得查询到的债权产品的最大贷款期限与传入的贷款期限条件相减取绝对值算出贷款期限的匹配度
  83. let match_term = 0;
  84. const minus_term = Math.abs(Number(product.claims_max_term) - _claims_term);
  85. if (minus_term <= 1) {
  86. match_term = 100;
  87. } else if (minus_term <= 3) {
  88. match_term = 80;
  89. } else if (minus_term <= 5) {
  90. match_term = 60;
  91. } else {
  92. match_term = 40;
  93. }
  94. // 将贷款金额的匹配度和贷款期限的匹配度取平均数放入对应产品的匹配度字段
  95. const match = (match_money + match_term) / 2 + '%';
  96. product.match = match;
  97. result_info.push({ productid, match });
  98. }
  99. const date = new Date();
  100. const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  101. data.create_time = create_time;
  102. data.result_info = result_info;
  103. await this.model.create(data);
  104. return products;
  105. }
  106. // 企业查询智能对接历史记录方法
  107. async companySearch({ uid }) {
  108. const dockings = await this.model.find({ uid });
  109. const data = [];
  110. for (const docking of dockings) {
  111. const create_time = docking.create_time;
  112. const claims_money = docking.claims_money;
  113. const claims_term = docking.claims_term;
  114. let ensure_id = docking.ensure_id;
  115. // 根据担保方式code查询担保方式名称
  116. const ensure_name = await this.ctx.service.dictionary.query({ code: ensure_id, type: 'db' });
  117. if (ensure_name) {
  118. ensure_id = ensure_name[0].name;
  119. }
  120. for (const result of docking.result_info) {
  121. const productid = result.productid;
  122. const product = await this.ctx.service.financeclaims.fetch(ObjectId(productid));
  123. const institution = await this.ctx.service.institution.fetch(ObjectId(product.uid));
  124. const res = { create_time, claims_money, claims_term, ensure_id, match: result.match, name: product.name, institution_name: institution.name };
  125. data.push(res);
  126. }
  127. }
  128. return { data, total: data.length };
  129. }
  130. }
  131. module.exports = SmartdockingService;