'use strict'; const assert = require('assert'); const _ = require('lodash'); const sd = require('silly-datetime'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class SearchautoService extends CrudService { constructor(ctx) { super(ctx, 'institution'); this.model = this.ctx.model.Institution; this.tmodel = this.ctx.model.Tfinanceclaims; this.smodel = this.ctx.model.Companysearch; this.cmodel = this.ctx.model.Company; this.fmodel = this.ctx.model.Financefollow; this.nmodel = this.ctx.model.Claimneed; } // 查询股权信息 async institutionsearch(data) { const { title, profession, round, skip, limit, userid } = data; const newquery = { type: { $ne: 0 }, status: '2' }; if (title) { newquery.name = { $regex: title }; } if (profession) { profession.push('不限'); newquery.profession = { $in: profession }; } if (round) { round.push('不限'); newquery.round = { $in: round }; } console.log(newquery); const total = await this.model.count(newquery); const res = await this.model.find(newquery).limit(Number(limit)).skip(Number(skip)); // 将查询信息插入表中,方便后期统计 const seachdata = {}; if (userid) { seachdata.userid = userid; const company = await this.cmodel.findById(userid); if (company) { seachdata.company_name = company.company_name; } } seachdata.searchinfo = JSON.stringify(newquery); seachdata.create_time = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss'); seachdata.result_info = JSON.stringify(res); seachdata.type = '1'; try { await this.smodel.create(seachdata); } catch (error) { console.log(error); } return { data: res, total }; } // 查询债权信息 async financeclaimssearch(data) { const { userid, name, mongey_min_rate, title, claims_max_term, ensure_id, repayment_id, cplx_id,claims_max_money,uid, skip, limit } = data; const match = { status: '1' }; if (claims_max_term) { // 贷款期限 if (claims_max_term === '13') { match.claims_max_term = { $gte: parseInt(claims_max_term) }; } else { match.claims_max_term = { $lte: parseInt(claims_max_term) }; } } if (ensure_id) { // 担保方式 match.ensure_id = ensure_id; } if (uid) { // 金融机构 match.uid = uid; } if (repayment_id) { // 还款方式 match.ensure_id = repayment_id; } if (cplx_id) { // 产品类型 match.cplx_id = cplx_id; } if (claims_max_money) { // 贷款额度 if (claims_max_money === '1001') { match.claims_max_money = { $gte: parseInt(claims_max_money) }; } else { match.claims_max_money = { $lte: parseInt(claims_max_money) }; } } if (title) { // 产品名称 match.name = { $regex: title }; } if (name) { const institutionlist = await this.model.find({ name: { $regex: name } }); const uids = []; for (const ins of institutionlist) { uids.push(ins.id); } match.uid = { $in: uids }; } console.log(match); const _sort = {}; if (mongey_min_rate) { // 排序 _sort.mongey_min_rate = 1; } const total = await this.tmodel.count(match); const res = await this.tmodel.find(match).sort(_sort).limit(Number(limit)) .skip(Number(skip)); const result = []; for (const elm of res) { const institution = await this.model.findById(ObjectId(elm.uid)); const _elm = JSON.stringify(elm); console.log(institution); let joincount = 0; if (institution) { const claimneeds = await this.nmodel.find({ jg_pro_id: elm.id }); for (const cln of claimneeds) { const ff = await this.fmodel.find({ finceId: cln.id, orcredit: '1' }); if (ff) { joincount = joincount + ff.length; } } const newdata = { ...JSON.parse(_elm), institution_name: institution.name, logo: institution.logo, joincount }; result.push(newdata); } else { const newdata = { ...JSON.parse(_elm), joincount }; result.push(newdata); } } // 将查询信息插入表中,方便后期统计 const seachdata = {}; if (userid) { seachdata.userid = userid; const company = await this.cmodel.findById(userid); if (company) { seachdata.company_name = company.company_name; } } seachdata.searchinfo = JSON.stringify(match); seachdata.create_time = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss'); seachdata.result_info = JSON.stringify(result); seachdata.type = '1'; try { await this.smodel.create(seachdata); } catch (error) { console.log(error); } return { data: result, total }; } async successnum(data) { let joincount = 0; const claimneeds = await this.nmodel.find({ jg_pro_id: data.id }); for (const cln of claimneeds) { const ff = await this.fmodel.find({ finceId: cln.id, orcredit: '1' }); if (ff) { joincount = joincount + ff.length; } } return {num:joincount} } } module.exports = SearchautoService;