Program.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Winsoft.GOV.Framework.Model;
  8. using Winsoft.GOV.Framework.Provider;
  9. using System.Configuration;
  10. namespace Winsoft.GOV.ConsoleSyncData
  11. {
  12. class Program
  13. {
  14. static Dictionary<County, PowerMattersDetail> countyOfLastUpdateTime = new Dictionary<County, PowerMattersDetail>();
  15. static void Main(string[] args)
  16. {
  17. InitCountySources();
  18. if (countyOfLastUpdateTime.Count == 0)
  19. {
  20. Console.WriteLine("没有配置需要同步的县市源,请按任意键退出。");
  21. Console.ReadKey();
  22. return;
  23. }
  24. IEnumerable<PowerMattersDetail> pms = null;
  25. while (true)
  26. {
  27. foreach (County c in countyOfLastUpdateTime.Keys)
  28. {
  29. Update(c);
  30. }
  31. Console.WriteLine("休眠1小时");
  32. Thread.Sleep(3600000);
  33. }
  34. }
  35. static bool Update(County c)
  36. {
  37. do
  38. {
  39. DateTime t = countyOfLastUpdateTime[c].UPDATE_DATE;
  40. Console.WriteLine(c.ToString() + "最近更新时间:" + t.ToString());
  41. try
  42. {
  43. IEnumerable<PowerMattersDetail> pms = ProvidersFactory.GetQLSXProvider(c).Select(t);
  44. if (pms.Count() == 0)
  45. break;
  46. foreach (PowerMattersDetail pm in pms)
  47. {
  48. if (pm.QL_NAME.First<char>() == '$')
  49. {
  50. Console.WriteLine(String.Format("忽略--权力单位:{0}; 数据推送时间:{1}", pm.QL_DEP, pm.UPDATE_DATE.ToString()));
  51. continue;
  52. }
  53. Branch b = ProvidersFactory.GetBranchProvider(c).Find(pm.OUGUID);
  54. if (b == null)
  55. {
  56. b = new Branch()
  57. {
  58. ShortName = pm.QL_DEP,
  59. GUID = pm.OUGUID,
  60. Position = 999
  61. };
  62. ProvidersFactory.GetBranchProvider(c).Insert(b);
  63. }
  64. ProvidersFactory.GetPowerMettersDetailProvider(c).UpdateOrInsert(pm);
  65. PowerMattersBase p = new PowerMattersBase()
  66. {
  67. QL_NAME = pm.QL_NAME,
  68. QL_INNER_CODE = pm.QL_INNER_CODE,
  69. QL_DEP = b.ShortName,
  70. OUGUID = pm.OUGUID,
  71. QL_STATE = pm.QL_STATE
  72. };
  73. if (!ProvidersFactory.GetPowerMettersBaseProvider(c).IsExistByQL_INNER_CODE(pm.QL_INNER_CODE))
  74. {
  75. Console.WriteLine(String.Format("新增--权力单位:{0}; 数据推送时间:{1}", p.QL_DEP, pm.UPDATE_DATE.ToString()));
  76. ProvidersFactory.GetPowerMettersBaseProvider(c).Insert(p);
  77. Notify(pm, p, c, ActionType.insert);
  78. }
  79. else
  80. {
  81. Console.WriteLine(String.Format("更新--权力单位:{0}; 数据推送时间:{1}", p.QL_DEP, pm.UPDATE_DATE.ToString()));
  82. ProvidersFactory.GetPowerMettersBaseProvider(c).Update(p);
  83. if (pm.QL_STATE == "1")
  84. Notify(pm, p, c, ActionType.update);
  85. else
  86. Notify(pm, p, c, ActionType.delete);
  87. }
  88. }
  89. if (pms != null && pms.Count() > 0)
  90. {
  91. countyOfLastUpdateTime[c].UPDATE_DATE = pms.Last().UPDATE_DATE;
  92. SaveUpdateDate(pms.Last().UPDATE_DATE, c);
  93. continue;
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. Console.WriteLine("更新" + c.ToString() + "权力数据库时,发生错误!");
  99. Console.WriteLine(e.Message);
  100. return false;
  101. }
  102. } while (true);
  103. return true;
  104. }
  105. private static void InitCountySources()
  106. {
  107. string strSources = ConfigurationManager.AppSettings["Countys"];
  108. string[] strs = strSources.Split(',');
  109. foreach(string s in strs)
  110. {
  111. County c;
  112. if (Enum.TryParse<County>(s, out c) && !countyOfLastUpdateTime.ContainsKey(c))
  113. {
  114. try
  115. {
  116. ProvidersFactory.GetNotificationOfActionProvider(c).CreateTable();
  117. }catch(Exception e)
  118. {
  119. Console.WriteLine(c.ToString() + "创建提醒数据表失败");
  120. Console.WriteLine(e.Message);
  121. continue;
  122. }
  123. DateTime t = GetUpdateDate(c);
  124. PowerMattersDetail last = new PowerMattersDetail
  125. {
  126. UPDATE_DATE = t
  127. };
  128. countyOfLastUpdateTime.Add(c, last);
  129. }
  130. }
  131. }
  132. private static void Notify(PowerMattersDetail p, PowerMattersBase pb, County c, ActionType a)
  133. {
  134. if(c == County.LS)
  135. {
  136. NotificationOfAction n = NotificationOfAction.CopyFrom(p);
  137. n.QL_DEP = pb.QL_DEP;
  138. n.AType = a;
  139. ProvidersFactory.GetNotificationOfActionProvider(c).InserOrUpdate(n);
  140. }
  141. }
  142. private static DateTime GetUpdateDate(County c)
  143. {
  144. //获取Configuration对象
  145. DateTime t = DateTime.Now;
  146. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  147. if (config.AppSettings.Settings["UpdateDate"] == null)
  148. {
  149. t = ProvidersFactory.GetPowerMettersDetailProvider(c).FindLastUpdateDate();
  150. return t;
  151. }
  152. else
  153. {
  154. DateTime t2 = ProvidersFactory.GetPowerMettersDetailProvider(c).FindLastUpdateDate();
  155. if (DateTime.TryParse(config.AppSettings.Settings["UpdateDate"].Value, out t))
  156. {
  157. Console.WriteLine("是否指定开始时间?");
  158. if (Console.ReadKey().KeyChar == 'y')
  159. {
  160. return t;
  161. }
  162. return t > t2 ? t : t2;
  163. }
  164. else
  165. return t2;
  166. }
  167. }
  168. private static void SaveUpdateDate(DateTime t, County c)
  169. {
  170. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  171. if (config.AppSettings.Settings[c.ToString() + "UpdateDate"] == null)
  172. {
  173. config.AppSettings.Settings.Add(c.ToString() + "UpdateDate", t.ToString());
  174. }
  175. else
  176. {
  177. config.AppSettings.Settings[c.ToString() + "UpdateDate"].Value = t.ToString();
  178. }
  179. config.Save(ConfigurationSaveMode.Modified);
  180. ConfigurationManager.RefreshSection("appSettings");
  181. }
  182. }
  183. }