Diplom_O/DataBase/CorrStajDB.cs

180 lines
3.3 KiB
C#
Raw Normal View History

2024-09-20 08:53:52 +05:00
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;
}
}
}
}