forked from VinokurovVE/tests
Map
This commit is contained in:
@ -1,14 +1,46 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import { tediousQuery } from '../../utils/tedious';
|
||||
import { GeneralDB } from '../../constants/db';
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/regions/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM ${GeneralDB}..vRegions;
|
||||
`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
} catch (err) {
|
||||
res.status(500)
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/districts/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { region_id } = req.query
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT c.*, d.name AS district_name
|
||||
FROM ${GeneralDB}..vCities c
|
||||
JOIN ${GeneralDB}..vDistricts d ON d.id_region = c.id_region AND d.id = c.id_district
|
||||
WHERE c.id_region = ${region_id};
|
||||
`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
} catch (err) {
|
||||
res.status(500)
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/cities/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { offset, limit, search, id } = req.query
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM nGeneral..Cities
|
||||
SELECT * FROM ${GeneralDB}..Cities
|
||||
${id ? `WHERE id = '${id}'` : ''}
|
||||
${search ? `WHERE name LIKE '%${search || ''}%'` : ''}
|
||||
ORDER BY id
|
||||
@ -26,7 +58,7 @@ router.get('/types/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM nGeneral..tTypes
|
||||
SELECT * FROM ${GeneralDB}..tTypes
|
||||
ORDER BY id
|
||||
`
|
||||
)
|
||||
@ -42,7 +74,7 @@ router.get('/objects/all', async (req: Request, res: Response) => {
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM nGeneral..vObjects
|
||||
SELECT * FROM ${GeneralDB}..vObjects
|
||||
${city_id ? `WHERE id_city = ${city_id}` : ''}
|
||||
ORDER BY object_id
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
@ -81,31 +113,57 @@ router.get('/objects/list', async (req: Request, res: Response) => {
|
||||
// ) = ${planning};
|
||||
// `
|
||||
`
|
||||
WITH cte_split(type_id, split_value, caption_params) AS
|
||||
(
|
||||
-- anchor member
|
||||
SELECT DISTINCT
|
||||
type_id,
|
||||
CAST(LEFT(caption_params, CHARINDEX(',', caption_params + ',') - 1) AS VARCHAR(255)), -- Explicitly casting to VARCHAR
|
||||
STUFF(caption_params, 1, CHARINDEX(',', caption_params + ','), '')
|
||||
FROM New_Gis..caption_params
|
||||
WHERE city_id = -1 AND user_id = -1
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- recursive member
|
||||
SELECT
|
||||
vObjects.*,
|
||||
CASE
|
||||
WHEN vObjects.boiler_id IS NOT NULL THEN vBoilers.name
|
||||
ELSE CAST(tValues.value AS varchar(max))
|
||||
END AS name
|
||||
FROM
|
||||
vObjects
|
||||
JOIN
|
||||
vBoilers ON vBoilers.id = vObjects.boiler_id
|
||||
JOIN
|
||||
tValues ON tValues.id_param = 4 AND tValues.id_object = vObjects.object_id
|
||||
type_id,
|
||||
CAST(LEFT(caption_params, CHARINDEX(',', caption_params + ',') - 1) AS VARCHAR(255)), -- Explicitly casting to VARCHAR
|
||||
STUFF(caption_params, 1, CHARINDEX(',', caption_params + ','), '')
|
||||
FROM cte_split
|
||||
WHERE caption_params > ''
|
||||
)
|
||||
SELECT
|
||||
o.object_id,
|
||||
o.type,
|
||||
o.id_city,
|
||||
o.year,
|
||||
o.planning,
|
||||
string_agg(cast(v.value as varchar), ',') as caption
|
||||
FROM ${GeneralDB}..vObjects o
|
||||
JOIN cte_split c ON o.type = c.type_id
|
||||
JOIN ${GeneralDB}..tParameters p ON p.id = split_value
|
||||
JOIN ${GeneralDB}..tValues v
|
||||
ON
|
||||
v.id_param = split_value
|
||||
AND v.id_object = o.object_id
|
||||
AND (v.date_po IS NULL)
|
||||
AND (v.date_s < DATEFROMPARTS(${Number(year) + 1},01,01))
|
||||
|
||||
WHERE
|
||||
vObjects.id_city = ${city_id}
|
||||
AND vObjects.year = ${year}
|
||||
AND type = ${type}
|
||||
o.id_city = ${city_id}
|
||||
AND o.year = ${year}
|
||||
AND o.type = ${type}
|
||||
AND
|
||||
(
|
||||
CASE
|
||||
WHEN TRY_CAST(vObjects.planning AS BIT) IS NOT NULL THEN TRY_CAST(vObjects.planning AS BIT)
|
||||
WHEN vObjects.planning = 'TRUE' THEN 1
|
||||
WHEN vObjects.planning = 'FALSE' THEN 0
|
||||
WHEN TRY_CAST(o.planning AS BIT) IS NOT NULL THEN TRY_CAST(o.planning AS BIT)
|
||||
WHEN o.planning = 'TRUE' THEN 1
|
||||
WHEN o.planning = 'FALSE' THEN 0
|
||||
ELSE NULL
|
||||
END
|
||||
) = ${planning};
|
||||
) = ${planning}
|
||||
GROUP BY object_id, type, id_city, year, planning;
|
||||
`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
@ -149,7 +207,7 @@ router.get('/objects/:id([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM nGeneral..vObjects
|
||||
SELECT * FROM ${GeneralDB}..vObjects
|
||||
${id ? `WHERE object_id = '${id}'` : ''}
|
||||
`
|
||||
)
|
||||
@ -179,9 +237,9 @@ router.get('/values/all', async (req: Request, res: Response) => {
|
||||
date_po,
|
||||
id_user
|
||||
FROM
|
||||
nGeneral..tValues v
|
||||
${GeneralDB}..tValues v
|
||||
JOIN
|
||||
nGeneral..tParameters p ON v.id_param = p.id
|
||||
${GeneralDB}..tParameters p ON v.id_param = p.id
|
||||
WHERE id_object = '${object_id}'
|
||||
`
|
||||
)
|
||||
@ -202,7 +260,7 @@ router.get('/params/all', async (req: Request, res: Response) => {
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM nGeneral..tParameters
|
||||
SELECT * FROM ${GeneralDB}..tParameters
|
||||
WHERE id = '${param_id}'
|
||||
`
|
||||
)
|
||||
@ -216,11 +274,11 @@ router.get('/params/all', async (req: Request, res: Response) => {
|
||||
const tcbParamQuery = (vtable: string, id_city: string) => {
|
||||
switch (vtable) {
|
||||
case 'vStreets':
|
||||
return `SELECT * FROM nGeneral..${vtable} WHERE id_city = ${id_city};`
|
||||
return `SELECT * FROM ${GeneralDB}..${vtable} WHERE id_city = ${id_city};`
|
||||
case 'vBoilers':
|
||||
return `SELECT * FROM nGeneral..${vtable} WHERE id_city = ${id_city};`
|
||||
return `SELECT * FROM ${GeneralDB}..${vtable} WHERE id_city = ${id_city};`
|
||||
default:
|
||||
return `SELECT * FROM nGeneral..${vtable};`
|
||||
return `SELECT * FROM ${GeneralDB}..${vtable};`
|
||||
}
|
||||
}
|
||||
|
||||
@ -236,7 +294,7 @@ router.get('/params/tcb', async (req: Request, res: Response) => {
|
||||
if (id) {
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM nGeneral..${vtable}
|
||||
SELECT * FROM ${GeneralDB}..${vtable}
|
||||
WHERE id = '${id}'
|
||||
`
|
||||
)
|
||||
@ -273,8 +331,8 @@ router.get('/search/objects', async (req: Request, res: Response) => {
|
||||
ROW_NUMBER() OVER (PARTITION BY id_object ORDER BY date_s DESC) AS rn,
|
||||
o.id_city AS id_city,
|
||||
o.year AS year
|
||||
FROM nGeneral..tValues
|
||||
JOIN nGeneral..tObjects o ON o.id = id_object
|
||||
FROM ${GeneralDB}..tValues
|
||||
JOIN ${GeneralDB}..tObjects o ON o.id = id_object
|
||||
WHERE CAST(value AS varchar(max)) LIKE '%${q}%'
|
||||
)
|
||||
SELECT
|
||||
|
@ -1,5 +1,6 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import { tediousQuery } from '../../utils/tedious';
|
||||
import { GeneralDB, GisDB } from '../../constants/db';
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/images/all', async (req: Request, res: Response) => {
|
||||
@ -8,7 +9,7 @@ router.get('/images/all', async (req: Request, res: Response) => {
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM New_Gis..images
|
||||
SELECT * FROM ${GisDB}..images
|
||||
${city_id ? `WHERE city_id = ${city_id}` : ''}
|
||||
ORDER BY city_id
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
@ -29,8 +30,8 @@ router.get('/figures/all', async (req: Request, res: Response) => {
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM New_Gis..figures f
|
||||
JOIN nGeneral..vObjects o ON f.object_id = o.object_id WHERE o.id_city = ${city_id} AND f.year = ${year}
|
||||
SELECT * FROM ${GisDB}..figures f
|
||||
JOIN ${GeneralDB}..vObjects o ON f.object_id = o.object_id WHERE o.id_city = ${city_id} AND f.year = ${year}
|
||||
ORDER BY f.year
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
@ -49,8 +50,8 @@ router.get('/lines/all', async (req: Request, res: Response) => {
|
||||
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM New_Gis..lines l
|
||||
JOIN nGeneral..vObjects o ON l.object_id = o.object_id WHERE o.id_city = ${city_id} AND l.year = ${year}
|
||||
SELECT * FROM ${GisDB}..lines l
|
||||
JOIN ${GeneralDB}..vObjects o ON l.object_id = o.object_id WHERE o.id_city = ${city_id} AND l.year = ${year}
|
||||
ORDER BY l.year
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
@ -62,4 +63,16 @@ router.get('/lines/all', async (req: Request, res: Response) => {
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/regions/borders', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await tediousQuery(
|
||||
`
|
||||
SELECT * FROM ${GisDB}..visual_regions`
|
||||
)
|
||||
res.status(200).json(result)
|
||||
} catch (err) {
|
||||
res.status(500)
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
7
ems/src/constants/db.ts
Normal file
7
ems/src/constants/db.ts
Normal file
@ -0,0 +1,7 @@
|
||||
const GeneralDB = 'nGeneral'
|
||||
const GisDB = 'New_Gis'
|
||||
|
||||
export {
|
||||
GeneralDB,
|
||||
GisDB
|
||||
}
|
Reference in New Issue
Block a user