'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, phone: 1, money: 1, claims_min_term: 1, claims_max_term: 1, mongey_min_rate: 1, mongey_max_rate: 1, when: 1, refuse_times: 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: 'intelligent_follow', localField: 'xqId', foreignField: 'intelligentId', as: 'follow', }, }, { $unwind: { path: '$company', preserveNullAndEmptyArrays: true } }, { $unwind: { path: '$institution', preserveNullAndEmptyArrays: true } }, { $unwind: { path: '$finance_claims', preserveNullAndEmptyArrays: true } }, { $unwind: { path: '$dictionary', preserveNullAndEmptyArrays: true } }, { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } }, { $project: { ensure_id: 1, person: 1, phone: 1, money: 1, claims_min_term: 1, claims_max_term: 1, mongey_min_rate: 1, mongey_max_rate: 1, when: 1, refuse_times: 1, additional_information: 1, status: 1, creditStatus: '$follow.creditStatus', jg_id: { $toObjectId: '$jg_id' }, cid: { $toObjectId: '$cid' }, uid: '$uid', xqId: { $toString: '$_id' }, time: '$meta.createdAt', company: '$company', institution: '$institution', finance_claims: '$finance_claims', dictionary: '$dictionary', follow: '$follow', uuid: { $toObjectId: '$follow.uuid' }, }, }, { $lookup: { from: 'other_user', localField: 'uuid', foreignField: '_id', as: 'otherUser', }, }, { $unwind: { path: '$otherUser', 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 ids.push({ jr_id: intelligent.jg_id, reason }); const items = products.filter(item => { if (ids.length === 0) { return true; } return !ids.map(item => { return item.jr_id; }).includes(item.uid); }); console.log('对接的产品items', items); console.log('ids', ids); 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 { // 拒绝后没有匹配的银行 guanzhu.creditStatus = '3'; await guanzhu.save(); // 对接需求表状态 intelligent.status = '1'; await intelligent.save(); this.ctx.service.viewnews.insertViewNews('智能对接', '无银行受理该需求,改需求已自动关闭', intelligent.uid); return '没有更多对接产品!'; } // intelligent.ids.push({ jr_id: intelligent.jg_id, reason }); intelligent.ids = ids; 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;