Last fixes

This commit is contained in:
2024-09-20 06:53:52 +03:00
parent 09f10a924c
commit d07c586ade
139 changed files with 12409 additions and 1234 deletions

226
DataBase/AccessDB.cs Normal file
View File

@@ -0,0 +1,226 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class Access
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public User User { get; set; }
public int FormId { get; set; }
public int FormatAccess { get; set; }
}
public static partial class FuncDB
{
public static void AccessCheckValid(Access obj)
{
try
{
if (obj == null) throw new Exception("Ошибка инициализации.");
if (UserGetById(obj.UserId) == null) throw new Exception("Пользователь не указан.");
if (obj.FormId < 0 || obj.FormId >= FormArray.formName.Length) throw new Exception("Неверно указана форма.");
if (obj.FormId < 0 || obj.FormatAccess >= FormArray.formAccess.Length) throw new Exception("Неверно указаны разрешения");
}
catch { throw; }
}
public static Access AccessGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Accessy
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static Access[] AccessGetByUserIdFormId(int UserId, int FormId)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Accessy
where a.UserId == UserId && a.FormId == FormId
select a;
try
{
return res.ToArray();
}
catch
{
throw;
}
}
}
catch
{
throw;
}
}
public static Access AccessGetByUserIdFormIdAccessId(int UserId, int FormId, int AccessId)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Accessy
where
a.UserId == UserId &&
a.FormId == FormId &&
a.FormatAccess == AccessId
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void AccessAdd(Access obj)
{
try
{
AccessCheckValid(obj);
var local_obj = AccessGetByUserIdFormIdAccessId(obj.UserId, obj.FormId, obj.FormatAccess);
if (local_obj == null)
using (var db = new MainDB())
{
db.Accessy.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void AccessDelete(Access obj)
{
try
{
if (obj == null)
return;
var localObj = AccessGetById(obj.Id);
if (localObj == null)
throw new Exception("Разрешение не существует.");
using (var db = new MainDB())
{
db.Accessy.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static Access[] AccessList(int UserId)
{
try
{
using (var db = new MainDB())
{
var access = (
from a in db.Accessy
where a.UserId == UserId
orderby a.FormId
orderby a.FormatAccess
select a
).ToArray();
return access;
}
}
catch
{
throw;
}
}
public static bool[,] AccessListTable(int UserId)
{
try
{
var result = new bool[FormArray.formName.Length, FormArray.formAccess.Length];
using (var db = new MainDB())
{
var list = AccessList(UserId);
foreach (var r in list)
result[r.FormId, r.FormatAccess] = true;
return result;
}
}
catch
{
throw;
}
}
public static int AccessToSettingsFullCountWithoutUserId(User user)
{
try
{
using (var db = new MainDB())
{
var users =
(from a in db.Usery
where a.Id != user.Id
select a).ToArray();
var count = 0;
foreach(var usr in users)
{
var access =
from a in db.Accessy
where
a.UserId == usr.Id &&
a.FormId == 7
select a;
if (access.Count() == FormArray.formAccess.Length)
count++;
}
return count;
}
}
catch
{
throw;
}
}
}
}

322
DataBase/ChelDB.cs Normal file
View File

@@ -0,0 +1,322 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
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 Phone { get; set; }
public string SNILS { get; set; }
public string Pasport { get; set; }
public bool Pensia { get; set; }
public List<CorrStaj> CorrStajy { get; set; }
public List<Learn> Learny { get; set; }
public List<Rabotnik> Rabotniky { get; set; }
public List<Free> Freey { get; set; }
}
public static partial class FuncDB
{
public static void ChelCheckValid(Chel obj)
{
try
{
if (obj == null) throw new Exception("Ошибка инициализации человека.");
if (string.IsNullOrEmpty(obj.FName)) throw new Exception("Фамилия не указана.");
if (string.IsNullOrEmpty(obj.SName)) throw new Exception("Имя не указано.");
if (obj.Birthday > DateTime.Now) throw new Exception("Дата рождения не верна.");
if (obj.Birthday > DateTime.Now.AddYears(-18).AddDays(1)) throw new Exception("Нельза добавить несовершеннолетнего.");
var rgx = new Regex("^[0-9]{12}$");
if (!rgx.IsMatch(obj.INN)) throw new Exception("ИНН указан неверно.");
rgx = new Regex("^[0-9]{3}-[0-9]{3}-[0-9]{3} [0-9]{2}$");
if (!rgx.IsMatch(obj.SNILS)) throw new Exception("СНИЛС указан неверно.");
if (string.IsNullOrEmpty(obj.Pasport)) throw new Exception("Надо добавить паспорт.");
}
catch { throw; }
}
public static Chel ChelGetById(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 ChelGetByINN(string inn)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Chely
where a.INN == inn
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static Chel ChelGetBySNILS(string snils)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Chely
where a.SNILS == snils
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static string ChelGetFIOShortById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
(from a in db.Chely
where a.Id == id
select a).Single();
try
{
return res.FName + " " +
res.SName[0].ToString() + "." +
((!string.IsNullOrEmpty(res.TName)) ?
res.TName[0].ToString() + "." : "");
}
catch
{
return "";
}
}
}
catch
{
throw;
}
}
public static void ChelAdd(Chel obj)
{
try
{
ChelCheckValid(obj);
if (ChelGetByINN(obj.INN) != null)
throw new Exception("ИНН повторяется.");
if (ChelGetBySNILS(obj.SNILS) != null)
throw new Exception("СНИЛС повторяется.");
using (var db = new MainDB())
{
db.Chely.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void ChelChange(Chel obj)
{
try
{
ChelCheckValid(obj);
var localObj = ChelGetById(obj.Id);
if (localObj == null)
throw new Exception("Человека не существует.");
var innObj = ChelGetByINN(obj.INN);
if (innObj != null && innObj.Id != localObj.Id)
throw new Exception("ИНН повторяется.");
var snilsObj = ChelGetBySNILS(obj.SNILS);
if (snilsObj != null && snilsObj.Id != localObj.Id)
throw new Exception("СНИЛС повторяется.");
using (var db = new MainDB())
{
db.Chely.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void ChelDelete(Chel obj)
{
try
{
if (obj == null)
return;
var localObj = ChelGetById(obj.Id);
if (localObj == null)
throw new Exception("Человека не существует.");
using (var db = new MainDB())
{
db.Chely.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static Chel[] ChelList(string filter = null, bool isWorker = true)
{
try
{
using (var db = new MainDB())
{
var chels = (
from a in db.Chely
select a
).ToList();
filter = filter == null ? "" : filter.ToLower();
for (var i = 0; i < chels.Count;)
{
if (!isWorker)
{
var check = new List<Rabotnik>(RabotnikHistoryByChelId(chels[i].Id));
var a = check.FindAll(x => !x.End.HasValue || (x.Start >= DateTime.Now && x.End.Value <= DateTime.Now)).Count();
if (a > 0)
{
chels.RemoveAt(i);
continue;
}
}
var fname = chels[i].FName.ToLower();
var sname = chels[i].SName.ToLower();
var tname = chels[i].TName.ToLower();
var bday = chels[i].Birthday.ToString("yyyy.MM.dd");
var adres = chels[i].Address.ToLower();
var inn = chels[i].INN.ToLower();
var snils = chels[i].SNILS.ToLower();
var pasp = chels[i].Pasport.ToLower();
if (
fname.Contains(filter) || sname.Contains(filter) ||
tname.Contains(filter) || bday.Contains(filter) ||
adres.Contains(filter) || inn.Contains(filter) ||
snils.Contains(filter) || pasp.Contains(filter)
)
i++;
else
chels.RemoveAt(i);
}
return chels.ToArray();
}
}
catch
{
throw;
}
}
public static string[][] ChelListTableMain(string filter = null, bool isWorker = true)
{
try
{
return ChelListTableMain(ChelList(filter, isWorker));
}
catch
{
throw;
}
}
public static string[][] ChelListTableMain(Chel[] data)
{
try
{
var result = new List<string[]>();
foreach (var chel in data)
result.Add(
new string[]
{
chel.Id.ToString(),
chel.FName.ToString(),
chel.SName.ToString(),
chel.TName.ToString(),
chel.Male ? "Муж." : "Жен.",
chel.Birthday.ToString("yyyy.MM.dd"),
chel.Address.ToString(),
chel.Phone.ToString(),
chel.INN.ToString(),
chel.SNILS.ToString(),
chel.Pasport.ToString(),
chel.Pensia ? "Да" : "Нет",
}
);
return result.ToArray();
}
catch
{
throw;
}
}
}
}

264
DataBase/ChildDB.cs Normal file
View File

@@ -0,0 +1,264 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class Child
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Chel")]
public int? Parent1Id { get; set; }
public Chel Parent1 { get; set; }
[ForeignKey("Chel")]
public int? Parent2Id { get; set; }
public Chel Parent2 { 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 static partial class FuncDB
{
public static void ChildCheckValid(Child obj)
{
try
{
if (obj == null)
throw new Exception("Ошибка инициализации ребенка.");
if (string.IsNullOrEmpty(obj.FName))
throw new Exception("Пустая фамилия.");
if (string.IsNullOrEmpty(obj.FName))
throw new Exception("Пустое имя.");
if (!obj.Parent2Id.HasValue && !obj.Parent1Id.HasValue)
throw new Exception("Не указано ни одного родителя.");
if (obj.Parent2Id.HasValue && obj.Parent1Id.HasValue && obj.Parent1Id.Value == obj.Parent2Id.Value)
throw new Exception("Указан один и тот же родитель 2 раза.");
}
catch
{
throw;
}
}
public static Child ChildGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Childy
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void ChildAdd(Child obj)
{
try
{
ChildCheckValid(obj);
if (!obj.Parent1Id.HasValue && obj.Parent2Id.HasValue)
{
obj.Parent1Id = obj.Parent2Id;
obj.Parent2Id = null;
}
using (var db = new MainDB())
{
db.Childy.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void ChildChange(Child obj)
{
try
{
ChildCheckValid(obj);
var localObjById = ChildGetById(obj.Id);
if (localObjById == null)
throw new Exception("Не существует запись о ребенке.");
if (!obj.Parent1Id.HasValue && obj.Parent2Id.HasValue)
{
obj.Parent1Id = obj.Parent2Id;
obj.Parent2Id = null;
}
using (var db = new MainDB())
{
db.Childy.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void ChildDelete(Child obj)
{
try
{
if (obj == null)
return;
var localObj = ChildGetById(obj.Id);
if (localObj == null)
throw new Exception("Запись о ребенке не существует.");
using (var db = new MainDB())
{
db.Childy.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static Child[] ChildList(string filter = null)
{
try
{
using (var db = new MainDB())
{
var child = (
from a in db.Childy
select a
).ToList();
var _filter = string.IsNullOrEmpty(filter) ? "" : filter.ToLower();
for (var i = 0; i < child.Count;)
{
var fname = child[i].FName.ToLower();
var sname = child[i].SName.ToLower();
var tname = child[i].TName.ToLower();
var bday = child[i].Birthday.ToString("yyyy.MM.dd");
if (
fname.Contains(filter) || sname.Contains(filter) ||
tname.Contains(filter) || bday.Contains(filter)
)
i++;
else
child.RemoveAt(i);
}
return child.ToArray();
}
}
catch
{
throw;
}
}
public static string[][] ChildTableMain(string filter = null)
{
try
{
using (var db = new MainDB())
{
var result = new List<string[]>();
var childs = ChildList(filter);
foreach (var child in childs)
{
result.Add(
new string[]
{
child.Id.ToString(),
child.FName + " " +
child.SName[0].ToString() + "." +
((!string.IsNullOrEmpty(child.TName)) ?
child.TName[0].ToString() + "." : ""),
child.Male ? "Мальчик" : "Девочка",
child.Birthday.ToString("yyyy.MM.dd"),
child.Parent1Id.HasValue ? ChelGetFIOShortById(child.Parent1Id.Value) : "",
child.Parent2Id.HasValue ? ChelGetFIOShortById(child.Parent2Id.Value) : ""
}
);
}
return result.ToArray();
}
}
catch
{
throw;
}
}
public static int ChildCount(DateTime dt, bool? isMale = null)
{
try
{
using (var db = new MainDB())
{
var workIds = (
from a in db.Rabotniky
where
!a.End.HasValue ||
(
dt >= a.Start
&&
dt <= a.End.Value
)
select a.ChelId
).ToArray();
var _dt = dt.AddYears(-15);
var res = (
from a in db.Childy
where
a.Birthday > _dt
&&
(
!isMale.HasValue
||
a.Male == isMale.Value
)
&&
(
(
a.Parent1Id.HasValue
&&
workIds.Contains(a.Parent1Id.Value)
)
||
(
a.Parent2Id.HasValue
&&
workIds.Contains(a.Parent2Id.Value)
)
)
select a
).Count();
return res;
}
}
catch
{
throw;
}
}
}
}

View File

@@ -15,7 +15,14 @@ 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 DbSet<TypeStaj> TypeStajy { get; set; }
public DbSet<RabotnikTypeStaj> RabotnikTypeStajy { get; set; }
public DbSet<Child> Childy { get; set; }
public DbSet<Learn> Learny { get; set; }
public DbSet<CorrStaj> CorrStajy { get; set; }
public DbSet<Free> Freey { get; set; }
public DbSet<Access> Accessy { get; set; }
public DbSet<User> Usery { get; set; }
public MainDB()
{
@@ -29,67 +36,30 @@ namespace Diplom_O.DataBase
protected override void OnModelCreating(ModelBuilder modelBuilder) { }
}
public class Rabotnik
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Chel")]
public int ChelId { get; set; }
public Chel Chel { get; set; }
[ForeignKey("Shtat")]
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
public static partial class FuncDB
{
[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; }
}
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; }
public static void InitDB()
{
if (UserGetByLogin("Admin") != null)
return;
var user = new User()
{
Login = "Admin",
Pass = "Admin",
Hide = true,
Default = 7
};
UserAdd(user);
}
}
}
}

179
DataBase/CorrStajDB.cs Normal file
View File

@@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class CorrStaj
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Chel")]
public int ChelId { get; set; }
public Chel Chel { get; set; }
[ForeignKey("TypeStaj")]
public int TypeStajId { get; set; }
public TypeStaj TypeStaj { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public bool AddFlag { get; set; }
}
public static partial class FuncDB
{
public static void CorrStajValid(CorrStaj obj)
{
try
{
if (obj == null)
throw new Exception("Ошибка инициализации стажа.");
}
catch
{
throw;
}
}
public static CorrStaj CorrStajGetById(int id)
{
try
{
using (var db = new MainDB())
{
var corrstaj =
from a in db.CorrStajy
where a.Id == id
select a;
try
{
return corrstaj.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static CorrStaj CorrStaj(int chelId, string name)
{
try
{
using (var db = new MainDB())
{
var ts = TypeStajGetByName(name);
if (ts == null)
return null;
var corrstaj =
from a in db.CorrStajy
where a.TypeStajId == ts.Id && a.ChelId == chelId
select a;
try
{
return corrstaj.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void CorrStajAdd(CorrStaj obj)
{
try
{
CorrStajValid(obj);
using (var db = new MainDB())
{
db.CorrStajy.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void CorrStajChange(CorrStaj obj)
{
try
{
CorrStajValid(obj);
var localObjById = CorrStajGetById(obj.Id);
var localObjByName = CorrStaj(obj.ChelId, TypeStajGetById(obj.TypeStajId).Name);
if (localObjById == null)
throw new Exception("Корректировка стажа не существует.");
if (localObjByName != null &&
localObjByName.Id != localObjById.Id)
throw new Exception("Корректировка стажа существует.");
using (var db = new MainDB())
{
db.CorrStajy.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void CorrStajDelete(CorrStaj obj)
{
try
{
if (obj == null)
return;
var localObj = CorrStajGetById(obj.Id);
if (localObj == null)
throw new Exception("Корректироваки стажа не существует.");
using (var db = new MainDB())
{
db.CorrStajy.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static CorrStaj[] CorrStajList(int chelId)
{
try
{
using (var db = new MainDB())
{
var res = (
from a in db.CorrStajy
where a.ChelId == chelId
select a
).ToList();
return res.ToArray();
}
}
catch
{
throw;
}
}
}
}

224
DataBase/FreeDB.cs Normal file
View File

@@ -0,0 +1,224 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class Free
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Chel")]
public int ChelId { get; set; }
public Chel Chel { get; set; }
public string Type { get; set; }
public DateTime Start { get; set; }
public DateTime? End { get; set; }
}
public static partial class FuncDB
{
public static void FreeCheckValid(Free obj)
{
try
{
if (obj == null) throw new Exception("Ошибка инициализации.");
if (string.IsNullOrEmpty(obj.Type)) throw new Exception("Тип не указан.");
}
catch { throw; }
}
public static Free FreeGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Freey
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void FreeAdd(Free obj)
{
try
{
FreeCheckValid(obj);
using (var db = new MainDB())
{
obj.Start = new DateTime(obj.Start.Year, obj.Start.Month, obj.Start.Day, 0, 0, 0);
if (obj.End.HasValue)
obj.End = new DateTime(obj.End.Value.Year, obj.End.Value.Month, obj.End.Value.Day, 23, 59, 59);
db.Freey.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void FreeChange(Free obj)
{
try
{
FreeCheckValid(obj);
var localObj = FreeGetById(obj.Id);
if (localObj == null)
throw new Exception("Отстранение не указано.");
using (var db = new MainDB())
{
obj.Start = new DateTime(obj.Start.Year, obj.Start.Month, obj.Start.Day, 0, 0, 0);
if (obj.End.HasValue)
obj.End = new DateTime(obj.End.Value.Year, obj.End.Value.Month, obj.End.Value.Day, 23, 59, 59);
db.Freey.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void FreeDelete(Free obj)
{
try
{
if (obj == null)
return;
var localObj = FreeGetById(obj.Id);
if (localObj == null)
throw new Exception("Отстранение не существует.");
using (var db = new MainDB())
{
db.Freey.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static Free[] FreeList(int chelId)
{
try
{
using (var db = new MainDB())
{
var free = (
from a in db.Freey
where a.ChelId == chelId
select a
).ToArray();
return free;
}
}
catch
{
throw;
}
}
public static Dictionary<string, int> FreeListDays(int chelId, DateTime start, DateTime end)
{
try
{
using (var db = new MainDB())
{
var list =
from a in db.Freey
where
a.ChelId == chelId &&
a.Start <= end &&
(!a.End.HasValue || a.End.Value >= start)
select a;
var subres = new Dictionary<string, int>();
foreach(var r in list)
{
var s = r.Start <= start ? start : r.Start;
var e = r.End.HasValue ?
r.End.Value >= end ? end :
r.End.Value :
end;
var d = (int)Math.Ceiling((e - s).TotalDays);
if (d <= 0)
continue;
if (!subres.ContainsKey("Общее"))
subres.Add("Общее", 0);
subres["Общее"] = subres["Общее"] + d;
if (!subres.ContainsKey(r.Type))
subres.Add(r.Type, 0);
subres[r.Type] = subres[r.Type] + d;
}
return subres;
}
}
catch
{
throw;
}
}
public static string[][] FreeListTable(int chelId, DateTime? start = null, DateTime? end = null)
{
try
{
using (var db = new MainDB())
{
var list =
from a in db.Freey
where
a.ChelId == chelId &&
(!end.HasValue || a.Start <= end.Value) &&
(!a.End.HasValue || !start.HasValue || a.End.Value >= start.Value)
select a;
var subres = new List<string[]>();
foreach (var r in list)
{
var s = !start.HasValue ? r.Start :
r.Start <= start ? start.Value : r.Start;
var e = r.End.HasValue ?
end.HasValue ?
r.End.Value >= end ? end.Value :
r.End.Value :
end.Value :
DateTime.Now;
subres.Add(
new string[]{
r.Id.ToString(),
r.Type,
s.ToString("yyyy.MM.dd"),
e.ToString("yyyy.MM.dd"),
Math.Ceiling((e - s).TotalDays).ToString()
});
}
return subres.ToArray();
}
}
catch
{
throw;
}
}
}
}

View File

@@ -1,340 +0,0 @@
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
(!string.IsNullOrEmpty(a.FName) && a.FName.ToLower().Contains(f)) ||
(!string.IsNullOrEmpty(a.SName) && a.SName.ToLower().Contains(f)) ||
(!string.IsNullOrEmpty(a.TName) && a.TName.ToLower().Contains(f)) ||
a.Birthday.ToString("yyyy.MM.dd").ToLower().Contains(f) ||
(!string.IsNullOrEmpty(a.Address) && a.Address.ToLower().Contains(f)) ||
(!string.IsNullOrEmpty(a.INN) && a.INN.ToLower().Contains(f)) ||
(!string.IsNullOrEmpty(a.SNILS) && a.SNILS.ToLower().Contains(f)) ||
(!string.IsNullOrEmpty(a.Pasport) && 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; }
}
public static ChelToChel GetChelToChel(int id)
{
try
{
using (var db = new MainDB())
{
var res = from a in db.ChelToChely
where a.Id == id
select a;
try { return res.Single(); }
catch { return null; }
}
}
catch { throw; }
}
}
}

182
DataBase/LearnDB.cs Normal file
View File

@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class Learn
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Chel")]
public int ChelId { get; set; }
public Chel Chel { get; set; }
public string NameSchool { get; set; }
public string Type { get; set; }
public string Spec { get; set; }
public string Curs { get; set; }
public string ProfPer { get; set; }
public DateTime End { get; set; }
}
public static partial class FuncDB
{
public static void LearnCheckValid(Learn obj)
{
try
{
if (obj == null) throw new Exception("Ошибка инициализации.");
if (ChelGetById(obj.ChelId) == null) throw new Exception("Человек не указан.");
if (string.IsNullOrEmpty(obj.NameSchool)) throw new Exception("Обр. учр. не указано.");
if (string.IsNullOrEmpty(obj.Type)) throw new Exception("Уровень не указан.");
if (string.IsNullOrEmpty(obj.Spec)) throw new Exception("Специальность не указана.");
}
catch { throw; }
}
public static Learn LearnGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Learny
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void LearnAdd(Learn obj)
{
try
{
LearnCheckValid(obj);
using (var db = new MainDB())
{
db.Learny.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void LearnChange(Learn obj)
{
try
{
LearnCheckValid(obj);
var localObj = LearnGetById(obj.Id);
if (localObj == null)
throw new Exception("Образование не указано.");
using (var db = new MainDB())
{
db.Learny.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void LearnDelete(Learn obj)
{
try
{
if (obj == null)
return;
var localObj = LearnGetById(obj.Id);
if (localObj == null)
throw new Exception("Образование не существует.");
using (var db = new MainDB())
{
db.Learny.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static Learn[] LearnList(int chelId)
{
try
{
using (var db = new MainDB())
{
var learns = (
from a in db.Learny
where a.ChelId == chelId
select a
).ToArray();
return learns;
}
}
catch
{
throw;
}
}
public static string[][] LearnListTableMain(int chelId)
{
try
{
return LearnListTableMain(LearnList(chelId));
}
catch
{
throw;
}
}
public static string[][] LearnListTableMain(Learn[] data)
{
try
{
var result = new List<string[]>();
foreach (var learn in data)
result.Add(
new string[]
{
learn.Id.ToString(),
learn.NameSchool,
learn.Type,
learn.Spec,
learn.End.ToString("yyyy.MM.dd"),
learn.Curs,
learn.ProfPer
}
);
return result.ToArray();
}
catch
{
throw;
}
}
}
}

448
DataBase/RabotinkDB.cs Normal file
View File

@@ -0,0 +1,448 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class Rabotnik
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Chel")]
public int ChelId { get; set; }
public Chel Chel { get; set; }
[ForeignKey("Shtat")]
public int? ShtatId { get; set; }
public Shtat Shtat { get; set; }
public string TabNum { get; set; }
public bool AnotherWork { get; set; }
public DateTime Start { get; set; }
public DateTime? End { get; set; }
public List<RabotnikTypeStaj> RabotnikTypeStajy { get; set; }
}
public static partial class FuncDB
{
public static void RabotnikCheckValid(Rabotnik obj)
{
try
{
if (obj == null)
throw new Exception("Ошибка инициализации сотрудника.");
if (string.IsNullOrEmpty(obj.TabNum) && !obj.AnotherWork)
throw new Exception("Пустой табельный номер.");
if (
obj.Start > (obj.End.HasValue ? obj.End.Value : DateTime.Now)
)
throw new Exception("Неверная дата.");
}
catch
{
throw;
}
}
public static Rabotnik RabotnikGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Rabotniky
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static Rabotnik[] RabotnikHistoryById(int id)
{
try
{
var rabotnik = RabotnikGetById(id);
RabotnikCheckValid(rabotnik);
return
RabotnikHistoryByChelId(
rabotnik.ChelId
);
}
catch
{
throw;
}
}
public static Rabotnik[] RabotnikHistoryByRabotingId(Rabotnik obj)
{
try
{
RabotnikCheckValid(obj);
return RabotnikHistoryByChelId(
obj.ChelId
);
}
catch
{
throw;
}
}
public static Rabotnik[] RabotnikHistoryByChelId(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Rabotniky
where a.ChelId == id
orderby a.Start
select a;
return res.ToArray();
}
}
catch
{
throw;
}
}
public static bool RabotnikCheckDate(int chelId, DateTime start, DateTime? end, int rabotnikId = -1)
{
try
{
using (var db = new MainDB())
{
var now = DateTime.Now;
var _end = end ?? now;
return (
from a in db.Rabotniky
where
a.ChelId == chelId
&&
(rabotnikId != -1 ? a.Id != rabotnikId : true)
&&
(
(
a.Start >= start
&&
a.Start <= _end
)
||
(
(a.End ?? now) >= start
&&
(a.End ?? now) <= _end
)
)
select a
).Count() == 0;
}
}
catch
{
throw;
}
}
public static void RabotnikAdd(Rabotnik obj)
{
try
{
RabotnikCheckValid(obj);
if (!RabotnikCheckDate(obj.ChelId, obj.Start, obj.End))
throw new Exception("Попадает в существующий временной интервал.");
using (var db = new MainDB())
{
db.Rabotniky.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void RabotnikChange(Rabotnik obj)
{
try
{
RabotnikCheckValid(obj);
var localObjById = RabotnikGetById(obj.Id);
if (localObjById == null)
throw new Exception("Не существует запись о сотруднике.");
if (obj.ChelId != localObjById.ChelId)
throw new Exception("Нельзя менять человека.");
if (!RabotnikCheckDate(obj.ChelId, obj.Start, obj.End, obj.Id))
throw new Exception("Пересекатеся с другими записями о работе.");
/*if (ShtatGetById(obj.ShtatId) == null)
throw new Exception("Должность не существует.");*/
if (Program.IsDebug)
throw new Exception("НАДО ДОРАБОТАТЬ ПРОВЕРКУ ЗАПОЛНЕНИЯ ДОЛЖНОСТЕЙ");
using (var db = new MainDB())
{
db.Rabotniky.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void RabotnikDelete(Rabotnik obj)
{
try
{
if (obj == null)
return;
var localObj = RabotnikGetById(obj.Id);
if (localObj == null)
throw new Exception("Запись о сотруднике не существует.");
using (var db = new MainDB())
{
var type = FuncDB.RabotnikTypeStajList(obj.Id);
foreach (var r in type)
FuncDB.RabotnikTypeStajDelete(r);
db.Rabotniky.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static Rabotnik[] RabotnikList(string filter = null)
{
try
{
using (var db = new MainDB())
{
var _filter = string.IsNullOrEmpty(filter) ? null : filter.ToLower();
var rabotnik = (
from a in db.Rabotniky
select a
);
return rabotnik.ToArray();
}
}
catch
{
throw;
}
}
public static Rabotnik[] RabotnikListLast(string filter = null)
{
try
{
using (var db = new MainDB())
{
var _chel = FuncDB.ChelList();
var rabotnik = new List<Rabotnik>();
var _filter = string.IsNullOrEmpty(filter) ? null : filter.ToLower();
foreach (var c in _chel)
{
var rab = (
from a in db.Rabotniky
where a.End == null && a.ChelId == c.Id
select a
).ToArray();
if (rab.Length == 0)
{
rab = (
from a in db.Rabotniky
where a.ChelId == c.Id
orderby a.End descending
select a
).ToArray();
}
if (rab.Length > 0)
rabotnik.Add(rab[0]);
}
return rabotnik.ToArray();
}
}
catch
{
throw;
}
}
public static Rabotnik[] RabotnikListByChelId(int chelId)
{
try
{
using (var db = new MainDB())
{
var rabotnik = (
from a in db.Rabotniky
where a.ChelId == chelId
orderby a.Start
select a
);
return rabotnik.ToArray();
}
}
catch
{
throw;
}
}
public static string[][] RabotnikTableMain(string filter = null)
{
try
{
return RabotnikTableMain(RabotnikListLast(filter));
}
catch
{
throw;
}
}
public static string[][] RabotnikTableMain(Rabotnik[] data)
{
try
{
using (var db = new MainDB())
{
var result = new List<string[]>();
foreach (var rabotnik in data)
{
result.Add(
new string[]
{
rabotnik.Id.ToString(),
rabotnik.TabNum,
ChelGetFIOShortById(rabotnik.ChelId),
rabotnik.AnotherWork ?
"Иное место" :
ShtatGetById(rabotnik.ShtatId.Value).Doljnost
}
);
}
return result.ToArray();
}
}
catch
{
throw;
}
}
public static string[][] RabotnikByChelIdTable(int chelId)
{
try
{
return RabotnikByChelIdTable(RabotnikListByChelId(chelId));
}
catch
{
throw;
}
}
public static string[][] RabotnikByChelIdTable(Rabotnik[] data)
{
try
{
using (var db = new MainDB())
{
var result = new List<string[]>();
foreach (var rabotnik in data)
{
result.Add(
new string[]
{
rabotnik.Id.ToString(),
rabotnik.TabNum,
rabotnik.AnotherWork ?
"Иное место" :
ShtatGetById(rabotnik.ShtatId.Value).Doljnost,
rabotnik.Start.ToString("yyyy.MM.dd"),
rabotnik.End.HasValue ? rabotnik.End.Value.ToString("yyyy.MM.dd") : "По настоящее время"
}
);
}
return result.ToArray();
}
}
catch
{
throw;
}
}
public static (int year, int month, int day) RabotnikGetTotalTime(Rabotnik obj, DateTime? onDate = null)
{
if (!onDate.HasValue)
onDate = DateTime.Now;
var year = 0;
var month = 0;
var day = 0;
if (obj == null)
return (year, month, day);
var cursor =
obj.End.HasValue ?
obj.End.Value <= onDate ?
obj.End.Value :
onDate.Value :
onDate.Value;
var finish = obj.Start;
if (cursor <= finish)
return (year, month, day);
while (finish.Year < cursor.Year)
{
year++;
cursor = cursor.AddYears(-1);
}
if (finish.Month > cursor.Month)
{
year--;
cursor = cursor.AddYears(1);
}
while (finish.Month != cursor.Month)
{
month++;
cursor = cursor.AddMonths(-1);
}
if (finish.Day > cursor.Day)
{
month--;
cursor = cursor.AddMonths(1);
}
while (finish.Day != cursor.Day)
{
day++;
cursor = cursor.AddDays(-1);
}
while (day < 0)
{
day += 30;
month--;
}
while (month < 0)
{
month += 12;
year--;
}
return (year, month, day);
}
}
}

View File

@@ -0,0 +1,188 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class RabotnikTypeStaj
{
public int Id { get; set; }
[ForeignKey("Rabotnik")]
public int RabitnikId { get; set; }
[ForeignKey("TypeStaj")]
public int TypeStajId { get; set; }
}
public static partial class FuncDB
{
public static void RabotnikTypeStajCheckValid(RabotnikTypeStaj obj)
{
try
{
if (obj == null)
throw new Exception("Ошибка инициализации стажа сотрудника.");
}
catch
{
throw;
}
}
public static RabotnikTypeStaj RabotnikTypeStajGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.RabotnikTypeStajy
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static RabotnikTypeStaj RabotnikTypeStajGetByTypeStajIdRabotnikId(int typeStajId, int rabotnikId)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.RabotnikTypeStajy
where a.RabitnikId == rabotnikId &&
a.TypeStajId == typeStajId
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void RabotnikTypeStajAdd(RabotnikTypeStaj obj)
{
try
{
RabotnikTypeStajCheckValid(obj);
if (RabotnikTypeStajGetByTypeStajIdRabotnikId(obj.TypeStajId, obj.RabitnikId) != null )
throw new Exception("Тип стажа сотрудника существует.");
using (var db = new MainDB())
{
db.RabotnikTypeStajy.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void RabotnikTypeStajDelete(RabotnikTypeStaj obj)
{
try
{
if (obj == null)
return;
var localObj = RabotnikTypeStajGetById(obj.Id);
if (localObj == null)
throw new Exception("Тип стажа сотрудника не существует.");
using (var db = new MainDB())
{
db.RabotnikTypeStajy.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static RabotnikTypeStaj[] RabotnikTypeStajList(int rabId)
{
try
{
using (var db = new MainDB())
{
var res = (
from a in db.RabotnikTypeStajy
where a.RabitnikId == rabId
select a
).ToList();
return res.ToArray();
}
}
catch
{
throw;
}
}
public static string[][] RabotnikTypeStajListString(int rabId)
{
try
{
var rts = RabotnikTypeStajList(rabId);
var res = new List<string[]>();
foreach (var r in rts)
{
res.Add(
new string[]
{
r.Id.ToString(),
TypeStajGetById(r.TypeStajId).Name
}
);
}
return res.ToArray();
}
catch
{
throw;
}
}
public static RabotnikTypeStaj[] RabotnikTypeStajListByTypeStaj(int typeStaj)
{
try
{
using (var db = new MainDB())
{
var res = (
from a in db.RabotnikTypeStajy
where a.TypeStajId == typeStaj
select a
).ToList();
return res.ToArray();
}
}
catch
{
throw;
}
}
}
}

310
DataBase/ShtatDB.cs Normal file
View File

@@ -0,0 +1,310 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Diplom_O.DataBase
{
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 List<Rabotnik> Rabotniky { get; set; }
}
public static partial class FuncDB
{
public static void ShtatCheckValid(Shtat obj)
{
try
{
if (obj == null)
throw new Exception("Ошибка инициализации должности.");
if (string.IsNullOrEmpty(obj.Doljnost))
throw new Exception("Пустая должность.");
if (obj.Size <= 0)
throw new Exception("Количество мест <= 0.");
}
catch
{
throw;
}
}
private static void ShtatClear()
{
try
{
using (var db = new MainDB())
{
var shtat = (
from a in db.Shtat
where !a.Active
select a).ToArray();
for (var i = 0; i < shtat.Length; i++)
if(ShtatFreeSpaceByShtat(shtat[i]) == shtat[i].Size)
{
db.Shtat.Remove(shtat[i]);
db.SaveChanges();
}
}
}
catch
{
return;
}
}
public static Shtat ShtatGetById(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 ShtatGetByDoljnost(string doljnost)
{
try
{
using (var db = new MainDB())
{
var chkDolj =
from a in db.Shtat
where a.Doljnost == doljnost && a.Active
select a;
try
{
return chkDolj.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static int ShtatFreeSpaceById(int id)
{
try
{
return
ShtatFreeSpaceByShtat(
ShtatGetById(id)
);
}
catch
{
throw;
}
}
public static int ShtatFreeSpaceByShtat(Shtat obj)
{
try
{
ShtatCheckValid(obj);
using (var db = new MainDB())
{
var now = DateTime.Now;
var res = (
from a in db.Rabotniky
where
a.ShtatId == obj.Id
&&
(
!a.End.HasValue
||
(
now >= a.Start
&&
now <= a.End.Value
)
)
select a).Count();
return obj.Size - res;
}
}
catch
{
throw;
}
}
public static void ShtatAdd(Shtat obj)
{
try
{
ShtatCheckValid(obj);
if (ShtatGetByDoljnost(obj.Doljnost) != null)
throw new Exception("Должность существует.");
using (var db = new MainDB())
{
obj.Active = true;
db.Shtat.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void ShtatChange(Shtat obj)
{
try
{
ShtatCheckValid(obj);
var localObjById = ShtatGetById(obj.Id);
var localObjByDoljnost = ShtatGetByDoljnost(obj.Doljnost);
if (localObjById == null)
throw new Exception("Должность не существует.");
if (localObjByDoljnost != null &&
localObjByDoljnost != localObjById)
throw new Exception("Должность существует.");
if (localObjById.Size - ShtatFreeSpaceByShtat(localObjById) > obj.Size )
throw new Exception("Занятых мест больше > указанных.");
using (var db = new MainDB())
{
db.Shtat.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void ShtatDelete(Shtat obj)
{
try
{
if (obj == null)
return;
var localObj = ShtatGetById(obj.Id);
if (localObj == null)
throw new Exception("Должность не существует.");
if (ShtatFreeSpaceByShtat(localObj) != localObj.Size)
throw new Exception("Есть занятые места.");
localObj.Active = false;
using (var db = new MainDB())
{
db.Shtat.Update(localObj);
db.SaveChanges();
}
ShtatClear();
}
catch
{
throw;
}
}
public static (Shtat shtat, int free)[] ShtatList(string filter = null, bool isActual = false)
{
try
{
using (var db = new MainDB())
{
var shtat = (
from a in db.Shtat
where a.Active
select a
).ToList();
var res = new List<(Shtat shtat, int free)>();
for (var i = 0; i < shtat.Count; i++)
{
var free = ShtatFreeSpaceByShtat(shtat[i]);
if (isActual && free <= 0)
continue;
res.Add((
shtat[i],
free
));
}
if (string.IsNullOrEmpty(filter))
return res.ToArray();
filter = filter.ToLower();
for (var i = 0; i < res.Count;)
{
var dolj = res[i].shtat.Doljnost.ToLower();
if (!dolj.Contains(filter))
res.RemoveAt(i);
else
i++;
}
return res.ToArray();
}
}
catch
{
throw;
}
}
public static string[][] ShtatListTableMain(string filter = null, bool isActual = false)
{
try
{
return ShtatListTableMain(ShtatList(filter, isActual));
}
catch
{
throw;
}
}
public static string[][] ShtatListTableMain((Shtat shtat, int free)[] data)
{
try
{
var result = new List<string[]>();
foreach (var (shtat, free) in data)
result.Add(
new string[]
{
shtat.Id.ToString(),
shtat.Doljnost,
shtat.Size.ToString(),
free.ToString()
}
);
return result.ToArray();
}
catch
{
throw;
}
}
}
}

194
DataBase/TypeStajDB.cs Normal file
View File

@@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class TypeStaj
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public List<CorrStaj> CorrStajy { get; set; }
public List<RabotnikTypeStaj> RabotnikTypeStajy { get; set; }
}
public static partial class FuncDB
{
public static void TypeStajCheckValid(TypeStaj obj)
{
try
{
if (obj == null)
throw new Exception("Ошибка инициализации стажа.");
if (string.IsNullOrEmpty(obj.Name))
throw new Exception("Пустой стаж.");
}
catch
{
throw;
}
}
public static TypeStaj TypeStajGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.TypeStajy
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static TypeStaj TypeStajGetByName(string name)
{
try
{
using (var db = new MainDB())
{
var chkTypeStaj =
from a in db.TypeStajy
where a.Name == name
select a;
try
{
return chkTypeStaj.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void TypeStajAdd(TypeStaj obj)
{
try
{
TypeStajCheckValid(obj);
if (TypeStajGetByName(obj.Name) != null)
throw new Exception("Тип стажа существует.");
using (var db = new MainDB())
{
db.TypeStajy.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void TypeStajChange(TypeStaj obj)
{
try
{
TypeStajCheckValid(obj);
var localObjById = TypeStajGetById(obj.Id);
var localObjByName = TypeStajGetByName(obj.Name);
if (localObjById == null)
throw new Exception("Тип стажа не существует.");
if (localObjByName != null &&
localObjByName != localObjById)
throw new Exception("Тип стажа существует.");
using (var db = new MainDB())
{
db.TypeStajy.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void TypeStajDelete(TypeStaj obj)
{
try
{
if (obj == null)
return;
var localObj = TypeStajGetById(obj.Id);
if (localObj == null)
throw new Exception("Тип стажа не существует.");
if (RabotnikTypeStajListByTypeStaj(localObj.Id).Length > 0)
throw new Exception("Тип стажа используется.");
using (var db = new MainDB())
{
db.TypeStajy.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static TypeStaj[] TypeStajList(int[] filter = null)
{
try
{
using (var db = new MainDB())
{
var res = (
from a in db.TypeStajy
where filter == null || filter.Contains(a.Id)
select a
).ToList();
return res.ToArray();
}
}
catch
{
throw;
}
}
public static string[] TypeStajListString(int[] filter = null)
{
try
{
using (var db = new MainDB())
{
var res = (
from a in db.TypeStajy
where filter == null || filter.Contains(a.Id)
select a.Name
).ToList();
res.Remove("Общий");
return res.ToArray();
}
}
catch
{
throw;
}
}
}
}

209
DataBase/UserDB.cs Normal file
View File

@@ -0,0 +1,209 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class User
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Login { get; set; }
public string Pass { get; set; }
public bool Hide { get; set; }
public int Default { get; set; }
public List<Access> Accessy { get; set; }
}
public static partial class FuncDB
{
public static void UserCheckValid(User obj)
{
try
{
if (obj == null) throw new Exception("Ошибка инициализации.");
if (string.IsNullOrEmpty(obj.Login)) throw new Exception("Логин не указан.");
if (string.IsNullOrEmpty(obj.Pass)) throw new Exception("Пароль не указан.");
if (obj.Default < 0 || obj.Default > 7) throw new Exception("Форма не существует.");
}
catch { throw; }
}
public static User UserGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Usery
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static User UserGetByLogin(string login)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Usery
where a.Login == login
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void UserAdd(User obj)
{
try
{
UserCheckValid(obj);
if (UserGetByLogin(obj.Login) != null)
throw new Exception("Пользователь существует.");
using (var db = new MainDB())
{
db.Usery.Add(obj);
db.SaveChanges();
}
var user = UserGetByLogin(obj.Login);
for (var i = 0; i < FormArray.formAccess.Length; i++)
{
var access = new Access()
{
UserId = user.Id,
FormId = user.Default,
FormatAccess = i
};
AccessAdd(access);
}
}
catch
{
throw;
}
}
public static void UserChange(User obj)
{
try
{
UserCheckValid(obj);
var localObj = UserGetById(obj.Id);
if (localObj == null)
throw new Exception("Пользователь не указан.");
var localObjByLogin = UserGetByLogin(obj.Login);
if (localObjByLogin != null &&
localObjByLogin.Id != localObj.Id)
throw new Exception("Логин существует.");
if (AccessGetByUserIdFormIdAccessId(localObj.Id, obj.Default, 0) == null)
throw new Exception("Сначала нужен доступ к форме.");
using (var db = new MainDB())
{
db.Usery.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void UserDelete(User obj)
{
try
{
if (obj == null)
return;
var localObj = UserGetById(obj.Id);
if (localObj == null)
throw new Exception("Пользователь не существует.");
var acessy = AccessList(localObj.Id);
foreach (var a in acessy)
AccessDelete(a);
using (var db = new MainDB())
{
db.Usery.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static User[] UserList(bool showHide = false)
{
try
{
using (var db = new MainDB())
{
var user = (
from a in db.Usery
where (showHide || !a.Hide)
select a
).ToArray();
return user;
}
}
catch
{
throw;
}
}
public static string[][] UserListTable(bool showHide = false)
{
try
{
using (var db = new MainDB())
{
var list = UserList(showHide);
var subres = new List<string[]>();
foreach (var r in list)
{
subres.Add(
new string[]{
r.Login
});
}
return subres.ToArray();
}
}
catch
{
throw;
}
}
}
}