240 lines
9.5 KiB
JavaScript
240 lines
9.5 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 (
|
|
group_id, title, number, points, points_number, geo, osm_id, settlement, created_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,
|
|
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") {
|
|
let sql = `
|
|
INSERT INTO
|
|
homestead(
|
|
group_id,
|
|
title,
|
|
number,
|
|
points,
|
|
point_icons,
|
|
geo,
|
|
osm_id,
|
|
settlement,
|
|
created_at
|
|
)
|
|
VALUES
|
|
(?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
`;
|
|
|
|
db.run(sql, [
|
|
Number(data.group_id),
|
|
data.title,
|
|
data.number,
|
|
JSON.stringify(data.points),
|
|
JSON.stringify(data.point_icons),
|
|
JSON.stringify(data.geo),
|
|
JSON.stringify(data.osm_id),
|
|
data.settlement,
|
|
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 {
|
|
saveCards({ center: data.geo, wayId: data.osm_id, zoom: data.zoom ?? 17, type: "homestead", number: this.lastID, address: `${data.title} ${data.number}` })
|
|
res({ "status": "ok", "id": this.lastID });
|
|
}
|
|
});
|
|
} else {
|
|
return res(false);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = new ConstructorService(); |