'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 ClaimNeedService extends CrudService { constructor(ctx) { super(ctx, 'claim_need'); this.model = this.ctx.model.Claimneed; this.followmodel = this.ctx.model.Financefollow; } /*//根据金融机构查询债权需求(列表)--分为指向(传金融机构ID)和非指向(不用传) async getFclaim(data){ const match = {}; match.status='0'; if (data.jg_id) {//金融机构ID match.jg_id =data.jg_id; }else{ match.jg_id ='0'; } 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.find(match).sort({'meta.createdAt':-1}).limit(limit).skip((skip - 1) * limit); const newres = {res,total}; return newres; }*/ //根据金融机构查询债权需求(列表)--分为指向(传金融机构ID)和非指向(不用传) async getFclaim(data){ const match = {}; match.status='0'; if (data.jg_id) {//金融机构ID match.jg_id =data.jg_id; }else{ match.jg_id ='0'; } 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.find(match).sort({'meta.createdAt':-1}).limit(limit).skip((skip - 1) * limit);*/ const res = await this.model.aggregate([ { $project: { meta: 1, jg_id: 1, jg_pro_id: 1, status: 1, money: 1, use: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, ensure_id: 1, project_status: 1, remarks: 1, cdata: 1, userid: 1, id: { $toString: "$_id" }, demand_id: { $toString: "$_id" }, } }, { $lookup: { from: "t_urge_handle", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [{ $eq: ['$type', "1"] }, { $eq: ['$demand_id', "$$demand_id"] }, ] } } }, ], as: "t_urge_handles" } }, { $project: { meta: 1, jg_id: 1, jg_pro_id: 1, status: 1, money: 1, use: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, ensure_id: 1, project_status: 1, remarks: 1, cdata: 1, userid: 1, id: 1, is_exist: { $cond: { if : { $eq: [{ $size: "$t_urge_handles" }, 0] }, then: "2", else : "1" } }, } }, { $lookup: { from: 'company_identify', localField: 'userid', foreignField: 'uid', as: 'comnew' } }, { $unwind: '$comnew' }, { $lookup: { from: 't_finance_follow', localField: 'id', foreignField: 'finceId', as: 'follnew' } }, { $unwind: { path: '$follnew', preserveNullAndEmptyArrays: true } }, { $project: { meta: 1, jg_id: 1, jg_pro_id: 1, status: 1, money: 1, use: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, ensure_id: 1, project_status: 1, remarks: 1, cdata: 1, userid: 1, id: 1, is_exist: 1, comnewname:'$comnew.company_name', orcredit:'$follnew.orcredit' } }, { $match: match, }, { $sort: {'meta.createdAt':-1}, }, { $skip: (skip - 1) * limit, }, { $limit: limit, }, ]); const totalTemp = await this.model.aggregate([ { $project: { meta: 1, jg_id: 1, jg_pro_id: 1, status: 1, money: 1, use: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, ensure_id: 1, project_status: 1, remarks: 1, cdata: 1, userid: 1, id: { $toString: "$_id" }, demand_id: { $toString: "$_id" }, } }, { $lookup: { from: "t_urge_handle", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [{ $eq: ['$type', "1"] }, { $eq: ['$demand_id', "$$demand_id"] }, ] } } }, ], as: "t_urge_handles" } }, { $project: { meta: 1, jg_id: 1, jg_pro_id: 1, status: 1, money: 1, use: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, ensure_id: 1, project_status: 1, remarks: 1, cdata: 1, userid: 1, id: 1, is_exist: { $cond: { if : { $eq: [{ $size: "$t_urge_handles" }, 0] }, then: "2", else : "1" } }, } }, { $match: match, }, { $count: 'total', }, ]); if (totalTemp.length == 0) { totalTemp.push({ total: 0 }); } const [{ total }] = totalTemp; const newres = {res,total}; return newres; } //根据企业用户查询债权需求(列表) async getQyclaim(data){ const{qyid} = data; let match={}; match.userid=qyid; if(data.money){ match.money = data.money; } if(data.status){ match.status = data.status; } 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.find(match).sort({'meta.createdAt':-1}).limit(limit).skip((skip - 1) * limit); const result = {total,res} return result; } async getLittleqyclaim(data){ const{qyid} = data; let match={}; match.userid=qyid; match.jg_id ={$ne:'0'}; match.status='0'; const skip = Number.parseInt(data.skip) || 1; const limit = Number.parseInt(data.limit) || 10; const totalres = await this.model.aggregate([ { $match: match}, { $project: { jgid: {$toObjectId: '$jg_id'},id:{$toString: '$_id'},jgproid:{$toObjectId: '$jg_pro_id'}}}, { $lookup: { from: 't_finance_follow', localField: 'id', foreignField: 'finceId', as: 'follonew' } }, { $unwind: '$follonew' }, { $count:'total'} ]); let total=0 if(totalres.length>0){ total= totalres[0].total; } const res = await this.model.aggregate([ { $match: match}, { $project: { jgid: {$toObjectId: '$jg_id'},id:{$toString: '$_id'},jgproid:{$toObjectId: '$jg_pro_id'}}}, { $lookup: { from: 't_finance_follow', localField: 'id', foreignField: 'finceId', as: 'follonew' } }, { $unwind: '$follonew' }, { $lookup: { from: 'institution', localField: 'jgid', foreignField: '_id', as: 'instinew' } }, { $unwind: '$instinew' }, { $lookup: { from: 't_finance_claims', localField: 'jgproid', foreignField: '_id', as: 'tfnews' } }, { $unwind: '$tfnews' }, {$project: { uuid: {$toObjectId: '$follonew.uuid'},_id:1,cpname:'$tfnews.name',logo:'$instinew.name',orcredit:'$follonew.orcredit'}}, { $lookup: { from: 'other_user', localField: 'uuid', foreignField: '_id', as: 'othernews' } }, { $unwind: '$othernews' }, {$project:{_id:1,cpname:1,logo:1,orcredit:1,cpjlname:'$othernews.name',phone:'$othernews.phone'}}, { $skip: (skip - 1) * limit }, { $limit: limit }, ]); const result = {total,res} return result; } //金融机构关注债权需求 async getFollowClaim(data){ const { finceId, userid,uuid } = data;//债权需求ID,金融机构金融机构ID const newdata={}; newdata.finceType = '0'; newdata.finceId = finceId; newdata.userid = userid; newdata.uuid = uuid; newdata.orcredit = '0'; newdata.credit_money = 0; newdata.accept_time = 0; newdata.credit_time = 0; const res = await this.followmodel.create(newdata); const qy = await this.model.findById(finceId); this.ctx.service.viewnews.insertViewNews("债权需求信息","您有新的债权需求被关注,请查看需求列表",qy.userid); return res; } //债权需求详情 async getOne(data){ const { id } = data;//债权需求ID const financ= await this.model.findById(id); console.log(JSON.stringify(financ)) if(financ.jg_pro_id === '0'){ const res = await this.model.aggregate([ { $match: {_id:ObjectId(id)}}, { $lookup: { from: 'dictionary', localField: 'ensure_id', foreignField: 'code', as: 'dictnew' } }, { $unwind: '$dictnew' }, { $project: {money:1,use:1,mongey_min_rate:1,jg_pro_id:1,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,project_status:1,remarks:1, hyname:1,cdata:1,folid:{$toString:'$_id'}} },//hyname(担保名称) { $lookup: { from: 't_finance_follow', localField: 'folid', foreignField: 'finceId', as: 'follownew' } }, { $unwind: '$follownew' }, { $project: { jg_pro_id:1,money:1,use:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,project_status:1,remarks:1, hyname:1,cdata:1,folid:{$toString:'$_id'},jgpronew:1,sxcplilue:'$follownew.sxcplilue',sxcpname:'$follownew.sxcpname',sxhowlong:'$follownew.sxhowlong',credit_money:'$follownew.credit_money'}} ]); return res; }else{ const resmore = await this.model.aggregate([ { $match: {_id:ObjectId(id)}}, { $lookup: { from: 'dictionary', localField: 'ensure_id', foreignField: 'code', as: 'dictnew' } }, { $unwind: '$dictnew' }, { $project: { jg_pro_id: {$toObjectId: '$jg_pro_id'},money:1,use:1,mongey_min_rate:1,project_status:1,remarks:1, hyname:'$dictnew.name',cdata:1,finuserid:'$userid',userid:{$toObjectId: '$userid'}} }, { $lookup: { from: 't_finance_claims', localField: 'jg_pro_id', foreignField: '_id', as: 'jgpronew' } }, { $unwind: '$jgpronew' }, { $project: { jg_pro_id:1,money:1,use:1,project_status:1,remarks:1, hyname:1,cdata:1,folid:{$toString:'$_id'},jgpronew:1,finuserid:1,userid:1}}, { $lookup: { from: 'company_user', localField: 'userid', foreignField: '_id', as: 'cusernew' } }, { $unwind: { path: '$cusernew', preserveNullAndEmptyArrays: true } }, { $lookup: { from: 'company_identify', localField: 'finuserid', foreignField: 'uid', as: 'cotifynew' } }, { $unwind: { path: '$cotifynew', preserveNullAndEmptyArrays: true } }, { $lookup: { from: 't_finance_follow', localField: 'folid', foreignField: 'finceId', as: 'follownew' } }, { $unwind: { path: '$follownew', preserveNullAndEmptyArrays: true } }, { $project: { jg_pro_id:1,money:1,use:1,project_status:1,remarks:1, hyname:1,cdata:1,folid:{$toString:'$_id'},jgpronew:1,sxcplilue:'$follownew.sxcplilue', sxcpname:'$follownew.sxcpname',sxhowlong:'$follownew.sxhowlong',credit_money:'$follownew.credit_money', finuserid:1,userphone:'$cusernew.phone',usernnmae:'$cusernew.person',cotifynew:1}} ]); return resmore; } } //关注前接口 async getBeforFollow(data){ const { finceId, userid } = data;//债权需求ID,金融机构ID const match = {}; if (userid) {//金融机构ID match.userid =userid; } if (finceId) {//需求ID match.finceId =finceId; } const result = {}; const res = await this.followmodel.find(match); if(res.length>0){ result.finstatus='SUCCESS' }else{ result.finstatus='ERROR' } return result; } } module.exports = ClaimNeedService;