'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 ManageMoneyService extends CrudService { constructor(ctx) { super(ctx, 'manage_money'); this.model = this.ctx.model.Managemoney; } async getOrUpdate(data) { const { mmid } = data; const finalStutus = {}; const relea = await this.model.findById(mmid); if (relea.status === "1") { finalStutus.status = 'ERROR'; } else { finalStutus.status = 'SUCCESS'; } return finalStutus; } async getMoneyList(data) { const match = {}; if (data.innewname) { // 机构名称 match.innewname = { $regex: data.innewname }; } if (data.title) { // 产品名称 match.title = { $regex: data.title }; } 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.jgid = 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: { jgid: {$toObjectId: '$jgid'},title:1,buymin:1,status:1,rate:1,news:1,video:1} }, { $lookup: { from: 'institution', localField: 'jgid', foreignField: '_id', as: 'innew' } }, { $unwind: '$innew' }, { $project: { _id: 1,status: 1, title: 1, buymin: 1,innewname: '$innew.name',jgid:{$toString: '$jgid'},rate:1,news:1} }, { $match: match }, { $count:'total'} ]); let total=0 if(totalres.length>0){ total= totalres[0].total; } const res = await this.model.aggregate([ { $project: { jgid: {$toObjectId: '$jgid'},title:1,buymin:1,status:1,rate:1,news:1,video:1} }, { $lookup: { from: 'institution', localField: 'jgid', foreignField: '_id', as: 'innew' } }, { $unwind: '$innew' }, { $project: { _id: 1,status: 1, title: 1, buymin: 1,innewname: '$innew.name',jgid:{$toString: '$jgid'},rate:1,news:1} }, { $match: match }, { $skip: (skip - 1) * limit }, { $limit: limit }, ]); const newres = {res,total}; return newres; } async makePublish(data) { const {sta,finid} = data;//finid 理财产品发布 const finalStutus = {}; const financ = await this.model.findById(finid); if(sta=='0'){//是0 代表当前未发布 应该发布 financ.status = '1'; }else{ financ.status = '0'; } const res = await financ.save(); if (res) { finalStutus.status = 'SUCCESS'; } else { finalStutus.status = 'ERROR'; } return finalStutus; } } module.exports = ManageMoneyService;