123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- '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.logo',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:'$dictnew.name',cdata:1} }//hyname(担保名称)
- ]);
- 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,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,project_status:1,remarks:1,
- hyname:'$dictnew.name',cdata:1} },
- { $lookup: {
- from: 't_finance_claims',
- localField: 'jg_pro_id',
- foreignField: '_id',
- as: 'jgpronew' } },
- { $unwind: '$jgpronew' }
- ]);
- 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;
|