CtrlSysSettings.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. using System.Security.Policy;
  26. using DevExpress.Utils.About;
  27. using XdCxRhDW.Dto;
  28. using System.Configuration;
  29. using XdCxRhDW.Framework;
  30. namespace XdCxRhDW.App.UserControl
  31. {
  32. public partial class CtrlSysSettings : DevExpress.XtraEditors.XtraUserControl
  33. {
  34. public CtrlSysSettings()
  35. {
  36. InitializeComponent();
  37. this.layoutControl1.UseDefault();
  38. }
  39. private void CtrlSysSettings_Load(object sender, EventArgs e)
  40. {
  41. try
  42. {
  43. txtWmtsSource.Properties.Items.AddEnum<EnumWmtsSource>();
  44. txtLayer.Items.AddEnum<EnumMapLayerType>();
  45. txtLayer.Items.RemoveAt(0);
  46. txtWmtsSource.SelectedIndex = 0;
  47. txtLayer.SelectedIndex = 0;
  48. if (SysConfig.Config != null)
  49. {
  50. this.txtHttpPort.Text = SysConfig.Config.HttpPort.ToString();
  51. this.txtTimeZone.TimeZoneId = SysConfig.Config.TimeZoneID;
  52. if (!string.IsNullOrEmpty(SysConfig.Config.XLDirectory))
  53. this.txtXLDirectory.Text = SysConfig.Config.XLDirectory.ToString();
  54. if (SysConfig.Config.YDPZThreshold > 0)
  55. this.txtYDPZThreshold.Text = SysConfig.Config.YDPZThreshold.ToString();
  56. if (this.txtMapType.SelectedIndex == SysConfig.Config.MapType)
  57. txtMapType_SelectedIndexChanged(null, null);
  58. else
  59. this.txtMapType.SelectedIndex = SysConfig.Config.MapType;
  60. if (txtMapType.SelectedIndex != 0)
  61. {
  62. this.txtWmtsSource.EditValue = SysConfig.Config.WmtsSource;
  63. for (int i = 0; i < txtLayer.Items.Count; i++)
  64. {
  65. if (SysConfig.Config.MapLayerType.HasFlag(EnumMapLayerType.SatMap))
  66. {
  67. this.txtLayer.Items[EnumMapLayerType.SatMap].CheckState = CheckState.Checked;
  68. }
  69. if (SysConfig.Config.MapLayerType.HasFlag(EnumMapLayerType.RoadMap))
  70. {
  71. this.txtLayer.Items[EnumMapLayerType.RoadMap].CheckState = CheckState.Checked;
  72. }
  73. if (SysConfig.Config.MapLayerType.HasFlag(EnumMapLayerType.XZQH_Map))
  74. {
  75. this.txtLayer.Items[EnumMapLayerType.XZQH_Map].CheckState = CheckState.Checked;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. Serilog.Log.Error(ex, "加载配置信息异常");
  84. DxHelper.MsgBoxHelper.ShowError("加载配置信息异常");
  85. }
  86. layoutControl1.BestFit();
  87. }
  88. private async void btnSave_Click(object sender, EventArgs e)
  89. {
  90. try
  91. {
  92. if (!int.TryParse(txtHttpPort.Text, out int port))
  93. {
  94. DxHelper.MsgBoxHelper.ShowError("Http端口错误!");
  95. return;
  96. }
  97. if (string.IsNullOrWhiteSpace(txtTimeZone.TimeZoneId))
  98. {
  99. DxHelper.MsgBoxHelper.ShowError("系统时区错误!");
  100. return;
  101. }
  102. using (RHDWContext db = new RHDWContext())
  103. {
  104. var res = await db.SysSetings.FirstOrDefaultAsync();
  105. if (res == null) res = new SysSetings();
  106. bool needStartHttpSvr = res.HttpPort != port;
  107. if (!needStartHttpSvr)
  108. {
  109. needStartHttpSvr = res.TimeZoneID != txtTimeZone.TimeZoneId;
  110. }
  111. res.TimeZoneID = txtTimeZone.TimeZoneId;
  112. TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(txtTimeZone.TimeZoneId);
  113. if (tz.DisplayName == "UTC")
  114. {
  115. res.TimeZoneUTC = tz.DisplayName;
  116. res.TimeZoneName = "";
  117. }
  118. else
  119. {
  120. res.TimeZoneUTC = tz.DisplayName.Substring(1, tz.DisplayName.IndexOf(")") - 1).Trim();
  121. res.TimeZoneName = tz.DisplayName.Substring(tz.DisplayName.IndexOf(")") + 1).Trim();
  122. }
  123. res.ZoneHours = tz.BaseUtcOffset.TotalHours;
  124. res.HttpPort = port;
  125. res.XLDirectory = txtXLDirectory.Text;
  126. res.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text);
  127. bool mapChanged = res.MapType != txtMapType.SelectedIndex;
  128. res.MapType = txtMapType.SelectedIndex;
  129. if (txtMapType.SelectedIndex != 0)
  130. {
  131. var wmtsSource= (EnumWmtsSource)txtWmtsSource.EditValue;
  132. if (!mapChanged)
  133. {
  134. mapChanged = res.WmtsSource != wmtsSource;
  135. }
  136. res.WmtsSource = wmtsSource;
  137. EnumMapLayerType layerType = EnumMapLayerType.None;
  138. for (int i = 0; i < txtLayer.Items.Count; i++)
  139. {
  140. if (txtLayer.GetItemChecked(i))
  141. {
  142. layerType |= (EnumMapLayerType)txtLayer.Items[i].Value;
  143. }
  144. }
  145. layerType = layerType & ~EnumMapLayerType.None;//移除
  146. if (!mapChanged)
  147. mapChanged = res.MapLayerType != layerType;
  148. res.MapLayerType = layerType;
  149. }
  150. db.SysSetings.AddOrUpdate(res);
  151. await db.SaveChangesAsync();
  152. SysConfig.Config = res;
  153. if (mapChanged)
  154. Messenger.Defalut.Pub("地图类型改变", res);
  155. Messenger.Defalut.Pub("时区改变", res);
  156. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  157. if (needStartHttpSvr)
  158. {
  159. Application.Restart();
  160. }
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. Serilog.Log.Error(ex, "配置信息保存异常");
  166. DxHelper.MsgBoxHelper.ShowError("配置信息保存异常");
  167. }
  168. if (this.Parent is Form frm)
  169. frm.Close();
  170. }
  171. private void txtMapType_SelectedIndexChanged(object sender, EventArgs e)
  172. {
  173. if (txtMapType.SelectedIndex == 0)
  174. {
  175. itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  176. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  177. }
  178. else
  179. {
  180. itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  181. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  182. }
  183. layoutControl1.BestFit();
  184. }
  185. private void toolTipController1_GetActiveObjectInfo(object sender, ToolTipControllerGetActiveObjectInfoEventArgs e)
  186. {
  187. RadioGroup rg = e.SelectedControl as RadioGroup;
  188. if (rg == null) return;
  189. for (int i = 0; i < rg.Properties.Items.Count; i++)
  190. {
  191. Rectangle rect = rg.GetItemRectangle(i);
  192. if (rect.Contains(e.ControlMousePosition))
  193. {
  194. var val = (EnumWmtsSource)rg.Properties.Items[i].Value;
  195. if (val == EnumWmtsSource.ZCJ)
  196. {
  197. var str =AppConfigHelper.Get("ZCJ_URL","");
  198. e.Info = new ToolTipControlInfo(i, $"{rg.Properties.Items[i].Description}:{str}");
  199. }
  200. else
  201. {
  202. var str = AppConfigHelper.Get("SJZX_URL","");
  203. e.Info = new ToolTipControlInfo(i, $"{rg.Properties.Items[i].Description}:{str}");
  204. }
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. }