'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 TNewAssignService extends CrudService { constructor(ctx) { super(ctx, 't_new_assign'); this.model = this.ctx.model.TNewAssign; this.cModel = this.ctx.model.Claimneed; this.iModel = this.ctx.model.IntelligentDocking; } // 重写创建方法 async create(data) { const {type, userid,product_name} = data; const res = await this.model.create(data); let tempMsg = ''; switch(type) { case '1': tempMsg = '债权需求'; break; case '2': tempMsg = '融资需求'; break; } let msg = await this.ctx.service.viewnews.insertViewNews('指派操作',`您申请的${tempMsg}已指派为新的金融产品${product_name},请知晓。`,userid); return data; } // 条件查询 async select(payload) { let {skip = 0, limit = 10} = payload; let match = {}; if (payload.company_name) { match.company_name = {$regex: payload.company_name, $options: '$i'}; } if (payload.institution_name) { match.institution_name = {$regex: payload.institution_name, $options: '$i'}; } if (payload.product_name) { match.product_name = {$regex: payload.product_name, $options: '$i'}; } if (payload.is_exist) { match.is_exist = payload.is_exist; } if (payload.demand_id) { match.demand_id = payload.demand_id; } if (payload.government_name) { match[`information.government_name`] = {$regex: payload.government_name, $options: '$i'}; } if (payload.status) { match[`information.status`] = payload.status; } if (payload.company_type) { match[`information.company_type`] = payload.company_type; } let sort = {}; if (payload.sort && payload.asc) { sort[payload.sort] = Number.parseInt(payload.asc); } else { sort = {'meta.createdAt': -1}; } let dataAgg = [ { $match: { 'status': '0' }, }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 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, cdata: 1, ensure_id: 1, meta: 1, } }, { $lookup: { from: 'company_identify', localField: 'userid', foreignField: 'uid', as: 'company_identifys', }, }, { $unwind: { path: '$company_identifys', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "institution", let: { jg_id: "$jg_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [{ $toString: "$_id" }, "$$jg_id"] }, ] } } }, ], as: "institutions" } }, { $unwind: { path: '$institutions', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "t_finance_claims", let: { jg_pro_id: "$jg_pro_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [{ $toString: "$_id" }, "$$jg_pro_id"] }, ] } } }, ], as: "t_finance_claimss" } }, { $unwind: { path: '$t_finance_claimss', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "dictionary", let: { ensure_id: "$ensure_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$type', "db"] }, { $eq: ['$code', "$$ensure_id"] }, ] } } }, ], as: "dictionarys" } }, { $unwind: { path: '$dictionarys', preserveNullAndEmptyArrays: false, }, }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 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, cdata: 1, ensure_id: 1, ensure_name:'$dictionarys.name', company_name: '$company_identifys.company_name', institution_name: '$institutions.name', product_name: '$t_finance_claimss.name', demand_id: { $toString: "$_id" }, meta: 1, } }, { $lookup: { from: "t_new_assign", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$type', "1"] }, { $eq: ['$demand_id', "$$demand_id"] }, ] } } }, ], as: "t_new_assigns" } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 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, cdata: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: { $cond: { if : { $eq: [{ $size: "$t_new_assigns" }, 0] }, then: "2", else : "1" } }, information: { $cond: { if : { $eq: [{ $size: "$t_new_assigns" }, 0] }, then: {}, else : { $arrayElemAt: [ "$t_new_assigns", 0 ] }, } }, meta: 1, } }, { $lookup: { from: "t_finance_follow", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$finceType', "0"] }, { $eq: ['$finceId', "$$demand_id"] }, ] } } }, ], as: "t_finance_follows" } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 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, cdata: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: 1, information: 1, meta: 1, finance_follow: { $cond: { if : { $eq: [{ $size: "$t_finance_follows" }, 0] }, then: {orcredit:"9"}, else : { $arrayElemAt: [ "$t_finance_follows", 0 ] }, } }, } }, { $match: match, }, { $sort: sort, }, { $skip: Number.parseInt(skip), }, { $limit:Number.parseInt(limit), }, ]; let totalAgg = [ { $match: { 'status': '0' }, }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 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, cdata: 1, ensure_id: 1, meta: 1, } }, { $lookup: { from: 'company_identify', localField: 'userid', foreignField: 'uid', as: 'company_identifys', }, }, { $unwind: { path: '$company_identifys', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "institution", let: { jg_id: "$jg_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [{ $toString: "$_id" }, "$$jg_id"] }, ] } } }, ], as: "institutions" } }, { $unwind: { path: '$institutions', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "t_finance_claims", let: { jg_pro_id: "$jg_pro_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [{ $toString: "$_id" }, "$$jg_pro_id"] }, ] } } }, ], as: "t_finance_claimss" } }, { $unwind: { path: '$t_finance_claimss', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "dictionary", let: { ensure_id: "$ensure_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$type', "db"] }, { $eq: ['$code', "$$ensure_id"] }, ] } } }, ], as: "dictionarys" } }, { $unwind: { path: '$dictionarys', preserveNullAndEmptyArrays: false, }, }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 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, cdata: 1, ensure_id: 1, ensure_name:'$dictionarys.name', company_name: '$company_identifys.company_name', institution_name: '$institutions.name', product_name: '$t_finance_claimss.name', demand_id: { $toString: "$_id" }, meta: 1, } }, { $lookup: { from: "t_new_assign", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$type', "1"] }, { $eq: ['$demand_id', "$$demand_id"] }, ] } } }, ], as: "t_new_assigns" } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 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, cdata: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: { $cond: { if : { $eq: [{ $size: "$t_new_assigns" }, 0] }, then: "2", else : "1" } }, information: { $cond: { if : { $eq: [{ $size: "$t_new_assigns" }, 0] }, then: {}, else : { $arrayElemAt: [ "$t_new_assigns", 0 ] }, } }, meta: 1, } }, { $lookup: { from: "t_finance_follow", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$finceType', "0"] }, { $eq: ['$finceId', "$$demand_id"] }, ] } } }, ], as: "t_finance_follows" } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 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, cdata: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: 1, information: 1, meta: 1, finance_follow: { $cond: { if : { $eq: [{ $size: "$t_finance_follows" }, 0] }, then: {orcredit:"9"}, else : { $arrayElemAt: [ "$t_finance_follows", 0 ] }, } }, } }, { $match: match, }, { $count: 'total', }, ]; const data = await this.cModel.aggregate(dataAgg); const totalTemp = await this.cModel.aggregate(totalAgg); if (totalTemp.length == 0) { totalTemp.push({ total: 0 }); } const [{ total }] = totalTemp; return { data, total, }; } // 条件查询 async financingSelect(payload) { let {skip = 0, limit = 10} = payload; let match = {}; if (payload.company_name) { match.company_name = {$regex: payload.company_name, $options: '$i'}; } if (payload.institution_name) { match.institution_name = {$regex: payload.institution_name, $options: '$i'}; } if (payload.product_name) { match.product_name = {$regex: payload.product_name, $options: '$i'}; } if (payload.is_exist) { match.is_exist = payload.is_exist; } if (payload.demand_id) { match.demand_id = payload.demand_id; } if (payload.government_name) { match[`information.government_name`] = {$regex: payload.government_name, $options: '$i'}; } if (payload.status) { match[`information.status`] = payload.status; } if (payload.company_type) { match[`information.company_type`] = payload.company_type; } let sort = {}; if (payload.sort && payload.asc) { sort[payload.sort] = Number.parseInt(payload.asc); } else { sort = {'meta.createdAt': -1}; } let dataAgg = [ { $project: { jg_id: 1, jg_pro_id: '$cid', userid: '$uid', money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: '$additional_information', ensure_id: 1, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, } }, { $lookup: { from: 'company_identify', localField: 'userid', foreignField: 'uid', as: 'company_identifys', }, }, { $unwind: { path: '$company_identifys', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "institution", let: { jg_id: "$jg_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [{ $toString: "$_id" }, "$$jg_id"] }, ] } } }, ], as: "institutions" } }, { $unwind: { path: '$institutions', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "t_finance_claims", let: { jg_pro_id: "$jg_pro_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [{ $toString: "$_id" }, "$$jg_pro_id"] }, ] } } }, ], as: "t_finance_claimss" } }, { $unwind: { path: '$t_finance_claimss', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "dictionary", let: { ensure_id: "$ensure_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$type', "db"] }, { $eq: ['$code', "$$ensure_id"] }, ] } } }, ], as: "dictionarys" } }, { $unwind: { path: '$dictionarys', preserveNullAndEmptyArrays: false, }, }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 1, money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: 1, ensure_id: 1, ensure_name:'$dictionarys.name', company_name: '$company_identifys.company_name', institution_name: '$institutions.name', product_name: '$t_finance_claimss.name', demand_id: { $toString: "$_id" }, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, } }, { $lookup: { from: "t_new_assign", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$type', "2"] }, { $eq: ['$demand_id', "$$demand_id"] }, ] } } }, ], as: "t_new_assigns" } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 1, money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: { $cond: { if : { $eq: [{ $size: "$t_new_assigns" }, 0] }, then: "2", else : "1" } }, information: { $cond: { if : { $eq: [{ $size: "$t_new_assigns" }, 0] }, then: {}, else : { $arrayElemAt: [ "$t_new_assigns", 0 ] }, } }, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, } }, { $lookup: { from: "intelligent_follow", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$intelligentId', "$$demand_id"] }, ] } } }, ], as: "intelligent_follows" } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 1, money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: 1, information: 1, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, intelligent_follow: { $cond: { if : { $eq: [{ $size: "$intelligent_follows" }, 0] }, then: {creditStatus:"9"}, else : { $arrayElemAt: [ "$intelligent_follows", 0 ] }, } }, } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 1, money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: 1, information: 1, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, finance_follow: {orcredit:"$intelligent_follow.creditStatus"}, } }, { $match: match, }, { $sort: sort, }, { $skip: Number.parseInt(skip), }, { $limit:Number.parseInt(limit), }, ]; let totalAgg = [ { $project: { jg_id: 1, jg_pro_id: '$cid', userid: '$uid', money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: '$additional_information', ensure_id: 1, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, } }, { $lookup: { from: 'company_identify', localField: 'userid', foreignField: 'uid', as: 'company_identifys', }, }, { $unwind: { path: '$company_identifys', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "institution", let: { jg_id: "$jg_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [{ $toString: "$_id" }, "$$jg_id"] }, ] } } }, ], as: "institutions" } }, { $unwind: { path: '$institutions', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "t_finance_claims", let: { jg_pro_id: "$jg_pro_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [{ $toString: "$_id" }, "$$jg_pro_id"] }, ] } } }, ], as: "t_finance_claimss" } }, { $unwind: { path: '$t_finance_claimss', preserveNullAndEmptyArrays: false, }, }, { $lookup: { from: "dictionary", let: { ensure_id: "$ensure_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$type', "db"] }, { $eq: ['$code', "$$ensure_id"] }, ] } } }, ], as: "dictionarys" } }, { $unwind: { path: '$dictionarys', preserveNullAndEmptyArrays: false, }, }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 1, money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: 1, ensure_id: 1, ensure_name:'$dictionarys.name', company_name: '$company_identifys.company_name', institution_name: '$institutions.name', product_name: '$t_finance_claimss.name', demand_id: { $toString: "$_id" }, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, } }, { $lookup: { from: "t_new_assign", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$type', "2"] }, { $eq: ['$demand_id', "$$demand_id"] }, ] } } }, ], as: "t_new_assigns" } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 1, money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: { $cond: { if : { $eq: [{ $size: "$t_new_assigns" }, 0] }, then: "2", else : "1" } }, information: { $cond: { if : { $eq: [{ $size: "$t_new_assigns" }, 0] }, then: {}, else : { $arrayElemAt: [ "$t_new_assigns", 0 ] }, } }, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, } }, { $lookup: { from: "intelligent_follow", let: { demand_id: "$demand_id" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['$intelligentId', "$$demand_id"] }, ] } } }, ], as: "intelligent_follows" } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 1, money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: 1, information: 1, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, intelligent_follow: { $cond: { if : { $eq: [{ $size: "$intelligent_follows" }, 0] }, then: {creditStatus:"9"}, else : { $arrayElemAt: [ "$intelligent_follows", 0 ] }, } }, } }, { $project: { jg_id: 1, jg_pro_id: 1, userid: 1, money: 1, mongey_min_rate: 1, mongey_max_rate: 1, claims_min_term: 1, claims_max_term: 1, remarks: 1, ensure_id: 1, ensure_name: 1, demand_id: 1, company_name: 1, institution_name: 1, product_name: 1, is_exist: 1, information: 1, meta: 1, person: 1, phone: 1, opening_bank: 1, orientation: 1, when: 1, finance_follow: {orcredit:"$intelligent_follow.creditStatus"}, } }, { $match: match, }, { $count: 'total', }, ]; const data = await this.iModel.aggregate(dataAgg); const totalTemp = await this.iModel.aggregate(totalAgg); if (totalTemp.length == 0) { totalTemp.push({ total: 0 }); } const [{ total }] = totalTemp; return { data, total, }; } } module.exports = TNewAssignService;