134 lines
3.2 KiB
C#
134 lines
3.2 KiB
C#
|
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; }
|
|||
|
}
|
|||
|
}
|
|||
|
}
|