Work on Chel
This commit is contained in:
@@ -15,6 +15,7 @@ namespace Diplom_O.DataBase
|
||||
public DbSet<Shtat> Shtat { get; set; }
|
||||
public DbSet<Rabotnik> Rabotniky { get; set; }
|
||||
public DbSet<Chel> Chely { get; set; }
|
||||
public DbSet<ChelToChel> ChelToChely { get; set; }
|
||||
|
||||
public MainDB()
|
||||
{
|
||||
@@ -71,6 +72,19 @@ namespace Diplom_O.DataBase
|
||||
public string Pasport { get; set; }
|
||||
public List<Rabotnik> Rabota { get; set; }
|
||||
}
|
||||
public class ChelToChel
|
||||
{
|
||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
[ForeignKey("Chel1")]
|
||||
public int Chel1Id { get; set; }
|
||||
public Chel Chel1 { get; set; }
|
||||
[ForeignKey("Chel2")]
|
||||
public int Chel2Id { get; set; }
|
||||
public Chel Chel2 { get; set; }
|
||||
public int Lvl { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Diplom_O.DataBase
|
||||
@@ -130,6 +131,20 @@ namespace Diplom_O.DataBase
|
||||
catch { throw; }
|
||||
}
|
||||
|
||||
public static void ChkChel(Chel chel)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(chel.FName)) throw new Exception("Фамилия не указана.");
|
||||
if (string.IsNullOrEmpty(chel.SName)) throw new Exception("Имя не указано.");
|
||||
if (chel.Birthday > DateTime.Now) throw new Exception("Дата рождения не верна.");
|
||||
var rgx = new Regex("^[0-9]{12}$");
|
||||
if (!rgx.IsMatch(chel.INN)) throw new Exception("ИНН указан неверно.");
|
||||
rgx = new Regex("^[0-9]{3}-[0-9]{3}-[0-9]{3} [0-9]{2}$");
|
||||
if (!rgx.IsMatch(chel.SNILS)) throw new Exception("СНИЛС указан неверно.");
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
public static Chel GetChel(int id)
|
||||
{
|
||||
try
|
||||
@@ -170,7 +185,7 @@ namespace Diplom_O.DataBase
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
public static bool HaveChelFromRabotniky(int id, bool all=false)
|
||||
public static bool HaveChelRabotnik(int id, bool all=false)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -184,6 +199,126 @@ namespace Diplom_O.DataBase
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
public static void AddChel(Chel chel)
|
||||
{
|
||||
try
|
||||
{
|
||||
ChkChel(chel);
|
||||
using (var db = new MainDB())
|
||||
{
|
||||
db.Chely.Add(chel);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
public static void ChangeChel(Chel chel)
|
||||
{
|
||||
try
|
||||
{
|
||||
ChkChel(chel);
|
||||
if (GetChel(chel.Id) == null) throw new Exception("Человека не существует.");
|
||||
using (var db = new MainDB())
|
||||
{
|
||||
db.Chely.Update(chel);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
public static void DelChel(Chel chel)
|
||||
{
|
||||
try
|
||||
{
|
||||
ChkChel(chel);
|
||||
if (GetChel(chel.Id) == null) throw new Exception("Человека не существует.");
|
||||
if (HaveChelRabotnik(chel.Id, true)) throw new Exception("Человек есть в таблице работников.");
|
||||
using (var db = new MainDB())
|
||||
{
|
||||
db.Chely.Remove(chel);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
public static bool HaveChelToChel(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var db = new MainDB())
|
||||
{
|
||||
var res = (from a in db.ChelToChely
|
||||
where a.Chel1Id == id || a.Chel2Id == id
|
||||
select a).Count();
|
||||
return res > 0;
|
||||
}
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
public static bool CanBirthdayChelToChel(int id, DateTime date)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var db = new MainDB())
|
||||
{
|
||||
var res = (from a in db.ChelToChely
|
||||
join b in db.Chely on a.Chel2Id equals b.Id
|
||||
join c in db.Chely on a.Chel1Id equals c.Id
|
||||
where (a.Chel1Id == id && date > b.Birthday) ||
|
||||
(a.Chel2Id == id && date < c.Birthday)
|
||||
select a).Count();
|
||||
return res == 0;
|
||||
}
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
public static (int chelToChelId, Chel chel, int lvl)[] ListRodFromChel(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var rodChel = new List<(int chelToChelId, Chel chel, int lvl)>();
|
||||
using (var db = new MainDB())
|
||||
{
|
||||
var parents = new List<(Chel chel,int lvl)>() { (GetChel(id), 0) };
|
||||
var childrens = new List<(Chel chel, int lvl)>() { (GetChel(id), 0) };
|
||||
while (parents.Count > 0 || childrens.Count > 0)
|
||||
{
|
||||
var newParents = new List<(Chel chel, int lvl)>();
|
||||
var newChildrens = new List<(Chel chel, int lvl)>();
|
||||
for(var i = 0; i < parents.Count || i < childrens.Count; i++)
|
||||
{
|
||||
var parentsLinks =
|
||||
(from a in db.ChelToChely
|
||||
where (i < parents.Count) && a.Chel2Id == parents[i].chel.Id
|
||||
select a).ToArray();
|
||||
var childrensLinks =
|
||||
(from a in db.ChelToChely
|
||||
where (i < childrens.Count) && a.Chel1Id == childrens[i].chel.Id
|
||||
select a).ToArray();
|
||||
for (var j = 0; j < parentsLinks.Length || j < childrensLinks.Length; j++)
|
||||
{
|
||||
var chel = j < parentsLinks.Length ? GetChel(parentsLinks[j].Chel1Id) : null;
|
||||
if (chel != null && rodChel.FindAll(x => x.chel.Id == chel.Id).Count == 0)
|
||||
{
|
||||
rodChel.Add((parentsLinks[j].Id, chel, parents[i].lvl - parentsLinks[j].Lvl));
|
||||
newParents.Add((chel, parents[i].lvl - parentsLinks[j].Lvl));
|
||||
}
|
||||
|
||||
chel = j < childrensLinks.Length ? GetChel(childrensLinks[j].Chel2Id) : null;
|
||||
if (chel != null && rodChel.FindAll(x => x.chel.Id == chel.Id).Count == 0)
|
||||
{
|
||||
rodChel.Add((childrensLinks[j].Id, chel, childrens[i].lvl + childrensLinks[j].Lvl));
|
||||
newChildrens.Add((chel, childrens[i].lvl + childrensLinks[j].Lvl));
|
||||
}
|
||||
}
|
||||
}
|
||||
parents = newParents;
|
||||
childrens = newChildrens;
|
||||
}
|
||||
}
|
||||
return rodChel.OrderByDescending(x => x.lvl).ToArray();
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user