123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- '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 IntelligentDockingService extends CrudService {
- constructor(ctx) {
- super(ctx, 'intelligent_docking');
- this.model = this.ctx.model.IntelligentDocking;
- this.fmodel = this.ctx.model.IntelligentFollow;
- this.tmodel = this.ctx.model.Tfinanceclaims;
- }
- // 小程序 银企对接接口
- async intelligentDocking(data) {
- // const { uid, company_name, code, person, opening_bank, orientation, money, claims_min_term, claims_max_term,
- // mongey_min_rate, mongey_max_rate, ensure_id, when, additional_information } = data;
- const { uid, company_name, person, phone, money, ensure_id, orientation, additional_information } = data;
- assert(uid, company_name && person && phone && money && ensure_id && orientation && additional_information, '缺少部分信息项');
- // 判断该企业是否有在进行中的需求(一个企业只能有一个进行中的需求)
- // const companyList = await this.model.find({ uid, status: '0' });
- const companyList = await this.model.aggregate([
- { $match: { uid } },
- { $project:
- {
- uid: 1,
- xqId: { $toString: '$_id' },
- },
- },
- { $lookup:
- {
- from: 'intelligent_follow',
- localField: 'xqId',
- foreignField: 'intelligentId',
- as: 'intelligent',
- },
- },
- { $unwind: { path: '$intelligent' } },
- {
- $project:
- {
- xqid: 1,
- uid: 1,
- creditStatus: '$intelligent.creditStatus',
- },
- },
- {
- $match:
- {
- $expr:
- {
- $and:
- [{
- $ne: [ '$creditStatus', '1' ],
- },
- {
- $ne: [ '$creditStatus', '3' ],
- },
- ],
- },
- },
- },
- ]);
- if (companyList.length > 0) {
- return '您有一个正在进行中的对接需求,请完成后再近些申请';
- }
- // 需求
- const condition = { orientation, money, ensure_id };
- // 对接产品
- const products = await this.findFinanceClaimsList(condition);
- const date = new Date();
- const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
- data.create_time = create_time;
- let res;
- if (products.length > 0) {
- console.log(products[0]);
- data.cid = products[0]._id;
- data.jg_id = products[0].uid;// 金融机构id
- try {
- res = await this.model.create(data);
- } catch (error) {
- console.log(error);
- res = error;
- }
- } else {
- res = '没有符合条件的对接产品,请修改对接需求';
- }
- return res;
- }
- // 根据对接条件查询产品
- async findFinanceClaimsList(data) {
- // opening_bank 传的是金融机构的id
- const { orientation, money, ensure_id } = data;
- // 已发布的产品
- const match = { status: '1' };
- // 担保方式【企业选择】、贷款额度【企业选择】+融资取向对产品进行筛选
- // 担保方式
- if (ensure_id) {
- match.ensure_id = ensure_id;
- }
- // 贷款额度
- if (money) {
- match.claims_max_money = { $gte: parseInt(money) };
- }
- // 融资取向 按第一个取向查询,如果没有结果,按第二个取向查...
- const cattributeMatch = { status: '1' };
- if (orientation && orientation.length > 0) {
- for (const item of orientation) {
- cattributeMatch.cattribute = { $in: item };
- const res1 = await this.tmodel.find(cattributeMatch);
- if (res1.length > 0) {
- match.cattribute = { $in: item };
- break;
- }
- }
- }
- console.log('match', match);
- const _sort = {};
- // 排序
- if (orientation && orientation[0] === '2501') {
- _sort.claims_max_term = -1;
- }
- if (orientation && orientation[0] === '2503') {
- _sort.mongey_min_rate = 1;
- }
- if (orientation && orientation[0] === '2505') {
- _sort.claims_max_money = -1;
- }
- const res = await this.tmodel.find(match).sort(_sort).limit(Number(5));
- return res;
- }
- // 查询对接记录(金融机构,企业)
- async dockingFinance(data) {
- const query = {};
- if (data.id) { // 单查 对接需求id
- query._id = ObjectId(data.id);
- }
- if (data.jg_id) { // 金融机构ID
- query.jg_id = data.jg_id;
- }
- if (data.uid) { // 企业ID
- query.uid = data.uid;
- }
- if (data.cid) { // 产品id
- query.cid = data.cid;
- }
- if (data.status) { // 状态
- query.status = data.status;
- }
- const skip = Number.parseInt(data.skip) || 1;
- const limit = Number.parseInt(data.limit) || 10;
- const total = await this.model.countDocuments(query);
- const result = await this.model.aggregate([
- { $match: query },
- { $project:
- {
- ensure_id: 1,
- person: 1,
- money: 1,
- claims_min_term: 1,
- claims_max_term: 1,
- mongey_min_rate: 1,
- mongey_max_rate: 1,
- when: 1,
- additional_information: 1,
- status: 1,
- jg_id: { $toObjectId: '$jg_id' },
- cid: { $toObjectId: '$cid' },
- uid: '$uid',
- xqId: { $toString: '$_id' },
- time: '$meta.createdAt',
- },
- },
- { $lookup:
- {
- from: 'company_identify',
- localField: 'uid',
- foreignField: 'uid',
- as: 'company',
- },
- },
- { $lookup:
- {
- from: 'institution',
- localField: 'jg_id',
- foreignField: '_id',
- as: 'institution',
- },
- },
- { $lookup:
- {
- from: 't_finance_claims',
- localField: 'cid',
- foreignField: '_id',
- as: 'finance_claims',
- },
- },
- { $lookup:
- {
- from: 'dictionary',
- localField: 'ensure_id',
- foreignField: 'code',
- as: 'dictionary',
- },
- },
- { $lookup:
- {
- from: 'dictionary',
- localField: 'when',
- foreignField: 'code',
- as: 'when',
- },
- },
- { $lookup:
- {
- from: 'intelligent_follow',
- localField: 'xqId',
- foreignField: 'intelligentId',
- as: 'follow',
- },
- },
- { $unwind: '$company' },
- { $unwind: '$institution' },
- { $unwind: '$finance_claims' },
- { $unwind: '$dictionary' },
- { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } },
- { $unwind: { path: '$when', preserveNullAndEmptyArrays: true } },
- { $skip: (skip - 1) * limit },
- { $limit: limit },
- { $sort: { time: -1 } },
- ]);
- return { result, total };
- }
- // 拒绝接口
- async refuse(data) {
- const { intelligentId, guanzhuid, userid, reason } = data;// intelligentId, userid, reason 需求id,银行id,拒绝理由
- const intelligent = await this.model.findById(intelligentId);// 对接需求
- const guanzhu = await this.fmodel.findById(guanzhuid);
- let res;
- if (intelligent.refuse_times < 3) {
- // 重新对接产品
- // 需求
- const condition = {
- orientation: intelligent.orientation,
- money: intelligent.money,
- claims_min_term: intelligent.claims_min_term,
- claims_max_term: intelligent.claims_max_term,
- mongey_min_rate: intelligent.mongey_min_rate,
- mongey_max_rate: intelligent.mongey_max_rate,
- ensure_id: intelligent.ensure_id,
- };
- // 对接产品
- const products = await this.findFinanceClaimsList(condition);
- const ids = intelligent.ids;
- if (products.length > 0) {
- console.log('对接的产品', products);
- // 拒绝ids 里存在对接的产品id
- const items = products.filter(item => {
- if (ids.length === 0) {
- return true;
- }
- return !ids.map(item => {
- return item.jr_id;
- }).includes(item.uid);
- });
- if (items.length > 0) {
- intelligent.cid = items[0]._id;
- intelligent.jg_id = items[0].uid;// 金融机构id
- const date = new Date();
- const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
- intelligent.create_time = create_time;
- } else {
- return '没有更多对接产品!';
- }
- intelligent.ids.push({ jr_id: userid, reason });
- intelligent.refuse_times = intelligent.refuse_times + 1;
- if (intelligent.refuse_times === 3) {
- guanzhu.creditStatus = '3';
- guanzhu.refusemessage = reason;
- await guanzhu.save();
- // 对接需求表状态
- intelligent.status = '1';
- this.ctx.service.viewnews.insertViewNews('智能对接', '您已被拒绝3次,不再指派银行受理,请重新提交申请,详情请查看智能对接需求列表', intelligent.uid);
- }
- res = await intelligent.save();
- } else {
- return '没有更多对接产品!';
- }
- } else {
- res = '您已被拒绝3次,请重新提交申请';
- }
- return res;
- }
- }
- module.exports = IntelligentDockingService;
|