diff --git a/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/RandomGeneratorLogic.cs b/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/RandomGeneratorLogic.cs index 9f496aa..a007962 100644 --- a/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/RandomGeneratorLogic.cs +++ b/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/RandomGeneratorLogic.cs @@ -31,18 +31,18 @@ namespace ConstructionCompanyBusinessLogic.BusinessLogics public void GenerateEmployees() { - int posInd = 1; + int posInd = 0; for (int i = 0; i < 200; i++) { _employee.Create(new EmployeeBindingModel { EmployeeName = "testEmp" + (i + 1), PositionID = posInd }); posInd++; - if (posInd == 11) posInd = 1; + if (posInd == 20) posInd = 0; } } public void GenerateEmployeesOrders() { - for (int i = 1; i <= 200; i++) + for (int i = 0; i < 200; i++) { _employeeOrder.Create(new EmployeeOrderBindingModel { EmployeeId = i, OrderId = i}); } @@ -51,7 +51,7 @@ namespace ConstructionCompanyBusinessLogic.BusinessLogics public void GenerateMaterialOrders() { int quantity = 1; - for (int i = 1; i <= 200; i++) + for (int i = 0; i < 200; i++) { _materialOrder.Create(new MaterialOrderBindingModel { MaterialId = i, OrderId = i, Quantity = quantity}); quantity++; @@ -65,8 +65,8 @@ namespace ConstructionCompanyBusinessLogic.BusinessLogics int quantity = 1; for (int i = 1; i <= 100; i++) { - int mat = rand.Next(1, 10); - int ord = rand.Next(1, 100); + int mat = rand.Next(0, 10); + int ord = rand.Next(0, 100); if (_materialOrder.ReadElement(new MaterialOrderSearchModel { MaterialId = mat, OrderId = ord}) != null) { i--; diff --git a/ConstructionCompany/ConstructionCompanyBusiness/ConstructionCompanyBusinessLogic.csproj b/ConstructionCompany/ConstructionCompanyBusiness/ConstructionCompanyBusinessLogic.csproj index 3a7f827..3158497 100644 --- a/ConstructionCompany/ConstructionCompanyBusiness/ConstructionCompanyBusinessLogic.csproj +++ b/ConstructionCompany/ConstructionCompanyBusiness/ConstructionCompanyBusinessLogic.csproj @@ -12,6 +12,7 @@ + diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/ConstructionCompanyDatabase.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/ConstructionCompanyDatabase.cs index 67a30d2..50bdb14 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/ConstructionCompanyDatabase.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/ConstructionCompanyDatabase.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ConstructionCompanyDataModels.Enums; +using ConstructionCompanyContracts.ViewModels; namespace ConstructionCompanyMongoDBImplement { @@ -75,6 +76,12 @@ namespace ConstructionCompanyMongoDBImplement } } + public ConstructionCompanyDatabase() + { + refreshDb(); + } + + public static ConstructionCompanyDatabase GetInstance() { if (_instance == null) @@ -86,7 +93,87 @@ namespace ConstructionCompanyMongoDBImplement public BsonDocument ReadDocument(BsonDocument filter, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); - BsonDocument result = collection.Find(filter).ToList()[0]; + 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; } diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/MaterialStorage.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/MaterialStorage.cs index d988169..cf89c99 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/MaterialStorage.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/MaterialStorage.cs @@ -96,17 +96,29 @@ namespace ConstructionCompanyMongoDBImplement.Implements } public List? GetEmployeesUsingMaterial(MaterialBindingModel model) { - //var command = Material.GetEmployeeCommand(model); - //if (string.IsNullOrEmpty(command)) + if (model == null) + { + return null; + } + + //List employeesList = _source.Employees; + //BsonDocument filter = new BsonDocument { { "materials.materialId", new BsonDocument("$eq", model.Id) } }; + //List fiteredOrders = _source.FilterDocuments(filter, "Orders"); + //List employees = new List(); + //foreach (var order in fiteredOrders) //{ - // return null; - //} - //var employeesId = _source.ExecuteReader(command, 1); - List employees = new List(); - //foreach (var id in employeesId) - //{ - // employees.Add(_source.Employees.First(x => x.Id == Convert.ToInt32(id[0])).GetViewModel); + // var orderEmployees = order[8].AsBsonArray; + // foreach (var employee in orderEmployees) + // { + // var emp = employees.FirstOrDefault(x => x.Id == employee.ToInt32()); + // if (emp == null) + // { + // employees.Add(employeesList.First(x => x.Id == employee.ToInt32()).GetViewModel); + // } + // } //} + + var employees = _source.reportMaterials(model.Id); return employees; } } diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs index 84e2902..f91f90b 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs @@ -103,14 +103,8 @@ namespace ConstructionCompanyMongoDBImplement.Implements public List GetPositionsAverage(DateTime dateFrom, DateTime dateTo) { - //var command = Position.PositionsAVGCommnad(dateFrom, dateTo); - //var result = _source.ExecuteReader(command, 2); - List positionsAverages = new List(); - //foreach (var posAvgPair in result) - //{ - // string positionName = _source.Positions.First(x => x.Id == Convert.ToInt32(posAvgPair[0])).PositionName; - // positionsAverages.Add(new BrigadeReportViewModel { PositionId = Convert.ToInt32(posAvgPair[0]), PositionName = positionName, materialAvg = Convert.ToDouble(posAvgPair[1]) }); - //} + //_source.flushDb(); + List positionsAverages = _source.reportPositionsAverage(dateFrom, dateTo); return positionsAverages; } } diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Employee.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Employee.cs index 8d8f60f..541ce74 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Employee.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Employee.cs @@ -43,7 +43,7 @@ namespace ConstructionCompanyMongoDBImplement.Models { {"_id", model.Id}, {"EmployeeName", $"{model.EmployeeName}"}, - {"positionId", $"{model.PositionID}" } + {"positionId", model.PositionID} }; } @@ -57,7 +57,7 @@ namespace ConstructionCompanyMongoDBImplement.Models { {"_id", model.Id}, {"EmployeeName", $"{model.EmployeeName}"}, - {"positionId", $"{model.PositionID}" } + {"positionId", model.PositionID} }; } public EmployeeViewModel GetViewModel => new() diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Order.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Order.cs index d56bdf5..2dd362f 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Order.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Order.cs @@ -55,7 +55,7 @@ namespace ConstructionCompanyMongoDBImplement.Models {"_id", model.Id}, {"description", $"{model.Description}"}, {"adress", $"{model.Adress}"}, - {"price", $"{model.Price}"}, + {"price", model.Price}, {"status", $"{model.Status}"}, {"customerNumber", $"{model.CustomerNumber}"}, {"dateBegin", new BsonDateTime(model.DateBegin)}, @@ -76,7 +76,7 @@ namespace ConstructionCompanyMongoDBImplement.Models {"_id", model.Id}, {"description", $"{model.Description}"}, {"adress", $"{model.Adress}"}, - {"price", $"{model.Price}"}, + {"price", model.Price}, {"status", $"{model.Status}"}, {"customerNumber", $"{model.CustomerNumber}"}, {"dateBegin", new BsonDateTime(model.DateBegin)}, diff --git a/ConstructionCompany/ConstructionCompanyView/App.config b/ConstructionCompany/ConstructionCompanyView/App.config new file mode 100644 index 0000000..1ef6e9e --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/App.config @@ -0,0 +1,21 @@ + + + + +
+
+ + + + + + postgres + + + + + postgres + + + + \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyView/ConstructionCompanyView.csproj b/ConstructionCompany/ConstructionCompanyView/ConstructionCompanyView.csproj index ca46dc5..17e1e12 100644 --- a/ConstructionCompany/ConstructionCompanyView/ConstructionCompanyView.csproj +++ b/ConstructionCompany/ConstructionCompanyView/ConstructionCompanyView.csproj @@ -41,6 +41,11 @@ True Resources.resx + + True + True + Settings.settings + @@ -54,6 +59,10 @@ Always + + SettingsSingleFileGenerator + Settings.Designer.cs + \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyView/FormBrigadeReport.cs b/ConstructionCompany/ConstructionCompanyView/FormBrigadeReport.cs index 0da53ca..60ac3a2 100644 --- a/ConstructionCompany/ConstructionCompanyView/FormBrigadeReport.cs +++ b/ConstructionCompany/ConstructionCompanyView/FormBrigadeReport.cs @@ -23,7 +23,7 @@ namespace ConstructionCompanyView private void buttonShow_Click(object sender, EventArgs e) { - if (dateTimePickerFrom.Value > dateTimePickerTo.Value) + if (dateTimePickerFrom.Value.Date > dateTimePickerTo.Value.Date) { MessageBox.Show("Неверно выбраны даты!"); return; diff --git a/ConstructionCompany/ConstructionCompanyView/FormLogin.Designer.cs b/ConstructionCompany/ConstructionCompanyView/FormLogin.Designer.cs index a3c01b2..2425218 100644 --- a/ConstructionCompany/ConstructionCompanyView/FormLogin.Designer.cs +++ b/ConstructionCompany/ConstructionCompanyView/FormLogin.Designer.cs @@ -33,6 +33,8 @@ this.buttonManager = new System.Windows.Forms.Button(); this.buttonHR = new System.Windows.Forms.Button(); this.buttonGenerate = new System.Windows.Forms.Button(); + this.buttonSwitchDB = new System.Windows.Forms.Button(); + this.buttonSync = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 @@ -77,20 +79,42 @@ // // buttonGenerate // - this.buttonGenerate.Location = new System.Drawing.Point(12, 309); + this.buttonGenerate.Location = new System.Drawing.Point(33, 309); this.buttonGenerate.Name = "buttonGenerate"; - this.buttonGenerate.Size = new System.Drawing.Size(154, 29); + this.buttonGenerate.Size = new System.Drawing.Size(174, 29); this.buttonGenerate.TabIndex = 4; this.buttonGenerate.Text = "Создать компанию"; this.buttonGenerate.UseVisualStyleBackColor = true; this.buttonGenerate.Click += new System.EventHandler(this.buttonGenerate_Click); // + // buttonSwitchDB + // + this.buttonSwitchDB.Location = new System.Drawing.Point(543, 309); + this.buttonSwitchDB.Name = "buttonSwitchDB"; + this.buttonSwitchDB.Size = new System.Drawing.Size(229, 29); + this.buttonSwitchDB.TabIndex = 5; + this.buttonSwitchDB.Text = "Переключить на "; + this.buttonSwitchDB.UseVisualStyleBackColor = true; + this.buttonSwitchDB.Click += new System.EventHandler(this.buttonSwitchDB_Click); + // + // buttonSync + // + this.buttonSync.Location = new System.Drawing.Point(299, 309); + this.buttonSync.Name = "buttonSync"; + this.buttonSync.Size = new System.Drawing.Size(174, 29); + this.buttonSync.TabIndex = 6; + this.buttonSync.Text = "Синхронизировать БД"; + this.buttonSync.UseVisualStyleBackColor = true; + this.buttonSync.Click += new System.EventHandler(this.buttonSync_Click); + // // FormLogin // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.ActiveCaption; this.ClientSize = new System.Drawing.Size(784, 350); + this.Controls.Add(this.buttonSync); + this.Controls.Add(this.buttonSwitchDB); this.Controls.Add(this.buttonGenerate); this.Controls.Add(this.buttonHR); this.Controls.Add(this.buttonManager); @@ -98,6 +122,7 @@ this.Controls.Add(this.label1); this.Name = "FormLogin"; this.Text = "Вход"; + this.Load += new System.EventHandler(this.FormLogin_Load); this.ResumeLayout(false); this.PerformLayout(); @@ -110,5 +135,7 @@ private Button buttonManager; private Button buttonHR; private Button buttonGenerate; + private Button buttonSwitchDB; + private Button buttonSync; } } \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyView/FormLogin.cs b/ConstructionCompany/ConstructionCompanyView/FormLogin.cs index 169c7d7..40e2321 100644 --- a/ConstructionCompany/ConstructionCompanyView/FormLogin.cs +++ b/ConstructionCompany/ConstructionCompanyView/FormLogin.cs @@ -24,6 +24,7 @@ namespace ConstructionCompanyView private void buttonWarehouse_Click(object sender, EventArgs e) { + //MessageBox.Show(Properties.Settings.Default.bdType); var service = Program.ServiceProvider?.GetService(typeof(FormWarehouseMenu)); if (service is FormWarehouseMenu form) { @@ -85,5 +86,31 @@ namespace ConstructionCompanyView MessageBox.Show($"materials={materials}, positions={positions}, employees={employees}, orders={orders}, materialOrders={materialOrders}, employeeOrders={employeesOrders}", "Результаты"); MessageBox.Show("Готово!"); } + + private void FormLogin_Load(object sender, EventArgs e) + { + string dbType = Properties.Settings.Default.bdType; + if (dbType == "postgres") buttonSwitchDB.Text = "Переклюить на MongoDB"; + else buttonSwitchDB.Text = "Переключить на PostgreSQL"; + } + + private void buttonSwitchDB_Click(object sender, EventArgs e) + { + var dialogResult = MessageBox.Show("Данное действие требует перезапуска программы!", "Продолжить?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (dialogResult == DialogResult.No || dialogResult == DialogResult.Cancel) return; + string dbType = Properties.Settings.Default.bdType; + if (dbType == "postgres") Properties.Settings.Default.bdType = "mongoDB"; + else Properties.Settings.Default.bdType = "postgres"; + Properties.Settings.Default.Save(); + MessageBox.Show("Сейчас программа автоматически выключится.", "Готово"); + this.Close(); + } + + private void buttonSync_Click(object sender, EventArgs e) + { + ConstructionCompanyPsqlImplement.ConstructionCompanyDatabase psqlDb = ConstructionCompanyPsqlImplement.ConstructionCompanyDatabase.GetInstance(); + ConstructionCompanyMongoDBImplement.ConstructionCompanyDatabase mongoDb = ConstructionCompanyMongoDBImplement.ConstructionCompanyDatabase.GetInstance(); + mongoDb.flushDb(); + } } } diff --git a/ConstructionCompany/ConstructionCompanyView/Program.cs b/ConstructionCompany/ConstructionCompanyView/Program.cs index 4647445..79b6d2e 100644 --- a/ConstructionCompany/ConstructionCompanyView/Program.cs +++ b/ConstructionCompany/ConstructionCompanyView/Program.cs @@ -2,6 +2,7 @@ using ConstructionCompanyBusinessLogic.BusinessLogics; using ConstructionCompanyContracts.BusinessLogicContracts; using ConstructionCompanyContracts.StorageContracts; using ConstructionCompanyMongoDBImplement.Implements; +using ConstructionCompanyPsqlImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; @@ -34,12 +35,9 @@ namespace ConstructionCompanyView option.SetMinimumLevel(LogLevel.Information); option.AddNLog("nlogConstruction.config"); }); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + + if (Properties.Settings.Default.bdType == "postgres") addPgsql(services); + else addMongoDB(services); services.AddTransient(); services.AddTransient(); @@ -68,5 +66,25 @@ namespace ConstructionCompanyView services.AddTransient(); services.AddTransient(); } + + private static void addPgsql(ServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + } + + private static void addMongoDB(ServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + } } } \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyView/Properties/Settings.Designer.cs b/ConstructionCompany/ConstructionCompanyView/Properties/Settings.Designer.cs new file mode 100644 index 0000000..803b734 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/Properties/Settings.Designer.cs @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +// +// Этот код создан программой. +// Исполняемая версия:4.0.30319.42000 +// +// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае +// повторной генерации кода. +// +//------------------------------------------------------------------------------ + +namespace ConstructionCompanyView.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.3.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("postgres")] + public string bdType { + get { + return ((string)(this["bdType"])); + } + set { + this["bdType"] = value; + } + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyView/Properties/Settings.settings b/ConstructionCompany/ConstructionCompanyView/Properties/Settings.settings new file mode 100644 index 0000000..96e32e5 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/Properties/Settings.settings @@ -0,0 +1,9 @@ + + + + + + postgres + + + \ No newline at end of file