运维服务中台
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.

183 lines
6.5 KiB

'use strict';
const moment = require('moment');
async function getRecord (ctx) {
try {
const { redis } = ctx.app
const { models } = ctx.fs.dc;
const sequelize = ctx.fs.dc.ORM;
const { clickHouse } = ctx.app.fs
const { startTime, endTime, pageSize, pageIndex } = ctx.query
2 years ago
console.log('queryz', ctx.query)
let resCount = await models.MaintenanceRecord.count({
where: {
$and: [
sequelize.where(sequelize.fn('date', sequelize.col('occurrence_time')), '>=', moment(startTime).format('YYYY-MM-DD')),
sequelize.where(sequelize.fn('date', sequelize.col('occurrence_time')), '<=', moment(endTime).format('YYYY-MM-DD')),
]
}
})
let recordRes = await models.MaintenanceRecord.findAndCountAll({
order: [['id', 'DESC']],
attributes: ['id', 'sketch', 'occurrenceTime', 'solvingTime', 'interruptDuration', 'type', 'record','files'],
offset: (pageIndex - 1) * pageSize,
limit: pageSize,
where: {
$and: [
sequelize.where(sequelize.fn('date', sequelize.col('occurrence_time')), '>=', moment(startTime).format('YYYY-MM-DD')),
sequelize.where(sequelize.fn('date', sequelize.col('occurrence_time')), '<=', moment(endTime).format('YYYY-MM-DD')),
]
},
include: [{
attributes: ['id', 'maintenanceRecordId', 'pepUserId'],
model: models.MaintenanceRecordExecuteUser
}]
})
//console.log('recordRes', recordRes)
const arrayUserId = []
const recordId = []
recordRes.rows.forEach((item) => {
recordId.push(item.id)
item.maintenanceRecordExecuteUsers.forEach((item1) => { arrayUserId.push(item1.pepUserId) })
})
const arrayUserIdCopy = [...new Set(arrayUserId)]
// console.log('(' + arrayUserIdCopy.toString() + ')', '22222')
let userRes = await redis.get('allUser')
userRes = JSON.parse(userRes)
userRes = userRes.filter((item) => {
return arrayUserIdCopy.some((children) => { return children == item.id })
})
//console.log('userRes', userRes)
let planList = await models.MaintenancePlan.findAll({
where: { recordId: { $overlap: recordId } },
})
const res = await recordRes.rows.map((item) => {
return {
id: item.id,
interruptDuration: item.interruptDuration,
occurrenceTime: item.occurrenceTime,
record: item.record,
sketch: item.sketch,
solvingTime: item.solvingTime,
type: item.type,
files: item.files || null,
planList: planList.filter(v => v.recordId && v.recordId.includes(item.id)) || [],
maintenanceRecordExecuteUsers:
item.maintenanceRecordExecuteUsers.map((item1) => {
2 years ago
const userArr = userRes.find((ac) => { return ac.id == item1.pepUserId })
return {
id: item1.id,
maintenanceRecordId: item1.maintenanceRecordId,
pepUserId: item1.pepUserId,
name: userArr ? userArr.name : ''
}
})
}
}
)
//console.log('res1', res)
ctx.body = { count: resCount, res: res }
ctx.status = 200
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: '获取服务记录失败'
}
}
}
2 years ago
//新增和编辑服务记录
async function addRecord (ctx) {
const transaction = await ctx.fs.dc.orm.transaction();
const { models } = ctx.fs.dc
const params = ctx.request.body
const { solvingTime, occurrencTime, sketch, record, settler, type, id, msg, files } = params
const breakTime = (Date.parse(solvingTime) - Date.parse(occurrencTime)) / 1000
try {
//中断时长
//console.log('resss1', Date.parse(solvingTime), occurrencTime)
2 years ago
if (id) {
await models.MaintenanceRecord.update({
solvingTime, occurrenceTime: occurrencTime, sketch, record, settler, type, interruptDuration: breakTime, files: files
2 years ago
}, { where: { id } })
await models.MaintenanceRecordExecuteUser.destroy({ where: { maintenanceRecordId: id } })
const resArry = settler.map((item) => {
return {
maintenanceRecordId: id, pepUserId: item
}
})
await models.MaintenanceRecordExecuteUser.bulkCreate(resArry)
} else {
const aa = await models.MaintenanceRecord.create({ solvingTime, occurrenceTime: occurrencTime, sketch, record, settler, type, interruptDuration: breakTime,files: files })
2 years ago
const recordId = aa.id
const resArry = settler.map((item) => {
return {
maintenanceRecordId: recordId, pepUserId: item
}
})
await models.MaintenanceRecordExecuteUser.bulkCreate(resArry)
}
//console.log('params1', params)
2 years ago
await transaction.commit();
ctx.status = 200
} catch (error) {
await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
2 years ago
message: `${msg}失败`
}
}
}
//删除服务记录
async function delRecord (ctx) {
2 years ago
const transaction = await ctx.fs.dc.orm.transaction();
const { models } = ctx.fs.dc
const params = ctx.params
console.log('params', params)
try {
await models.MaintenanceRecordExecuteUser.destroy({ where: { maintenanceRecordId: params.id } })
await models.MaintenanceRecord.destroy({ where: { id: params.id } })
await transaction.commit();
ctx.status = 200
} catch (error) {
await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`)
ctx.status = 400
ctx.body = {
message: `删除服务记录失败`
}
}
}
async function respondRecord (ctx) {
try {
const { models } = ctx.fs.dc;
let res = await models.MaintenanceRecord.findAll({
order: [['id', 'DESC']],
attributes: ['id', 'sketch', 'occurrenceTime'],
})
ctx.body = res
ctx.status = 200
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: '获取响应记录数据失败'
}
}
}
module.exports = {
getRecord,
addRecord,
delRecord,
respondRecord
};