Diplom_O/DataBase/ShtatDB.cs
2024-09-20 06:53:52 +03:00

311 lines
5.8 KiB
C#

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;
}
}
}
}