achieve_apply_expert.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. this.expert = this.ctx.model.AchieveExpert;
  12. }
  13. async query(querys, { skip = 0, limit = 0 } = {}) {
  14. const { expert = false, apply, ...query } = querys;
  15. const pop = [];
  16. if (expert) pop.push('expert_id');
  17. if (apply)pop.push('apply_id');
  18. const res = await this.model.find(query)
  19. .populate(pop)
  20. .skip(parseInt(skip))
  21. .limit(parseInt(limit));
  22. return res;
  23. }
  24. async count(querys) {
  25. const { expert = false, apply, ...query } = querys;
  26. const res = await this.model.count(query);
  27. return res;
  28. }
  29. async create(data) {
  30. const { apply_id, expert_id, type } = data;
  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. async update({ id }, body) {
  50. const data = await this.model.findById(id);
  51. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '数据不存在');
  52. const { expert_id } = data;
  53. // 专家变!不可用
  54. await this.expert.updateOne({ _id: expert_id }, { status: '1' });
  55. await this.model.updateOne({ _id: id }, body);
  56. }
  57. async getWork({ expert_id }) {
  58. const data = await this.model.findOne({ expert_id }).populate('apply_id');
  59. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到您需要审核的成果');
  60. return data;
  61. }
  62. }
  63. module.exports = Achieve_apply_expertService;