using GummingCommon; using GummingControl; using GummingEntity; using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GummingBusiness { public class SysDebugDA { public static int ReadTotalCount(PagerEntity se) { string dbPath = FilesDirectory.DatabasePath; if (!File.Exists(dbPath)) { throw new Exception("database missing!"); } try { SQLiteConnection conn = SQLiteHelper.CreateConnection(); if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = conn; cmd.CommandText = "SELECT count(*) FROM Sys_Debug WHERE 1=1 " + se.Conditions; SQLiteDataReader sr = cmd.ExecuteReader(); sr.Read(); int count = sr.GetInt32(0); sr.Close(); conn.Close(); return count; } } catch (Exception ex) { throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message); } return 0; } public static RecordEntity Load(PagerEntity se) { string dbPath = FilesDirectory.DatabasePath; if (!File.Exists(dbPath)) { throw new Exception("database missing!"); } try { int count = ReadTotalCount(se); //se.OrderBy, se.SortOrder, se.PageIndex, se.Rows SQLiteConnection conn = SQLiteHelper.CreateConnection(); if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); DataTable dt = SQLiteHelper.ExecuteDataTable(conn, "SELECT * FROM Sys_Debug WHERE 1=1 " + se.Conditions + " order by ModifyTime desc limit " + Convert.ToString(se.Rows) + " offset " + Convert.ToString((se.PageIndex - 1) * se.Rows)); List rows = SQLiteHelper.DataTableToIList(dt) as List; conn.Close(); RecordEntity result = new RecordEntity(); result.total = (int)Math.Ceiling(count * 1.0 / se.Rows); result.page = se.PageIndex; result.records = count; result.rows = rows; return result; } } catch (Exception ex) { throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message); } return null; } public static void Save(SysDebugEntity segment) { string dbPath = FilesDirectory.DatabasePath; if (!File.Exists(dbPath)) { throw new Exception("database missing!"); } try { SQLiteConnection conn = SQLiteHelper.CreateConnection(); if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); using (SQLiteTransaction tran = conn.BeginTransaction()) { Save(conn, segment); tran.Commit(); } conn.Close(); } } catch (Exception ex) { throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message); } } public static void Save(SQLiteConnection conn, SysDebugEntity segment) { SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = conn; cmd.CommandText = "INSERT INTO Sys_Debug (RecId,LogType,LogPage,LogFunction,LogIndex,LogDetail,DebugCode,DebugName,OrderBy,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values(@RecId,@LogType,@LogPage,@LogFunction,@LogIndex,@LogDetail,@DebugCode,@DebugName,@OrderBy,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)"; cmd.Parameters.AddWithValue("@RecId", Guid.NewGuid().ToString()); cmd.Parameters.AddWithValue("@LogType", segment.LogType); cmd.Parameters.AddWithValue("@LogPage", segment.LogPage); cmd.Parameters.AddWithValue("@LogFunction", segment.LogFunction); cmd.Parameters.AddWithValue("@LogIndex", segment.LogIndex); cmd.Parameters.AddWithValue("@LogDetail", segment.LogDetail); cmd.Parameters.AddWithValue("@DebugCode", segment.DebugCode); cmd.Parameters.AddWithValue("@DebugName", segment.DebugName); cmd.Parameters.AddWithValue("@OrderBy", segment.OrderBy); cmd.Parameters.AddWithValue("@CreateBy", Global.CurrentUserCode); cmd.Parameters.AddWithValue("@CreateTime", DateTime.Now); cmd.Parameters.AddWithValue("@ModifyBy", Global.CurrentUserCode); cmd.Parameters.AddWithValue("@ModifyTime", DateTime.Now); cmd.Parameters.AddWithValue("@IsActive", 1); cmd.ExecuteNonQuery(); } public static void Update(SysDebugEntity segment) { string dbPath = FilesDirectory.DatabasePath; if (!File.Exists(dbPath)) { throw new Exception("database missing!"); } try { SQLiteConnection conn = SQLiteHelper.CreateConnection(); if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); using (SQLiteTransaction tran = conn.BeginTransaction()) { Update(conn, segment); tran.Commit(); } conn.Close(); } } catch (Exception ex) { throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message); } } public static void Update(SQLiteConnection conn, SysDebugEntity segment) { SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = conn; cmd.CommandText = "UPDATE Sys_Debug SET LogType = @LogType, LogPage = @LogPage, LogFunction = @LogFunction, LogIndex = @LogIndex, LogDetail = @LogDetail, DebugCode = @DebugCode, DebugName = @DebugName, OrderBy = @OrderBy, ModifyBy = @ModifyBy, ModifyTime = @ModifyTime, IsActive = @IsActive WHERE RecId = @RecId"; cmd.Parameters.AddWithValue("@RecId", segment.RecId); cmd.Parameters.AddWithValue("@LogType", segment.LogType); cmd.Parameters.AddWithValue("@LogPage", segment.LogPage); cmd.Parameters.AddWithValue("@LogFunction", segment.LogFunction); cmd.Parameters.AddWithValue("@LogIndex", segment.LogIndex); cmd.Parameters.AddWithValue("@LogDetail", segment.LogDetail); cmd.Parameters.AddWithValue("@DebugCode", segment.DebugCode); cmd.Parameters.AddWithValue("@DebugName", segment.DebugName); cmd.Parameters.AddWithValue("@OrderBy", segment.OrderBy); cmd.Parameters.AddWithValue("@ModifyBy", Global.CurrentUserCode); cmd.Parameters.AddWithValue("@ModifyTime", DateTime.Now); cmd.Parameters.AddWithValue("@IsActive", 1); cmd.ExecuteNonQuery(); } public static void Delete(string recid) { string dbPath = FilesDirectory.DatabasePath; if (!File.Exists(dbPath)) { throw new Exception("database missing!"); } try { SQLiteConnection conn = SQLiteHelper.CreateConnection(); if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); Delete(conn, recid); conn.Close(); } } catch (Exception ex) { throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message); } } public static void Delete(SQLiteConnection conn, string recid) { SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = conn; cmd.CommandText = "delete from Sys_Debug where recid ='" + recid + "'"; cmd.ExecuteNonQuery(); } public static void Clear() { string dbPath = FilesDirectory.DatabasePath; if (!File.Exists(dbPath)) { throw new Exception("database missing!"); } try { SQLiteConnection conn = SQLiteHelper.CreateConnection(); if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = conn; cmd.CommandText = "delete from Sys_Debug"; cmd.ExecuteNonQuery(); conn.Close(); } } catch (Exception ex) { throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message); } } } }