'use strict'; const assert = require('assert'); const moment = require('moment'); const Excel = require('exceljs'); const Path = require('path'); const _ = require('lodash'); const { sep } = require('path'); const fs = require('fs'); const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const { trimData } = require('naf-core').Util; const { ObjectId } = require('mongoose').Types; // 专利交易 class Patent_transService extends CrudService { constructor(ctx) { super(ctx, 'patenttrans'); this.model = this.ctx.model.Patent.Patenttrans; this.notice = this.ctx.model.Patent.Patentexamine; this.root_path = _.get(this.ctx.app.config.export, 'root_path'); if (process.env.NODE_ENV === 'development') { this.root_path = 'E:\\exportFile\\'; } this.file_type = ''; if (!fs.existsSync(`${this.root_path}${this.file_type}`)) { // 如果不存在文件夹,就创建 fs.mkdirSync(`${this.root_path}${this.file_type}`); } this.excel_path = `${sep}excel${sep}`; this.domain = 'http://127.0.0.1'; this.export_limit = 50; } /** * 专利交易审核 * @param {body} body 参数 * @property id 数据id * @property status 交底书要改变成的状态 * @property transfer_date 专利权转移时间 * @property info 其他数据,当做多个备注,记录使用 */ async check({ id, status, remark, transfer_date }) { await this.model.updateOne( { _id: ObjectId(id) }, { status, transfer_date } ); // 换成对应的状态码,record在下面 return await this.record({ id, method: status, remark }); } async record({ id, method, remark }) { let word = ''; switch (`${method}`) { case 'create': word = '已申请'; break; case 'update': word = '修改'; break; case '1': word = '审核通过'; break; case '-1': word = '审核拒绝'; break; case '2': word = '合同审核'; break; case '3': word = '合同审核通过,用户确认'; break; case '-3': word = '合同审核拒绝'; break; case '4': word = '用户确认,待归档'; break; case '5': word = '归档完成,交易完成'; break; default: word = '未知状态'; break; } const data = await this.model.findById(id); if (!data) { throw new BusinessError( ErrorCode.DATA_NOT_EXIST, '添加记录----未找到数据' ); } const obj = { time: moment().format('YYYY-MM-DD HH:mm:ss'), word, remark, }; data.record.push(obj); const res = await data.save(); this.toNotice(id, method); return res; } async toNotice(id, code) { const data = await this.model.findById(id); if (!data) return; const { user_id, mech_id, status, patent_name } = data; const arr = []; let content = ''; let to = ''; switch (code) { case 'create': content = `用户的【${patent_name}】发起了专利交易,请机构及时查看并处理`; to = mech_id; break; case 'update': if (status === '0') { content = `用户的【${patent_name}】修改了专利交易,请及时查看处理`; to = mech_id; } else if (status === '2') { content = `用户的【${patent_name}】专利交易提交了交易合同,请及时查看处理`; to = mech_id; } else if (status === '4') { content = `用户的【${patent_name}】专利交易已确认,请及时查看处理`; to = mech_id; } break; case '-1': content = `用户的【${patent_name}】未通过机构的审核,请您及时修改,重新填写专利交易`; to = user_id; break; case '1': content = `用户的【${patent_name}】专利交易通过了机构审核,系统可查询`; to = user_id; break; case '2': content = `用户的【${patent_name}】专利结束交易,请机构用户及时查看并进行合同审核`; to = mech_id; break; case '3': content = `用户的【${patent_name}】专利交易合同审核通过,请您及时点击确认结束交易`; to = user_id; break; case '-3': content = `用户的【${patent_name}】专利交易合同审核未通过,请您及时查看审核结果,并进行修改`; to = user_id; break; case '4': content = `用户的【${patent_name}】专利结束交易已确认,请机构用户进行专利交易归档`; to = mech_id; break; case '5': content = `用户的【${patent_name}】专利交易归档完成,交易完成`; to = user_id; break; default: break; } if (arr.length > 0) { await this.notice.insertMany(arr); } else { const obj = { to, content }; await this.notice.create(obj); } } async toImport({ uri }) { assert(uri, '未获取到文件地址'); const file = await this.ctx.curl(`${this.domain}${uri}`); if (!(file && file.data)) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定文件'); } const workbook = new Excel.Workbook(); await workbook.xlsx.load(file.data); const sheet = workbook.getWorksheet(1); const arr = []; const allNotice = []; sheet.eachRow((row, rindex) => { if (rindex !== 1) { const create_number = row.getCell(2).value || undefined, patent_name = row.getCell(3).value || undefined, type = row.getCell(4).value || undefined, budget = row.getCell(5).value || undefined, on_obligee = row.getCell(6).value || undefined, on_afterobligee = row.getCell(7).value || undefined, transfer_date = moment(row.getCell(8).value).format('YYYY-MM-DD') || undefined, contact = row.getCell(9).value || undefined, phone = row.getCell(10).value || undefined, email = row.getCell(11).value || undefined, requirementdesc = row.getCell(12).value || undefined, expect = row.getCell(13).value || undefined, condition = row.getCell(14).value || undefined, abstract = row.getCell(15).value || undefined, status = row.getCell(16).value || '0'; const obj = { create_number, patent_name, type, budget, on_obligee, on_afterobligee, transfer_date, contact, phone, email, requirementdesc, expect, condition, abstract, status, }; // 此处添加判断条件,不限制则不需要加,直接放过即可 const { result, notice } = this.tocheckData(obj); if (result) { arr.push(obj); } else { allNotice.push(notice); } } }); if (allNotice.length > 0) return allNotice; await this.model.insertMany(arr); } /** * 检查数据是否没填 必填项 * @param {Object} object 每行数据,已转换成model的字段名 */ tocheckData(object) { let result = true; const { number } = object; let notice; const arr = [ { column: 'create_number', zh: '专利号' }, ]; const word = []; for (const o of arr) { const { column, zh } = o; if (!_.get(object, column)) { result = false; word.push(`${zh}`); } } if (!result) { notice = `序号${number}缺少:${word.join(';')}`; } return { result, notice }; } } module.exports = Patent_transService;