123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const moment = require('moment');
- class SmartdockingService extends CrudService {
- constructor(ctx) {
- super(ctx, 'smartdocking');
- this.model = this.ctx.model.Smartdocking;
- }
- // 门户网站银企对接方法
- async docking(data) {
- const { company_name, phone, passwd, type, claims_money, claims_term, ensure_id } = data;
- let company = '';
- // 如果企业未注册,创建企业用户
- if (type === '0') {
- company = await this.ctx.service.companyuser.create({ company_name, phone, passwd });
- }
- // 如果企业已注册,登录企业用户
- else if (type === '1') {
- company = await this.ctx.service.companyuser.loginss({ phone, passwd });
- // 将企业用户的企业名称放入数据中
- data.company_name = company.company_name;
- }
- // 将企业用户的id放入数据中
- const uid = company._id;
- data.uid = uid;
- // 将贷款期限转为数字类型并与相应的数比较得出传入查询债权产品方法的参数
- let claims_max_term;
- const _claims_term = Number(claims_term);
- if (_claims_term <= 3) {
- claims_max_term = '3';
- } else if (_claims_term <= 6) {
- claims_max_term = '6';
- } else if (_claims_term <= 9) {
- claims_max_term = '9';
- } else if (_claims_term <= 12) {
- claims_max_term = '12';
- } else if (_claims_term > 12) {
- claims_max_term = '13';
- }
- // 将贷款金额转为数字类型并与相应的数比较得出传入查询债权产品方法的参数
- let claims_max_money;
- const _claims_money = Number(claims_money);
- if (_claims_money <= 100) {
- claims_max_money = '100';
- } else if (_claims_money <= 200) {
- claims_max_money = '200';
- } else if (_claims_money <= 500) {
- claims_max_money = '500';
- } else if (_claims_money <= 1000) {
- claims_max_money = '1000';
- } else if (_claims_money > 1000) {
- claims_max_money = '1001';
- }
- // 通过查询债权产品接口用条件查询出相应的债权产品
- const condition = { userid: uid, claims_max_term, claims_max_money, ensure_id };
- const products = await this.ctx.service.searchauto.financeclaimssearch(condition);
- const result_info = [];
- // 遍历查询的到的债权产品
- for (const product of products.data) {
- const productid = product.id;
- // 根据担保方式code查询担保方式名称
- const ensure_name = await this.ctx.service.dictionary.query({ code: product.ensure_id, type: 'db' });
- if (ensure_name) {
- product.ensure_id = ensure_name[0].name;
- }
- // 取得查询到的债权产品的最大贷款额度与传入的贷款额度条件相减取绝对值算出贷款金额的匹配度
- let match_money = 0;
- const minus_money = Math.abs(Number(product.claims_max_money) - _claims_money);
- if (minus_money <= 100) {
- match_money = 100;
- } else if (minus_money <= 300) {
- match_money = 80;
- } else if (minus_money <= 500) {
- match_money = 60;
- } else {
- match_money = 40;
- }
- // 取得查询到的债权产品的最大贷款期限与传入的贷款期限条件相减取绝对值算出贷款期限的匹配度
- let match_term = 0;
- const minus_term = Math.abs(Number(product.claims_max_term) - _claims_term);
- if (minus_term <= 1) {
- match_term = 100;
- } else if (minus_term <= 3) {
- match_term = 80;
- } else if (minus_term <= 5) {
- match_term = 60;
- } else {
- match_term = 40;
- }
- // 将贷款金额的匹配度和贷款期限的匹配度取平均数放入对应产品的匹配度字段
- const match = (match_money + match_term) / 2 + '%';
- product.match = match;
- result_info.push({ productid, match });
- }
- const date = new Date();
- const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
- data.create_time = create_time;
- data.result_info = result_info;
- await this.model.create(data);
- return products;
- }
- // 企业查询智能对接历史记录方法
- async companySearch({ uid }) {
- const dockings = await this.model.find({ uid });
- const data = [];
- for (const docking of dockings) {
- const create_time = docking.create_time;
- const claims_money = docking.claims_money;
- const claims_term = docking.claims_term;
- let ensure_id = docking.ensure_id;
- // 根据担保方式code查询担保方式名称
- const ensure_name = await this.ctx.service.dictionary.query({ code: ensure_id, type: 'db' });
- if (ensure_name) {
- ensure_id = ensure_name[0].name;
- }
- for (const result of docking.result_info) {
- const productid = result.productid;
- const product = await this.ctx.service.financeclaims.fetch(ObjectId(productid));
- const institution = await this.ctx.service.institution.fetch(ObjectId(product.uid));
- const res = { create_time, claims_money, claims_term, ensure_id, match: result.match, name: product.name, institution_name: institution.name };
- data.push(res);
- }
- }
- return { data, total: data.length };
- }
- }
- module.exports = SmartdockingService;
|