|
|
|
'use strict';
|
|
|
|
const { QueryTypes } = require('sequelize');
|
|
|
|
|
|
|
|
async function reportList (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { limit, page, startTime, endTime, keyword, userId, reportType } = ctx.query
|
|
|
|
let findOption = {
|
|
|
|
where: {
|
|
|
|
|
|
|
|
},
|
|
|
|
attributes: ['id', 'road', 'time', 'projectType', 'roadSectionStart', 'roadSectionEnd', 'reportType', 'content'],
|
|
|
|
include: [{
|
|
|
|
model: models.User,
|
|
|
|
attributes: ['name']
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
if (limit) {
|
|
|
|
findOption.limit = limit
|
|
|
|
}
|
|
|
|
if (page && limit) {
|
|
|
|
findOption.offset = page * limit
|
|
|
|
}
|
|
|
|
if (startTime && endTime) {
|
|
|
|
findOption.where = {
|
|
|
|
time: {
|
|
|
|
'$between': [startTime, endTime]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (keyword) {
|
|
|
|
findOption.where.road = {
|
|
|
|
'$like': `%${keyword}%`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (userId) {
|
|
|
|
findOption.where.userId = userId
|
|
|
|
}
|
|
|
|
if (reportType) {
|
|
|
|
findOption.where.reportType = reportType
|
|
|
|
}
|
|
|
|
const reportRes = await models.Report.findAll(findOption)
|
|
|
|
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = reportRes
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: typeof error == 'string' ? error : undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function reportPosition (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { startTime, endTime, userId, reportType } = ctx.query
|
|
|
|
const sequelize = ctx.fs.dc.ORM;
|
|
|
|
|
|
|
|
let findMxTimeOption = {
|
|
|
|
attributes: [
|
|
|
|
'userId',
|
|
|
|
[sequelize.fn('MAX', sequelize.col('time')), 'maxTime'],
|
|
|
|
],
|
|
|
|
where: {
|
|
|
|
|
|
|
|
},
|
|
|
|
group: ['report.user_id'],
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startTime && endTime) {
|
|
|
|
findMxTimeOption.where = {
|
|
|
|
time: {
|
|
|
|
'$between': [startTime, endTime]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userId) {
|
|
|
|
findMxTimeOption.where.userId = userId
|
|
|
|
}
|
|
|
|
if (reportType) {
|
|
|
|
findMxTimeOption.where.reportType = reportType
|
|
|
|
}
|
|
|
|
|
|
|
|
const reportMaxTimeRes = await models.Report.findAll(findMxTimeOption)
|
|
|
|
const timeArr = reportMaxTimeRes.map(item => item.dataValues.maxTime)
|
|
|
|
const reportRes = await models.Report.findAll({
|
|
|
|
where: {
|
|
|
|
time: { '$in': timeArr }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = reportRes
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: typeof error == 'string' ? error : undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function reportDetail (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { reportId } = ctx.params
|
|
|
|
|
|
|
|
const reportRes = await models.Report.findOne({
|
|
|
|
where: {
|
|
|
|
id: reportId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = reportRes
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: typeof error == 'string' ? error : undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createReport (ctx) {
|
|
|
|
try {
|
|
|
|
const { userId } = ctx.fs.api
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const data = ctx.request.body;
|
|
|
|
|
|
|
|
await models.Report.create({
|
|
|
|
...data,
|
|
|
|
userId,
|
|
|
|
time: new Date(),
|
|
|
|
})
|
|
|
|
|
|
|
|
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 deleteReport (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { reportId } = ctx.params;
|
|
|
|
|
|
|
|
await models.Report.destroy({
|
|
|
|
where: {
|
|
|
|
id: reportId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO 小程序填写道路名称的时候的道路筛选 是一起都返回 还是不断传关键字搜索返回
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
reportList,
|
|
|
|
reportPosition,
|
|
|
|
reportDetail, createReport, deleteReport,
|
|
|
|
};
|