achieve_apply.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. const { ObjectId } = require('mongoose').Types;
  7. // 成果评价申请
  8. class Achieve_applyService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'achieve_apply');
  11. this.model = this.ctx.model.AchieveApply;
  12. this.httpUtil = this.ctx.service.util.httpUtil;
  13. this.util = this.ctx.service.util.util;
  14. }
  15. async create(body) {
  16. // 没有账号需要创建账号
  17. const { user_id, basic } = body;
  18. let res;
  19. if (!user_id) {
  20. const { apply_personal, apply_phone, email, addr } = basic;
  21. // 拿手机号去个人里找
  22. const users = await this.httpUtil.cget('/users/personal', 'live', { phone: apply_phone });
  23. let user;
  24. if (users.length <= 0) {
  25. // 该手机号没有用户使用,创建
  26. const personalData = { name: apply_personal, phone: apply_phone, email, addr };
  27. personalData.password = '123456';
  28. personalData.code = 'CGPIXT';
  29. personalData.status = '1';
  30. user = await this.httpUtil.cpost('/users/personal', 'live', personalData);
  31. if (!user) throw new BusinessError(ErrorCode.SERVICE_FAULT, '用户创建失败!');
  32. } else {
  33. // 该手机号有用户使用,取出来
  34. user = _.head(users);
  35. }
  36. body.user_id = user._id;
  37. res = await this.model.create(body);
  38. } else {
  39. res = await this.model.create(body);
  40. }
  41. return res;
  42. }
  43. async getOne(query) {
  44. const keys = Object.keys(query);
  45. if (keys.length <= 0) throw new BusinessError(ErrorCode.BADPARAM, '缺少参数');
  46. const res = await this.model.findOne(query);
  47. return res;
  48. }
  49. async researchCreate({ id }, body) {
  50. const res = await this.model.findById(id);
  51. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  52. res.research.push(body);
  53. await res.save();
  54. return;
  55. }
  56. async researchUpdate({ id, research_id }, body) {
  57. const res = await this.model.findById(id);
  58. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  59. const person = res.research.id(research_id);
  60. if (!person) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该申请下指定的主研人员');
  61. const keys = Object.keys(body);
  62. for (const key of keys) {
  63. person[key] = body[key];
  64. }
  65. await res.save();
  66. }
  67. async researchDelete({ id, research_id }) {
  68. const res = await this.model.findById(id);
  69. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  70. res.research.remove(research_id);
  71. await res.save();
  72. }
  73. async menuRemind({ user_id }) {
  74. let res = await this.model.aggregate([
  75. { $match: { user_id: ObjectId(user_id) } },
  76. { $group: {
  77. _id: '$status',
  78. count: { $sum: 1 },
  79. } },
  80. ]);
  81. res = JSON.parse(JSON.stringify(res));
  82. res = res.map(i => ({ status: i._id, count: 1 }));
  83. // 状态合并
  84. // cs:0,-1,-5,6合成一个数据
  85. // pf:1,-2合成一个
  86. // hs:3,4,-5合成一个
  87. // zs:5,6合成一个
  88. // {name}
  89. const computed = list => {
  90. return res.reduce((p, n) => {
  91. let num = 0;
  92. if (list.includes(n.status)) num = n.count;
  93. return p + num;
  94. }, 0);
  95. };
  96. const obj = {
  97. cs: computed([ '0', '-1' ]),
  98. pf: computed([ '1', '-2' ]),
  99. hs: computed([ '3', '4' ]),
  100. zs: computed([ '5', '6' ]),
  101. };
  102. return obj;
  103. }
  104. }
  105. module.exports = Achieve_applyService;