using ConstructionCompanyMongoDBImplement.Models; using ConstructionCompanyContracts.BindingModels; using MongoDB.Bson; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ConstructionCompanyDataModels.Enums; using ConstructionCompanyContracts.ViewModels; namespace ConstructionCompanyMongoDBImplement { public class ConstructionCompanyDatabase { static string connectionString = "mongodb://localhost:27017/"; static string databaseName = "ConstructionCompany"; private static ConstructionCompanyDatabase? _instance; private MongoClient client = new MongoClient(connectionString); private List _materials = new List(); private List _employees = new List(); private List _positions = new List(); private List _orders = new List(); private List _employeeOrders = new List(); private List _materialOrders = new List(); public List Materials { get { refreshDb(); return _materials; } } public List Employees { get { refreshDb(); return _employees; } } public List Positions { get { refreshDb(); return _positions; } } public List Orders { get { refreshDb(); return _orders; } } public List EmployeeOrders { get { refreshDb(); return _employeeOrders; } } public List MaterialOrders { get { refreshDb(); return _materialOrders; } } public ConstructionCompanyDatabase() { refreshDb(); } public static ConstructionCompanyDatabase GetInstance() { if (_instance == null) { _instance = new ConstructionCompanyDatabase(); } return _instance; } public BsonDocument ReadDocument(BsonDocument filter, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); BsonDocument result = collection.Find(filter).First(); return result; } public void flushDb() { var db = client.GetDatabase(databaseName); db.GetCollection("Employees").DeleteMany(new BsonDocument()); db.GetCollection("Orders").DeleteMany(new BsonDocument()); db.GetCollection("Materials").DeleteMany(new BsonDocument()); db.GetCollection("Positions").DeleteMany(new BsonDocument()); } public List reportMaterials(int materialId) { var materials = client.GetDatabase(databaseName).GetCollection("Materials"); var res = materials.Aggregate() .Lookup( "Orders", "_id", "materials.materialId", "order" ) .Lookup( "Employees", "order.employeesId", "_id", "employee" ) .Match(new BsonDocument { { "_id", materialId} }) .ToList(); BsonArray resEmployees = res[0][4].AsBsonArray; List result = new List(); foreach (var employee in resEmployees) { result.Add(_employees.First(x => x.Id == employee[0].ToInt32()).GetViewModel); } return result; } public List reportPositionsAverage(DateTime from, DateTime to) { var positions = client.GetDatabase(databaseName).GetCollection("Positions"); var res = positions.Aggregate() .Lookup( "Employees", "_id", "positionId", "employee" ) .Lookup( "Orders", "employee._id", "employeesId", "order" ) .Unwind("order") .Match(new BsonDocument { { "order.dateBegin", new BsonDocument("$gte", new BsonDateTime(from)) } }) .Match(new BsonDocument { { "order.dateBegin", new BsonDocument("$lte", new BsonDateTime(to)) } }) .Unwind("order.materials") .Group(new BsonDocument { {"_id", "$_id"}, {"average", new BsonDocument("$avg", "$order.materials.quantity")} }) .ToList(); var result = new List(); foreach (var report in res) { int posId = report[0].ToInt32(); string posName = _positions.First(x => x.Id == posId).PositionName; int avg = report[1].ToInt32(); result.Add(new BrigadeReportViewModel { PositionId = posId, PositionName = posName, materialAvg = avg }); } return result; } public List FilterDocuments(BsonDocument filter, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); List result = collection.Find(filter).ToList(); return result; } public void InsertDocument(BsonDocument bson, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); collection.InsertOne(bson); refreshDb(); } public void ReplaceDocument(BsonDocument bson, BsonDocument filter, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); collection.ReplaceOne(filter, bson); refreshDb(); } public void DeleteDocument(BsonDocument filter, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); collection.DeleteOne(filter); refreshDb(); } private void refreshDb() { _materials.Clear(); _positions.Clear(); _employees.Clear(); _orders.Clear(); _employeeOrders.Clear(); _materialOrders.Clear(); IMongoDatabase database = client.GetDatabase(databaseName); List positions = database.GetCollection("Positions").Find(new BsonDocument()).ToList(); foreach (var position in positions) { int id = position[0].ToInt32(); string? name = position[1].ToString(); double salary = position[2].ToDouble(); Position? newPosition = Position.Create(new PositionBindingModel { Id = id, PositionName = name, Salary = salary }); if (newPosition != null) _positions.Add(newPosition); } List materials = database.GetCollection("Materials").Find(new BsonDocument()).ToList(); foreach (var material in materials) { int id = material[0].ToInt32(); string? name = material[1].ToString(); int quantity = material[2].ToInt32(); Material? newMaterial = Material.Create(new MaterialBindingModel { Id = id, MaterialName = name, Quantity = quantity }); if (newMaterial != null) _materials.Add(newMaterial); } List employees = database.GetCollection("Employees").Find(new BsonDocument()).ToList(); foreach (var employee in employees) { int id = employee[0].ToInt32(); string? name = employee[1].ToString(); int positionId = employee[2].ToInt32(); Employee? newEmployee = Employee.Create(new EmployeeBindingModel { Id = id, EmployeeName = name, PositionID = positionId }, _positions); if (newEmployee != null) _employees.Add(newEmployee); } List orders = database.GetCollection("Orders").Find(new BsonDocument()).ToList(); foreach (var order in orders) { int id = order[0].ToInt32(); string? description = order[1].ToString(); string? adress = order[2].ToString(); double price = order[3].ToDouble(); string? status = order[4].ToString(); OrderStatus orderStatus = OrderStatus.Неизвестен; switch (status) { case "Принят": orderStatus = OrderStatus.Принят; break; case "Выполняется": orderStatus = OrderStatus.Выполняется; break; case "Завершён": orderStatus = OrderStatus.Завершён; break; } string? customerNumber = order[5].ToString(); DateTime dateBegin = order[6].ToUniversalTime(); DateTime? dateEnd = null; if (order[7].ToString().Length > 5) dateEnd = order[7].ToUniversalTime(); Order? newOrder = Order.Create(new OrderBindingModel { Id = id, Description = description, Adress = adress, Price = price, Status = orderStatus, CustomerNumber = customerNumber, DateBegin = dateBegin, DateEnd = dateEnd }); if (newOrder != null) _orders.Add(newOrder); BsonArray employeesId = order[8].AsBsonArray; foreach (var employee in employeesId) { EmployeeOrder? employeeOrder = EmployeeOrder.Create(new EmployeeOrderBindingModel { EmployeeId = employee.ToInt32(), OrderId = id }, _employees, _orders); if (employeeOrder != null) _employeeOrders.Add(employeeOrder); } BsonArray orderMaterials = order[9].AsBsonArray; foreach (var material in orderMaterials) { int materialId = material[0].ToInt32(); int quantity = material[1].ToInt32(); MaterialOrder? materialOrder = MaterialOrder.Create(new MaterialOrderBindingModel { MaterialId = materialId, OrderId = id, Quantity = quantity }, _materials, _orders); if (materialOrder != null) _materialOrders.Add(materialOrder); } } } } }