'use strict'; const moment = require('moment') async function overspeedGet (ctx) { try { const models = ctx.fs.dc.models; const { limit, page, nameOfInspectionPoint, licensePlate, numberOfAxles, overrunRateUpper, overrunRateFloor, testTime } = ctx.query const sequelize = ctx.fs.dc.ORM; let findOption = { where: { }, order: [['id', 'DESC']], } if (limit) { findOption.limit = limit } if (page && limit) { findOption.offset = page * limit } if (nameOfInspectionPoint || licensePlate) { findOption.where['$or'] = {} if (nameOfInspectionPoint) { findOption.where['$or']. nameOfInspectionPoint = { $like: `%${nameOfInspectionPoint}%` } } if (licensePlate) { findOption.where['$or']. licensePlate = { $like: `%${licensePlate}%` } } } if (numberOfAxles) { findOption.where.numberOfAxles = numberOfAxles } if (overrunRateUpper && overrunRateFloor) { findOption.where['$and'] = [] findOption.where['$and'].push ({ overrunRate: { $lte: overrunRateUpper } }) findOption.where['$and'].push ({ overrunRate: { $gte: overrunRateFloor } }) } else { if (overrunRateUpper) { findOption.where.overrunRate = { $lte: overrunRateUpper } } if (overrunRateFloor) { findOption.where.overrunRate = { $gte: overrunRateFloor } } } if (testTime) { findOption.where.testTime = { $between: [moment(testTime).startOf('day').format(), moment(testTime).endOf('day').format()] } } const overspeedRes = await models.Overspeed.findAndCountAll(findOption) ctx.status = 200; ctx.body = overspeedRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function overspeedEdit (ctx) { try { const models = ctx.fs.dc.models; const data = ctx.request.body; if (!data.overspeedId) { await models.Overspeed.create(data) } else { await models.Overspeed.update( data, { where: { id: data.overspeedId } }) } 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 overspeedDel (ctx) { try { const models = ctx.fs.dc.models; const { overspeedId } = ctx.params; await models.Overspeed.destroy({ where: { id: overspeedId } }) 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 } } } module.exports = { overspeedGet, overspeedEdit, overspeedDel, };