CtrlSysSettings.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. XdCxRhDW.Framework.LogHelper.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. bool needStartHttpSvr = false;
  103. using (MySqlContext db = new MySqlContext())
  104. {
  105. var res = await db.SysSetings.FirstOrDefaultAsync();
  106. if (res == null) res = new SysSetings();
  107. needStartHttpSvr = res.HttpPort != port;
  108. if (!needStartHttpSvr)
  109. {
  110. needStartHttpSvr = res.TimeZoneID != txtTimeZone.TimeZoneId;
  111. }
  112. res.TimeZoneID = txtTimeZone.TimeZoneId;
  113. TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(txtTimeZone.TimeZoneId);
  114. if (tz.DisplayName == "UTC")
  115. {
  116. res.TimeZoneUTC = tz.DisplayName;
  117. res.TimeZoneName = "";
  118. }
  119. else
  120. {
  121. res.TimeZoneUTC = tz.DisplayName.Substring(1, tz.DisplayName.IndexOf(")") - 1).Trim();
  122. res.TimeZoneName = tz.DisplayName.Substring(tz.DisplayName.IndexOf(")") + 1).Trim();
  123. }
  124. res.ZoneHours = tz.BaseUtcOffset.TotalHours;
  125. res.HttpPort = port;
  126. res.XLDirectory = txtXLDirectory.Text;
  127. res.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text);
  128. bool mapChanged = res.MapType != txtMapType.SelectedIndex;
  129. res.MapType = txtMapType.SelectedIndex;
  130. if (txtMapType.SelectedIndex != 0)
  131. {
  132. var wmtsSource = (EnumWmtsSource)txtWmtsSource.EditValue;
  133. if (!mapChanged)
  134. {
  135. mapChanged = res.WmtsSource != wmtsSource;
  136. }
  137. res.WmtsSource = wmtsSource;
  138. EnumMapLayerType layerType = EnumMapLayerType.None;
  139. for (int i = 0; i < txtLayer.Items.Count; i++)
  140. {
  141. if (txtLayer.GetItemChecked(i))
  142. {
  143. layerType |= (EnumMapLayerType)txtLayer.Items[i].Value;
  144. }
  145. }
  146. layerType = layerType & ~EnumMapLayerType.None;//移除
  147. if (!mapChanged)
  148. mapChanged = res.MapLayerType != layerType;
  149. res.MapLayerType = layerType;
  150. }
  151. db.SysSetings.AddOrUpdate(res);
  152. await db.SaveChangesAsync();
  153. SysConfig.Config = res;
  154. if (mapChanged)
  155. Messenger.Defalut.Pub("地图类型改变", res);
  156. Messenger.Defalut.Pub("时区改变", res);
  157. }
  158. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  159. if (needStartHttpSvr)
  160. {
  161. Application.Restart();
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. XdCxRhDW.Framework.LogHelper.Error("配置信息保存异常", ex);
  167. DxHelper.MsgBoxHelper.ShowError("配置信息保存异常");
  168. }
  169. if (this.Parent is Form frm)
  170. frm.Close();
  171. }
  172. private void txtMapType_SelectedIndexChanged(object sender, EventArgs e)
  173. {
  174. if (txtMapType.SelectedIndex == 0)
  175. {
  176. itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  177. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  178. }
  179. else
  180. {
  181. itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  182. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  183. }
  184. layoutControl1.BestFit();
  185. }
  186. private void toolTipController1_GetActiveObjectInfo(object sender, ToolTipControllerGetActiveObjectInfoEventArgs e)
  187. {
  188. RadioGroup rg = e.SelectedControl as RadioGroup;
  189. if (rg == null) return;
  190. for (int i = 0; i < rg.Properties.Items.Count; i++)
  191. {
  192. Rectangle rect = rg.GetItemRectangle(i);
  193. if (rect.Contains(e.ControlMousePosition))
  194. {
  195. var val = (EnumWmtsSource)rg.Properties.Items[i].Value;
  196. if (val == EnumWmtsSource.ZCJ)
  197. {
  198. var str = AppConfigHelper.Get("ZCJ_URL", "");
  199. e.Info = new ToolTipControlInfo(i, $"{rg.Properties.Items[i].Description}:{str}");
  200. }
  201. else
  202. {
  203. var str = AppConfigHelper.Get("SJZX_URL", "");
  204. e.Info = new ToolTipControlInfo(i, $"{rg.Properties.Items[i].Description}:{str}");
  205. }
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. }