200 lines
8.4 KiB
JavaScript
200 lines
8.4 KiB
JavaScript
const db = require("../config/db");
|
|
|
|
class HistoryEntranceService {
|
|
getHistoryEntrance(entrance_id) {
|
|
return new Promise((res, rej) => {
|
|
let sql = `
|
|
SELECT
|
|
*
|
|
FROM
|
|
entrance_history
|
|
WHERE
|
|
entrance_history.entrance_id = '${entrance_id}'
|
|
ORDER BY
|
|
entrance_history.date_start
|
|
`;
|
|
|
|
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),
|
|
"entrance_id": Number(row.entrance_id),
|
|
"name": row.name,
|
|
"group_id": Number(row.group_id),
|
|
"sheep_id": Number(row.sheep_id),
|
|
"working": Number(row.working) == 0 ? false : true,
|
|
"date": {
|
|
"start": Number(row.date_start),
|
|
"end": row.date_end ? Number(row.date_end) : null
|
|
}
|
|
}
|
|
})
|
|
|
|
return res(data);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
createHistoryEntrance(entrance_id, data) {
|
|
return new Promise((res, rej) => {
|
|
let sql = 'INSERT INTO entrance_history(entrance_id, name, date_start, group_id, sheep_id, working) VALUES (?, ?, ?, ?, ?, ?)';
|
|
|
|
db.run(sql, [
|
|
entrance_id,
|
|
data.name,
|
|
Math.floor(new Date(Date.now()).getTime()),
|
|
Number(data.group_id),
|
|
Number(data.sheep_id),
|
|
1
|
|
], function (err) {
|
|
if (err) {
|
|
console.error(err.message);
|
|
return res(false);
|
|
} else if (this.changes === 0) {
|
|
return res(false);
|
|
} else {
|
|
res({ "create": "ok", "id": this.lastID });
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
updateHistoryEntrance(entrance_history_id) {
|
|
return new Promise((res, rej) => {
|
|
const endTime = Date.now(); // 🕒 Час завершення
|
|
const entranceHistoryId = Number(entrance_history_id);
|
|
|
|
// 🔧 Оновлюємо запис в entrance_history
|
|
db.run(
|
|
`UPDATE entrance_history SET date_end = ?, working = 0 WHERE id = ?`,
|
|
[endTime, entranceHistoryId],
|
|
function (err) {
|
|
if (err || this.changes === 0) {
|
|
console.error("❌ Помилка оновлення entrance_history:", err?.message);
|
|
return res(false);
|
|
}
|
|
|
|
// 🏢 Отримуємо entrance_id
|
|
db.get(
|
|
`SELECT entrance_id FROM entrance_history WHERE id = ?`,
|
|
[entranceHistoryId],
|
|
(err, entrance) => {
|
|
if (err || !entrance) {
|
|
console.error("❌ Помилка отримання під’їзду:", err?.message);
|
|
return res(false);
|
|
}
|
|
|
|
// 🧱 Отримуємо квартири
|
|
db.all(
|
|
`SELECT * FROM apartments WHERE entrance_id = ? ORDER BY id`,
|
|
[entrance.entrance_id],
|
|
(err, apartments) => {
|
|
if (err) {
|
|
console.error("❌ Помилка отримання квартир:", err.message);
|
|
return res(false);
|
|
}
|
|
|
|
if (!apartments.length) {
|
|
return res({ update: "ok", id: entranceHistoryId, inserted: 0 });
|
|
}
|
|
|
|
const toUpdate = apartments.filter(a => ![2, 6, 0, null].includes(a.status));
|
|
|
|
// 📝 Готуємо вставку в історію
|
|
const stmtInsert = db.prepare(`
|
|
INSERT INTO apartments_history
|
|
(sheep_id, apartments_id, status, description, created_at)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
`);
|
|
|
|
const stmtUpdate = db.prepare(`
|
|
UPDATE apartments
|
|
SET status = NULL, description = NULL, sheep_id = NULL, updated_at = ?
|
|
WHERE id = ?
|
|
`);
|
|
|
|
let completed = 0;
|
|
let inserted = 0;
|
|
|
|
if (!toUpdate.length) {
|
|
stmtInsert.finalize();
|
|
stmtUpdate.finalize();
|
|
return res({ update: "ok", id: entranceHistoryId, inserted: 0 });
|
|
}
|
|
|
|
for (const apt of toUpdate) {
|
|
// 🔄 Оновлюємо квартиру
|
|
stmtUpdate.run([endTime, apt.id]);
|
|
|
|
// 🪵 Вставляємо в історію
|
|
stmtInsert.run(
|
|
[apt.sheep_id, apt.id, apt.status, apt.description, endTime],
|
|
(err) => {
|
|
if (!err) inserted++;
|
|
completed++;
|
|
if (completed === toUpdate.length) {
|
|
stmtInsert.finalize();
|
|
stmtUpdate.finalize();
|
|
return res({
|
|
update: "ok",
|
|
id: entranceHistoryId,
|
|
inserted,
|
|
});
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
);
|
|
}
|
|
);
|
|
});
|
|
|
|
|
|
|
|
|
|
// return new Promise((res, rej) => {
|
|
// console.log(Number(entrance_id));
|
|
|
|
// let sql = 'UPDATE entrance_history SET date_end = ?, working = ? WHERE id = ?';
|
|
// db.run(sql, [
|
|
// Math.floor(new Date(Date.now()).getTime()),
|
|
// 0,
|
|
// Number(entrance_id)
|
|
// ], function(err) {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// return res(false);
|
|
// } else if (this.changes === 0) {
|
|
// return res(false);
|
|
// } else {
|
|
// res({ "update": "ok", "id": entrance_id });
|
|
// }
|
|
// });
|
|
// });
|
|
}
|
|
|
|
deleteHistoryEntrance(entrance_id) {
|
|
return new Promise((res, rej) => {
|
|
db.run('DELETE FROM entrance_history WHERE id = ?', [Number(entrance_id)], function (err) {
|
|
if (err) {
|
|
console.error(err.message);
|
|
return res(false);
|
|
} else if (this.changes === 0) {
|
|
return res(false);
|
|
} else {
|
|
res({ "delete": "ok", "id": entrance_id });
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = new HistoryEntranceService(); |