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.
143 lines
3.8 KiB
143 lines
3.8 KiB
'use strict';
|
|
const moment = require('moment')
|
|
|
|
async function assessGet (ctx) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { unit, month, page, limit } = ctx.query;
|
|
|
|
let findOption = {
|
|
where: {
|
|
|
|
},
|
|
order: [['id', 'DESC']]
|
|
}
|
|
if (month) {
|
|
findOption.where.month = {
|
|
$between: [moment(month).startOf('month').format(), moment(month).endOf('month').format()]
|
|
}
|
|
}
|
|
if (unit) {
|
|
findOption.where.unit = unit
|
|
}
|
|
|
|
if (limit) {
|
|
findOption.limit = limit
|
|
}
|
|
if (page && limit) {
|
|
findOption.offset = (page - 1) * limit
|
|
}
|
|
|
|
const roadRes = await models.Assess.findAndCountAll(findOption)
|
|
|
|
ctx.status = 200;
|
|
ctx.body = roadRes
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
async function assessEdit (ctx) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const data = ctx.request.body;
|
|
|
|
const repeatRes = await models.Assess.findOne({
|
|
where: {
|
|
unit: data.unit,
|
|
month: { $between: [moment(data.month).startOf('month').format(), moment(data.month).endOf('month').format()] }
|
|
}
|
|
})
|
|
|
|
if (!data.assessId) {
|
|
if (repeatRes) {
|
|
throw '已有相同月份的考核记录'
|
|
}
|
|
|
|
await models.Assess.create(data)
|
|
} else {
|
|
if (repeatRes && repeatRes.id != data.assessId) {
|
|
throw '已有相同月份的考核记录'
|
|
}
|
|
await models.Assess.update(
|
|
data, {
|
|
where: {
|
|
id: data.assessId
|
|
}
|
|
})
|
|
}
|
|
|
|
ctx.status = 204
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
async function assessDel (ctx) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { assessId } = ctx.params;
|
|
|
|
await models.Assess.destroy({
|
|
where: {
|
|
id: assessId
|
|
}
|
|
})
|
|
|
|
ctx.status = 204
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
async function nearestSourceData (ctx) {
|
|
try {
|
|
const { models } = ctx.fs.dc;
|
|
const { monthRange } = ctx.query
|
|
|
|
// 查最近的有数据的月份的数据
|
|
const nearestRes = await models.Assess.findOne({
|
|
order: [['month', 'DESC']],
|
|
})
|
|
|
|
let rslt = []
|
|
if (nearestRes) {
|
|
rslt = await models.Assess.findAll({
|
|
attributes: ['totalPoints', 'unit', 'month'],
|
|
where: {
|
|
month: {
|
|
$between: [
|
|
moment(nearestRes.month).subtract(monthRange || 0, 'month').startOf('month').format(), moment(nearestRes.month).endOf('month').format()
|
|
]
|
|
}
|
|
},
|
|
order: [['month', 'DESC']],
|
|
})
|
|
}
|
|
|
|
ctx.status = 200;
|
|
ctx.body = rslt
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
assessGet, assessEdit, assessDel, nearestSourceData,
|
|
};
|