Files
Sheep-Service/api/services/constructor.service.js
2025-09-09 00:10:53 +03:00

366 lines
15 KiB
JavaScript

const db = require("../config/db");
const saveCards = require("../middleware/genCards");
class ConstructorService {
// createPack(data) {
// return new Promise((res, rej) => {
// let sql = `
// INSERT INTO
// house(
// group_id,
// title,
// number,
// points,
// points_number,
// geo,
// osm_id,
// settlement,
// created_at
// )
// VALUES
// (?, ?, ?, ?, ?, ?, ?, ?, ?)
// `;
// db.run(sql, [
// Number(data.house.group_id),
// data.house.title,
// data.house.number,
// JSON.stringify(data.house.points),
// JSON.stringify(data.house.points_number),
// JSON.stringify(data.house.geo),
// JSON.stringify(data.house.osm_id),
// data.house.settlement,
// Math.floor(Date.now())
// ], function (err) {
// if (err) {
// console.error(err.message);
// return res(false);
// } else if (this.changes === 0) {
// return res(false);
// } else {
// const houseId = this.lastID;
// const entranceStmt = db.prepare(`
// INSERT INTO
// entrance(
// house_id,
// entrance_number,
// title,
// points,
// points_number,
// created_at
// )
// VALUES
// (?, ?, ?, ?, ?, ?)`);
// const apartmentStmt = db.prepare(`
// INSERT INTO
// apartments(
// entrance_id,
// apartment_number,
// floors_number,
// updated_at
// )
// VALUES
// (?, ?, ?, ?)`);
// data.entrance.forEach((e, index) => {
// entranceStmt.run(
// houseId,
// Number(e.entrance_number),
// e.title,
// JSON.stringify(e.points),
// JSON.stringify(e.points_number),
// Math.floor(Date.now()),
// function (err) {
// if (err) {
// console.error(err.message);
// return;
// }
// const entranceId = this.lastID;
// if (data.apartments[e.editor_id]) {
// data.apartments[e.editor_id].forEach(apartment => {
// apartmentStmt.run(
// entranceId,
// apartment.apartment_number,
// apartment.floors_number,
// Math.floor(Date.now())
// );
// });
// }
// }
// );
// });
// entranceStmt.finalize();
// apartmentStmt.finalize();
// // res({ "status": "ok", "id": houseId });
// }
// });
// });
// }
// createPack(data) {
// return new Promise((res, rej) => {
// if (data.type == "house") {
// const sql = `
// INSERT INTO house (
// title, number, points, points_number, geo, osm_id, settlement, created_at
// ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
// db.run(sql, [
// data.title,
// data.number,
// JSON.stringify(data.points),
// JSON.stringify(data.points_number),
// JSON.stringify(data.geo),
// JSON.stringify(data.osm_id),
// data.settlement,
// Math.floor(Date.now())
// ], function (err) {
// if (err) {
// console.error(err.message);
// return res(false);
// }
// if (this.changes === 0) {
// return res(false);
// }
// const houseId = this.lastID;
// saveCards({ center: data.geo, wayId: data.osm_id, zoom: data.zoom ?? 18, type: "house", number: houseId, address: `${data.title} ${data.number}` });
// const entranceStmt = db.prepare(`
// INSERT INTO entrance (
// house_id, entrance_number, title, points, points_number, created_at
// ) VALUES (?, ?, ?, ?, ?, ?)`);
// const apartmentStmt = db.prepare(`
// INSERT INTO apartments (
// entrance_id, apartment_number, title, floors_number
// ) VALUES (?, ?, ?, ?)`);
// const entranceIdMap = {}; // Для сопоставления editor_id → entrance_id
// let pendingEntrances = data.entrance.length;
// data.entrance.forEach((e) => {
// entranceStmt.run(
// houseId,
// Number(e.entrance_number),
// e.title,
// JSON.stringify(e.points),
// JSON.stringify(e.points_number),
// Math.floor(Date.now()),
// function (err) {
// if (err) {
// console.error(err.message);
// return;
// }
// const entranceId = this.lastID;
// entranceIdMap[e.editor_id] = entranceId;
// if (--pendingEntrances === 0) {
// insertApartments();
// }
// }
// );
// });
// function insertApartments() {
// for (const [editor_id, apartments] of Object.entries(data.apartments)) {
// const entranceId = entranceIdMap[editor_id];
// if (!entranceId) continue;
// apartments.forEach(apartment => {
// apartmentStmt.run(
// entranceId,
// Number(apartment.apartment_number),
// apartment.title,
// apartment.floors_number
// );
// });
// }
// entranceStmt.finalize();
// apartmentStmt.finalize();
// res({ "status": "ok", "id": houseId });
// }
// });
// } else if (data.type == "homestead") {
// return res(false);
// } else {
// return res(false);
// }
// });
// }
createPack(data) {
return new Promise((res, rej) => {
if (data.type === "house") {
const sql = `
INSERT INTO house (
title, number, points, points_number, geo, osm_id, settlement, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
db.run(sql, [
data.title,
data.number,
JSON.stringify(data.points),
JSON.stringify(data.points_number),
JSON.stringify(data.geo),
JSON.stringify(data.osm_id),
data.settlement,
Math.floor(Date.now())
], function (err) {
if (err) {
console.error(err.message);
return res(false);
}
if (this.changes === 0) {
return res(false);
}
const houseId = this.lastID;
// saveCards({ center: data.geo, wayId: data.osm_id, zoom: data.zoom ?? 18, type: "house", number: houseId, address: `${data.title} ${data.number}` });
const entranceStmt = db.prepare(`
INSERT INTO entrance (
house_id, entrance_number, title, created_at
) VALUES (?, ?, ?, ?)`);
const apartmentStmt = db.prepare(`
INSERT INTO apartments (
entrance_id, apartment_number, title, floors_number
) VALUES (?, ?, ?, ?)`);
let pendingEntrances = data.entrances.length;
const entranceIdMap = {};
if (pendingEntrances === 0) return finalize();
// Вставляем подъезды
data.entrances.forEach(entrance => {
entranceStmt.run(
houseId,
Number(entrance.entrance_number),
entrance.title,
Math.floor(Date.now()),
function (err) {
if (err) {
console.error(err.message);
return res(false);
}
const entranceID = this.lastID;
entranceIdMap[entrance.entrance_number] = entranceID;
// Вставляем квартиры данного подъезда
let pendingApartments = entrance.apartments.length;
if (pendingApartments === 0) {
if (--pendingEntrances === 0) finalize();
return;
}
entrance.apartments.forEach(apartment => {
apartmentStmt.run(
entranceID,
Number(apartment.apartment_number),
apartment.title,
Number(apartment.floors_number),
function (err) {
if (err) console.error(err.message);
if (--pendingApartments === 0) {
if (--pendingEntrances === 0) finalize();
}
}
);
});
}
);
});
function finalize() {
entranceStmt.finalize();
apartmentStmt.finalize();
res({ status: "ok", id: houseId });
}
});
} else if (data.type === "homestead") {
const sql = `
INSERT INTO homestead (
title, number, zoom, points, geo, osm_id, settlement, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`;
db.run(sql, [
data.title,
data.number,
Number(data.zoom),
JSON.stringify(data.points),
JSON.stringify(data.geo),
JSON.stringify(data.osm_id),
data.settlement,
Math.floor(Date.now())
], function (err) {
if (err) {
console.error(err.message);
return res(false);
}
if (this.changes === 0) {
return res(false);
}
const homesteadId = this.lastID;
// saveCards({ center: data.geo, wayId: data.osm_id, zoom: data.zoom ?? 17, type: "homestead", number: homesteadId, address: `${data.title} ${data.number}` });
const buildingStmt = db.prepare(`
INSERT INTO buildings (
homestead_id, title, geo
) VALUES (?, ?, ?)
`);
let pendingBuildings = data.buildings.length;
if (pendingBuildings === 0) return finalize();
data.buildings.forEach(building => {
buildingStmt.run(
homesteadId,
building.title,
JSON.stringify(building.geo),
function (err) {
if (err) {
console.error(err.message);
return res(false);
}
if (--pendingBuildings === 0) finalize();
}
);
});
function finalize() {
buildingStmt.finalize();
res({ status: "ok", id: homesteadId });
}
});
} else {
return res(false);
}
});
}
}
module.exports = new ConstructorService();