achieve_apply.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. personalData.role = '4';
  31. user = await this.httpUtil.cpost('/users/personal', 'live', personalData);
  32. if (!user) throw new BusinessError(ErrorCode.SERVICE_FAULT, '用户创建失败!');
  33. } else {
  34. // 该手机号有用户使用,取出来
  35. user = _.head(users);
  36. }
  37. body.user_id = user._id;
  38. res = await this.model.create(body);
  39. } else {
  40. res = await this.model.create(body);
  41. }
  42. return res;
  43. }
  44. async getOne(query) {
  45. const keys = Object.keys(query);
  46. if (keys.length <= 0) throw new BusinessError(ErrorCode.BADPARAM, '缺少参数');
  47. const res = await this.model.findOne(query);
  48. return res;
  49. }
  50. async researchCreate({ id }, body) {
  51. const res = await this.model.findById(id);
  52. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  53. res.research.push(body);
  54. await res.save();
  55. return;
  56. }
  57. async researchUpdate({ id, research_id }, body) {
  58. const res = await this.model.findById(id);
  59. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  60. const person = res.research.id(research_id);
  61. if (!person) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该申请下指定的主研人员');
  62. const keys = Object.keys(body);
  63. for (const key of keys) {
  64. person[key] = body[key];
  65. }
  66. await res.save();
  67. }
  68. async researchDelete({ id, research_id }) {
  69. const res = await this.model.findById(id);
  70. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  71. res.research.remove(research_id);
  72. await res.save();
  73. }
  74. async menuRemind({ user_id }) {
  75. let res = await this.model.aggregate([
  76. { $match: { user_id: ObjectId(user_id) } },
  77. { $group: {
  78. _id: '$status',
  79. count: { $sum: 1 },
  80. } },
  81. ]);
  82. res = JSON.parse(JSON.stringify(res));
  83. res = res.map(i => ({ status: i._id, count: 1 }));
  84. // 状态合并
  85. // cs:0,-1,-5,6合成一个数据
  86. // pf:1,-2合成一个
  87. // hs:3,4,-5合成一个
  88. // zs:5,6合成一个
  89. // {name}
  90. const computed = list => {
  91. return res.reduce((p, n) => {
  92. let num = 0;
  93. if (list.includes(n.status)) num = n.count;
  94. return p + num;
  95. }, 0);
  96. };
  97. const obj = {
  98. cs: computed([ '0', '-1' ]),
  99. pf: computed([ '1', '-2' ]),
  100. hs: computed([ '3', '4' ]),
  101. zs: computed([ '5', '6' ,'7']),
  102. };
  103. return obj;
  104. }
  105. }
  106. module.exports = Achieve_applyService;