achieve_apply_expert.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. // 专家分配的申请表
  7. class Achieve_apply_expertService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'achieve_apply_expert');
  10. this.model = this.ctx.model.AchieveApplyExpert;
  11. }
  12. async query(querys, { skip = 0, limit = 0 } = {}) {
  13. const { expert = false, apply, ...query } = querys;
  14. const pop = [];
  15. if (expert) pop.push('expert_id');
  16. if (apply)pop.push('apply_id');
  17. const res = await this.model.find(query)
  18. .populate(pop)
  19. .skip(parseInt(skip))
  20. .limit(parseInt(limit));
  21. return res;
  22. }
  23. async count(querys) {
  24. const { expert = false, apply, ...query } = querys;
  25. const res = await this.model.count(query);
  26. return res;
  27. }
  28. async create(data) {
  29. const { apply_id, expert_id, type } = data;
  30. console.log(expert_id);
  31. let idata = [];
  32. if (_.isArray(expert_id)) {
  33. for (const e_id of expert_id) {
  34. const obj = { apply_id, expert_id: e_id, type };
  35. const res = await this.isInApply(obj);
  36. if (res)idata.push(obj);
  37. }
  38. await this.model.insertMany(idata);
  39. } else {
  40. idata = { apply_id, expert_id, type };
  41. const res = await this.isInApply(idata);
  42. if (res) { await this.model.create(idata); }
  43. }
  44. }
  45. async isInApply(data) {
  46. const count = await this.model.count(data);
  47. return count <= 0;
  48. }
  49. }
  50. module.exports = Achieve_apply_expertService;