CtrlSysSettings.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.AddEnum<EnumWmsType>();
  39. using (RHDWContext db = new RHDWContext())
  40. {
  41. var res = await db.SysSetings.FirstOrDefaultAsync();
  42. if (res != null)
  43. {
  44. this.txtHttpPort.Text = res.HttpPort.ToString();
  45. if (!string.IsNullOrEmpty(res.XLDirectory))
  46. this.txtXLDirectory.Text = res.XLDirectory.ToString();
  47. this.txtWMSType.EditValue = res.WMSType;
  48. if (res.YDPZThreshold > 0)
  49. this.txtYDPZThreshold.Text = res.YDPZThreshold.ToString();
  50. //this.txtLayer.Text = res.LayerName;
  51. if (this.txtMapType.SelectedIndex == res.MapType)
  52. {
  53. txtMapType_SelectedIndexChanged(null, null);
  54. }
  55. else
  56. this.txtMapType.SelectedIndex = res.MapType;
  57. }
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. Serilog.Log.Error(ex, "加载配置信息异常");
  63. DxHelper.MsgBoxHelper.ShowError("加载配置信息异常");
  64. }
  65. layoutControl1.BestFit();
  66. }
  67. private async void btnSave_Click(object sender, EventArgs e)
  68. {
  69. bool needStartHttpSvr = false;
  70. try
  71. {
  72. if (txtMapType.SelectedIndex != 0 && txtWMSType.EditValue == null)
  73. {
  74. DxHelper.MsgBoxHelper.ShowInfo("请选择行政图类型!");
  75. return;
  76. }
  77. if (txtMapType.SelectedIndex != 0 && string.IsNullOrEmpty(txtLayer.Text))
  78. {
  79. DxHelper.MsgBoxHelper.ShowInfo("请填写地图图层名称!");
  80. return;
  81. }
  82. using (RHDWContext db = new RHDWContext())
  83. {
  84. var res = await db.SysSetings.FirstOrDefaultAsync();
  85. if (res == null)
  86. {
  87. SysSetings resNull = new SysSetings();
  88. resNull.HttpPort = Convert.ToInt32(txtHttpPort.Text);
  89. resNull.XLDirectory = txtXLDirectory.Text;
  90. resNull.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text);
  91. resNull.MapType = txtMapType.SelectedIndex;
  92. //if (txtMapType.SelectedIndex == 0)
  93. //{
  94. // resNull.WMSType = null;
  95. // resNull.LayerName = null;
  96. //}
  97. //else
  98. //{
  99. // resNull.WMSType = (EnumWmsType)txtWMSType.EditValue;
  100. // resNull.LayerName = txtLayer.Text;
  101. //}
  102. db.SysSetings.Add(resNull);
  103. needStartHttpSvr = true;
  104. }
  105. else
  106. {
  107. needStartHttpSvr = res.HttpPort != Convert.ToInt32(txtHttpPort.Text);
  108. res.HttpPort = Convert.ToInt32(txtHttpPort.Text);
  109. res.XLDirectory = txtXLDirectory.Text;
  110. res.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text);
  111. res.MapType = txtMapType.SelectedIndex;
  112. //if (txtMapType.SelectedIndex == 0)
  113. //{
  114. // res.WMSType = null;
  115. // res.LayerName = null;
  116. //}
  117. //else
  118. //{
  119. // res.WMSType = (EnumWmsType)txtWMSType.EditValue;
  120. // res.LayerName = txtLayer.Text;
  121. //}
  122. }
  123. await db.SaveChangesAsync();
  124. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  125. Messenger.Defalut.Pub("系统配置改变", await db.SysSetings.FirstOrDefaultAsync());
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. Serilog.Log.Error(ex, "配置信息保存异常");
  131. DxHelper.MsgBoxHelper.ShowError("配置信息保存异常");
  132. }
  133. if (needStartHttpSvr)
  134. {
  135. try
  136. {
  137. using (RHDWContext db = new RHDWContext())
  138. {
  139. var settings = await db.SysSetings.FirstOrDefaultAsync();
  140. Startup.Start(settings.HttpPort, "多模式融合定位平台.Xml", "XdCxRhDW.Dto.xml");
  141. }
  142. }
  143. catch (System.Reflection.TargetInvocationException ex)
  144. {
  145. Serilog.Log.Error(ex, $"启动Http服务失败!");
  146. if (ex.InnerException is HttpListenerException)
  147. {
  148. DxHelper.MsgBoxHelper.ShowWarning($"{ex.InnerException.Message}");
  149. }
  150. else
  151. {
  152. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. Serilog.Log.Error(ex, $"启动Http服务失败!");
  158. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
  159. }
  160. }
  161. }
  162. private void txtMapType_SelectedIndexChanged(object sender, EventArgs e)
  163. {
  164. if (txtMapType.SelectedIndex == 0)
  165. {
  166. itemMapSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  167. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  168. }
  169. else
  170. {
  171. itemMapSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  172. itemMapSource.Text = "WMS地址";
  173. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  174. }
  175. layoutControl1.BestFit();
  176. }
  177. }
  178. }