Diplom_O/DataBase/FuncDB.cs
2021-07-25 09:43:21 +05:00

325 lines
8.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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; }
}
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
{
using (var db = new MainDB())
{
var res = from a in db.Chely
where a.Id == id
select a;
try { return res.Single(); }
catch { return null; }
}
}
catch { throw; }
}
public static Chel[] ListChel(string filter = null)
{
try
{
using (var db = new MainDB())
{
var f = (string.IsNullOrEmpty(filter)) ? "" : filter.ToLower();
return (string.IsNullOrEmpty(f)) ?
(from a in db.Chely
select a).ToArray() :
(from a in db.Chely
where
a.FName.ToLower().Contains(f) ||
a.SName.ToLower().Contains(f) ||
a.TName.ToLower().Contains(f) ||
a.Birthday.ToString("yyyy.MM.dd").ToLower().Contains(f) ||
a.Address.ToLower().Contains(f) ||
a.INN.ToLower().Contains(f) ||
a.SNILS.ToLower().Contains(f) ||
a.Pasport.ToLower().Contains(f)
select a).ToArray();
}
}
catch { throw; }
}
public static bool HaveChelRabotnik(int id, bool all=false)
{
try
{
using (var db = new MainDB())
{
var res = (from a in db.Rabotniky
where a.ChelId == id && (all || !a.End.HasValue)
select a).ToArray();
return res.Length > 0;
}
}
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; }
}
}
}