'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); const moment = require('moment'); const { ObjectId } = require('mongoose').Types; // 高企申报 class DeclareService extends CrudService { constructor(ctx) { super(ctx, 'declare'); this.model = this.ctx.model.Cysci.Declare; } async query({ skip = 0, limit = 0, ...query } = {}) { const nquery = _.omit(query, [ 'name' ]); if (nquery.user_id) nquery.user_id = ObjectId(nquery.user_id); if (nquery.medium_id) nquery.medium_id = ObjectId(nquery.medium_id); const arr = [ { $match: nquery }, { $lookup: { from: 'organization', localField: 'user_id', foreignField: '_id', as: 'companyInfo', }, }, { $unwind: '$companyInfo' }, { $addFields: { company: '$companyInfo.name' } }, { $project: { companyInfo: 0 } }, ]; if (_.get(query, 'name')) { arr.push({ $match: { name: new RegExp(_.get(query, 'name')) } }); } let res = await this.model.aggregate(arr); const total = res.length; if (limit && limit !== '0') { res = _.slice(res, parseInt(skip), parseInt(limit)); } return { data: res, total }; } async fetch({ id }) { let res = await this.model.findById(id); if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定数据'); res = JSON.parse(JSON.stringify(res)); const { user_id } = res; if (user_id) { const company = await this.ctx.model.User.Organization.findById(user_id); if (company) res.name = company.name; } return res; } // async update({ id }, body) { // const data = await this.model.findById(id); // if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据'); // const keys = [ 'user_id', 'name', 'medium_id' ]; // const toUpdate = _.omit(body, keys); // await this.model.updateOne({ _id: id }, toUpdate); // } } module.exports = DeclareService;