const schedule = require('node-schedule'); const moment = require('moment') let isDev = false module.exports = function (app, opts) { const workOrder = app.fs.scheduleInit( { interval: '0 * * * *',//一小时执行一次 immediate: isDev, proRun: !isDev, }, async()=>{ try{ //前一次执行时间 console.log('工单数据抽取开始') let lastExecutionTime = null; const { parseProcessData } = app.fs.utils const startTime = moment() const { models } = app.fs.dc const { clickHouse } = app.fs const { database: camWorkflow } = clickHouse.camWorkflow.opts.config //新建表是否有数据 const rescount=await models.FormDataTable.count() if (lastExecutionTime === null) { lastExecutionTime = moment().subtract(1, 'hour'); } const formRes = await clickHouse.pepEmis.query( `SELECT story.id AS historyId, story.procinst_id as procinstId, story.apply_user AS pepUserId, story.form_data AS formData, story.submit_form_data AS submitFormData, story.create_at as createTime, fform.form_schema AS formSchema, fprocess.name AS processName, procin.state_ AS state, procin.end_time_ as endTime, fform.id AS formId, fversion.id AS versionId, fgroup.name AS groupName FROM workflow_process_history AS story INNER JOIN workflow_process_version AS fversion ON fversion.id = story.version_id INNER JOIN workflow_process_form AS fform ON fform.id = fversion.form_id INNER JOIN workflow_process AS fprocess ON fprocess.id = fform.process_id AND fprocess.name = '运维中台售后问题处理工单' INNER JOIN workflow_group AS fgroup ON fgroup.id = fprocess.group_id INNER JOIN ${camWorkflow}.act_hi_procinst AS procin ON procin.id_ = story.procinst_id ${rescount ?` WHERE procin.end_time_ is not null AND procin.end_time_ > '${lastExecutionTime}' AND procin.end_time_ <='${startTime}'` : ''}` ).toPromise() console.log('formRes1',formRes) const procinstIds = [...new Set(formRes.map(e => e.procinstId))]; // 获取流程实例变量 if(formRes && formRes.length > 0){ let procinstsVariables = await ctx.app.camunda.request.post(encodeURI(`/engine-rest/history/variable-instance`), { processInstanceIdIn: procinstIds }) for (let f of formRes) { if(f?.formData?.formData && f?.formData?.workflowProcessVersion?.workflowProcessForm?.formSchema) { const parseData = parseProcessData({ formSchema: JSON.parse(f.formSchema), formData: JSON.parse(f.formData) }) await models.Workorder.create({ projectId:parseData.pomsProjectId.value || null, formname:procinstsVariables.find(item=>item.id===f.procinstId).name||null, state: f.state||null, endTime:f.endTime||null, startTime:f.createTime||null }) console.log('工单数据抽取结束') } } } }catch(error){ console.error(error) } } ); return { workOrder, } }