Diplom_B/DB/WorkDB.cs

308 lines
5.9 KiB
C#
Raw Normal View History

2021-07-15 23:13:18 +05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diplom_B.DB
{
2021-07-16 16:43:55 +05:00
public static class WorkDB
{
public static void Init()
{
2021-07-15 23:13:18 +05:00
using (var DB = new MainDB())
{
{
2021-07-17 09:33:09 +05:00
var usr = (from u in DB.Users
where u.Name == "admin"
select u).ToArray();
if (usr.Length < 1)
2021-07-15 23:13:18 +05:00
{
2021-07-17 09:33:09 +05:00
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 = 1
});
}
2021-07-15 23:13:18 +05:00
}
2021-07-17 09:33:09 +05:00
{
var stat = (from u in DB.Statusy
select u).ToArray();
if (stat.Length < 1)
{
DB.Statusy.Add(new Status() {Stat = "Без статуса." });
}
}
2021-07-15 23:13:18 +05:00
DB.SaveChanges();
}
2021-07-16 16:43:55 +05:00
}
2021-07-15 23:13:18 +05:00
public static Izdelie[] ListIzdelie(string filter = "")
2021-07-16 16:43:55 +05:00
{
var f = filter.ToLower();
try
{
2021-07-15 23:13:18 +05:00
using (var db = new MainDB())
2021-07-16 16:43:55 +05:00
{
2021-07-15 23:13:18 +05:00
if (string.IsNullOrEmpty(filter))
{
var tmp = (from a in db.Izdeliya
select a).ToArray();
return tmp;
}
2021-07-16 16:43:55 +05:00
else
{
2021-07-15 23:13:18 +05:00
var tmp = (from a in db.Izdeliya
where
2021-07-16 16:43:55 +05:00
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)
2021-07-15 23:13:18 +05:00
select a).ToArray();
return tmp;
}
2021-07-16 16:43:55 +05:00
}
}
2021-07-15 23:13:18 +05:00
catch { throw; }
2021-07-16 16:43:55 +05:00
}
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; }
}
2021-07-15 23:13:18 +05:00
public static void AddIzdelie(Izdelie izd)
2021-07-16 16:43:55 +05:00
{
2021-07-15 23:13:18 +05:00
try
{
using (var db = new MainDB())
{
db.Izdeliya.Add(izd);
db.SaveChanges();
}
}
catch { throw; }
2021-07-16 16:43:55 +05:00
}
public static void ChangeIzdelie(Izdelie izd)
{
try
{
2021-07-15 23:13:18 +05:00
using (var db = new MainDB())
2021-07-16 16:43:55 +05:00
{
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 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 DeleteIzdelie(Izveschenie izv)
{
try
{
using (var db = new MainDB())
{
db.Izvescheniya.Remove(izv);
db.SaveChanges();
}
}
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
2021-07-15 23:13:18 +05:00
where a.Id == id
select a).ToArray();
if (res.Length != 1)
return null;
else
return res[0];
2021-07-16 16:43:55 +05:00
}
}
catch { throw; }
}
public static void AddZakazchik(Zakazchik zak)
{
try
{
using (var db = new MainDB())
{
db.Zakazchiki.Add(zak);
db.SaveChanges();
}
}
2021-07-15 23:13:18 +05:00
catch { throw; }
2021-07-16 16:43:55 +05:00
}
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; }
}
}
2021-07-15 23:13:18 +05:00
}