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 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 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 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 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 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 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 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 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 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 a in db.Documenty where a.DocIzvs.FindAll(x => x.IzveschenieId == id).Count > 0 select a).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 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 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 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 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 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 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 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 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; } } } }