运维服务中台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.8 KiB

1 year ago
const schedule = require('node-schedule');
const moment = require('moment')
let isDev = true
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
1 year ago
AND procin.state_='COMPLETED'
${rescount ?` WHERE procin.end_time_ is not null
AND procin.end_time_ > '${lastExecutionTime}'
AND procin.end_time_ <='${startTime}'`
: ''}`
).toPromise()
const procinstIds = [...new Set(formRes.map(e => e.procinstId))];
1 year ago
console.log('formRes1',procinstIds)
// 获取流程实例变量
if(formRes && formRes.length > 0){
let procinstsVariables = await ctx.app.camunda.request.post(encodeURI(`/engine-rest/history/variable-instance`), {
processInstanceIdIn: procinstIds
})
1 year ago
console.log('formRes2',procinstsVariables)
for (let f of formRes) {
const parseData = parseProcessData({
formSchema: JSON.parse(f.formSchema),
formData: JSON.parse(f.formData)
})
1 year ago
const res=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
})
1 year ago
console.log('formRes3',res)
console.log('工单数据抽取结束')
1 year ago
}
}
}catch(error){
1 year ago
console.error('失败原因',error)
}
}
);
return {
workOrder,
}
}