using Microsoft.EntityFrameworkCore.Metadata.Internal; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Diplom_B.DB { public static class WorkDB { public static void Init() { using (var DB = new MainDB()) { { var usr = (from u in DB.Users where u.Name == "admin" select u).ToArray(); if (usr.Length < 1) { DB.Users.Add(new DB.User() { Name = "admin", Pass = "admin", Dog = 3, Doc = 3, Izv = 3, Post = 3, Izd = 3, Zak = 3, Set = 3, Default = 6 }); } } { var stat = (from u in DB.Statusy select u).ToArray(); if (stat.Length < 1) { DB.Statusy.Add(new Status() {Stat = "Без статуса." }); } } DB.SaveChanges(); } } public static Document GetDocument(int id) { try { using (var db = new MainDB()) { var res = (from a in db.Documenty where a.Id == id select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static Dogovor GetDogovor(int id) { try { using (var db = new MainDB()) { var res = (from a in db.Dogovory where a.Id == id select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static Izdelie GetIzdelie(int id) { try { using (var db = new MainDB()) { var res = (from a in db.Izdeliya where a.Id == id select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static Izveschenie GetIzveschenie(int id) { try { using (var db = new MainDB()) { var res = (from a in db.Izvescheniya where a.Id == id select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static Platej GetPlatej(int id) { try { using (var db = new MainDB()) { var platej = (from a in db.Platejy where a.Id == id select a).ToArray(); if (platej.Length != 1) return null; else return platej[0]; } } catch { throw; } } public static Postavka GetPostavka(int id) { try { using (var db = new MainDB()) { var res = (from a in db.Postavki where a.Id == id select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static Status GetStatus(int id) { try { using (var db = new MainDB()) { var res = (from a in db.Statusy where a.Id == id select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static User GetUser(int id) { try { using (var db = new MainDB()) { var res = (from a in db.Users where a.Id == id select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static Zakazchik GetZakazchik(int id) { try { using (var db = new MainDB()) { var res = (from a in db.Zakazchiki where a.Id == id select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static DogIzd GetDogIzd(int id) { try { using (var db = new MainDB()) { var dogIzd = (from a in db.DogIzds where a.Id == id select a).ToArray(); if (dogIzd.Length != 1) return null; else return dogIzd[0]; } } catch { throw; } } public static DogIzd GetDogIzd(int dogId, int izdId) { try { using (var db = new MainDB()) { var dogIzd = (from a in db.DogIzds where a.DogovorId == dogId && a.IzdelieId == izdId select a).ToArray(); if (dogIzd.Length != 1) return null; else return dogIzd[0]; } } catch { throw; } } public static DocIzv GetDocIzv(int docId, int izvId) { try { using (var db = new MainDB()) { var res = (from a in db.DocIzvs where a.DocumentId == docId && a.IzveschenieId == izvId select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static DogDoc GetDogDoc(int id) { try { using (var db = new MainDB()) { var dogDoc = (from a in db.DogDocs where a.Id == id select a).ToArray(); if (dogDoc.Length != 1) return null; else return dogDoc[0]; } } catch { throw; } } public static DogDoc GetDogDoc(int dogId, int docId) { try { using (var db = new MainDB()) { var res = (from a in db.DogDocs where a.DogovorId == dogId && a.DocumentId == docId select a).ToArray(); if (res.Length != 1) return null; else return res[0]; } } catch { throw; } } public static Dogovor[] ListDogovor(string filter = "") { var f = filter.ToLower(); try { using (var db = new MainDB()) { if (string.IsNullOrEmpty(filter)) { var tmp = (from a in db.Dogovory select a).ToArray(); return tmp; } else { var tmp = (from a in db.Dogovory where a.Id.ToString().ToLower().Contains(f) || a.DogNum.ToLower().Contains(f) || a.DataPostavky.ToString("yyyy.MM.dd").Contains(f) || a.PrikazZapusk.ToLower().Contains(f) || a.Garantiy.ToLower().Contains(f) || a.Primechanie.ToLower().Contains(f) select a).ToArray(); return tmp; } } } catch { throw; } } public static void AddDogovor(Dogovor dog) { try { using (var db = new MainDB()) { db.Dogovory.Add(dog); db.SaveChanges(); } } catch { throw; } } public static void ChangeDogovor(Dogovor dog) { try { using (var db = new MainDB()) { db.Dogovory.Update(dog); db.SaveChanges(); } } catch { throw; } } public static void DeleteDogovor(Dogovor dog) { try { using (var db = new MainDB()) { db.Dogovory.Remove(dog); db.SaveChanges(); } } catch { throw; } } public static User[] ListUser(string filter = "") { var f = filter.ToLower(); try { using (var db = new MainDB()) { if (string.IsNullOrEmpty(filter)) { var tmp = (from a in db.Users select a).ToArray(); return tmp; } else { var tmp = (from a in db.Users where a.Id.ToString().ToLower().Contains(f) || a.Name.ToLower().Contains(f) select a).ToArray(); return tmp; } } } catch { throw; } } public static void AddUser(User usr) { try { using (var db = new MainDB()) { db.Users.Add(usr); db.SaveChanges(); } } catch { throw; } } public static void ChangeUser(User usr) { try { using (var db = new MainDB()) { db.Users.Update(usr); db.SaveChanges(); } } catch { throw; } } public static void DeleteUser(User usr) { try { using (var db = new MainDB()) { db.Users.Remove(usr); db.SaveChanges(); } } catch { throw; } } public static string[] GetUserList() { try { using (var db = new MainDB()) { var usrName = (from a in db.Users select a.Name).ToArray(); return usrName; } } catch { throw; } } public static Izdelie[] ListIzdelie(string filter = "") { var f = filter.ToLower(); try { using (var db = new MainDB()) { if (string.IsNullOrEmpty(filter)) { var tmp = (from a in db.Izdeliya select a).ToArray(); return tmp; } else { var tmp = (from a in db.Izdeliya where a.Id.ToString().ToLower().Contains(f) || a.Name.ToLower().Contains(f) || a.DecNum.ToLower().Contains(f) || a.Shifr.ToLower().Contains(f) || a.Litera.ToLower().Contains(f) || a.Cena.ToString("F2").ToLower().Contains(f) || a.OtdelRazrab.ToString().ToLower().Contains(f) || a.Ved.ToLower().Contains(f) || a.GlavKonstr.ToLower().Contains(f) select a).ToArray(); return tmp; } } } catch { throw; } } public static void AddIzdelie(Izdelie izd) { try { using (var db = new MainDB()) { db.Izdeliya.Add(izd); db.SaveChanges(); } } catch { throw; } } public static void ChangeIzdelie(Izdelie izd) { try { using (var db = new MainDB()) { db.Izdeliya.Update(izd); db.SaveChanges(); } } catch { throw; } } public static void DeleteIzdelie(Izdelie izd) { try { using (var db = new MainDB()) { db.Izdeliya.Remove(izd); db.SaveChanges(); } } catch { throw; } } public static Dogovor[] GetDogovoryFromIzdeliya(int id) { try { using (var db = new MainDB()) { var post = (from a in db.DogIzds where a.IzdelieId == id select a.Dogovor).ToList(); for (var i = 0; i < post.Count; i++) while ((post.FindAll(x => x.Id == post[i].Id).Count > 1)) post.RemoveAt(i); return post.ToArray(); } } catch { throw; } } public static Izveschenie[] ListIzveschenie(string filter = "") { var f = filter.ToLower(); try { using (var db = new MainDB()) { if (string.IsNullOrEmpty(filter)) { var tmp = (from a in db.Izvescheniya select a).ToArray(); return tmp; } else { var tmp = (from a in db.Izvescheniya where a.Id.ToString().ToLower().Contains(f) || a.IzvNum.ToLower().Contains(f) || a.InvNum.ToString().ToLower().Contains(f) || a.IzmNum.ToString().ToLower().Contains(f) || a.UkazZad.ToLower().Contains(f) || a.UkazVnedr.ToLower().Contains(f) || a.FileName.ToLower().Contains(f) select a).ToArray(); return tmp; } } } catch { throw; } } public static void AddIzveschenie(Izveschenie izv) { try { using (var db = new MainDB()) { db.Izvescheniya.Add(izv); db.SaveChanges(); } } catch { throw; } } public static void ChangeIzveschenie(Izveschenie izv) { try { using (var db = new MainDB()) { db.Izvescheniya.Update(izv); db.SaveChanges(); } } catch { throw; } } public static void DeleteIzveschenie(Izveschenie izv) { try { using (var db = new MainDB()) { db.Izvescheniya.Remove(izv); db.SaveChanges(); } } catch { throw; } } public static Document[] GetDocumentyFromIzvechenie(int id) { try { using (var db = new MainDB()) { var doc = (from a1 in db.Documenty join a2 in db.DocIzvs on a1.Id equals a2.DocumentId where a2.IzveschenieId == id select a1).ToArray(); return doc; } } catch { throw; } } public static Zakazchik[] ListZakazchik(string filter = "") { var f = filter.ToLower(); try { using (var db = new MainDB()) { if (string.IsNullOrEmpty(filter)) { var tmp = (from a in db.Zakazchiki select a).ToArray(); return tmp; } else { var tmp = (from a in db.Zakazchiki where a.Id.ToString().ToLower().Contains(f) || a.Name.ToLower().Contains(f) || a.Adress.ToLower().Contains(f) || a.Phone.ToLower().Contains(f) || a.Email.ToLower().Contains(f) select a).ToArray(); return tmp; } } } catch { throw; } } public static void AddZakazchik(Zakazchik zak) { try { using (var db = new MainDB()) { db.Zakazchiki.Add(zak); db.SaveChanges(); } } catch { throw; } } public static void ChangeZakazchik(Zakazchik zak) { try { using (var db = new MainDB()) { db.Zakazchiki.Update(zak); db.SaveChanges(); } } catch { throw; } } public static void DeleteZakazchik(Zakazchik zak) { try { using (var db = new MainDB()) { db.Zakazchiki.Remove(zak); db.SaveChanges(); } } catch { throw; } } public static string[] GetZakazchikList() { try { using (var db = new MainDB()) { var usrName = (from a in db.Zakazchiki select a.Name).ToArray(); return usrName; } } catch { throw; } } public static Dogovor[] GetDogovoryFromZakazchik(int id) { try { using (var db = new MainDB()) { var dog = (from a in db.Dogovory where a.ZakazchikId == id select a).ToArray(); return dog; } } catch { throw; } } public static Status[] ListStatus() { try { using (var db = new MainDB()) { var tmp = (from a in db.Statusy select a).ToArray(); return tmp; } } catch { throw; } } public static void AddStatus(Status stat) { try { using (var db = new MainDB()) { db.Statusy.Add(stat); db.SaveChanges(); } } catch { throw; } } public static void ChangeStatus(Status stat) { try { using (var db = new MainDB()) { db.Statusy.Update(stat); db.SaveChanges(); } } catch { throw; } } public static void DeleteStatus(Status stat) { try { using (var db = new MainDB()) { db.Statusy.Remove(stat); db.SaveChanges(); } } catch { throw; } } public static string[] GetStatusList() { try { using (var db = new MainDB()) { var tmp = (from a in db.Statusy select a.Stat).ToArray(); return tmp; } } catch { throw; } } public static int? GetIdStatus(string name) { try { using (var db = new MainDB()) { var tmp = (from a in db.Statusy where a.Stat == name select a.Id).ToArray(); if (tmp.Length == 1) return tmp[0]; else return null; } } catch { throw; } } public static Postavka[] ListPostavka(string filter = "") { var f = filter.ToLower(); try { using (var db = new MainDB()) { if (string.IsNullOrEmpty(filter)) { var tmp = (from a in db.Postavki select a).ToArray(); return tmp; } else { var tmp = (from a in db.Postavki where a.Id.ToString().ToLower().Contains(f) || a.ZavNum.ToLower().Contains(f) || a.DataPostavki.ToString("yyyy.MM.dd").ToLower().Contains(f) || a.Status.Stat.ToString().ToLower().Contains(f) || a.Primechanie.ToLower().Contains(f) select a).ToArray(); return tmp; } } } catch { throw; } } public static void AddPostavka(Postavka post) { try { using (var db = new MainDB()) { db.Postavki.Add(post); db.SaveChanges(); } } catch { throw; } } public static void ChangePostavka(Postavka post) { try { using (var db = new MainDB()) { db.Postavki.Update(post); db.SaveChanges(); } } catch { throw; } } public static void DeletePostavka(Postavka post) { try { using (var db = new MainDB()) { db.Postavki.Remove(post); db.SaveChanges(); } } catch { throw; } } public static string[] GetPostavkiZavNum() { try { using (var db = new MainDB()) { var post = (from a in db.Postavki select a.ZavNum).ToArray(); return post; } } catch { throw; } } public static Postavka[] GetPostavkyFromIzdeliya(int id) { try { using (var db = new MainDB()) { var post = (from a in db.Postavki where a.IzdelieId == id select a).ToArray(); return post; } } catch { throw; } } public static Postavka[] GetPostavkyFromStatus(int id) { try { using (var db = new MainDB()) { var post = (from a in db.Postavki where a.StatusId == id select a).ToArray(); return post; } } catch { throw; } } public static Postavka[] GetPostavkyFromDogovor(int id) { try { using (var db = new MainDB()) { var post = (from a in db.Postavki where a.DogovorId == id select a).ToArray(); return post; } } catch { throw; } } public static Document[] ListDocumenty(string filter = "") { var f = filter.ToLower(); try { using (var db = new MainDB()) { if (string.IsNullOrEmpty(filter)) { var tmp = (from a in db.Documenty select a).ToArray(); return tmp; } else { var tmp = (from a in db.Documenty where a.Id.ToString().ToLower().Contains(f) || a.InvNum.ToLower().Contains(f) || a.DecNum.ToLower().Contains(f) || a.Name.ToLower().Contains(f) || a.FileName.ToLower().Contains(f) || a.Primechanie.ToLower().Contains(f) select a).ToArray(); return tmp; } } } catch { throw; } } public static void AddDocument(Document doc) { try { using (var db = new MainDB()) { db.Documenty.Add(doc); db.SaveChanges(); } } catch { throw; } } public static void ChangeDocument(Document doc) { try { using (var db = new MainDB()) { db.Documenty.Update(doc); db.SaveChanges(); } } catch { throw; } } public static void DeleteDocument(Document doc) { try { using (var db = new MainDB()) { db.Documenty.Remove(doc); db.SaveChanges(); } } catch { throw; } } public static Izveschenie[] GetIzvechenieFromDocumenty(int id) { try { using (var db = new MainDB()) { var izv = (from a1 in db.Izvescheniya join a2 in db.DocIzvs on a1.Id equals a2.IzveschenieId where a2.DocumentId == id select a1).ToArray(); return izv; } } catch { throw; } } public static Dogovor[] GetDogovorFromDocumenty(int id) { try { using (var db = new MainDB()) { var dog = (from a1 in db.Dogovory join a2 in db.DogDocs on a1.Id equals a2.DogovorId where a2.DocumentId == id select a1).ToArray(); return dog; } } catch { throw; } } public static (Izdelie izd, DogIzd dogIzd)[] GetIzdelieDogIzdFromDogovor(int id) { try { using (var db = new MainDB()) { var izd = (from a1 in db.DogIzds join a2 in db.Izdeliya on a1.IzdelieId equals a2.Id where a1.DogovorId == id orderby a1.Id select a2).ToArray(); var di = (from a1 in db.DogIzds join a2 in db.Izdeliya on a1.IzdelieId equals a2.Id where a1.DogovorId == id orderby a1.Id select a1).ToArray(); if (izd.Length != di.Length) return null; var izdDi = new List<(Izdelie izd, DogIzd dogIzd)>(); for (var i = 0; i < izd.Length; i++) izdDi.Add((izd[i], di[i])); return izdDi.ToArray(); } } catch { throw; } } public static Izdelie GetIzdelieFromDogIzd(int id) { try { using (var db = new MainDB()) { var izd = (from a1 in db.DogIzds join a2 in db.Izdeliya on a1.IzdelieId equals a2.Id where a1.Id == id select a2).ToArray(); if (izd.Length != 1) return null; else return izd[0]; } } catch { throw; } } public static (Document doc, DogDoc dogDoc)[] GetDocumentyDogDocFromDogovor(int id) { try { using (var db = new MainDB()) { var doc = (from a1 in db.DogDocs join a2 in db.Documenty on a1.DocumentId equals a2.Id where a1.DogovorId == id orderby a1.Id select a2).ToArray(); var dd = (from a1 in db.DogDocs join a2 in db.Documenty on a1.DocumentId equals a2.Id where a1.DogovorId == id orderby a1.Id select a1).ToArray(); if (doc.Length != dd.Length) return null; var docDd = new List<(Document doc, DogDoc dogDoc)>(); for (var i = 0; i < doc.Length; i++) docDd.Add((doc[i], dd[i])); return docDd.ToArray(); } } catch { throw; } } public static Document GetDocumentFromDogDoc(int id) { try { using (var db = new MainDB()) { var doc = (from a1 in db.DogDocs join a2 in db.Documenty on a1.DocumentId equals a2.Id where a1.Id == id select a2).ToArray(); if (doc.Length != 1) return null; else return doc[0]; } } catch { throw; } } public static void AddPlatej(Platej pl) { try { using (var db = new MainDB()) { db.Platejy.Add(pl); db.SaveChanges(); } } catch { throw; } } public static void ChangePlatej(Platej pl) { try { using (var db = new MainDB()) { db.Platejy.Update(pl); db.SaveChanges(); } } catch { throw; } } public static void DeletePlatej(Platej pl) { try { using (var db = new MainDB()) { db.Platejy.Remove(pl); db.SaveChanges(); } } catch { throw; } } public static Platej[] GetPlatejyFromDogovor(int id) { try { using (var db = new MainDB()) { var pl = (from a in db.Platejy where a.DogovorId == id select a).ToArray(); return pl; } } catch { throw; } } public static void AddDogIzd(DogIzd di) { try { using (var db = new MainDB()) { db.DogIzds.Add(di); db.SaveChanges(); } } catch { throw; } } public static void ChangeDogIzd(DogIzd di) { try { using (var db = new MainDB()) { db.DogIzds.Update(di); db.SaveChanges(); } } catch { throw; } } public static void DeleteDogIzd(DogIzd di) { try { using (var db = new MainDB()) { db.DogIzds.Remove(di); db.SaveChanges(); } } catch { throw; } } public static void AddDocIzv (DocIzv di) { try { using (var db = new MainDB()) { db.DocIzvs.Add(di); db.SaveChanges(); } } catch { throw; } } public static void DeleteDocIzv(DocIzv di) { try { using (var db = new MainDB()) { db.DocIzvs.Remove(di); db.SaveChanges(); } } catch { throw; } } public static void AddDogDoc(DogDoc dd) { try { using (var db = new MainDB()) { db.DogDocs.Add(dd); db.SaveChanges(); } } catch { throw; } } public static void DeleteDogDoc(DogDoc dd) { try { using (var db = new MainDB()) { db.DogDocs.Remove(dd); db.SaveChanges(); } } catch { throw; } } } }