patenttrans.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use strict';
  2. const assert = require('assert');
  3. const moment = require('moment');
  4. const Excel = require('exceljs');
  5. const Path = require('path');
  6. const _ = require('lodash');
  7. const { sep } = require('path');
  8. const fs = require('fs');
  9. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  10. const { BusinessError, ErrorCode } = require('naf-core').Error;
  11. const { trimData } = require('naf-core').Util;
  12. const { ObjectId } = require('mongoose').Types;
  13. // 专利交易
  14. class Patent_transService extends CrudService {
  15. constructor(ctx) {
  16. super(ctx, 'patenttrans');
  17. this.model = this.ctx.model.Patent.Patenttrans;
  18. this.notice = this.ctx.model.Patent.Patentexamine;
  19. this.root_path = _.get(this.ctx.app.config.export, 'root_path');
  20. if (process.env.NODE_ENV === 'development') {
  21. this.root_path = 'E:\\exportFile\\';
  22. }
  23. this.file_type = '';
  24. if (!fs.existsSync(`${this.root_path}${this.file_type}`)) {
  25. // 如果不存在文件夹,就创建
  26. fs.mkdirSync(`${this.root_path}${this.file_type}`);
  27. }
  28. this.excel_path = `${sep}excel${sep}`;
  29. this.domain = 'http://127.0.0.1';
  30. this.export_limit = 50;
  31. }
  32. /**
  33. * 专利交易审核
  34. * @param {body} body 参数
  35. * @property id 数据id
  36. * @property status 交底书要改变成的状态
  37. * @property transfer_date 专利权转移时间
  38. * @property info 其他数据,当做多个备注,记录使用
  39. */
  40. async check({ id, status, remark, transfer_date }) {
  41. await this.model.updateOne(
  42. { _id: ObjectId(id) },
  43. { status, transfer_date }
  44. );
  45. // 换成对应的状态码,record在下面
  46. return await this.record({ id, method: status, remark });
  47. }
  48. async record({ id, method, remark }) {
  49. let word = '';
  50. switch (`${method}`) {
  51. case 'create':
  52. word = '已申请';
  53. break;
  54. case 'update':
  55. word = '修改';
  56. break;
  57. case '1':
  58. word = '审核通过';
  59. break;
  60. case '-1':
  61. word = '审核拒绝';
  62. break;
  63. case '2':
  64. word = '合同审核';
  65. break;
  66. case '3':
  67. word = '合同审核通过,用户确认';
  68. break;
  69. case '-3':
  70. word = '合同审核拒绝';
  71. break;
  72. case '4':
  73. word = '用户确认,待归档';
  74. break;
  75. case '5':
  76. word = '归档完成,交易完成';
  77. break;
  78. default:
  79. word = '未知状态';
  80. break;
  81. }
  82. const data = await this.model.findById(id);
  83. if (!data) {
  84. throw new BusinessError(
  85. ErrorCode.DATA_NOT_EXIST,
  86. '添加记录----未找到数据'
  87. );
  88. }
  89. const obj = {
  90. time: moment().format('YYYY-MM-DD HH:mm:ss'),
  91. word,
  92. remark,
  93. };
  94. data.record.push(obj);
  95. const res = await data.save();
  96. this.toNotice(id, method);
  97. return res;
  98. }
  99. async toNotice(id, code) {
  100. const data = await this.model.findById(id);
  101. if (!data) return;
  102. const { user_id, mech_id, status, patent_name } = data;
  103. const arr = [];
  104. let content = '';
  105. let to = '';
  106. switch (code) {
  107. case 'create':
  108. content = `用户的【${patent_name}】发起了专利交易,请机构及时查看并处理`;
  109. to = mech_id;
  110. break;
  111. case 'update':
  112. if (status === '0') {
  113. content = `用户的【${patent_name}】修改了专利交易,请及时查看处理`;
  114. to = mech_id;
  115. } else if (status === '2') {
  116. content = `用户的【${patent_name}】专利交易提交了交易合同,请及时查看处理`;
  117. to = mech_id;
  118. } else if (status === '4') {
  119. content = `用户的【${patent_name}】专利交易已确认,请及时查看处理`;
  120. to = mech_id;
  121. }
  122. break;
  123. case '-1':
  124. content = `用户的【${patent_name}】未通过机构的审核,请您及时修改,重新填写专利交易`;
  125. to = user_id;
  126. break;
  127. case '1':
  128. content = `用户的【${patent_name}】专利交易通过了机构审核,系统可查询`;
  129. to = user_id;
  130. break;
  131. case '2':
  132. content = `用户的【${patent_name}】专利结束交易,请机构用户及时查看并进行合同审核`;
  133. to = mech_id;
  134. break;
  135. case '3':
  136. content = `用户的【${patent_name}】专利交易合同审核通过,请您及时点击确认结束交易`;
  137. to = user_id;
  138. break;
  139. case '-3':
  140. content = `用户的【${patent_name}】专利交易合同审核未通过,请您及时查看审核结果,并进行修改`;
  141. to = user_id;
  142. break;
  143. case '4':
  144. content = `用户的【${patent_name}】专利结束交易已确认,请机构用户进行专利交易归档`;
  145. to = mech_id;
  146. break;
  147. case '5':
  148. content = `用户的【${patent_name}】专利交易归档完成,交易完成`;
  149. to = user_id;
  150. break;
  151. default:
  152. break;
  153. }
  154. if (arr.length > 0) {
  155. await this.notice.insertMany(arr);
  156. } else {
  157. const obj = { to, content };
  158. await this.notice.create(obj);
  159. }
  160. }
  161. async toImport({ uri }) {
  162. assert(uri, '未获取到文件地址');
  163. const file = await this.ctx.curl(`${this.domain}${uri}`);
  164. if (!(file && file.data)) {
  165. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定文件');
  166. }
  167. const workbook = new Excel.Workbook();
  168. await workbook.xlsx.load(file.data);
  169. const sheet = workbook.getWorksheet(1);
  170. const arr = [];
  171. const allNotice = [];
  172. sheet.eachRow((row, rindex) => {
  173. if (rindex !== 1) {
  174. const create_number = row.getCell(2).value || undefined,
  175. patent_name = row.getCell(3).value || undefined,
  176. type = row.getCell(4).value || undefined,
  177. budget = row.getCell(5).value || undefined,
  178. on_obligee = row.getCell(6).value || undefined,
  179. on_afterobligee = row.getCell(7).value || undefined,
  180. transfer_date =
  181. moment(row.getCell(8).value).format('YYYY-MM-DD') || undefined,
  182. contact = row.getCell(9).value || undefined,
  183. phone = row.getCell(10).value || undefined,
  184. email = row.getCell(11).value || undefined,
  185. requirementdesc = row.getCell(12).value || undefined,
  186. expect = row.getCell(13).value || undefined,
  187. condition = row.getCell(14).value || undefined,
  188. abstract = row.getCell(15).value || undefined,
  189. status = row.getCell(16).value || '0';
  190. const obj = {
  191. create_number,
  192. patent_name,
  193. type,
  194. budget,
  195. on_obligee,
  196. on_afterobligee,
  197. transfer_date,
  198. contact,
  199. phone,
  200. email,
  201. requirementdesc,
  202. expect,
  203. condition,
  204. abstract,
  205. status,
  206. };
  207. // 此处添加判断条件,不限制则不需要加,直接放过即可
  208. const { result, notice } = this.tocheckData(obj);
  209. if (result) {
  210. arr.push(obj);
  211. } else {
  212. allNotice.push(notice);
  213. }
  214. }
  215. });
  216. if (allNotice.length > 0) return allNotice;
  217. await this.model.insertMany(arr);
  218. }
  219. /**
  220. * 检查数据是否没填 必填项
  221. * @param {Object} object 每行数据,已转换成model的字段名
  222. */
  223. tocheckData(object) {
  224. let result = true;
  225. const { number } = object;
  226. let notice;
  227. const arr = [
  228. { column: 'create_number', zh: '专利号' },
  229. ];
  230. const word = [];
  231. for (const o of arr) {
  232. const { column, zh } = o;
  233. if (!_.get(object, column)) {
  234. result = false;
  235. word.push(`${zh}`);
  236. }
  237. }
  238. if (!result) {
  239. notice = `序号${number}缺少:${word.join(';')}`;
  240. }
  241. return { result, notice };
  242. }
  243. }
  244. module.exports = Patent_transService;