|
|
|
'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 { startTime, endTime, pageSize, pageIndex } = ctx.query
|
|
|
|
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'],
|
|
|
|
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 = []
|
|
|
|
recordRes.rows.forEach((item) => { 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)
|
|
|
|
const res = 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,
|
|
|
|
maintenanceRecordExecuteUsers:
|
|
|
|
item.maintenanceRecordExecuteUsers.map((item1) => {
|
|
|
|
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 }
|
|
|
|
ctx.status = 200
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: '获取服务记录失败'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//新增和编辑服务记录
|
|
|
|
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 } = params
|
|
|
|
const breakTime = (Date.parse(solvingTime) - Date.parse(occurrencTime)) / 1000
|
|
|
|
try {
|
|
|
|
//中断时长
|
|
|
|
//console.log('resss1', Date.parse(solvingTime), occurrencTime)
|
|
|
|
if (id) {
|
|
|
|
await models.MaintenanceRecord.update({
|
|
|
|
solvingTime, occurrenceTime: occurrencTime, sketch, record, settler, type, interruptDuration: breakTime
|
|
|
|
}, { 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 })
|
|
|
|
const recordId = aa.id
|
|
|
|
const resArry = settler.map((item) => {
|
|
|
|
return {
|
|
|
|
maintenanceRecordId: recordId, pepUserId: item
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await models.MaintenanceRecordExecuteUser.bulkCreate(resArry)
|
|
|
|
}
|
|
|
|
//console.log('params1', params)
|
|
|
|
|
|
|
|
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: `${msg}失败`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//删除服务记录
|
|
|
|
async function delRecord(ctx) {
|
|
|
|
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: `删除服务记录失败`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getRecord, addRecord, delRecord
|
|
|
|
};
|