gongqiuhong 6 months ago
parent
commit
ee835f33c0

+ 1 - 1
DW5S.App/EditForms/X2D1DTOParamEditor.cs

@@ -15,7 +15,7 @@ using System.Threading.Tasks;
 using DW5S.KxcApi;
 using DW5S.DTO;
 using DW5S.Entity;
-using DW5S.Framework;
+
 using DW5S.Repostory;
 
 namespace DW5S.App.EditForms

+ 0 - 1
DW5S.App/FindRef/PosWithRefFind.cs

@@ -6,7 +6,6 @@ using System.Threading;
 using System.Threading.Tasks;
 using DW5S.DTO;
 using DW5S.Entity;
-using DW5S.Framework;
 using DW5S.Repostory;
 
 namespace DW5S.App

+ 1 - 1
DW5S.App/FindRef/TaskTarCache.cs

@@ -6,7 +6,7 @@ using System.Text;
 using System.Threading.Tasks;
 using DW5S.DTO;
 using DW5S.Entity;
-using DW5S.Framework;
+
 
 namespace DW5S.App
 {

+ 30 - 121
DW5S.App/MainForm.cs

@@ -16,9 +16,7 @@ using DevExpress.XtraEditors;
 using ExtensionsDev;
 using DevExpress.XtraBars.Forms;
 using DW5S.App.CorTools;
-using System.Data.Entity;
 using System.IO;
-using System.Data.Entity.Migrations;
 using DW5S.DTO;
 using DW5S.Entity;
 using DW5S.Repostory;
@@ -37,13 +35,20 @@ using System.Runtime.InteropServices;
 using System.Runtime.InteropServices.ComTypes;
 using DevExpress.LookAndFeel.Design;
 using System.Configuration;
-using DW5S.Framework;
-using DW5S.UI.Lib;
+
+using Microsoft.Extensions.Logging;
+using DW5S.Service;
 
 namespace DW5S
 {
     public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
     {
+        [Autowired]
+        private readonly ILogger logger;
+
+        [Autowired]
+        private readonly UnitOfWork unitOfWork;
+
         Dictionary<string, Type> ctrlTypes = new Dictionary<string, Type>();
         public MainForm()
         {
@@ -71,7 +76,6 @@ namespace DW5S
             ctrlTypes.Add("服务状态", typeof(CtrlSvrs));
             ctrlTypes.Add("服务日志", typeof(CtrlSvrLog));
             DxHelper.WaitHelper.UpdateSplashMessage("正在检查数据库环境...");
-            CheckDb();
 
         }
         private string text;
@@ -79,31 +83,11 @@ namespace DW5S
         {
             this.text = this.Text;
             this.HtmlText = $"<size=12>{this.text}";
-            using (RHDWContext db = new RHDWContext())
-            {
-                SysConfig.Config = db.SysSetings.FirstOrDefault();
-            }
-            if (SysConfig.Config != null)
-            {
-                StartWebApi();
-            }
+            var repsSys = unitOfWork.Reps<SysSetings>();
+            var settings = await repsSys.FirstOrDefaultAsync();
             btn_ItemClick(null, null);
             ServerContext.Instance.Init();
-            if (SysConfig.Config == null)
-            {
-                var size = new Size(500, 600);
-                XtraForm frm = new XtraForm() { StartPosition = FormStartPosition.CenterScreen, CloseBox = false, MinimizeBox = false, MaximizeBox = false };
-                frm.MinimumSize = frm.MaximumSize = frm.Size = size;
-                new CtrlSysSettings() { Dock = DockStyle.Fill, Parent = frm };
-                if (frm.ShowDialog() != DialogResult.Cancel)
-                    StartWebApi();
-                else
-                {
-                    this.Close();
-                    return;
-                }
-            }
-            this.HtmlText = $"<size=12>{text}(<size=9>{SysConfig.Config.TimeZoneDisplayName}</size>)";
+            this.HtmlText = $"<size=12>{text}(<size=9>{settings.TimeZoneDisplayName}</size>)";
             Messenger.Defalut.Sub<SysSetings>("时区改变", settings =>
             {
                 this.HtmlText = $"<size=12>{text}(<size=9>{settings.TimeZoneDisplayName}</size>)";
@@ -129,7 +113,7 @@ namespace DW5S
                     while (true)
                     {
                         //第二章方法获取系统CPU和内存使用情况
-                        LogUI.Info($"设备CPU:{sys.CpuLoad:f1}%,设备内存:{(sys.PhysicalMemory - sys.MemoryAvailable) / GB_DIV:f2}GB/{sys.PhysicalMemory / (double)GB_DIV:f2}GB,平台内存:{sys.ProcessMemory() / (double)GB_DIV:f2}GB,平台线程:{sys.ProcessThread()}").Wait();
+                        logger.LogInformation($"设备CPU:{sys.CpuLoad:f1}%,设备内存:{(sys.PhysicalMemory - sys.MemoryAvailable) / GB_DIV:f2}GB/{sys.PhysicalMemory / (double)GB_DIV:f2}GB,平台内存:{sys.ProcessMemory() / (double)GB_DIV:f2}GB,平台线程:{sys.ProcessThread()}");
                         Thread.Sleep(interval);
                     }
                 }
@@ -138,92 +122,27 @@ namespace DW5S
             });
 
         }
-        private void CheckDb()
-        {
-            try
-            {
-                using (RHDWContext db = new RHDWContext())
-                {
-                    if (File.Exists(db.DbFile))
-                    {
-                        FileInfo fInfo = new FileInfo(db.DbFile);
-                        if (fInfo.IsReadOnly)
-                        {
-                            fInfo.IsReadOnly = false;
-                        }
-
-                    }
-
-                }
-                using (RHDWLogContext db = new RHDWLogContext())
-                {
-                    if (File.Exists(db.DbFile))
-                    {
-                        FileInfo fInfo = new FileInfo(db.DbFile);
-                        if (fInfo.IsReadOnly)
-                        {
-                            fInfo.IsReadOnly = false;
-                        }
-                    }
-                }
-                using (RHDWContext db = new RHDWContext())
-                {
-                    db.SyncDb();
-                }
-                using (RHDWLogContext db = new RHDWLogContext())
-                {
-                    db.SyncDb();
-                }
-            }
-            catch (Exception ex)
-            {
-                LogUI.Error("同步数据库结构异常", ex).Wait(5000);
-            }
-        }
-        private void StartWebApi()
-        {
-            try
-            {
-
-                Startup.Start(SysConfig.Config.HttpPort, $"{Process.GetCurrentProcess().ProcessName}.Xml", "DW5S.Dto.xml", SysConfig.Config.TimeZoneUTC);
-            }
-            catch (System.Reflection.TargetInvocationException ex)
-            {
-                DW5S.Framework.LogHelper.Error($"启动Http服务失败!", ex);
-                if (ex.InnerException is HttpListenerException)
-                {
-                    DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败,{ex.InnerException.Message}");
-                }
-                else
-                {
-                    DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
-                }
-            }
-            catch (Exception ex)
-            {
-                DW5S.Framework.LogHelper.Error($"启动Http服务失败!", ex);
-                DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
-            }
-        }
         //自动导入Tle
         private async Task XlScan()
         {
             while (true)
             {
                 await Task.Delay(10000);
-                if (SysConfig.Config == null
-                    || string.IsNullOrWhiteSpace(SysConfig.Config.XLDirectory)
-                    || !Directory.Exists(SysConfig.Config.XLDirectory))
+                var repsSys = unitOfWork.Reps<SysSetings>();
+                var settings = await repsSys.FirstOrDefaultAsync();
+                if (settings == null
+                    || string.IsNullOrWhiteSpace(settings.XLDirectory)
+                    || !Directory.Exists(settings.XLDirectory))
                 {
                     continue;
                 }
-                DirectoryInfo dir = new DirectoryInfo(SysConfig.Config.XLDirectory);
-                var backupSucceed = Path.Combine(dir.FullName, "Backup","Succeed");
+                DirectoryInfo dir = new DirectoryInfo(settings.XLDirectory);
+                var backupSucceed = Path.Combine(dir.FullName, "Backup", "Succeed");
                 var backupFailed = Path.Combine(dir.FullName, "Backup", "Failed");
                 Directory.CreateDirectory(backupSucceed);
                 Directory.CreateDirectory(backupFailed);
 
-                var files = Directory.EnumerateFiles(SysConfig.Config.XLDirectory, "*", SearchOption.TopDirectoryOnly);
+                var files = Directory.EnumerateFiles(settings.XLDirectory, "*", SearchOption.TopDirectoryOnly);
                 foreach (string file in files)
                 {
                     bool succeed = false;
@@ -234,7 +153,7 @@ namespace DW5S
                         var res = await HttpHelper.PostRequestAsync<RecordRes>(SysConfig.GetUrl("Xl/ImportTleAsync"), dto);
                         if (res.code == 200)
                         {
-                            LogUI.Info($"星历文件[{file}]自动导入成功!").Wait(5000);
+                            logger.LogInformation($"星历文件[{file}]自动导入成功!");
                             try
                             {
                                 //导入完成的文件放在备份目录
@@ -244,12 +163,12 @@ namespace DW5S
                                 File.Move(file, newFile);
                             }
                             catch
-                            { 
+                            {
                             }
                         }
                         else
                         {
-                            LogUI.Error($"星历文件[{file}]自动导入失败.{res.msg}").Wait(5000);
+                            logger.LogError($"星历文件[{file}]自动导入失败.{res.msg}");
                             try
                             {
                                 //导入完成的文件放在备份目录
@@ -265,7 +184,7 @@ namespace DW5S
                     }
                     catch (Exception ex)
                     {
-                        LogUI.Error($"星历文件[{file}]自动导入失败", ex).Wait(5000);
+                        logger.LogError(ex, $"星历文件[{file}]自动导入失败");
                         try
                         {
                             //导入完成的文件放在备份目录
@@ -294,21 +213,11 @@ namespace DW5S
             {
                 try
                 {
-                    List<XlInfo> clearData = new List<XlInfo>();
-                    using (RHDWContext db = new RHDWContext())
-                    {
-                        DateTime dt = DateTime.Now.AddYears(-1);
-                        clearData = await db.XlInfos.Where(p => p.UpdateTime < dt).Take(1000).ToListAsync();
-                        if (clearData.Any())
-                        {
-                            db.XlInfos.RemoveRange(clearData);
-                            await db.SaveChangesAsync();
-                        }
-                    }
-                    if (!clearData.Any())
-                    {
-                        await Task.Delay(60 * 1000);
-                    }
+                    var repsXl = unitOfWork.Reps<XlInfo>();
+                    DateTime dt = DateTime.Now.AddYears(-1);
+                    await repsXl.DeleteAsync(p => p.UpdateTime < dt);
+                    await unitOfWork.SaveAsync();
+                    await Task.Delay(60 * 1000);
 
                 }
                 catch (Exception ex)

+ 1 - 1
DW5S.App/Program.cs

@@ -8,7 +8,7 @@ using System.IO;
 using System.Reflection;
 using System.Security.Principal;
 using System.Windows.Forms;
-using DW5S.Framework;
+
 using DW5S.UI.Lib;
 using DW5S.WebApi;
 

+ 1 - 1
DW5S.App/UserControl/CtrlCgRes.cs

@@ -23,7 +23,7 @@ using System.Windows.Documents;
 using System.Windows.Forms;
 using DW5S.App.Model;
 using DW5S.Entity;
-using DW5S.Framework;
+
 using DW5S.Repostory;
 
 namespace DW5S.App.UserControl

+ 1 - 1
DW5S.App/UserControl/CtrlSysSettings.cs

@@ -26,7 +26,7 @@ using System.Security.Policy;
 using DevExpress.Utils.About;
 using DW5S.DTO;
 using System.Configuration;
-using DW5S.Framework;
+
 
 namespace DW5S.App.UserControl
 {

+ 1 - 1
DW5S.App/UserControl/RHGDOPParam.cs

@@ -13,7 +13,7 @@ using DevExpress.Utils.About;
 using DevExpress.XtraEditors.DXErrorProvider;
 using DW5S.DTO;
 using System.Configuration;
-using DW5S.Framework;
+
 
 namespace DW5S.App.UserControl
 {

+ 1 - 1
DW5S.App/UserControl/X1D1GDOPParam.cs

@@ -12,7 +12,7 @@ using DevExpress.XtraCharts.Design;
 using System.Runtime.ConstrainedExecution;
 using DW5S.DTO;
 using System.Configuration;
-using DW5S.Framework;
+
 
 namespace DW5S.App.UserControl
 {

+ 1 - 1
DW5S.App/UserControl/X2D1GDOPParam.cs

@@ -13,7 +13,7 @@ using DevExpress.Utils.About;
 using DevExpress.XtraEditors.DXErrorProvider;
 using DW5S.DTO;
 using System.Configuration;
-using DW5S.Framework;
+
 
 namespace DW5S.App.UserControl
 {

+ 1 - 1
DW5S.App/UserControl/X2DFGDOPParam.cs

@@ -12,7 +12,7 @@ using System.Linq;
 using DW5S.KxcApi;
 using DW5S.DTO;
 using DW5S.Entity;
-using DW5S.Framework;
+
 using DW5S.Repostory;
 namespace DW5S.App.UserControl
 {

+ 1 - 1
DW5S.App/UserControl/X3DFGDOPParam .cs

@@ -10,7 +10,7 @@ using System.Linq;
 using DW5S.KxcApi;
 using DW5S.DTO;
 using DW5S.Entity;
-using DW5S.Framework;
+
 using DW5S.Repostory;
 
 namespace DW5S.App.UserControl

+ 1 - 1
DW5S.App/UserControl/X3GDOPParam.cs

@@ -12,7 +12,7 @@ using System.Drawing;
 using DevExpress.XtraEditors.DXErrorProvider;
 using DW5S.DTO;
 using System.Configuration;
-using DW5S.Framework;
+
 namespace DW5S.App.UserControl
 {
     public partial class X3GDOPParam : DevExpress.XtraEditors.XtraUserControl

+ 34 - 31
DW5S.Repostory/Repository/IRepository.cs

@@ -19,7 +19,7 @@ namespace DW5S.Repostory
         Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate);
         Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector);
 
-        Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate);
+        Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null);
         Task<T> AddAsync(T entity);
         Task AddRangeAsync(IEnumerable<T> entitis);
 
@@ -27,11 +27,7 @@ namespace DW5S.Repostory
         void Update(T entity);
         void Delete(T entity);
 
-        /// <summary>
-        /// 根据ID删除
-        /// </summary>
-        /// <param name="ids">ids可以是int、long、IEnumerable等类型</param>
-        void DeleteById(object ids);
+        Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null);
     }
 
     public class Repository<T> : IRepository<T> where T : class
@@ -61,9 +57,11 @@ namespace DW5S.Repostory
         {
             return await dbSet.Where(predicate).ToListAsync();
         }
-        public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate)
+        public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null)
         {
-            return await dbSet.Where(predicate).FirstOrDefaultAsync();
+            if (predicate != null)
+                return await dbSet.Where(predicate).FirstOrDefaultAsync();
+            return await dbSet.FirstOrDefaultAsync();
         }
 
         public async Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector)
@@ -89,30 +87,35 @@ namespace DW5S.Repostory
         {
             dbSet.Remove(entity);
         }
-        /// <summary>
-        /// 高性能的删除
-        /// </summary>
-        /// <param name="id">ids可以是int、long、IEnumerable<int>、IEnumerable<long>等类型</param>
-        public async void DeleteById(object id)
+        ///// <summary>
+        ///// 高性能的删除
+        ///// </summary>
+        ///// <param name="id">ids可以是int、long、IEnumerable<int>、IEnumerable<long>等类型</param>
+        //public async void DeleteById(object id)
+        //{
+        //    if (id == null) return;
+        //    string table = typeof(T).Name;
+        //    if (id is IEnumerable<long> ids1)
+        //    {
+        //        //如果ID是连续的,这里可以使用大于小于等条件进一步优化
+        //        string sql = $"delete from {table} where id in ({string.Join(",", ids1)})";
+        //        await ctx.ExecuteSqlAsync(sql);
+        //    }
+        //    else if (id is IEnumerable<int> ids2)
+        //    {
+        //        string sql = $"delete from {table} where id in ({string.Join(",", ids2)})";
+        //        await ctx.ExecuteSqlAsync(sql);
+        //    }
+        //    else
+        //    {
+        //        string sql = $"delete from {table} where id={id}";
+        //        await ctx.ExecuteSqlAsync(sql);
+        //    }
+        //}
+
+        public async Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null)
         {
-            if (id == null) return;
-            string table = typeof(T).Name;
-            if (id is IEnumerable<long> ids1)
-            {
-                //如果ID是连续的,这里可以使用大于小于等条件进一步优化
-                string sql = $"delete from {table} where id in ({string.Join(",", ids1)})";
-                await ctx.ExecuteSqlAsync(sql);
-            }
-            else if (id is IEnumerable<int> ids2)
-            {
-                string sql = $"delete from {table} where id in ({string.Join(",", ids2)})";
-                await ctx.ExecuteSqlAsync(sql);
-            }
-            else
-            {
-                string sql = $"delete from {table} where id={id}";
-                await ctx.ExecuteSqlAsync(sql);
-            }
+           return await dbSet.Where(predicate).ExecuteDeleteAsync();
         }
     }