'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; class FinanceFollowService extends CrudService { constructor(ctx) { super(ctx, 't_finance_follow'); this.model = this.ctx.model.Financefollow; } //授信接口 async getCredit(data){ const { id,money,orcredit,senhemessage,jindiaomessage,sxcpname,sxhowlong,sxcplilue} = data;//id:关注ID money:授信额度 var now = new Date(); var nowtime =now.getTime();//当前时间戳(授信时间) const financefollow = await this.model.findById(id); if(senhemessage){ financefollow.senhemessage = senhemessage; } if(jindiaomessage){ financefollow.jindiaomessage = jindiaomessage; } if(sxcpname){ financefollow.sxcpname = sxcpname; } if(sxhowlong){ financefollow.sxhowlong = sxhowlong; } if(sxcplilue){ financefollow.sxcplilue = sxcplilue; } financefollow.orcredit = orcredit; if(money){ financefollow.credit_money = parseInt(money); } var creattime = new Date(financefollow.meta.createdAt).getTime();//创建时间时的时间戳 let accept_time = parseInt(nowtime)-parseInt(creattime);//授理时间(授信时间-创建时间) financefollow.accept_time=parseInt(accept_time); financefollow.credit_time=parseInt(nowtime); const res = await financefollow.save(); return res; } //授信关注债权列表 async getFollowList(data){ const match = {}; if (data.userid) {//金融机构ID match.uuid =data.userid; } if (data.finceType) {//债权和股权 match.finceType =data.finceType; } if(data.finalorc){ match.orcredit = data.finalorc; }else{ match.orcredit={ $ne: '1' }; } const skip = Number.parseInt(data.skip) || 1; const limit = Number.parseInt(data.limit) || 10; const total = await this.model.count(match); const res = await this.model.aggregate([ { $match: match}, { $project: { finceId: {$toObjectId: '$finceId'},savetime:'$meta.createdAt',orcredit:1}}, { $lookup: { from: 'claim_need', localField: 'finceId', foreignField: '_id', as: 'clamnew' } }, { $unwind: '$clamnew' }, { $lookup: { from: 'company_identify', localField: 'clamnew.userid', foreignField: 'uid', as: 'comnew' } }, { $unwind: '$comnew' }, { $project: {clamnew:1,finceId:1,savetime:1,orcredit:1,_id:1,comnewname:'$comnew.company_name'}}, { $skip: (skip - 1) * limit }, { $limit: limit }, { $sort : { savetime:-1 } } ]); const newres = {res,total}; return newres; } //授信关注股权列表 async getFollowstock(data){ const match = {}; if (data.userid) {//金融机构ID match.userid =data.userid; } if (data.finceType) {//债权和股权 match.finceType =data.finceType; } const skip = Number.parseInt(data.skip) || 1; const limit = Number.parseInt(data.limit) || 10; const total = await this.model.count(match); const res = await this.model.aggregate([ { $match: match}, { $project: { finceId: {$toObjectId: '$finceId'},savetime:'$meta.createdAt'}}, { $lookup: { from: 'stock_need', localField: 'finceId', foreignField: '_id', as: 'stockmnew' } }, { $unwind: '$stockmnew' }, { $skip: (skip - 1) * limit }, { $limit: limit }, { $sort : { savetime:-1 } } ]); const newres = {res,total}; return newres; } //授信债权历史列表 async getFollowClaimHistory(data){ const match = {}; if (data.userid) {//金融机构ID match.userid =data.userid; }else if(data.userid){ match.uuid =data.uid; } match.finceType ='0'; match.orcredit='1'; const skip = Number.parseInt(data.skip) || 1; const limit = Number.parseInt(data.limit) || 10; const total = await this.model.count(match); const res = await this.model.aggregate([ { $match: match}, { $project: { finceId: {$toObjectId: '$finceId'},savetime:'$meta.createdAt',credit_money:1,credit_time:1}}, { $lookup: { from: 'claim_need', localField: 'finceId', foreignField: '_id', as: 'clamnew' } }, { $unwind: '$clamnew' }, { $skip: (skip - 1) * limit }, { $limit: limit }, { $sort : { savetime:-1 } } ]); const newres = {res,total}; return newres; } //首页对接成功列表(详情) async getFirstsuccess(data){ const match = {}; if (data.finid) {//列表ID match._id =ObjectId(data.finid); } match.finceType ='0'; match.orcredit='1'; const skip = Number.parseInt(data.skip) || 1; const limit = Number.parseInt(data.limit) || 10; const total = await this.model.count(match); const res = await this.model.aggregate([ { $match: match}, { $project: { finceId: {$toObjectId: '$finceId'},userid:{$toObjectId: '$userid'},savetime:'$meta.createdAt',credit_money:1,credit_time:1}}, { $lookup: { from: 'claim_need', localField: 'finceId', foreignField: '_id', as: 'clamnew' } }, { $unwind: '$clamnew' }, { $lookup: { from: 'institution', localField: 'userid', foreignField: '_id', as: 'jgnew' } }, { $unwind: '$jgnew' }, { $project: { finceId: {$toObjectId: '$finceId'},userid:{$toObjectId: '$userid'},qyid:{$toObjectId: '$clamnew.userid'},jgname:'$jgnew.name'}}, { $lookup: { from: 'company_user', localField: 'qyid', foreignField: '_id', as: 'qynew' } }, { $unwind: '$qynew' }, { $skip: 0 }, { $limit: 3 }, { $sort : { savetime:-1 } } ]); const newres = {res,total}; return newres; } async getFinceNews(data){ const res = await this.model.aggregate([ { $match: {finceId:data.finceId}}, { $project: { userid: {$toObjectId: '$userid'},credit_money:1,meta:1,credit_time:1,orcredit:1}}, { $lookup: { from: 'institution', localField: 'userid', foreignField: '_id', as: 'innew' } }, { $unwind: '$innew' }, { $skip: 0 }, { $limit: 2 }, { $sort : { orcredit:-1 } } ]);//.findOne({finceId:data.finceId}); return res; } async getThreenews(){ const res = await this.model.aggregate([ { $match: {'orcredit':'1'}}, { $project: { xqId: {$toObjectId: '$finceId'},jgId: {$toObjectId: '$userid'}}}, { $lookup: { from: 'claim_need', localField: 'xqId', foreignField: '_id', as: 'clamnew' } }, { $unwind: '$clamnew' }, { $lookup: { from: 'institution', localField: 'jgId', foreignField: '_id', as: 'jgnew' } }, { $unwind: '$jgnew' }, { $project: { qyId: {$toObjectId: '$clamnew.userid'},jgname:'$jgnew.name'}}, { $lookup: { from: 'company_user', localField: 'qyId', foreignField: '_id', as: 'qynew' } }, { $unwind: '$qynew' }, { $project: { qyname: '$qynew.company_name',jgname:1}}, { $skip: 0}, { $limit: 3 } ]); return res; } } module.exports = FinanceFollowService;