FormFindIdentical.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using ESRI.ArcGIS.esriSystem;
  14. using ESRI.ArcGIS.Geodatabase;
  15. using ESRI.ArcGIS.DataManagementTools;
  16. namespace FindIdentical.Win
  17. {
  18. public partial class FormFindIdentical : Form
  19. {
  20. /// <summary>
  21. /// 上一次操作的时间(短日期格式yyyy-MM-dd)
  22. /// </summary>
  23. private string _strLastExcuteDate = "";
  24. private System.Object thisLock = new System.Object();
  25. private List<string> _lstIdenticalName = new List<string>() {"BOOSTER_FindIdentical", "BOOSTER_GW_FindIdentical", "PIPESECTION_FindIdentical", "PIPESECTION_GW_FindIdentical" };
  26. private int _excuteDateEnum = 6;
  27. /// <summary>
  28. /// 当前编辑的工作空间
  29. /// </summary>
  30. private IWorkspace _wsProcessing;
  31. public FormFindIdentical()
  32. {
  33. InitializeComponent();
  34. }
  35. private void FormTrayCompress_Load(object sender, EventArgs e)
  36. {
  37. this.timerMonitor.Interval = 1 * 10 * 1000;
  38. this.tsbtnStartService.Enabled = true;
  39. this.tsbtnStopService.Enabled = false;
  40. string _identicalName = ConfigurationManager.AppSettings["DataName"];
  41. _lstIdenticalName = _identicalName.Split(',').ToList();
  42. _excuteDateEnum = Convert.ToInt32(ConfigurationManager.AppSettings["RunDate"]);
  43. }
  44. private void TimerMonitor_Tick(object sender, EventArgs e)
  45. {
  46. try
  47. {
  48. DateTime __dtimeNow = DateTime.Now;
  49. string __strCompressStartTime = ConfigurationManager.AppSettings["CompressStartTime"];
  50. if (string.IsNullOrEmpty(__strCompressStartTime))
  51. {
  52. throw new System.Exception("未读取到有效起始时间.");
  53. }
  54. DateTime __dtimeMin = Convert.ToDateTime(string.Format("{0} {1}", __dtimeNow.ToString("yyyy-MM-dd"), __strCompressStartTime));
  55. DateTime __dtimeMax = __dtimeMin.AddHours(1.0);
  56. if (__dtimeNow > __dtimeMin && __dtimeNow < __dtimeMax && __dtimeNow.DayOfWeek == (DayOfWeek)_excuteDateEnum)
  57. {
  58. if (__dtimeNow.ToString("yyyy-MM-dd") == this._strLastExcuteDate) //限制一天自动执行一次压缩操作
  59. {
  60. return;
  61. }
  62. this.timerMonitor.Stop(); //关闭计时器
  63. this.ShowProcessMesssage("开始进行查找重复数据……");
  64. this._strLastExcuteDate = __dtimeNow.ToString("yyyy-MM-dd"); //更新‘上一次压缩日期’
  65. string __strServer = ConfigurationManager.AppSettings["ServerName"];
  66. string __strService = @"sde:oracle11g:" + ConfigurationManager.AppSettings["ServerName"];
  67. string __strDataBase = ConfigurationManager.AppSettings["DatabaseName"];
  68. string __strVersion = ConfigurationManager.AppSettings["DataVersion"];
  69. string __strSDEUser = ConfigurationManager.AppSettings["SDEUser"];
  70. string __strPassword = ConfigurationManager.AppSettings["Password"];
  71. string __strConnection = ConfigurationManager.AppSettings["ConnectionStr"];
  72. this._wsProcessing = ConnectToTransactionalVersion(__strServer, __strService, __strSDEUser, __strPassword, __strDataBase, "SDE.DEFAULT");
  73. for (int i = 0; i < _lstIdenticalName.Count; i++)
  74. {
  75. string _identicalName = _lstIdenticalName[i];
  76. try
  77. {
  78. ITable _table = (_wsProcessing as IFeatureWorkspace).OpenTable(_identicalName);
  79. if (_table is IDataset && (_table as IDataset).CanDelete())
  80. {
  81. (_table as IDataset).Delete();
  82. }
  83. }
  84. catch { }
  85. //string _deleteSql = string.Format("drop table {0}", _identicalName);
  86. //_wsProcessing.ExecuteSQL(_deleteSql);
  87. ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
  88. ESRI.ArcGIS.DataManagementTools.FindIdentical _findIdentical = new ESRI.ArcGIS.DataManagementTools.FindIdentical();
  89. _findIdentical.in_dataset = __strConnection + string.Format(@"{0}.GASDATASET\{0}.{1}", __strSDEUser, _identicalName.Replace("_FindIdentical", ""));
  90. _findIdentical.out_dataset = __strConnection + string.Format(@"{0}.{1}", __strSDEUser, _identicalName);
  91. _findIdentical.fields = "SHAPE";
  92. _findIdentical.output_record_option = "true";//"ONLY_DUPLICATES";
  93. _findIdentical.z_tolerance = 0;
  94. gp.Execute(_findIdentical, null);
  95. }
  96. Marshal.FinalReleaseComObject(_wsProcessing);
  97. _wsProcessing = null;
  98. GC.Collect();
  99. CreateIdentifyFile();
  100. this.ShowProcessMesssage("完成查找重复数据.");
  101. this.ShowProcessMesssage("");
  102. this.timerMonitor.Start(); //重启计时器
  103. }
  104. }
  105. catch (System.Exception ex)
  106. {
  107. this._strLastExcuteDate = ""; //重置‘上一次查找重复数据日期’
  108. this.timerMonitor.Start(); //重启计时器
  109. this.ShowProcessMesssage(ex.Message);
  110. }
  111. }
  112. private void tsbtnStartService_Click(object sender, EventArgs e)
  113. {
  114. this._strLastExcuteDate = ""; //重置‘上一次查找重复数据日期’
  115. this.timerMonitor.Start();
  116. this.tsbtnStartService.Enabled = false;
  117. this.tsbtnStopService.Enabled = true;
  118. this.ShowProcessMesssage("成功启动查找重复数据服务……");
  119. }
  120. private void tsbtnStopService_Click(object sender, EventArgs e)
  121. {
  122. this._strLastExcuteDate = ""; //重置‘上一次查找重复数据日期’
  123. this.timerMonitor.Stop();
  124. this.tsbtnStartService.Enabled = true;
  125. this.tsbtnStopService.Enabled = false;
  126. this.ShowProcessMesssage("成功停止查找重复数据服务……");
  127. }
  128. private void tsbtnExit_Click(object sender, EventArgs e)
  129. {
  130. this.Close();
  131. }
  132. private void notifyIconCompress_DoubleClick(object sender, EventArgs e)
  133. {
  134. if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
  135. {
  136. this.WindowState = System.Windows.Forms.FormWindowState.Normal;
  137. //Tofly.GISUI.Utils.WindowStateHelper.SetWindowPositon(this);
  138. }
  139. }
  140. private void FormTrayCompress_SizeChanged(object sender, EventArgs e)
  141. {
  142. this.ShowInTaskbar = (this.WindowState != System.Windows.Forms.FormWindowState.Minimized);
  143. }
  144. /// <summary>
  145. /// 显示处理消息
  146. /// </summary>
  147. /// <param name="message"></param>
  148. private void ShowProcessMesssage(string message)
  149. {
  150. if (string.IsNullOrEmpty(message) == false)
  151. {
  152. this.lboxMessage.Items.Add(string.Format("{0} {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message));
  153. }
  154. else
  155. {
  156. this.lboxMessage.Items.Add(message);
  157. }
  158. this.lboxMessage.TopIndex = this.lboxMessage.Items.Count - 1; //新添加项可见
  159. }
  160. private IWorkspace ConnectToTransactionalVersion(string server, string instance, string user, string password, string database, string version)
  161. {
  162. IPropertySet propertySet = new PropertySet();
  163. propertySet.SetProperty("SERVER", server);
  164. propertySet.SetProperty("INSTANCE", instance);
  165. propertySet.SetProperty("DATABASE", database);
  166. propertySet.SetProperty("USER", user);
  167. propertySet.SetProperty("PASSWORD", password);
  168. propertySet.SetProperty("VERSION", version);
  169. Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
  170. IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
  171. return workspaceFactory.Open(propertySet, 0);
  172. }
  173. private void CreateIdentifyFile()
  174. {
  175. lock (thisLock)
  176. {
  177. string __strIdentifyLogFolder = string.Format(@"{0}\Log", Application.StartupPath);
  178. if (Directory.Exists(__strIdentifyLogFolder) == false)
  179. {
  180. Directory.CreateDirectory(__strIdentifyLogFolder);
  181. }
  182. string __strIdentifyFile = string.Format(@"{0}\FindIdenticalSuccess{1}.idf", __strIdentifyLogFolder, DateTime.Now.ToString("yyyyMMdd"));
  183. File.Create(__strIdentifyFile);
  184. Application.DoEvents();
  185. }
  186. }
  187. }
  188. }