using LiteDB; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace AutoPekage { class LiteDbHelper { private static string liteDbPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "AutoPackage", "sittings.db"); public static void init() { if (!File.Exists(liteDbPath)) { Directory.CreateDirectory(Path.GetDirectoryName(liteDbPath)); AddStmpServer("qq", "smtp.qq.com"); CreateDefaultSettings(); } } public static SettingsDao CreateDefaultSettings() { SettingsDao dao = new SettingsDao(); dao.AskAutoDeleteFile = true; dao.AskAutoLoadFile = true; dao.AutoDeleteFile = 0; dao.AutoLoadFile = 0; dao.DefaultFileName = "我的打包文件.zip"; dao.LastPath = null; dao.SendMail = false; dao.TitleModel = 0; Settings = dao; return dao; } public static void AddStmpServer(string serverName, string stmp) { var dao = new StmpServerDao(); dao.ServerName = serverName; dao.Stmp = stmp; AddStmpServer(dao); } public static void AddStmpServer(StmpServerDao dao) { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("stmpservers"); col.Insert(dao); } } public static List GetAllStmpServer() { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("stmpservers"); return col.FindAll().ToList(); } } public static void AddSender(string name) { var dao = new SenderDao(); dao.Name = name; AddSender(dao); } public static void AddSender(string name, string username, string password, string nikename, string stmpServer) { var dao = new SenderDao(); dao.Name = name; dao.NickName = nikename; dao.Password = password; dao.StmpServer = stmpServer; dao.UserName = username; AddSender(dao); } public static void AddSender(SenderDao dao) { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("senders"); if (dao.Id == 0) { col.Insert(dao); } else { col.Update(dao); } } } public static void DelSender(SenderDao dao) { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("senders"); col.Delete(dao.Id); } } public static List GetAllSender() { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("senders"); return col.FindAll().ToList(); } } public static SenderDao GetSenderById(int id) { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("senders"); return col.FindById(id); } } public static void AddMailTo(string mailto) { var mailToDao = new MailToDao(); mailToDao.MailTo = mailto; using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("mailto"); col.Insert(mailToDao); } } public static List GetAllMailTo() { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("mailto"); return col.FindAll().ToList(); } } public static SettingsDao Settings { get { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("settings"); var dao = col.FindOne(Query.All()); if (dao == null) { dao = CreateDefaultSettings(); } return dao; } } set { using (var db = new LiteDatabase(liteDbPath)) { var col = db.GetCollection("settings"); if (col.Count() > 0) { col.Update(value); } else { col.Insert(value); } } } } } }