declare.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. const { ObjectId } = require('mongoose').Types;
  8. // 高企申报
  9. class DeclareService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'declare');
  12. this.model = this.ctx.model.Cysci.Declare;
  13. }
  14. async query({ skip = 0, limit = 0, ...query } = {}) {
  15. const nquery = _.omit(query, [ 'name' ]);
  16. if (nquery.user_id) nquery.user_id = ObjectId(nquery.user_id);
  17. if (nquery.medium_id) nquery.medium_id = ObjectId(nquery.medium_id);
  18. const arr = [
  19. { $match: nquery },
  20. {
  21. $lookup: {
  22. from: 'organization',
  23. localField: 'user_id',
  24. foreignField: '_id',
  25. as: 'companyInfo',
  26. },
  27. },
  28. { $unwind: '$companyInfo' },
  29. { $addFields: { company: '$companyInfo.name' } },
  30. { $project: { companyInfo: 0 } },
  31. ];
  32. if (_.get(query, 'name')) {
  33. arr.push({ $match: { name: new RegExp(_.get(query, 'name')) } });
  34. }
  35. let res = await this.model.aggregate(arr);
  36. const total = res.length;
  37. if (limit && limit !== '0') {
  38. res = _.slice(res, parseInt(skip), parseInt(limit));
  39. }
  40. return { data: res, total };
  41. }
  42. async fetch({ id }) {
  43. let res = await this.model.findById(id);
  44. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定数据');
  45. res = JSON.parse(JSON.stringify(res));
  46. const { user_id } = res;
  47. if (user_id) {
  48. const company = await this.ctx.model.User.Organization.findById(user_id);
  49. if (company) res.name = company.name;
  50. }
  51. return res;
  52. }
  53. // async update({ id }, body) {
  54. // const data = await this.model.findById(id);
  55. // if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
  56. // const keys = [ 'user_id', 'name', 'medium_id' ];
  57. // const toUpdate = _.omit(body, keys);
  58. // await this.model.updateOne({ _id: id }, toUpdate);
  59. // }
  60. }
  61. module.exports = DeclareService;