'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 FubabceClaimsService extends CrudService { constructor(ctx) { super(ctx, 't_finance_claims'); this.model = this.ctx.model.Tfinanceclaims; this.releaseModel = this.ctx.model.Release; } async getTop(data) { const match = {}; match.top = 1; const skip = Number.parseInt(data.skip) || 1; const limit = Number.parseInt(data.limit) || 10; const res = await this.model.aggregate([ { $project: { uid: {$toObjectId: '$uid'},top:1,status:1,name:1,mongey_min_rate:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,ensure_id:1,repayment_id:1,claims_min_money:1 ,claims_max_money:1,meta:1} }, { $lookup: { from: 'institution', localField: 'uid', foreignField: '_id', as: 'innew' } }, { $unwind: '$innew' }, { $project: { uid: {$toObjectId: '$uid'},top:1,status:1,name:1,mongey_min_rate:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,ensure_id:1,repayment_id:1,claims_min_money:1 ,claims_max_money:1,metatime:'$meta.updatedAt',innew:1} }, { $match: match }, { $skip: (skip - 1) * limit }, { $limit: limit }, { $sort : { metatime:1 } } ]); return res; } // 债券显示列表(type和 uid 必须要传) async getClaimsList(data) { const match = {}; if (data.term) { // 融资期限 match.mongey_max_rate = { $lt: parseInt(data.term) }; } if (data.eid) { // 担保方式 match.ensure_id = data.eid; } if (data.money) { // 融资额度 match.claims_max_money = { $lt: parseInt(data.money) }; } if (data.innewname) { // 机构名称 match.innewname = { $regex: data.innewname }; } if (data.name) { // 产品名称 match.name = { $regex: data.name }; } if (Number.parseInt(data.type) == 0) {//金控 if(data.status){ match.status = data.status; }else{ match.status = { $ne: '2' }; } } else if (Number.parseInt(data.type)== 1) {//金融 match.uid = data.uid; match.status = { $ne: '2' }; } else if (Number.parseInt(data.type)== 3) {//企业 match.status = '1'; } const skip = Number.parseInt(data.skip) || 1; const limit = Number.parseInt(data.limit) || 10; const totalres = await this.model.aggregate([ { $project: { uid: {$toObjectId: '$uid'},name:1,mongey_min_rate:1,mongey_max_rate:1,status:1} }, { $lookup: { from: 'institution', localField: 'uid', foreignField: '_id', as: 'innew' } }, { $unwind: '$innew' }, { $project: { _id: 1, uid: {$toString: '$uid'}, status: 1, name: 1, publish_time: 1, mongey_min_rate: 1, mongey_max_rate: 1, innewname: '$innew.name', innewpic: '$innew.logo' } }, { $match: match }, { $count:'total'} ]); let total=0 if(totalres.length>0){ total= totalres[0].total; } const res = await this.model.aggregate([ { $project: { uid: {$toObjectId: '$uid'},name:1,mongey_min_rate:1,mongey_max_rate:1,status:1,video:1,metatime:'$meta.updatedAt'} }, { $lookup: { from: 'institution', localField: 'uid', foreignField: '_id', as: 'innew' } }, { $unwind: '$innew' }, { $project: { _id: 1, uid: {$toString: '$uid'}, status: 1, name: 1, publish_time: 1, mongey_min_rate: 1, mongey_max_rate: 1,video:1, innewname: '$innew.name', innewpic: '$innew.logo',metatime:1} }, { $match: match }, { $sort : { metatime:-1} }, { $skip: (skip - 1) * limit }, { $limit: limit } ]); const newres = {res,total}; return newres; } // publish:发布还是取消发布;finid:债权产品ID;userid:金控管理员ID;type:0-债权产品; async makePublish(data) { const { publish, finid } = data; const finalStutus = {}; const newdata = {}; newdata.review_id = data.userid; newdata.product_id = finid; newdata.type = '0'; if (publish === '1') { newdata.status = publish; const financ = await this.model.findById(finid); financ.status = '1'; const res = await financ.save(); if (res) { finalStutus.status = 'SUCCESS'; const relea = await this.releaseModel.find({ review_id: data.userid, product_id: finid }); if(relea.length>0){}else{ await this.releaseModel.create(newdata); } } else { finalStutus.status = 'ERROR'; } } else { newdata.status = publish; const financ = await this.model.findById(finid); financ.status = '0'; const res = await financ.save(); if (res) { finalStutus.status = 'SUCCESS'; const relea = await this.releaseModel.find({ review_id: data.userid, product_id: finid }); relea[0].status = '0'; await relea[0].save(); } else { finalStutus.status = 'ERROR'; } } return finalStutus; } async getOrUpdate(data) { const { finid } = data; const finalStutus = {}; const relea = await this.model.findById(finid); if (relea.status === "1") { finalStutus.status = 'ERROR'; } else { finalStutus.status = 'SUCCESS'; } return finalStutus; } async getJustNameId(data){ const{uid} = data; const match = {}; match.status = '1'; match.uid = uid; const res = await this.model.aggregate([ {$match:match}, { $project: { _id: 1,name:1}} ]); return res; } } module.exports = FubabceClaimsService;