public static void ChuangJianFeed() { //增加一个父feed /* * INSERT INTO feed( * id, parent_id,`name`, key_type, value_type, updated, creator_id,`status`, access, owner_id, host_id * ) VALUES( * 17867, 0, 'f17865c7', 0, 0, now(), 78, 0, 0, 78, 0); */ //增加一个子feed /* * INSERT INTO feed( * id, parent_id,`name`, key_type, value_type, updated, creator_id,`status`, access, owner_id, host_id * ) VALUES( * 17867, 17865, 'f17865c7', 1, 2, now(), 78, 0, 0, 78, 0); */ // VALUES //(100, 'Name 1', 'Value 1', 'Other 1'), //(101, 'Name 2', 'Value 2', 'Other 2'), //C:\Users\Administrator\Desktop\SMS\大合集_67668修改\大合集\2017 List <Fmodel> fmodels = new List <Fmodel>(); //id,name,pid int fidBegin = 17868; string dP17 = @"C:\Users\Administrator\Desktop\SMS\大合集_67668修改\大合集\2017\"; string dP18 = @"C:\Users\Administrator\Desktop\SMS\大合集_67668修改\大合集\2018\"; dP17 = @"C:\Users\Administrator\Desktop\baicheng\新建文件夹\大合集\2017\"; dP18 = @"C:\Users\Administrator\Desktop\baicheng\新建文件夹\大合集\2018\"; Action <string> act = (dirP) => { string[] dirs = Directory.GetDirectories(dirP); foreach (var item in dirs) { if (item.Contains("气象") || item.Contains("土壤")) { continue; //气象和土壤先跳过 } Fmodel fm = new Fmodel(); fm.Feed = new Feed() { Id = fidBegin, FileFullName = item, Name = item.Replace(dirP, ""), Pid = 0 }; fm.Children = new List <Feed>(); //如果有了,找出来,赋值另一个文件路径FileFullName2 if (fmodels.Exists(f => f.Feed.Name == fm.Feed.Name)) { Fmodel fm2 = fmodels.Find(f => f.Feed.Name == fm.Feed.Name); string[] file2s = Directory.GetFiles(item); foreach (var f in file2s) { string name = f.Replace(item + "\\", "").Replace(".csv", ""); Feed fc = fm2.Children.Find(c => c.Name == name); if (fc != null) { fc.FileFullName2 = f; } } continue; } fmodels.Add(fm); fidBegin++; string[] files = Directory.GetFiles(item); foreach (var f in files) { Feed fd = new Feed() { Id = fidBegin, FileFullName = f, Name = f.Replace(item + "\\", "").Replace(".csv", ""), Pid = fm.Feed.Id }; fm.Children.Add(fd); fidBegin++; } } }; act(dP17); act(dP18); //时间无需做出特殊处理. //开始导入数据 //获取文件记录的所有数据 Action <Feed, List <string> > setAllData = (feed, dfs) => { foreach (string fname in dfs) { string[] lines = File.ReadAllLines(fname); bool s = true; foreach (string line in lines) { if (s) { s = false; continue; } //跳过第一行 string[] spLine = line.Split(','); DateTime dt = DateTime.Parse(spLine[0]); decimal v = 0; if (!string.IsNullOrWhiteSpace(spLine[1])) { v = decimal.Parse(spLine[1]); } if (feed.datas.ContainsKey(dt)) { continue; //跳过重复数据 } feed.datas.Add(dt, v); } } }; foreach (var item in fmodels) { foreach (var c in item.Children) { if (string.IsNullOrWhiteSpace(c.FileFullName2)) { setAllData(c, new List <string> { c.FileFullName }); } else { setAllData(c, new List <string> { c.FileFullName, c.FileFullName2 }); } } } Insert(fmodels); Console.WriteLine("AllOver"); }
public static void ChuLiQiXiangZhanHeTuRang() { string prefix = @"C:\Users\Administrator\Desktop\SMS\"; // prefix = @"C:\Users\Administrator\Desktop\baicheng\"; string dPqxz = prefix + @"气象土壤\气象站\"; string dirP = prefix + @"气象土壤\土壤\"; //首先依然是创建Fmdel List <Fmodel> fmodels = new List <Fmodel>(); int fidBegin = 18013; //先处理气象站 Dictionary <DateTime, decimal> qxzData = new Dictionary <DateTime, decimal>(); string f1 = prefix + @"气象土壤\雨量2017.csv"; string f2 = prefix + @"气象土壤\雨量2018.csv"; Action <string> actQxz = (file) => { string[] lines = File.ReadAllLines(file); bool s = true; foreach (string line in lines) { if (s) { s = false; continue; } //跳过第一行 string[] spLine = line.Split(','); DateTime dt = DateTime.Parse(spLine[0]); decimal v = 0; if (!string.IsNullOrWhiteSpace(spLine[1])) { v = decimal.Parse(spLine[1]); } if (qxzData.ContainsKey(dt)) { continue; //跳过重复数据 } qxzData.Add(dt, v); } }; actQxz(f1); actQxz(f2); string[] dirs = Directory.GetDirectories(dPqxz); foreach (var item in dirs) { Fmodel fm = new Fmodel(); fm.Feed.Pid = 0; fm.Feed.Id = fidBegin++; fm.Feed.Name = item.Replace(dPqxz, ""); Feed fd = new Feed() { Id = fidBegin++, Name = "雨量", Pid = fm.Feed.Id, datas = qxzData }; fm.Children.Add(fd); fmodels.Add(fm); } //再处理土壤 string[] dirts = Directory.GetDirectories(dirP); foreach (var item in dirts) { Fmodel fm = new Fmodel(); string name = item.Replace(dirP, ""); //如果有了,找出来 if (fmodels.Exists(f => f.Feed.Name == name)) { fm = fmodels.Find(f => f.Feed.Name == name); } else { fm.Feed = new Feed() { Id = fidBegin++, FileFullName = item, Name = name, Pid = 0 }; fmodels.Add(fm); } string[] files = Directory.GetFiles(item); foreach (var f in files) { Feed fd = new Feed() { Id = fidBegin++, FileFullName = f, Name = f.Replace(item + "\\", "").Replace(".csv", ""), Pid = fm.Feed.Id }; fm.Children.Add(fd); } } Action <Feed, string> setAllData = (feed, fname) => { string[] lines = File.ReadAllLines(fname); bool s = true; foreach (string line in lines) { if (s) { s = false; continue; } //跳过第一行 string[] spLine = line.Split(','); DateTime dt = DateTime.Parse(spLine[0]); decimal v = 0; if (!string.IsNullOrWhiteSpace(spLine[1])) { v = decimal.Parse(spLine[1]); } if (feed.datas.ContainsKey(dt)) { continue; //跳过重复数据 } feed.datas.Add(dt, v); } }; //填土壤数据 foreach (var item in fmodels) { foreach (var c in item.Children) { if (!string.IsNullOrWhiteSpace(c.FileFullName)) { setAllData(c, c.FileFullName); } } } //Insert(fmodels); Console.WriteLine("OK"); }