CtrlSysSettings.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using DevExpress.XtraEditors;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Data.Entity;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Net.Sockets;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Documents;
  14. using System.Windows.Forms;
  15. using XdCxRhDW.Repostory;
  16. using System.IO;
  17. using System.Data.Entity.Migrations;
  18. using ExtensionsDev;
  19. using DevExpress.Utils;
  20. using DevExpress.XtraMap;
  21. using XdCxRhDW.WebApi;
  22. using DevExpress.XtraPrinting;
  23. using XdCxRhDW.Entity;
  24. using DevExpress.XtraEditors.Controls;
  25. namespace XdCxRhDW.App.UserControl
  26. {
  27. public partial class CtrlSysSettings : DevExpress.XtraEditors.XtraUserControl
  28. {
  29. public CtrlSysSettings()
  30. {
  31. InitializeComponent();
  32. //this.layoutControl1.UseDefault();
  33. }
  34. private async void CtrlSysSettings_Load(object sender, EventArgs e)
  35. {
  36. try
  37. {
  38. txtWMSType.Properties.Items.Add(new ImageComboBoxItem("请选择", null, -1));
  39. txtWMSType.Properties.Items.AddEnum<EnumWmsType>();
  40. using (RHDWContext db = new RHDWContext())
  41. {
  42. var res = await db.SysSetings.FirstOrDefaultAsync();
  43. if (res != null)
  44. {
  45. this.txtHttpPort.Text = res.HttpPort.ToString();
  46. if (!string.IsNullOrEmpty(res.XLDirectory))
  47. this.txtXLDirectory.Text = res.XLDirectory.ToString();
  48. if (res.WMSType != null)
  49. this.txtWMSType.EditValue = res.WMSType;
  50. if (res.YDPZThreshold > 0)
  51. this.txtYDPZThreshold.Text = res.YDPZThreshold.ToString();
  52. this.txtLayer.Text = res.LayerName;
  53. if (this.txtMapType.SelectedIndex == res.MapType)
  54. {
  55. txtMapType_SelectedIndexChanged(null, null);
  56. }
  57. else
  58. this.txtMapType.SelectedIndex = res.MapType;
  59. }
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. Serilog.Log.Error(ex, "加载配置信息异常");
  65. DxHelper.MsgBoxHelper.ShowError("加载配置信息异常");
  66. }
  67. layoutControl1.BestFit();
  68. }
  69. private async void btnSave_Click(object sender, EventArgs e)
  70. {
  71. bool needStartHttpSvr = false;
  72. try
  73. {
  74. if (txtMapType.SelectedIndex != 0 && txtWMSType.EditValue == null)
  75. {
  76. DxHelper.MsgBoxHelper.ShowInfo("请选择行政图类型!");
  77. return;
  78. }
  79. if (txtMapType.SelectedIndex != 0 && string.IsNullOrEmpty(txtLayer.Text))
  80. {
  81. DxHelper.MsgBoxHelper.ShowInfo("请填写地图图层名称!");
  82. return;
  83. }
  84. using (RHDWContext db = new RHDWContext())
  85. {
  86. var res = await db.SysSetings.FirstOrDefaultAsync();
  87. if (res == null)
  88. {
  89. SysSetings resNull = new SysSetings();
  90. resNull.HttpPort = Convert.ToInt32(txtHttpPort.Text);
  91. resNull.XLDirectory = txtXLDirectory.Text;
  92. resNull.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text);
  93. resNull.MapType = txtMapType.SelectedIndex;
  94. if (txtMapType.SelectedIndex == 0)
  95. {
  96. resNull.WMSType = null;
  97. resNull.LayerName = null;
  98. }
  99. else
  100. {
  101. resNull.WMSType = (EnumWmsType)txtWMSType.EditValue;
  102. resNull.LayerName = txtLayer.Text;
  103. }
  104. db.SysSetings.Add(resNull);
  105. needStartHttpSvr = true;
  106. }
  107. else
  108. {
  109. needStartHttpSvr = res.HttpPort != Convert.ToInt32(txtHttpPort.Text);
  110. res.HttpPort = Convert.ToInt32(txtHttpPort.Text);
  111. res.XLDirectory = txtXLDirectory.Text;
  112. res.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text);
  113. res.MapType = txtMapType.SelectedIndex;
  114. if (txtMapType.SelectedIndex == 0)
  115. {
  116. res.WMSType = null;
  117. res.LayerName = null;
  118. }
  119. else
  120. {
  121. res.WMSType = (EnumWmsType)txtWMSType.EditValue;
  122. res.LayerName = txtLayer.Text;
  123. }
  124. }
  125. await db.SaveChangesAsync();
  126. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  127. Messenger.Defalut.Pub("系统配置改变", await db.SysSetings.FirstOrDefaultAsync());
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. Serilog.Log.Error(ex, "配置信息保存异常");
  133. DxHelper.MsgBoxHelper.ShowError("配置信息保存异常");
  134. }
  135. if (needStartHttpSvr)
  136. {
  137. try
  138. {
  139. using (RHDWContext db = new RHDWContext())
  140. {
  141. var settings = await db.SysSetings.FirstOrDefaultAsync();
  142. Startup.Start(settings.HttpPort, "多模式融合定位平台.Xml", "XdCxRhDW.Dto.xml");
  143. }
  144. }
  145. catch (System.Reflection.TargetInvocationException ex)
  146. {
  147. Serilog.Log.Error(ex, $"启动Http服务失败!");
  148. if (ex.InnerException is HttpListenerException)
  149. {
  150. DxHelper.MsgBoxHelper.ShowWarning($"{ex.InnerException.Message}");
  151. }
  152. else
  153. {
  154. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. Serilog.Log.Error(ex, $"启动Http服务失败!");
  160. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
  161. }
  162. }
  163. }
  164. private void txtMapType_SelectedIndexChanged(object sender, EventArgs e)
  165. {
  166. if (txtMapType.SelectedIndex == 0)
  167. {
  168. itemUrl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  169. itemLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  170. }
  171. else
  172. {
  173. itemUrl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  174. itemUrl.Text = "WMS地址";
  175. itemLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  176. }
  177. layoutControl1.BestFit();
  178. }
  179. }
  180. }