This commit is contained in:
2025-09-09 00:10:53 +03:00
parent 38f2a05107
commit 204fc092d7
239 changed files with 22447 additions and 9536 deletions

View File

@@ -1,6 +1,68 @@
const db = require("../config/db");
class HousesService {
getListEntrance() {
return new Promise((res, rej) => {
let sql = `
SELECT
entrance.*,
COALESCE((SELECT entrance_history.working FROM entrance_history WHERE entrance_history.entrance_id = entrance.id ORDER BY entrance_history.date_start DESC LIMIT 1), 0) AS working,
(SELECT entrance_history.name FROM entrance_history WHERE entrance_history.entrance_id = entrance.id ORDER BY entrance_history.date_start DESC LIMIT 1) AS entrance_history_name,
(SELECT entrance_history.group_id FROM entrance_history WHERE entrance_history.entrance_id = entrance.id ORDER BY entrance_history.date_start DESC LIMIT 1) AS entrance_history_group_id,
(SELECT entrance_history.sheep_id FROM entrance_history WHERE entrance_history.entrance_id = entrance.id ORDER BY entrance_history.date_start DESC LIMIT 1) AS entrance_history_sheep_id,
(SELECT entrance_history.id FROM entrance_history WHERE entrance_history.entrance_id = entrance.id ORDER BY entrance_history.date_start DESC LIMIT 1) AS entrance_history_id,
(SELECT entrance_history.date_start FROM entrance_history WHERE entrance_history.entrance_id = entrance.id ORDER BY entrance_history.date_start DESC LIMIT 1) AS entrance_history_date_start,
(SELECT entrance_history.date_end FROM entrance_history WHERE entrance_history.entrance_id = entrance.id ORDER BY entrance_history.date_start DESC LIMIT 1) AS entrance_history_date_end,
(SELECT house.settlement FROM house WHERE house.id = entrance.house_id) AS house_settlement,
(SELECT house.title FROM house WHERE house.id = entrance.house_id) AS house_title,
(SELECT house.number FROM house WHERE house.id = entrance.house_id) AS house_number
FROM
entrance
`;
db.all(sql, (err, rows) => {
if (err) {
console.error(err.message);
return res(false);
} else {
let data = rows.map((row) => {
return {
"id": Number(row.id),
"house": {
"id": Number(row.house_id),
"title": row.house_title,
"number": row.house_number,
"settlement": row.house_settlement
},
"entrance_number": Number(row.entrance_number),
"title": row.title,
"points": JSON.parse(row.points),
"points_number": JSON.parse(row.points_number),
"floors_quantity": row.floors_quantity,
"apartments_quantity": row.apartments_quantity,
"description": row.description,
"created_at": Number(row.created_at),
"updated_at": Number(row.updated_at),
"working": Number(row.working) == 0 ? false : true,
"history": {
"id": row.entrance_history_id ? Number(row.entrance_history_id) : null,
"name": row.entrance_history_name,
"group_id": row.entrance_history_group_id ? Number(row.entrance_history_group_id) : null,
"sheep_id": row.entrance_history_sheep_id ? Number(row.entrance_history_sheep_id) : null,
"date": {
"start": row.entrance_history_date_start ? Number(row.entrance_history_date_start) : null,
"end": row.entrance_history_date_end ? Number(row.entrance_history_date_end) : null
}
}
}
})
return res(data);
}
});
});
}
getList(group, sheepName) {
return new Promise((res, rej) => {
let sql = `
@@ -14,18 +76,25 @@ class HousesService {
if (group != "0" && !sheepName) {
sql = `
SELECT
SELECT DISTINCT
house.*,
(SELECT COUNT(DISTINCT entrance_history.id) FROM entrance_history JOIN entrance ON entrance.id = entrance_history.entrance_id WHERE entrance.house_id = house.id AND entrance_history.working = 1 ORDER BY entrance_history.date_start DESC) AS working,
(SELECT COUNT(*) FROM entrance WHERE entrance.house_id = house.id) AS entrance_quantity
FROM
house
house
JOIN
entrance ON entrance.house_id = house.id
JOIN
entrance_history ON entrance_history.entrance_id = entrance.id
WHERE
group_id == '${group}'
entrance_history.working = 1
AND
entrance_history.name = 'Групова'
AND
entrance_history.group_id == '${group}'
`;
}
}
if (sheepName) {
sql = `
SELECT DISTINCT
@@ -39,11 +108,9 @@ class HousesService {
JOIN
entrance_history ON entrance_history.entrance_id = entrance.id
WHERE
house.group_id = '${group}'
AND
entrance_history.working = 1
AND
entrance_history.name IN ('Групова', '${sheepName}');
entrance_history.name = '${sheepName}'
`;
}
@@ -55,12 +122,12 @@ class HousesService {
let data = rows.map((row) => {
return {
"id": Number(row.id),
"group_id": Number(row.group_id),
"title": row.title,
"number": row.number,
"points": JSON.parse(row.points),
"points_number": JSON.parse(row.points_number),
"geo": JSON.parse(row.geo),
"zoom": Number(row.zoom),
"osm_id": JSON.parse(row.osm_id),
"settlement": row.settlement,
"description": row.description,
@@ -102,12 +169,12 @@ class HousesService {
} else {
let data = {
"id": Number(row.id),
"group_id": Number(row.group_id),
"title": row.title,
"number": row.number,
"points": JSON.parse(row.points),
"points_number": JSON.parse(row.points_number),
"geo": JSON.parse(row.geo),
"zoom": Number(row.zoom),
"osm_id": JSON.parse(row.osm_id),
"settlement": row.settlement,
"description": row.description,
@@ -129,12 +196,12 @@ class HousesService {
let sql = `
INSERT INTO
house(
group_id,
title,
number,
points,
points_number,
geo,
zoom
osm_id,
settlement,
description,
@@ -146,12 +213,12 @@ class HousesService {
`;
db.run(sql, [
Number(data.group_id),
data.title,
data.number,
JSON.stringify(data.points),
JSON.stringify(data.points_number),
JSON.stringify(data.geo),
Number(data.zoom),
JSON.stringify(data.osm_id),
data.settlement,
data.description,
@@ -176,12 +243,12 @@ class HousesService {
UPDATE
house
SET
group_id = ?,
title = ?,
number = ?,
points = ?,
points_number = ?,
geo = ?,
zoom = ?,
osm_id = ?,
settlement = ?,
description = ?,
@@ -190,12 +257,12 @@ class HousesService {
id = ?
`;
db.run(sql, [
Number(data.group_id),
data.title,
data.number,
JSON.stringify(data.points),
JSON.stringify(data.points_number),
JSON.stringify(data.geo),
Number(data.zoom),
JSON.stringify(data.osm_id),
data.settlement,
data.description,