瀏覽代碼

Merge branch 'master' of http://139.155.15.221:3000/gongqiuhong/DataSimulation

wyq 1 年之前
父節點
當前提交
0e65a8eaf2

+ 16 - 0
DataSimulation.Forms/DataSimulation.Forms.csproj

@@ -64,6 +64,9 @@
     <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
       <HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll</HintPath>
     </Reference>
+    <Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+      <HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.8.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
+    </Reference>
     <Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
       <HintPath>..\packages\Serilog.3.1.1\lib\net471\Serilog.dll</HintPath>
     </Reference>
@@ -116,7 +119,19 @@
     <Reference Include="System.Runtime.Serialization" />
     <Reference Include="System.Runtime.Serialization.Formatters.Soap" />
     <Reference Include="System.Security" />
+    <Reference Include="System.Text.Encodings.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+      <HintPath>..\packages\System.Text.Encodings.Web.8.0.0\lib\net462\System.Text.Encodings.Web.dll</HintPath>
+    </Reference>
+    <Reference Include="System.Text.Json, Version=8.0.0.3, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+      <HintPath>..\packages\System.Text.Json.8.0.3\lib\net462\System.Text.Json.dll</HintPath>
+    </Reference>
+    <Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+      <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
+    </Reference>
     <Reference Include="System.Transactions" />
+    <Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+      <HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
+    </Reference>
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />
@@ -132,6 +147,7 @@
     <Compile Include="DxHelper\EnumExtension.cs" />
     <Compile Include="DxHelper\MD5Helper.cs" />
     <Compile Include="DxHelper\MsgBoxHelper.cs" />
+    <Compile Include="DxHelper\ObjectExtends.cs" />
     <Compile Include="DxHelper\PopupHelper.cs" />
     <Compile Include="DxHelper\SvgHelper.cs" />
     <Compile Include="DxHelper\WaitHelper.cs" />

+ 169 - 0
DataSimulation.Forms/DxHelper/ObjectExtends.cs

@@ -0,0 +1,169 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.Encodings.Web;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+using System.Text.Unicode;
+using System.Threading.Tasks;
+
+public static class ObjectExtends
+{
+    private class DateTimeConverter : JsonConverter<DateTime>
+    {
+        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+        {
+            return DateTime.Parse(reader.GetString());
+        }
+
+        public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
+        {
+            writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
+        }
+    }
+
+    /// <summary>
+    /// 判断obj是否是数字
+    /// <para>bool也算数字</para>
+    /// <para>null返回false</para>
+    /// </summary>
+    /// <param name="o"></param>
+    /// <returns></returns>
+    public static bool IsNumericType(this Type t)
+    {
+        //常用类型放前面提高效率
+        if (t == null) return false;
+        if (t == typeof(string) || t == typeof(DateTime)) return false;
+        if (t == typeof(int)) return true;
+        if (t == typeof(double)) return true;
+        if (t == typeof(float)) return true;
+        if (t == typeof(long)) return true;
+        if (t == typeof(bool)) return true;
+        if (t == typeof(byte)) return true;
+
+        if (t == typeof(sbyte)) return true;
+        if (t == typeof(ushort)) return true;
+        if (t == typeof(uint)) return true;
+        if (t == typeof(ulong)) return true;
+        if (t == typeof(short)) return true;
+        if (t == typeof(decimal)) return true;
+        return false;
+    }
+
+    /// <summary>
+    /// 判断obj是否是非整数类型
+    /// <para>包含float、double、decimal</para>
+    /// <para>null返回false</para>
+    /// </summary>
+    /// <param name="o"></param>
+    /// <returns></returns>
+    public static bool IsFloatType(this Type t)
+    {
+        if (t == null) return false;
+        if (t == typeof(double)) return true;
+        if (t == typeof(float)) return true;
+        if (t == typeof(decimal)) return true;
+        return false;
+    }
+
+    /// <summary>
+    /// <para>将源对象的属性值映射到target对象上,(target==null时直接返回)</para>
+    /// <para>该方法使用反射</para>
+    /// <para>源对象和目标对象可以是不同对象,只要属性名称一致即可,如Model1映射到Model2</para>
+    /// </summary>
+    /// <typeparam name="T">target泛型类</typeparam>
+    /// <param name="obj">源对象</param>
+    /// <param name="target">目标对象</param>
+    public static void MapTo<T>(this object obj, T target)
+    {
+        if (obj == null || target == null) return;
+        var objProps = obj.GetType().GetProperties();
+        if (objProps == null || objProps.Length == 0) return;
+        var listObjProps = objProps.ToList();
+        //var str = JsonConvert.SerializeObject(obj);
+        //var foo = JsonConvert.DeserializeObject<T>(str);
+        var props = typeof(T).GetProperties();
+        foreach (var item in props)
+        {
+            if (!item.CanWrite) continue;
+            var find = listObjProps.Find(p => p.Name == item.Name);
+            if (find == null) continue;
+
+
+            var value = find.GetValue(obj);
+            if (value == null) continue;
+            ;
+            if (item.PropertyType != value.GetType() && !item.PropertyType.Name.Contains("Nullable"))
+            {
+                value = Convert.ChangeType(value, item.PropertyType);
+            }
+            item.SetValue(target, value);
+        }
+    }
+
+    /// <summary>
+    /// <para>将源对象的映射为一个新对象</para>
+    /// <para>该方法使用json序列化,对象嵌套深度不能超过8层</para>
+    /// <para>源对象和目标对象可以是不同对象,只要属性名称一致即可,如Model1映射到Model2</para>
+    /// </summary>
+    /// <typeparam name="T">返回对象泛型类型</typeparam>
+    /// <param name="obj">源对象</param>
+    /// <returns></returns>
+    public static T MapTo<T>(this object obj)
+    {
+        string str;
+        JsonSerializerOptions options = null;
+        if (obj == null)
+            str = "";
+        else if (obj.GetType() == typeof(string))
+            str = obj.ToString();
+        else if (obj.GetType() == typeof(StringBuilder))
+            str = ((StringBuilder)obj).ToString();
+        else
+        {
+            options = new JsonSerializerOptions();
+            options.WriteIndented = false;
+            options.IncludeFields = true;
+            options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
+            options.Converters.Add(new DateTimeConverter());//yyyy-MM-dd HH:mm:ss
+            options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
+            str = JsonSerializer.Serialize(obj, options);
+        }
+        if (options == null)
+        {
+            options = new JsonSerializerOptions();
+            options.WriteIndented = false;
+            options.IncludeFields = true;
+            options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
+            options.Converters.Add(new DateTimeConverter());//yyyy-MM-dd HH:mm:ss
+            options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
+        }
+        if (string.IsNullOrWhiteSpace(str)) return default;
+        var res = JsonSerializer.Deserialize<T>(str, options);
+        return res;
+    }
+
+    /// <summary>
+    /// <para>对象转JsonString</para>
+    /// <para>如果对象本身是String类型则直接返回</para>
+    /// </summary>
+    /// <param name="obj">对象</param>
+    /// <param name="formarting">是否开启格式化,格式化会多占用一些字节,默认不格式化</param>
+    /// <returns></returns>
+    public static string ToJsonStr(this object obj, bool formarting = false)
+    {
+        if (obj == null) return "";
+        if (obj.GetType() == typeof(string)) return obj as string;
+        if (obj.GetType() == typeof(StringBuilder)) return ((StringBuilder)obj).ToString();
+        var options = new JsonSerializerOptions();
+        options.WriteIndented = formarting;
+        options.IncludeFields = true;
+        options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
+        options.Converters.Add(new DateTimeConverter());//yyyy-MM-dd HH:mm:ss
+        //不加这句非ASCII字符会被序列化为Unicode编号,类似于\u8C03
+        options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
+        var res = JsonSerializer.Serialize(obj, options);
+        return res;
+    }
+}

+ 9 - 11
DataSimulation.Forms/EditForms/TaskEditor.Designer.cs

@@ -29,11 +29,6 @@
         private void InitializeComponent()
         {
             this.components = new System.ComponentModel.Container();
-            DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions2 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
-            DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject5 = new DevExpress.Utils.SerializableAppearanceObject();
-            DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject6 = new DevExpress.Utils.SerializableAppearanceObject();
-            DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject7 = new DevExpress.Utils.SerializableAppearanceObject();
-            DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject8 = new DevExpress.Utils.SerializableAppearanceObject();
             DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions3 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
             DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject9 = new DevExpress.Utils.SerializableAppearanceObject();
             DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject10 = new DevExpress.Utils.SerializableAppearanceObject();
@@ -44,6 +39,11 @@
             DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject14 = new DevExpress.Utils.SerializableAppearanceObject();
             DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject15 = new DevExpress.Utils.SerializableAppearanceObject();
             DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject16 = new DevExpress.Utils.SerializableAppearanceObject();
+            DevExpress.XtraEditors.Controls.EditorButtonImageOptions editorButtonImageOptions1 = new DevExpress.XtraEditors.Controls.EditorButtonImageOptions();
+            DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject1 = new DevExpress.Utils.SerializableAppearanceObject();
+            DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject2 = new DevExpress.Utils.SerializableAppearanceObject();
+            DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject3 = new DevExpress.Utils.SerializableAppearanceObject();
+            DevExpress.Utils.SerializableAppearanceObject serializableAppearanceObject4 = new DevExpress.Utils.SerializableAppearanceObject();
             this.layoutControl1 = new DevExpress.XtraLayout.LayoutControl();
             this.txtHj = new DevExpress.XtraEditors.SearchLookUpEdit();
             this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
@@ -152,18 +152,16 @@
             this.txtHj.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
             new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
             this.txtHj.Properties.PopupView = this.gridView1;
-            this.txtHj.Properties.EditValueChanged += new System.EventHandler(this.txtHj_Properties_EditValueChanged);
             this.txtHj.Size = new System.Drawing.Size(291, 20);
             this.txtHj.StyleController = this.layoutControl1;
             this.txtHj.TabIndex = 17;
+            this.txtHj.EditValueChanged += new System.EventHandler(this.txtHj_EditValueChanged);
             // 
             // gridView1
             // 
             this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
             this.gridView1.Name = "gridView1";
             this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
-            this.gridView1.OptionsSelection.MultiSelect = true;
-            this.gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
             this.gridView1.OptionsView.ShowGroupPanel = false;
             // 
             // mapControl
@@ -199,7 +197,7 @@
             this.txtSpeed.Location = new System.Drawing.Point(12, 467);
             this.txtSpeed.Name = "txtSpeed";
             this.txtSpeed.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
-            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "m/s", -1, false, true, false, editorButtonImageOptions2, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppearanceObject5, serializableAppearanceObject6, serializableAppearanceObject7, serializableAppearanceObject8, "", null, null, DevExpress.Utils.ToolTipAnchor.Default)});
+            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "m/s", -1, false, true, false, editorButtonImageOptions3, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppearanceObject9, serializableAppearanceObject10, serializableAppearanceObject11, serializableAppearanceObject12, "", null, null, DevExpress.Utils.ToolTipAnchor.Default)});
             this.txtSpeed.Properties.MaskSettings.Set("MaskManagerType", typeof(DevExpress.Data.Mask.NumericMaskManager));
             this.txtSpeed.Properties.MaskSettings.Set("mask", "f3");
             this.txtSpeed.Size = new System.Drawing.Size(291, 23);
@@ -211,7 +209,7 @@
             this.txtBand.Location = new System.Drawing.Point(12, 423);
             this.txtBand.Name = "txtBand";
             this.txtBand.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
-            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "MHz", -1, false, true, false, editorButtonImageOptions3, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppearanceObject9, serializableAppearanceObject10, serializableAppearanceObject11, serializableAppearanceObject12, "", null, null, DevExpress.Utils.ToolTipAnchor.Default)});
+            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "MHz", -1, false, true, false, editorButtonImageOptions4, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppearanceObject13, serializableAppearanceObject14, serializableAppearanceObject15, serializableAppearanceObject16, "", null, null, DevExpress.Utils.ToolTipAnchor.Default)});
             this.txtBand.Properties.MaskSettings.Set("MaskManagerType", typeof(DevExpress.Data.Mask.NumericMaskManager));
             this.txtBand.Properties.MaskSettings.Set("mask", "f3");
             this.txtBand.Size = new System.Drawing.Size(291, 23);
@@ -223,7 +221,7 @@
             this.txtFreqUp.Location = new System.Drawing.Point(12, 379);
             this.txtFreqUp.Name = "txtFreqUp";
             this.txtFreqUp.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
-            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "MHz", -1, false, true, false, editorButtonImageOptions4, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppearanceObject13, serializableAppearanceObject14, serializableAppearanceObject15, serializableAppearanceObject16, "", null, null, DevExpress.Utils.ToolTipAnchor.Default)});
+            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "MHz", -1, false, true, false, editorButtonImageOptions1, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppearanceObject1, serializableAppearanceObject2, serializableAppearanceObject3, serializableAppearanceObject4, "", null, null, DevExpress.Utils.ToolTipAnchor.Default)});
             this.txtFreqUp.Properties.MaskSettings.Set("MaskManagerType", typeof(DevExpress.Data.Mask.NumericMaskManager));
             this.txtFreqUp.Properties.MaskSettings.Set("mask", "f3");
             this.txtFreqUp.Size = new System.Drawing.Size(291, 23);

+ 13 - 7
DataSimulation.Forms/EditForms/TaskEditor.cs

@@ -38,8 +38,6 @@ namespace DataSimulation.Forms.EditForms
             }
             this.radioSimulationType.Properties.Items.AddRange(radioGroups.ToArray());
             this.radioSimulationType.SelectedIndex = 0;
-
-          
         }
 
         public TaskEditor(TaskInfo info)
@@ -78,11 +76,13 @@ namespace DataSimulation.Forms.EditForms
             });
 
             var hjList = await SimulationCache.GetAllAsync();
-            this.txtHj.UseDefault();
+            // this.txtHj.UseDefault();
             // this.txtHj.SetData(hjList, nameof(SimulationInfo.SimulationName));
-            txtHj.Properties.DataSource = hjList;
-            txtHj.Properties.ValueMember = nameof(SimulationInfo.ID);
-            txtHj.Properties.DisplayMember = nameof(SimulationInfo.SimulationName);
+            this.txtHj.Properties.DataSource = hjList;
+            this.txtHj.Properties.ValueMember = nameof(SimulationInfo);
+           // this.txtHj.Properties.KeyMember = nameof(SimulationInfo.CreateTime);
+            this.txtHj.Properties.DisplayMember = nameof(SimulationInfo.SimulationName);
+            this.txtHj.SetSearchGridLookUpEditMultiSelected<SimulationInfo>();
 
 
             using (SimulationContext db = new SimulationContext())
@@ -221,9 +221,15 @@ namespace DataSimulation.Forms.EditForms
             validation();
         }
 
-        private void txtHj_Properties_EditValueChanged(object sender, EventArgs e)
+        private void txtHj_EditValueChanged(object sender, EventArgs e)
         {
+            var selectedValue = txtHj.Properties.Tag.MapTo<List<SimulationInfo>>();
+            if (selectedValue != null) {
+                for (int i = 0; i < selectedValue.Count(); i++)
+                {
 
+                }
+            }
         }
     }
 }

+ 40 - 0
DataSimulation.Forms/ExtensionsDev/SearchLookUpEditExtension.cs

@@ -103,5 +103,45 @@ namespace ExtensionsDev
 
         }
 
+        public static SearchLookUpEdit SetSearchGridLookUpEditMultiSelected<T>(this SearchLookUpEdit ctrl,string nullText = "请选择")
+        {
+            var view = ctrl.Properties.PopupView as DevExpress.XtraGrid.Views.Grid.GridView;
+            view.OptionsSelection.MultiSelect = true;
+            view.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
+            view.OptionsSelection.CheckBoxSelectorColumnWidth = 45;
+            ctrl.EditValue = null;
+            ctrl.Properties.NullText = null;
+            ctrl.Properties.NullValuePrompt = nullText;
+            ctrl.Properties.AllowFocused = false;
+            ctrl.Properties.ShowNullValuePrompt = ShowNullValuePromptOptions.Default;
+            ctrl.Properties.ShowAddNewButton = false;
+            var values = new List<T>();
+            //var keys = new List<string>();
+            var texts = new List<string>();
+            ctrl.Properties.Tag = values;
+           // ctrl.Tag = keys;
+            ctrl.CloseUp += (s, e) =>
+            {
+                values.Clear();
+                var indexs = view.GetSelectedRows();
+                foreach ( var row in indexs )
+                {
+                    var value =view.GetRow(row).MapTo<T>();
+                    values.Add(value);
+                    var text = view.GetRowCellDisplayText(row, ctrl.Properties.DisplayMember);
+                    texts.Add(text);
+                    //var key = view.GetRowCellDisplayText(row, ctrl.Properties.KeyMember);
+                    //keys.Add(key);
+                }
+                ctrl.RefreshEditValue();
+            };
+            ctrl.Properties.CustomDisplayText += (s, e) =>
+            {
+                if (!ctrl.IsPopupOpen)
+                    e.DisplayText = string.Join(",", texts.ToArray());
+            };
+            return ctrl;
+        }
+
     }
 }

+ 5 - 0
DataSimulation.Forms/packages.config

@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
   <package id="EntityFramework" version="6.4.4" targetFramework="net472" />
+  <package id="Microsoft.Bcl.AsyncInterfaces" version="8.0.0" targetFramework="net472" />
   <package id="Serilog" version="3.1.1" targetFramework="net472" />
   <package id="Serilog.Sinks.Async" version="1.5.0" targetFramework="net472" />
   <package id="Serilog.Sinks.Console" version="5.0.1" targetFramework="net472" />
@@ -17,4 +18,8 @@
   <package id="System.Memory" version="4.5.5" targetFramework="net472" />
   <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
   <package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net472" />
+  <package id="System.Text.Encodings.Web" version="8.0.0" targetFramework="net472" />
+  <package id="System.Text.Json" version="8.0.3" targetFramework="net472" />
+  <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />
+  <package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
 </packages>