CtrlSysSettings.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. if (!string.IsNullOrEmpty(settings.XLDirectory))
  54. this.txtXLDirectory.Text = settings.XLDirectory.ToString();
  55. if (this.txtMapType.SelectedIndex == settings.MapType)
  56. txtMapType_SelectedIndexChanged(null, null);
  57. else
  58. this.txtMapType.SelectedIndex = settings.MapType;
  59. if (txtMapType.SelectedIndex != 0)
  60. {
  61. this.txtWmtsSource.EditValue = settings.WmtsSource;
  62. for (int i = 0; i < txtLayer.Items.Count; i++)
  63. {
  64. if (settings.MapLayerType.HasFlag(EnumMapLayerType.SatMap))
  65. {
  66. this.txtLayer.Items[EnumMapLayerType.SatMap].CheckState = CheckState.Checked;
  67. }
  68. if (settings.MapLayerType.HasFlag(EnumMapLayerType.RoadMap))
  69. {
  70. this.txtLayer.Items[EnumMapLayerType.RoadMap].CheckState = CheckState.Checked;
  71. }
  72. if (settings.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. string msg = "加载配置信息异常";
  83. IocContainer.Logger.Error(ex, msg);
  84. DxHelper.MsgBoxHelper.ShowError(msg);
  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. if (string.IsNullOrWhiteSpace(txtSampleDire.Text))
  103. {
  104. DxHelper.MsgBoxHelper.ShowError("请输入样本存储目录!");
  105. return;
  106. }
  107. try
  108. {
  109. Directory.CreateDirectory(txtSampleDire.Text);
  110. }
  111. catch (Exception ex)
  112. {
  113. DxHelper.MsgBoxHelper.ShowError($"创建样本存储目录失败,{ex.Message}!");
  114. return;
  115. }
  116. var unitOfWork = IocContainer.UnitOfWork;
  117. var repsSys = unitOfWork.Of<SysSetings>();
  118. var res = await repsSys.FirstOrDefaultAsync();
  119. if (res == null) res = new SysSetings();
  120. bool needStartHttpSvr = res.HttpPort != port;
  121. if (!needStartHttpSvr)
  122. {
  123. needStartHttpSvr = res.TimeZoneID != txtTimeZone.TimeZoneId;
  124. }
  125. res.TimeZoneID = txtTimeZone.TimeZoneId;
  126. TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(txtTimeZone.TimeZoneId);
  127. if (tz.DisplayName == "UTC")
  128. {
  129. res.TimeZoneUTC = tz.DisplayName;
  130. res.TimeZoneName = "";
  131. }
  132. else
  133. {
  134. res.TimeZoneUTC = tz.DisplayName.Substring(1, tz.DisplayName.IndexOf(")") - 1).Trim();
  135. res.TimeZoneName = tz.DisplayName.Substring(tz.DisplayName.IndexOf(")") + 1).Trim();
  136. }
  137. res.ZoneHours = tz.BaseUtcOffset.TotalHours;
  138. res.HttpPort = port;
  139. res.XLDirectory = txtXLDirectory.Text;
  140. res.SampleDirectory = txtSampleDire.Text;
  141. bool mapChanged = res.MapType != txtMapType.SelectedIndex;
  142. res.MapType = txtMapType.SelectedIndex;
  143. if (txtMapType.SelectedIndex != 0)
  144. {
  145. var wmtsSource = (EnumWmtsSource)txtWmtsSource.EditValue;
  146. if (!mapChanged)
  147. {
  148. mapChanged = res.WmtsSource != wmtsSource;
  149. }
  150. res.WmtsSource = wmtsSource;
  151. EnumMapLayerType layerType = EnumMapLayerType.None;
  152. for (int i = 0; i < txtLayer.Items.Count; i++)
  153. {
  154. if (txtLayer.GetItemChecked(i))
  155. {
  156. layerType |= (EnumMapLayerType)txtLayer.Items[i].Value;
  157. }
  158. }
  159. layerType = layerType & ~EnumMapLayerType.None;//移除
  160. if (!mapChanged)
  161. mapChanged = res.MapLayerType != layerType;
  162. res.MapLayerType = layerType;
  163. }
  164. await repsSys.AddOrUpdateAsync(res);
  165. await unitOfWork.SaveAsync();
  166. if (mapChanged)
  167. Messenger.Defalut.Pub("地图类型改变", res);
  168. Messenger.Defalut.Pub("时区改变", res);
  169. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  170. if (needStartHttpSvr)
  171. {
  172. //var files=Directory.GetFiles("Service", "*.exe.config");
  173. //foreach (var f in files)
  174. //{
  175. // File.ReadAllText(f);
  176. //}
  177. Application.Restart();
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. string msg = "配置信息保存异常";
  183. IocContainer.Logger.Error(ex, msg);
  184. DxHelper.MsgBoxHelper.ShowError(msg);
  185. }
  186. if (this.Parent is Form frm)
  187. frm.Close();
  188. }
  189. private void txtMapType_SelectedIndexChanged(object sender, EventArgs e)
  190. {
  191. if (txtMapType.SelectedIndex == 0)
  192. {
  193. itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  194. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  195. }
  196. else
  197. {
  198. itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  199. itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
  200. }
  201. layoutControl1.BestFit();
  202. }
  203. private void toolTipController1_GetActiveObjectInfo(object sender, ToolTipControllerGetActiveObjectInfoEventArgs e)
  204. {
  205. RadioGroup rg = e.SelectedControl as RadioGroup;
  206. if (rg == null) return;
  207. for (int i = 0; i < rg.Properties.Items.Count; i++)
  208. {
  209. Rectangle rect = rg.GetItemRectangle(i);
  210. if (rect.Contains(e.ControlMousePosition))
  211. {
  212. var val = (EnumWmtsSource)rg.Properties.Items[i].Value;
  213. if (val == EnumWmtsSource.ZCJ)
  214. {
  215. var str = AppConfigHelper.Get("ZCJ_URL", "");
  216. e.Info = new ToolTipControlInfo(i, $"{rg.Properties.Items[i].Description}:{str}");
  217. }
  218. else
  219. {
  220. var str = AppConfigHelper.Get("SJZX_URL", "");
  221. e.Info = new ToolTipControlInfo(i, $"{rg.Properties.Items[i].Description}:{str}");
  222. }
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. }