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

322 lines
7.1 KiB
C#

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