First Commit
This commit is contained in:
		
							
								
								
									
										81
									
								
								DataBase/ConnectDB.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								DataBase/ConnectDB.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,81 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.ComponentModel.DataAnnotations;
 | 
			
		||||
using System.ComponentModel.DataAnnotations.Schema;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
 | 
			
		||||
namespace Diplom_O.DataBase
 | 
			
		||||
{
 | 
			
		||||
	public class MainDB : DbContext
 | 
			
		||||
	{
 | 
			
		||||
		public DbSet<Shtat> Shtat { get; set; }
 | 
			
		||||
		public DbSet<Rabotnik> Rabotniky { get; set; }
 | 
			
		||||
		public DbSet<Chel> Chely { get; set; }
 | 
			
		||||
 | 
			
		||||
		public MainDB()
 | 
			
		||||
		{
 | 
			
		||||
			Database.EnsureCreated();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 | 
			
		||||
		{
 | 
			
		||||
			optionsBuilder.UseSqlite("Data Source=" + Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "Diplom_O.db;");
 | 
			
		||||
		}
 | 
			
		||||
		protected override void OnModelCreating(ModelBuilder modelBuilder) { }
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	public class Rabotnik
 | 
			
		||||
	{
 | 
			
		||||
		[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
 | 
			
		||||
		public int Id { get; set; }
 | 
			
		||||
		[ForeignKey("Dogovor")]
 | 
			
		||||
		public int ChelId { get; set; }
 | 
			
		||||
		public Chel Chel { get; set; }
 | 
			
		||||
		[ForeignKey("Dogovor")]
 | 
			
		||||
		public int ShtatId { get; set; }
 | 
			
		||||
		public Shtat Shtat { get; set; }
 | 
			
		||||
 | 
			
		||||
		public string KodSotr { get; set; }
 | 
			
		||||
		public string Kategoriya { get; set; }
 | 
			
		||||
		public bool Pensiya { get; set; }
 | 
			
		||||
 | 
			
		||||
		public DateTime Start { get; set; }
 | 
			
		||||
		public DateTime? End { get; set; }
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public class Shtat
 | 
			
		||||
	{
 | 
			
		||||
		[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
 | 
			
		||||
		public int Id { get; set; }
 | 
			
		||||
		public string Doljnost { get; set; }
 | 
			
		||||
		public int Size { get; set; }
 | 
			
		||||
		public bool Active { get; set; }
 | 
			
		||||
	}
 | 
			
		||||
	public class Chel
 | 
			
		||||
	{
 | 
			
		||||
		[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
 | 
			
		||||
		public int Id { get; set; }
 | 
			
		||||
		public string FName { get; set; }
 | 
			
		||||
		public string SName { get; set; }
 | 
			
		||||
		public string TName { get; set; }
 | 
			
		||||
		public bool Male { get; set; }
 | 
			
		||||
		public DateTime Birthday { get; set; }
 | 
			
		||||
		public string Address { get; set; }
 | 
			
		||||
		public string INN { get; set; }
 | 
			
		||||
		public string SNILS { get; set; }
 | 
			
		||||
		public string Pasport { get; set; }
 | 
			
		||||
		public List<Rabotnik> Rabota { get; set; }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										133
									
								
								DataBase/FuncDB.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								DataBase/FuncDB.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,133 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace Diplom_O.DataBase
 | 
			
		||||
{
 | 
			
		||||
	public static class FuncDB
 | 
			
		||||
	{
 | 
			
		||||
		public static void ChkShtat(Shtat obj)
 | 
			
		||||
		{
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				if (string.IsNullOrEmpty(obj.Doljnost)) throw new Exception("Пустая должность.");
 | 
			
		||||
				if (obj.Size <= 0) throw new Exception("Количество мест <= 0.");
 | 
			
		||||
			}
 | 
			
		||||
			catch { throw; }
 | 
			
		||||
		}
 | 
			
		||||
		public static Shtat GetShtat(int id)
 | 
			
		||||
		{
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				using (var db = new MainDB())
 | 
			
		||||
				{
 | 
			
		||||
					var res = from a in db.Shtat
 | 
			
		||||
										where a.Id == id
 | 
			
		||||
										select a;
 | 
			
		||||
					try { return res.Single(); }
 | 
			
		||||
					catch { return null; }
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch { throw; }
 | 
			
		||||
		}
 | 
			
		||||
		public static (Shtat shtat, int ost)[] ListShtat(string filter = null)
 | 
			
		||||
		{
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				using (var db = new MainDB())
 | 
			
		||||
				{
 | 
			
		||||
					var f = (string.IsNullOrEmpty(filter)) ? "" : filter.ToLower();
 | 
			
		||||
					var shtat = (string.IsNullOrEmpty(f)) ?
 | 
			
		||||
						(from a in db.Shtat
 | 
			
		||||
						 where a.Active
 | 
			
		||||
						 select a).ToArray() :
 | 
			
		||||
						 (from a in db.Shtat
 | 
			
		||||
							where a.Doljnost.ToLower().Contains(f) && a.Active
 | 
			
		||||
							select a).ToArray();
 | 
			
		||||
					var res = new List<(Shtat shtat, int ost)>();
 | 
			
		||||
					for(var i = 0; i < shtat.Length; i++)
 | 
			
		||||
						res.Add((shtat[i], shtat[i].Size - BusySizeShtat(shtat[i].Id)));
 | 
			
		||||
					return res.ToArray();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch { throw; }
 | 
			
		||||
		}
 | 
			
		||||
		public static void AddShtat(Shtat obj)
 | 
			
		||||
		{
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				ChkShtat(obj);
 | 
			
		||||
				if (HaveDoljFromShtat(obj.Doljnost)) throw new Exception("Должность существует.");
 | 
			
		||||
				using (var db = new MainDB())
 | 
			
		||||
				{
 | 
			
		||||
					obj.Active = true;
 | 
			
		||||
					db.Shtat.Add(obj);
 | 
			
		||||
					db.SaveChanges();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch { throw; }
 | 
			
		||||
		}
 | 
			
		||||
		public static void ChangeShtat(Shtat obj)
 | 
			
		||||
		{
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				ChkShtat(obj);
 | 
			
		||||
				if (GetShtat(obj.Id) == null) throw new Exception("Должность не существует.");
 | 
			
		||||
				if (GetShtat(obj.Id).Doljnost != obj.Doljnost && HaveDoljFromShtat(obj.Doljnost)) throw new Exception("Должность существует.");
 | 
			
		||||
				if (obj.Size < BusySizeShtat(obj.Id)) throw new Exception("Занятых мест больше > указанных.");
 | 
			
		||||
				using (var db = new MainDB())
 | 
			
		||||
				{
 | 
			
		||||
					db.Shtat.Update(obj);
 | 
			
		||||
					db.SaveChanges();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch { throw; }
 | 
			
		||||
		}
 | 
			
		||||
		public static void DelShtat(Shtat obj)
 | 
			
		||||
		{
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				ChkShtat(obj);
 | 
			
		||||
				if (GetShtat(obj.Id) == null) throw new Exception("Должность не существует.");
 | 
			
		||||
				if (BusySizeShtat(obj.Id) > 0) throw new Exception("Есть занятые места.");
 | 
			
		||||
				obj.Active = false;
 | 
			
		||||
				using (var db = new MainDB())
 | 
			
		||||
				{
 | 
			
		||||
					db.Shtat.Update(obj);
 | 
			
		||||
					db.SaveChanges();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch { throw; }
 | 
			
		||||
		}
 | 
			
		||||
		public static int BusySizeShtat(int id)
 | 
			
		||||
		{
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				using (var db = new MainDB())
 | 
			
		||||
				{
 | 
			
		||||
					var res = (from a in db.Rabotniky
 | 
			
		||||
										 where a.ShtatId == id && !a.End.HasValue
 | 
			
		||||
										 select a).ToArray();
 | 
			
		||||
					return res.Length;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch { throw; }
 | 
			
		||||
		}
 | 
			
		||||
		public static bool HaveDoljFromShtat(string dolj)
 | 
			
		||||
		{
 | 
			
		||||
			try
 | 
			
		||||
			{
 | 
			
		||||
				using (var db = new MainDB())
 | 
			
		||||
				{
 | 
			
		||||
					var chkDolj = from a in db.Shtat
 | 
			
		||||
												where a.Doljnost == dolj && a.Active
 | 
			
		||||
												select a;
 | 
			
		||||
					return chkDolj.Count() > 0;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			catch { throw; }
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user