CtrlSysSettings.cs 9.1 KB

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