289 lines
11 KiB
C#
289 lines
11 KiB
C#
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<Material> _materials = new List<Material>();
|
||
private List<Employee> _employees = new List<Employee>();
|
||
private List<Position> _positions = new List<Position>();
|
||
private List<Order> _orders = new List<Order>();
|
||
private List<EmployeeOrder> _employeeOrders = new List<EmployeeOrder>();
|
||
private List<MaterialOrder> _materialOrders = new List<MaterialOrder>();
|
||
public List<Material> Materials
|
||
{
|
||
get
|
||
{
|
||
refreshDb();
|
||
return _materials;
|
||
}
|
||
}
|
||
public List<Employee> Employees
|
||
{
|
||
get
|
||
{
|
||
refreshDb();
|
||
return _employees;
|
||
}
|
||
}
|
||
public List<Position> Positions
|
||
{
|
||
get
|
||
{
|
||
refreshDb();
|
||
return _positions;
|
||
}
|
||
}
|
||
public List<Order> Orders
|
||
{
|
||
get
|
||
{
|
||
refreshDb();
|
||
return _orders;
|
||
}
|
||
}
|
||
|
||
public List<EmployeeOrder> EmployeeOrders
|
||
{
|
||
get
|
||
{
|
||
refreshDb();
|
||
return _employeeOrders;
|
||
}
|
||
}
|
||
|
||
public List<MaterialOrder> 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<BsonDocument>(collectionName);
|
||
BsonDocument result = collection.Find(filter).First();
|
||
return result;
|
||
}
|
||
public void flushDb()
|
||
{
|
||
var db = client.GetDatabase(databaseName);
|
||
db.GetCollection<BsonDocument>("Employees").DeleteMany(new BsonDocument());
|
||
db.GetCollection<BsonDocument>("Orders").DeleteMany(new BsonDocument());
|
||
db.GetCollection<BsonDocument>("Materials").DeleteMany(new BsonDocument());
|
||
db.GetCollection<BsonDocument>("Positions").DeleteMany(new BsonDocument());
|
||
}
|
||
|
||
public List<EmployeeViewModel> reportMaterials(int materialId)
|
||
{
|
||
var materials = client.GetDatabase(databaseName).GetCollection<BsonDocument>("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<EmployeeViewModel> result = new List<EmployeeViewModel>();
|
||
foreach (var employee in resEmployees)
|
||
{
|
||
result.Add(_employees.First(x => x.Id == employee[0].ToInt32()).GetViewModel);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public List<BrigadeReportViewModel> reportPositionsAverage(DateTime from, DateTime to)
|
||
{
|
||
var positions = client.GetDatabase(databaseName).GetCollection<BsonDocument>("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<BrigadeReportViewModel>();
|
||
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<BsonDocument> FilterDocuments(BsonDocument filter, string collectionName)
|
||
{
|
||
var collection = client.GetDatabase(databaseName).GetCollection<BsonDocument>(collectionName);
|
||
List<BsonDocument> result = collection.Find(filter).ToList();
|
||
return result;
|
||
}
|
||
|
||
public void InsertDocument(BsonDocument bson, string collectionName)
|
||
{
|
||
var collection = client.GetDatabase(databaseName).GetCollection<BsonDocument>(collectionName);
|
||
collection.InsertOne(bson);
|
||
refreshDb();
|
||
}
|
||
|
||
public void ReplaceDocument(BsonDocument bson, BsonDocument filter, string collectionName)
|
||
{
|
||
var collection = client.GetDatabase(databaseName).GetCollection<BsonDocument>(collectionName);
|
||
collection.ReplaceOne(filter, bson);
|
||
refreshDb();
|
||
}
|
||
|
||
public void DeleteDocument(BsonDocument filter, string collectionName)
|
||
{
|
||
var collection = client.GetDatabase(databaseName).GetCollection<BsonDocument>(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<BsonDocument> positions = database.GetCollection<BsonDocument>("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<BsonDocument> materials = database.GetCollection<BsonDocument>("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<BsonDocument> employees = database.GetCollection<BsonDocument>("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<BsonDocument> orders = database.GetCollection<BsonDocument>("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);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|