'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.filter((ac) => { return ac.id == item1.pepUserId })[0] 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 } = params try { //console.log('params1', params) const aa = await models.MaintenanceRecord.create({ solvingTime, occurrenceTime: occurrencTime, sketch, record, settler, type }) const recordId = aa.id const resArry = settler.map((item) => { return { maintenanceRecordId: recordId, pepUserId: item } }) await models.MaintenanceRecordExecuteUser.bulkCreate(resArry) 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 };