using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Drawing; using System.Linq; using System.Net.Sockets; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Documents; using System.Windows.Forms; using XdCxRhDW.Repostory; using System.IO; using System.Data.Entity.Migrations; using ExtensionsDev; using DevExpress.Utils; using DevExpress.XtraMap; using XdCxRhDW.WebApi; using DevExpress.XtraPrinting; using XdCxRhDW.Entity; using DevExpress.XtraEditors.Controls; namespace XdCxRhDW.App.UserControl { public partial class CtrlSysSettings : DevExpress.XtraEditors.XtraUserControl { public CtrlSysSettings() { InitializeComponent(); //this.layoutControl1.UseDefault(); } private async void CtrlSysSettings_Load(object sender, EventArgs e) { try { txtWMSType.Properties.Items.Add(new ImageComboBoxItem("请选择", null, -1)); txtWMSType.Properties.Items.AddEnum(); using (RHDWContext db = new RHDWContext()) { var res = await db.SysSetings.FirstOrDefaultAsync(); if (res != null) { this.txtHttpPort.Text = res.HttpPort.ToString(); if (!string.IsNullOrEmpty(res.XLDirectory)) this.txtXLDirectory.Text = res.XLDirectory.ToString(); if (res.WMSType != null) this.txtWMSType.EditValue = res.WMSType; if (res.YDPZThreshold > 0) this.txtYDPZThreshold.Text = res.YDPZThreshold.ToString(); this.txtLayer.Text = res.LayerName; if (this.txtMapType.SelectedIndex == res.MapType) { txtMapType_SelectedIndexChanged(null, null); } else this.txtMapType.SelectedIndex = res.MapType; } } } catch (Exception ex) { Serilog.Log.Error(ex, "加载配置信息异常"); DxHelper.MsgBoxHelper.ShowError("加载配置信息异常"); } layoutControl1.BestFit(); } private async void btnSave_Click(object sender, EventArgs e) { bool needStartHttpSvr = false; try { if (txtMapType.SelectedIndex != 0 && txtWMSType.EditValue == null) { DxHelper.MsgBoxHelper.ShowInfo("请选择行政图类型!"); return; } if (txtMapType.SelectedIndex != 0 && string.IsNullOrEmpty(txtLayer.Text)) { DxHelper.MsgBoxHelper.ShowInfo("请填写地图图层名称!"); return; } using (RHDWContext db = new RHDWContext()) { var res = await db.SysSetings.FirstOrDefaultAsync(); if (res == null) { SysSetings resNull = new SysSetings(); resNull.HttpPort = Convert.ToInt32(txtHttpPort.Text); resNull.XLDirectory = txtXLDirectory.Text; resNull.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text); resNull.MapType = txtMapType.SelectedIndex; if (txtMapType.SelectedIndex == 0) { resNull.WMSType = null; resNull.LayerName = null; } else { resNull.WMSType = (EnumWmsType)txtWMSType.EditValue; resNull.LayerName = txtLayer.Text; } db.SysSetings.Add(resNull); needStartHttpSvr = true; } else { needStartHttpSvr = res.HttpPort != Convert.ToInt32(txtHttpPort.Text); res.HttpPort = Convert.ToInt32(txtHttpPort.Text); res.XLDirectory = txtXLDirectory.Text; res.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text); res.MapType = txtMapType.SelectedIndex; if (txtMapType.SelectedIndex == 0) { res.WMSType = null; res.LayerName = null; } else { res.WMSType = (EnumWmsType)txtWMSType.EditValue; res.LayerName = txtLayer.Text; } } await db.SaveChangesAsync(); DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!"); Messenger.Defalut.Pub("系统配置改变", await db.SysSetings.FirstOrDefaultAsync()); } } catch (Exception ex) { Serilog.Log.Error(ex, "配置信息保存异常"); DxHelper.MsgBoxHelper.ShowError("配置信息保存异常"); } if (needStartHttpSvr) { try { using (RHDWContext db = new RHDWContext()) { var settings = await db.SysSetings.FirstOrDefaultAsync(); Startup.Start(settings.HttpPort, "多模式融合定位平台.Xml", "XdCxRhDW.Dto.xml"); } } catch (System.Reflection.TargetInvocationException ex) { Serilog.Log.Error(ex, $"启动Http服务失败!"); if (ex.InnerException is HttpListenerException) { DxHelper.MsgBoxHelper.ShowWarning($"{ex.InnerException.Message}"); } else { DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } } catch (Exception ex) { Serilog.Log.Error(ex, $"启动Http服务失败!"); DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } } } private void txtMapType_SelectedIndexChanged(object sender, EventArgs e) { if (txtMapType.SelectedIndex == 0) { itemUrl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; itemLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; } else { itemUrl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always; itemUrl.Text = "WMS地址"; itemLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always; } layoutControl1.BestFit(); } } }