using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Winsoft.GOV.Framework.Model; using Winsoft.GOV.Framework.Provider; using System.Configuration; namespace Winsoft.GOV.ConsoleSyncData { class Program { static Dictionary countyOfLastUpdateTime = new Dictionary(); static void Main(string[] args) { InitCountySources(); if (countyOfLastUpdateTime.Count == 0) { Console.WriteLine("没有配置需要同步的县市源,请按任意键退出。"); Console.ReadKey(); return; } IEnumerable pms = null; while (true) { foreach (County c in countyOfLastUpdateTime.Keys) { Update(c); } Console.WriteLine("休眠1小时"); Thread.Sleep(3600000); } } static bool Update(County c) { do { DateTime t = countyOfLastUpdateTime[c].UPDATE_DATE; Console.WriteLine(c.ToString() + "最近更新时间:" + t.ToString()); try { IEnumerable pms = ProvidersFactory.GetQLSXProvider(c).Select(t); if (pms.Count() == 0) break; foreach (PowerMattersDetail pm in pms) { if (pm.QL_NAME.First() == '$') { Console.WriteLine(String.Format("忽略--权力单位:{0}; 数据推送时间:{1},原因:该事项是主项", pm.QL_DEP, pm.UPDATE_DATE.ToString())); continue; } Branch b = ProvidersFactory.GetBranchProvider(c).Find(pm.OUGUID); if (b == null) { Console.WriteLine(String.Format("忽略--权力单位:{0}; 数据推送时间:{1},原因:没有该事项部门", pm.QL_DEP, pm.UPDATE_DATE.ToString())); continue; //b = new Branch() //{ // ShortName = pm.QL_DEP, // GUID = pm.OUGUID, // Position = 999 //}; //ProvidersFactory.GetBranchProvider(c).Insert(b); } ProvidersFactory.GetPowerMettersDetailProvider(c).UpdateOrInsert(pm); PowerMattersBase p = new PowerMattersBase() { QL_NAME = pm.QL_NAME, QL_INNER_CODE = pm.QL_INNER_CODE, QL_DEP = b.ShortName, OUGUID = pm.OUGUID, QL_STATE = pm.QL_STATE }; if (!ProvidersFactory.GetPowerMettersBaseProvider(c).IsExistByQL_INNER_CODE(pm.QL_INNER_CODE)) { Console.WriteLine(String.Format("新增--权力单位:{0}; 数据推送时间:{1}", p.QL_DEP, pm.UPDATE_DATE.ToString())); ProvidersFactory.GetPowerMettersBaseProvider(c).Insert(p); Notify(pm, p, c, ActionType.insert); } else { Console.WriteLine(String.Format("更新--权力单位:{0}; 数据推送时间:{1}", p.QL_DEP, pm.UPDATE_DATE.ToString())); ProvidersFactory.GetPowerMettersBaseProvider(c).Update(p); if (pm.QL_STATE == "1") Notify(pm, p, c, ActionType.update); else Notify(pm, p, c, ActionType.delete); } } if (pms != null && pms.Count() > 0) { countyOfLastUpdateTime[c].UPDATE_DATE = pms.Last().UPDATE_DATE; SaveUpdateDate(pms.Last().UPDATE_DATE, c); continue; } } catch (Exception e) { Console.WriteLine("更新" + c.ToString() + "权力数据库时,发生错误!"); Console.WriteLine(e.Message); return false; } } while (true); return true; } private static void InitCountySources() { string strSources = ConfigurationManager.AppSettings["Countys"]; string[] strs = strSources.Split(','); foreach(string s in strs) { County c; if (Enum.TryParse(s, out c) && !countyOfLastUpdateTime.ContainsKey(c)) { try { ProvidersFactory.GetNotificationOfActionProvider(c).CreateTable(); }catch(Exception e) { Console.WriteLine(c.ToString() + "创建提醒数据表失败"); Console.WriteLine(e.Message); continue; } DateTime t = GetUpdateDate(c); PowerMattersDetail last = new PowerMattersDetail { UPDATE_DATE = t }; countyOfLastUpdateTime.Add(c, last); } } } private static void Notify(PowerMattersDetail p, PowerMattersBase pb, County c, ActionType a) { if(c == County.LS) { NotificationOfAction n = NotificationOfAction.CopyFrom(p); n.QL_DEP = pb.QL_DEP; n.AType = a; ProvidersFactory.GetNotificationOfActionProvider(c).InserOrUpdate(n); } } private static DateTime GetUpdateDate(County c) { //获取Configuration对象 DateTime t = DateTime.Now; Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.AppSettings.Settings["UpdateDate"] == null) { t = ProvidersFactory.GetPowerMettersDetailProvider(c).FindLastUpdateDate(); return t; } else { DateTime t2 = ProvidersFactory.GetPowerMettersDetailProvider(c).FindLastUpdateDate(); if (DateTime.TryParse(config.AppSettings.Settings["UpdateDate"].Value, out t)) { Console.WriteLine("是否指定开始时间?"); if (Console.ReadKey().KeyChar == 'y') { return t; } return t > t2 ? t : t2; } else return t2; } } private static void SaveUpdateDate(DateTime t, County c) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.AppSettings.Settings[c.ToString() + "UpdateDate"] == null) { config.AppSettings.Settings.Add(c.ToString() + "UpdateDate", t.ToString()); } else { config.AppSettings.Settings[c.ToString() + "UpdateDate"].Value = t.ToString(); } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } } }