CtrlSysSettings.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.ZKXT_Url = null;
  96. // resNull.ZKXT_LayerName = null;
  97. // resNull.SJZX_Url = null;
  98. // resNull.SJZX_LayerName = null;
  99. // resNull.LW_Url = null;
  100. // resNull.LW_LayerName = null;
  101. //}
  102. if (txtMapType.SelectedIndex != 0)
  103. {
  104. resNull.WMSType = (EnumWmsType)txtWMSType.EditValue;
  105. if (resNull.WMSType == EnumWmsType.ZKXT)
  106. {
  107. resNull.ZKXT_Url = txtUrl.Text;
  108. resNull.ZKXT_LayerName = txtLayer.Text;
  109. }
  110. else if (resNull.WMSType == EnumWmsType.SJZX)
  111. {
  112. resNull.SJZX_Url = txtUrl.Text;
  113. resNull.SJZX_LayerName = txtLayer.Text;
  114. }
  115. else if (resNull.WMSType == EnumWmsType.LW)
  116. {
  117. resNull.LW_Url = txtUrl.Text;
  118. resNull.LW_LayerName = txtLayer.Text;
  119. }
  120. }
  121. db.SysSetings.Add(resNull);
  122. needStartHttpSvr = true;
  123. }
  124. else
  125. {
  126. needStartHttpSvr = res.HttpPort != Convert.ToInt32(txtHttpPort.Text);
  127. res.HttpPort = Convert.ToInt32(txtHttpPort.Text);
  128. res.XLDirectory = txtXLDirectory.Text;
  129. res.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text);
  130. res.MapType = txtMapType.SelectedIndex;
  131. //if (txtMapType.SelectedIndex == 0)
  132. //{
  133. // res.WMSType = EnumWmsType.LW;
  134. // res.LayerName = null;
  135. //}
  136. if (txtMapType.SelectedIndex != 0)
  137. {
  138. res.WMSType = (EnumWmsType)txtWMSType.EditValue;
  139. if (res.WMSType == EnumWmsType.ZKXT)
  140. {
  141. res.ZKXT_Url = txtUrl.Text;
  142. res.ZKXT_LayerName = txtLayer.Text;
  143. }
  144. else if (res.WMSType == EnumWmsType.SJZX)
  145. {
  146. res.SJZX_Url = txtUrl.Text;
  147. res.SJZX_LayerName = txtLayer.Text;
  148. }
  149. else if (res.WMSType == EnumWmsType.LW)
  150. {
  151. res.LW_Url = txtUrl.Text;
  152. res.LW_LayerName = txtLayer.Text;
  153. }
  154. }
  155. }
  156. await db.SaveChangesAsync();
  157. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  158. Messenger.Defalut.Pub("系统配置改变", await db.SysSetings.FirstOrDefaultAsync());
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. Serilog.Log.Error(ex, "配置信息保存异常");
  164. DxHelper.MsgBoxHelper.ShowError("配置信息保存异常");
  165. }
  166. if (needStartHttpSvr)
  167. {
  168. try
  169. {
  170. using (RHDWContext db = new RHDWContext())
  171. {
  172. var settings = await db.SysSetings.FirstOrDefaultAsync();
  173. Startup.Start(settings.HttpPort, "多模式融合定位平台.Xml", "XdCxRhDW.Dto.xml");
  174. }
  175. }
  176. catch (System.Reflection.TargetInvocationException ex)
  177. {
  178. Serilog.Log.Error(ex, $"启动Http服务失败!");
  179. if (ex.InnerException is HttpListenerException)
  180. {
  181. DxHelper.MsgBoxHelper.ShowWarning($"{ex.InnerException.Message}");
  182. }
  183. else
  184. {
  185. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. Serilog.Log.Error(ex, $"启动Http服务失败!");
  191. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
  192. }
  193. }
  194. }
  195. private void txtMapType_SelectedIndexChanged(object sender, EventArgs e)
  196. {
  197. if (txtMapType.SelectedIndex == 0)
  198. {
  199. itemMapSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  200. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  201. itemMapUrl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  202. }
  203. else
  204. {
  205. itemMapSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  206. itemMapSource.Text = "WMS地址";
  207. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  208. itemMapUrl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  209. }
  210. layoutControl1.BestFit();
  211. }
  212. private async void txtWMSType_SelectedIndexChanged(object sender, EventArgs e)
  213. {
  214. using (RHDWContext db = new RHDWContext())
  215. {
  216. var res = await db.SysSetings.FirstOrDefaultAsync();
  217. if (res == null) return;
  218. if ((EnumWmsType)txtWMSType.EditValue == EnumWmsType.ZKXT)
  219. {
  220. txtUrl.Text = res.ZKXT_Url;
  221. txtLayer.Text = res.ZKXT_LayerName;
  222. }
  223. if ((EnumWmsType)txtWMSType.EditValue == EnumWmsType.SJZX)
  224. {
  225. txtUrl.Text = res.SJZX_Url;
  226. txtLayer.Text = res.SJZX_LayerName;
  227. }
  228. if ((EnumWmsType)txtWMSType.EditValue == EnumWmsType.LW)
  229. {
  230. txtUrl.Text = res.LW_Url;
  231. txtLayer.Text = res.LW_LayerName;
  232. }
  233. }
  234. }
  235. }
  236. }