disclosure.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 moment = require('moment');
  6. const assert = require('assert');
  7. const { ObjectId } = require('mongoose').Types;
  8. // 交底书
  9. class DisclosureService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'disclosure');
  12. this.model = this.ctx.model.Patent.Disclosure;
  13. this.notice = this.ctx.model.Patent.Notice;
  14. }
  15. /**
  16. * 查询有评估报告的交底书
  17. * @param {Object} param0 条件
  18. */
  19. async haveReport({ skip, limit, ...query }) {
  20. const { user_id, admin_id, mechanism_id } = query;
  21. if (user_id) query.user_id = ObjectId(user_id);
  22. if (admin_id) query.admin_id = ObjectId(admin_id);
  23. if (mechanism_id) query.mechanism_id = ObjectId(mechanism_id);
  24. const condition = [
  25. { $match: { ...query } },
  26. {
  27. $lookup: {
  28. from: 'disclosure_report',
  29. localField: '_id',
  30. foreignField: 'disclosure_id',
  31. as: 'report',
  32. },
  33. },
  34. { $match: { 'report.0': { $exists: true } } },
  35. { $sort: { 'meta.createdAt': -1 } },
  36. { $project: { report: 0 } },
  37. { $addFields: { has_report: true } },
  38. ];
  39. const ctotal = [
  40. ...condition,
  41. { $group: { _id: '', total: { $sum: 1 } } },
  42. ];
  43. const tres = await this.model.aggregate(ctotal);
  44. const total = _.get(_.head(tres), 'total', 0);
  45. if (skip && limit) {
  46. condition.push({ $skip: parseInt(skip) });
  47. condition.push({ $limit: parseInt(limit) });
  48. }
  49. const res = await this.model.aggregate(condition);
  50. return { data: res, total };
  51. }
  52. /**
  53. * 交底书审核
  54. * @param {body} body 参数
  55. * @property id 数据id
  56. * @property status 交底书要改变成的状态
  57. * @property info 其他数据,当做多个备注,记录使用
  58. */
  59. async check({ id, status, remark }) {
  60. const data = { status };
  61. if (status === '4') data['meta.state'] = 1;
  62. console.log(data);
  63. await this.model.updateOne({ _id: ObjectId(id) }, data);
  64. // 换成对应的状态码,record在下面
  65. return await this.record({ id, method: status, remark });
  66. }
  67. async record({ id, method, remark }) {
  68. let word = '';
  69. switch (`${method}`) {
  70. case 'create':
  71. word = '已申请';
  72. break;
  73. case 'update':
  74. word = '修改';
  75. break;
  76. case '1':
  77. word = '机构审核';
  78. break;
  79. case '-1':
  80. word = '机构审核未通过';
  81. break;
  82. case '2':
  83. word = '管理员评估';
  84. break;
  85. case '-2':
  86. word = '管理员评估未通过';
  87. break;
  88. case '3':
  89. word = '管理员评估通过,等待上传至国家库';
  90. break;
  91. case '4':
  92. word = '上传完成';
  93. break;
  94. default:
  95. word = '未知状态';
  96. break;
  97. }
  98. const data = await this.model.findById(id);
  99. if (!data) {
  100. throw new BusinessError(
  101. ErrorCode.DATA_NOT_EXIST,
  102. '添加记录----未找到数据'
  103. );
  104. }
  105. const obj = {
  106. time: moment().format('YYYY-MM-DD HH:mm:ss'),
  107. word,
  108. remark,
  109. };
  110. data.record.push(obj);
  111. const res = await data.save();
  112. this.toNotice(id, method);
  113. return res;
  114. }
  115. async toNotice(id, code) {
  116. const data = await this.model.findById(id);
  117. if (!data) return;
  118. const { user_id, admin_id, mechanism_id, status, name, apply_name } = data;
  119. const arr = [];
  120. let content = '';
  121. let to = '';
  122. switch (code) {
  123. case 'create':
  124. content = `${apply_name}提交了专利申请书(${name})的申报,请及时前往申请管理进行处理`;
  125. if (status === '1') {
  126. to = mechanism_id;
  127. } else {
  128. to = admin_id;
  129. }
  130. break;
  131. case 'update':
  132. content = `${apply_name}重新提交了专利申请书(${name})的申报,请及时前往申请管理进行处理`;
  133. if (status === '1') {
  134. to = mechanism_id;
  135. } else {
  136. to = admin_id;
  137. }
  138. break;
  139. case '-1':
  140. content = `您的专利申请书(${name})未通过机构的审核,请您及时修改,重新申请`;
  141. to = user_id;
  142. break;
  143. case '-2':
  144. content = `您的专利申请书(${name})未通过管理员的评估,请您及时修改,重新申请`;
  145. to = user_id;
  146. break;
  147. case '2':
  148. arr.push({
  149. content: `您的专利申请书(${name})已通过机构的审核,请您耐心等待管理员评估`,
  150. to: user_id,
  151. });
  152. arr.push({
  153. content: `${apply_name}的专利申请书(${name})已通过机构的审核,请您尽快对其进行评估`,
  154. to: admin_id,
  155. });
  156. break;
  157. case '3':
  158. content = `您的专利申请书(${name})已通过机构的审核,请您耐心等待上传至国家专利库中`;
  159. to = user_id;
  160. break;
  161. case '4':
  162. content = `您的专利申请书(${name})已上传至国家专利库中`;
  163. to = user_id;
  164. break;
  165. default:
  166. break;
  167. }
  168. if (code !== '2') {
  169. const obj = { to, content };
  170. await this.notice.create(obj);
  171. } else {
  172. await this.notice.insertMany(arr);
  173. }
  174. }
  175. }
  176. module.exports = DisclosureService;