'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 ApplymmoneyService extends CrudService { constructor(ctx) { super(ctx, 'apply_mmoney'); this.model = this.ctx.model.Applymmoney; } async getAmlist(data) { const match = {}; if (data.region) { match.status = data.region; } if (data.jg_id) { match.jgid = data.jg_id; } const skip = Number.parseInt(data.skip) || 1; const limit = Number.parseInt(data.limit) || 10; const totalres = await this.model.aggregate([ { $match: match }, { $project: { jgid:1,qyid:1,mid:{$toObjectId: '$mid'},status:1} }, { $lookup: { from: 'manage_money', localField: 'mid', foreignField: '_id', as: 'mmnew' } }, { $unwind: '$mmnew' }, { $lookup: { from: 'company', localField: 'qyid', foreignField: 'uid', as: 'companynew' } }, { $unwind: '$companynew' }, { $project: { _id: 1,status: 1, jgid: 1, mmtitle: '$mmnew.title',companyname:'$companynew.company_name'} }, { $count:'total'} ]); let total=0 if(totalres.length>0){ total= totalres[0].total; } const res = await this.model.aggregate([ { $match: match }, { $project: { jgid:1,qyid:1,mid:{$toObjectId: '$mid'},status:1} }, { $lookup: { from: 'manage_money', localField: 'mid', foreignField: '_id', as: 'mmnew' } }, { $unwind: '$mmnew' }, { $lookup: { from: 'company', localField: 'qyid', foreignField: 'uid', as: 'companynew' } }, { $unwind: '$companynew' }, { $project: { _id: 1,status: 1, jgid: 1, mmtitle: '$mmnew.title',companyname:'$companynew.company_name',buymin:'$mmnew.buymin'} }, { $skip: (skip - 1) * limit}, { $limit: limit }, ]); const newres = {res,total}; return newres; } async getOne(data) { const match = {}; if (data.mid) { match._id = ObjectId(data.mid); } const res = await this.model.aggregate([ { $match: match }, { $project: { jgid:1,qyid:1,mid:{$toObjectId: '$mid'},status:1} }, { $lookup: { from: 'manage_money', localField: 'mid', foreignField: '_id', as: 'mmnew' } }, { $unwind: '$mmnew' }, { $lookup: { from: 'company', localField: 'qyid', foreignField: 'uid', as: 'companynew' } }, { $unwind: '$companynew' }, { $project: { _id: 1,status: 1, jgid: 1, mmtitle: '$mmnew.title',rate:'$mmnew.rate',min_time:'$mmnew.min_time',max_time:'$mmnew.max_time',buymin:'$mmnew.buymin', companyname:'$companynew.company_name'} } ]); return res; } async getChangefollow(data) { const changeres = await this.model.findById(data.mid); if(data.status=='0'){ //前台传0 说明当前信息未关注 点击应该为想关注的操作 changeres.status = '1'; }else{ changeres.status = '0'; //取消关注 } const res = await changeres.save(); return res; } async getBeforecreat(data) { let finalres={}; const res = await this.model.find({"qyid":data.qyid,"mid":data.mid}); if(res.length>0){ finalres.status = 'ERROR';//说明已经申请过了 }else{ finalres.status = 'SUCCESS';//说明可以继续申请 } return finalres; } } module.exports = ApplymmoneyService;