CtrlSysSettings.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using DevExpress.XtraEditors;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Net.Sockets;
  9. using System.Net;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Documents;
  13. using System.Windows.Forms;
  14. using DW5S.Repostory;
  15. using System.IO;
  16. using ExtensionsDev;
  17. using DevExpress.Utils;
  18. using DevExpress.XtraMap;
  19. using DW5S.WebApi;
  20. using DevExpress.XtraPrinting;
  21. using DW5S.Entity;
  22. using DevExpress.XtraEditors.Controls;
  23. using System.Security.Policy;
  24. using DevExpress.Utils.About;
  25. using DW5S.DTO;
  26. using System.Configuration;
  27. using Serilog;
  28. using DW5S.Service;
  29. namespace DW5S.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 async 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. var unitOfWork = IocContainer.UnitOfWork;
  48. var settings = await unitOfWork.Of<SysSetings>().FirstOrDefaultAsync();
  49. if (settings != null)
  50. {
  51. this.txtHttpPort.Text = settings.HttpPort.ToString();
  52. this.txtTimeZone.TimeZoneId = settings.TimeZoneID;
  53. this.txtSampleDire.Text = settings.SampleDirectory;
  54. if (!string.IsNullOrEmpty(settings.XLDirectory))
  55. this.txtXLDirectory.Text = settings.XLDirectory.ToString();
  56. if (this.txtMapType.SelectedIndex == settings.MapType)
  57. txtMapType_SelectedIndexChanged(null, null);
  58. else
  59. this.txtMapType.SelectedIndex = settings.MapType;
  60. if (txtMapType.SelectedIndex != 0)
  61. {
  62. this.txtWmtsSource.EditValue = settings.WmtsSource;
  63. for (int i = 0; i < txtLayer.Items.Count; i++)
  64. {
  65. if (settings.MapLayerType.HasFlag(EnumMapLayerType.SatMap))
  66. {
  67. this.txtLayer.Items[EnumMapLayerType.SatMap].CheckState = CheckState.Checked;
  68. }
  69. if (settings.MapLayerType.HasFlag(EnumMapLayerType.RoadMap))
  70. {
  71. this.txtLayer.Items[EnumMapLayerType.RoadMap].CheckState = CheckState.Checked;
  72. }
  73. if (settings.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. string msg = "加载配置信息异常";
  84. IocContainer.Logger.Error(ex, msg);
  85. DxHelper.MsgBoxHelper.ShowError(msg);
  86. }
  87. layoutControl1.BestFit();
  88. }
  89. private async void btnSave_Click(object sender, EventArgs e)
  90. {
  91. try
  92. {
  93. if (!int.TryParse(txtHttpPort.Text, out int port))
  94. {
  95. DxHelper.MsgBoxHelper.ShowError("Http端口错误!");
  96. return;
  97. }
  98. if (string.IsNullOrWhiteSpace(txtTimeZone.TimeZoneId))
  99. {
  100. DxHelper.MsgBoxHelper.ShowError("系统时区错误!");
  101. return;
  102. }
  103. if (string.IsNullOrWhiteSpace(txtSampleDire.Text))
  104. {
  105. DxHelper.MsgBoxHelper.ShowError("请输入样本存储目录!");
  106. return;
  107. }
  108. try
  109. {
  110. Directory.CreateDirectory(txtSampleDire.Text);
  111. }
  112. catch (Exception ex)
  113. {
  114. DxHelper.MsgBoxHelper.ShowError($"创建样本存储目录失败,{ex.Message}!");
  115. return;
  116. }
  117. var unitOfWork = IocContainer.UnitOfWork;
  118. var repsSys = unitOfWork.Of<SysSetings>();
  119. var res = await repsSys.FirstOrDefaultAsync();
  120. if (res == null) res = new SysSetings();
  121. bool needStartHttpSvr = res.HttpPort != port;
  122. if (!needStartHttpSvr)
  123. {
  124. needStartHttpSvr = res.TimeZoneID != txtTimeZone.TimeZoneId;
  125. }
  126. res.TimeZoneID = txtTimeZone.TimeZoneId;
  127. TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(txtTimeZone.TimeZoneId);
  128. if (tz.DisplayName == "UTC")
  129. {
  130. res.TimeZoneUTC = tz.DisplayName;
  131. res.TimeZoneName = "";
  132. }
  133. else
  134. {
  135. res.TimeZoneUTC = tz.DisplayName.Substring(1, tz.DisplayName.IndexOf(")") - 1).Trim();
  136. res.TimeZoneName = tz.DisplayName.Substring(tz.DisplayName.IndexOf(")") + 1).Trim();
  137. }
  138. res.ZoneHours = tz.BaseUtcOffset.TotalHours;
  139. res.HttpPort = port;
  140. res.XLDirectory = txtXLDirectory.Text;
  141. res.SampleDirectory = txtSampleDire.Text;
  142. bool mapChanged = res.MapType != txtMapType.SelectedIndex;
  143. res.MapType = txtMapType.SelectedIndex;
  144. if (txtMapType.SelectedIndex != 0)
  145. {
  146. var wmtsSource = (EnumWmtsSource)txtWmtsSource.EditValue;
  147. if (!mapChanged)
  148. {
  149. mapChanged = res.WmtsSource != wmtsSource;
  150. }
  151. res.WmtsSource = wmtsSource;
  152. EnumMapLayerType layerType = EnumMapLayerType.None;
  153. for (int i = 0; i < txtLayer.Items.Count; i++)
  154. {
  155. if (txtLayer.GetItemChecked(i))
  156. {
  157. layerType |= (EnumMapLayerType)txtLayer.Items[i].Value;
  158. }
  159. }
  160. layerType = layerType & ~EnumMapLayerType.None;//移除
  161. if (!mapChanged)
  162. mapChanged = res.MapLayerType != layerType;
  163. res.MapLayerType = layerType;
  164. }
  165. await repsSys.AddOrUpdateAsync(res);
  166. await unitOfWork.SaveAsync();
  167. if (mapChanged)
  168. Messenger.Defalut.Pub("地图类型改变", res);
  169. Messenger.Defalut.Pub("时区改变", res);
  170. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  171. if (needStartHttpSvr)
  172. {
  173. //var files=Directory.GetFiles("Service", "*.exe.config");
  174. //foreach (var f in files)
  175. //{
  176. // File.ReadAllText(f);
  177. //}
  178. Application.Restart();
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. string msg = "配置信息保存异常";
  184. IocContainer.Logger.Error(ex, msg);
  185. DxHelper.MsgBoxHelper.ShowError(msg);
  186. }
  187. if (this.Parent is Form frm)
  188. frm.Close();
  189. }
  190. private void txtMapType_SelectedIndexChanged(object sender, EventArgs e)
  191. {
  192. if (txtMapType.SelectedIndex == 0)
  193. {
  194. itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  195. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  196. }
  197. else
  198. {
  199. itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  200. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  201. }
  202. layoutControl1.BestFit();
  203. }
  204. private void toolTipController1_GetActiveObjectInfo(object sender, ToolTipControllerGetActiveObjectInfoEventArgs e)
  205. {
  206. RadioGroup rg = e.SelectedControl as RadioGroup;
  207. if (rg == null) return;
  208. for (int i = 0; i < rg.Properties.Items.Count; i++)
  209. {
  210. Rectangle rect = rg.GetItemRectangle(i);
  211. if (rect.Contains(e.ControlMousePosition))
  212. {
  213. var val = (EnumWmtsSource)rg.Properties.Items[i].Value;
  214. if (val == EnumWmtsSource.ZCJ)
  215. {
  216. var str = AppConfigHelper.Get("ZCJ_URL", "");
  217. e.Info = new ToolTipControlInfo(i, $"{rg.Properties.Items[i].Description}:{str}");
  218. }
  219. else
  220. {
  221. var str = AppConfigHelper.Get("SJZX_URL", "");
  222. e.Info = new ToolTipControlInfo(i, $"{rg.Properties.Items[i].Description}:{str}");
  223. }
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. }