'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const { ObjectId } = require('mongoose').Types; const _ = require('lodash'); const assert = require('assert'); const moment = require('moment'); const { trimData } = require('naf-core').Util; // 专利运营已授权专利预警表 class PatentearlyService extends CrudService { constructor(ctx) { super(ctx, 'patentearly'); this.model = this.ctx.model.Patent.Patentearly; this.patentinfo = this.ctx.model.Patent.Patentinfo; this.personalModel = this.ctx.model.User.Personal; this.patentexamineModel = this.ctx.model.Patent.Patentexamine; } async query(query, { skip = 0, limit = 0 }) { const newquery = await this.resetCode(query); const res = await this.model.find(newquery).skip(parseInt(skip)).limit(parseInt(limit)) .sort({ 'meta.createdAt': -1 }); return res; } async count(query) { const newquery = await this.resetCode(query); const res = await this.model.countDocuments(trimData(newquery)).exec(); return res; } async resetCode(query) { let newquery = _.cloneDeep(query); newquery = this.ctx.service.util.util.dealQuery(newquery); const { code, user_id } = newquery; let ids = []; if (code) { const plist = await this.personalModel.find({ code }); ids = plist.map(i => i._id); if (ids.length > 0) { newquery.user_id = { $elemMatch: { $in: ids } }; delete newquery.code; } } else if (user_id) { newquery.user_id = { $elemMatch: { $in: [ ObjectId(user_id) ] } }; } return newquery; } async queryByOrg({ code, skip = 0, limit = 0 }) { assert(code, '缺少机构信息'); let pids = await this.personalModel.find({ code }, { _id: 1 }); if (pids.length <= 0) return { data: [], total: 0 }; pids = pids.map(i => i._id); const query = { user_id: { $elemMatch: { $in: pids } } }; const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit)) .sort({ 'meta.createdAt': -1 }); const total = await this.model.count(query); return { data, total }; } /** * 产生警告 */ async needWarning() { const limit = 5000; let skip = 0; // 一段一段查数据 // skip += limit; let loop = true; while (loop) { const total = await this.searchAndDeal(skip, limit); if (total <= 0) loop = false; skip += limit; } } /** * 查找并处理 * @param {Number} skip 开始位置 * @param {Number} limit 结束位置 */ async searchAndDeal(skip, limit) { let total = 0; let data = await this.patentinfo.find({ term: '有效' }, { name: 1, user_id: 1, create_date: 1 }).skip(skip).limit(limit); if (data.length > 0) data = JSON.parse(JSON.stringify(data)); total = data.length || 0; await this.dealData(data); return total; } /** * * @param {Array} data 处理数据 */ async dealData(data) { // 取出今天是不是在失效时间的前${limitMonth}个月范围内 const limitMonth = 3; for (const i of data) { try { const { create_date } = i; // 专利到期时间 const expire_date = moment(new Date()).format('YYYY') + '-' + moment(create_date).format('MM-DD'); // 专利到期时间延后一个月 const end = moment(expire_date).add(1, 'months').format('YYYY-MM-DD'); // 专利到期前三个月 const start = moment(end).subtract(limitMonth, 'months').format('YYYY-MM-DD'); // 专利到期前两个月 const start_two = moment(end).subtract(2, 'months').format('YYYY-MM-DD'); // 专利到期前一个月 const start_thr = moment(end).subtract(1, 'months').format('YYYY-MM-DD'); // 判断是否是三个月的区间 const r = moment().isBetween(start, end, null, '[]'); // 判断是否在一个月的区间 if (r) { // 三个月内的第一天||两个月内第一天||一个月内的每天 发送消息 // 是否发送的变量 let dr = false; const toDay = moment().format('YYYY-MM-DD'); if (toDay === start || toDay === start_two || toDay === start_thr) { dr = true; } // 不发就继续 if (!dr) continue; const { user_id } = i; const users = user_id.map(i => i.user_id); // 判断预警次数 const early_num = toDay === start ? 1 : toDay === start_two ? 2 : toDay === start_thr ? 3 : ''; // 截止日期 const lose_date = end; const content = '您可能需缴年费了,具体以缴费通知书为准 '; const nobj = { ..._.omit(i, [ '_id', 'id', 'users' ]), content, parent_id: i._id, user_id: users, early_num, lose_date, }; this.model.create(nobj); // 2021-11-04添加 向 patentexamine 表中添加数据 const patentexamineArray = []; for (const i of user_id) { const { user_id } = i; // const peContent = `【${name}】 专利即将过期,请用户及时到相应功能模块中处理专利信息!`; const peContent = '您可能需缴年费了,具体以缴费通知书为准'; const patentexamineObject = { to: user_id, content: peContent, }; patentexamineArray.push(patentexamineObject); } this.patentexamineModel.insertMany(patentexamineArray); } } catch (error) { continue; } } } } module.exports = PatentearlyService;