Files
Sheep-Service/api/services/houses.service.js
2025-03-31 00:22:21 +03:00

233 lines
8.9 KiB
JavaScript

const db = require("../config/db");
class HousesService {
getList(group, sheepName) {
return new Promise((res, rej) => {
let sql = `
SELECT
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
`;
if (group != "0" && !sheepName) {
sql = `
SELECT
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
WHERE
group_id == '${group}'
`;
}
if (sheepName) {
sql = `
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
JOIN
entrance ON entrance.house_id = house.id
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}');
`;
}
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),
"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),
"osm_id": JSON.parse(row.osm_id),
"settlement": row.settlement,
"description": row.description,
"created_at": Number(row.created_at),
"updated_at": Number(row.updated_at),
"entrance": {
"quantity": Number(row.entrance_quantity),
"working": Number(row.working)
}
}
})
return res(data);
}
});
});
}
getHouse(house_id) {
return new Promise((res, rej) => {
let sql = `
SELECT
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
WHERE
house.id = '${house_id}'
`;
db.get(sql, (err, row) => {
if (err) {
console.error(err.message);
return res(false);
} else if (!row) {
console.log({ "error": "house not found" });
return res(false);
} 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),
"osm_id": JSON.parse(row.osm_id),
"settlement": row.settlement,
"description": row.description,
"updated_at": Number(row.updated_at),
"entrance": {
"quantity": Number(row.entrance_quantity),
"working": Number(row.working) == 0 ? false : true
}
}
res(data);
}
});
});
}
createHouse(data) {
return new Promise((res, rej) => {
let sql = `
INSERT INTO
house(
group_id,
title,
number,
points,
points_number,
geo,
osm_id,
settlement,
description,
created_at,
updated_at
)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`;
db.run(sql, [
Number(data.group_id),
data.title,
data.number,
JSON.stringify(data.points),
JSON.stringify(data.points_number),
JSON.stringify(data.geo),
JSON.stringify(data.osm_id),
data.settlement,
data.description,
Math.floor(new Date(Date.now()).getTime()),
Math.floor(new Date(Date.now()).getTime()),
], function (err) {
if (err) {
console.error(err.message);
return res(false);
} else if (this.changes === 0) {
return res(false);
} else {
res({ "status": "ok", "id": this.lastID });
}
});
});
}
updateHouse(house_id, data) {
return new Promise((res, rej) => {
let sql = `
UPDATE
house
SET
group_id = ?,
title = ?,
number = ?,
points = ?,
points_number = ?,
geo = ?,
osm_id = ?,
settlement = ?,
description = ?,
updated_at = ?
WHERE
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),
JSON.stringify(data.osm_id),
data.settlement,
data.description,
Math.floor(new Date(Date.now()).getTime()),
house_id
], function (err) {
if (err) {
console.error(err.message);
return res(false);
} else if (this.changes === 0) {
return res(false);
} else {
res({ "status": "ok", "id": house_id });
}
});
});
}
deleteHouse(house_id) {
return new Promise((res, rej) => {
db.run('DELETE FROM house WHERE id = ?', [house_id], function (err) {
if (err) {
console.error(err.message);
return res(false);
} else if (this.changes === 0) {
return res(false);
} else {
res({ "status": "ok", "id": house_id });
}
});
});
}
}
module.exports = new HousesService();