Guest User

Untitled

a guest
Dec 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.IO;
  6.  
  7. // new
  8.  
  9. namespace rdc.KLADR
  10. {
  11. public class KLADR
  12. {
  13.  
  14. public string city { get; set; }
  15. public string status { get; set; }
  16.  
  17. internal static void removeCity(string city)
  18. {
  19.  
  20. RDCEntities context = new RDCEntities();
  21. foreach (KLADRMain rowToDelete in (from r in context.KLADRMains where r.City == city select r))
  22. {
  23. context.DeleteObject(rowToDelete);
  24. }
  25. context.SaveChanges();
  26. }
  27.  
  28. public string importCityFromFile(string pathToFile)
  29. {
  30. string line;
  31. StreamReader cityFile = new StreamReader(pathToFile, System.Text.Encoding.GetEncoding(866));
  32. int countLines = 0;
  33. while ((line = cityFile.ReadLine()) != null)
  34. {
  35. countLines++;
  36. if (countLines == 2)
  37. {
  38. line = line.Trim();
  39. city = line.Substring(0, line.IndexOf(" - УЛИЦЫ"));
  40. }
  41. }
  42. status = "Город успешно вычленен из файла";
  43. cityFile.Close();
  44. return city;
  45. }
  46.  
  47.  
  48. public bool cityAlreadyInDB()
  49. {
  50. RDCEntities context = new RDCEntities();
  51. int rowsWithCity = context.KLADRMains.Where(r => r.City == city).Count();
  52. status = rowsWithCity.ToString() + " улиц по городу " + city + " уже есть.";
  53. if (rowsWithCity == 0)
  54. {
  55. return false;
  56. }
  57. else
  58. {
  59. return true;
  60. }
  61.  
  62. }
  63. public void addCityFromFileToDB(string pathToFile)
  64. {
  65. string line;
  66. RDCEntities eKLADR = new RDCEntities();
  67.  
  68. StreamReader cityFile = new StreamReader(pathToFile, System.Text.Encoding.GetEncoding(866));
  69. int countLines = 0;
  70. while ((line = cityFile.ReadLine()) != null)
  71. {
  72. countLines++;
  73. string fullType = "";
  74. string shortType = "";
  75.  
  76. if (countLines > 5 && !line.StartsWith("Всего отобрано"))
  77. {
  78. string[] eachStreet = line.Split(';');
  79. shortType = eachStreet[1].Trim();
  80. fullType = (from r in eKLADR.KLADRMains where r.City == "ACR_NO_CITY" && r.StreetName == shortType select r).First().StreetType;
  81. // rdc.utils.indexToFirst();
  82.  
  83. eKLADR.AddToKLADRMains(KLADRMain.CreateKLADRMain(city, Utils.indexToFirst(eachStreet[0].Trim()), fullType, 0));
  84. eKLADR.SaveChanges();
  85. }
  86. }
  87. status = "Записи загружены";
  88. cityFile.Close();
  89. }
  90.  
  91. internal static void addCityFromCSVToDB(string pathToFile)
  92. {
  93. string line;
  94. RDCEntities eKLADR = new RDCEntities();
  95.  
  96. StreamReader cityFile = new StreamReader(pathToFile, System.Text.Encoding.GetEncoding(1251));
  97. // int countLines = 0;
  98. while ((line = cityFile.ReadLine()) != null)
  99. {
  100.  
  101. string[] eachStreet = line.Split(';');
  102.  
  103. eKLADR.AddToKLADRMains(KLADRMain.CreateKLADRMain(eachStreet[0], eachStreet[1], eachStreet[2], 0));
  104. eKLADR.SaveChanges();
  105. }
  106.  
  107. cityFile.Close();
  108. }
  109.  
  110.  
  111.  
  112. internal static void fullFill(ref List<string[]> table, int streetNameCol, int streetTypeCol, string cityName)
  113. {
  114. RDCEntities context = new RDCEntities();
  115. string streetType;
  116. string streetName;
  117. foreach (string[] line in table)
  118. {
  119.  
  120. streetType = line[streetTypeCol].Trim();
  121. streetName = Utils.indexToFirst(line[streetNameCol].Trim());
  122. line[streetNameCol] = streetName;
  123. if (string.IsNullOrWhiteSpace(streetType) && !string.IsNullOrWhiteSpace(streetName))
  124. {
  125. // try
  126. //{
  127. if (context.KLADRMains.Where(p => p.City == cityName).Where(p => p.StreetName == streetName).Count() == 1)
  128. {
  129. line[streetTypeCol] = (from p in context.KLADRMains where p.City == cityName && p.StreetName == streetName select p).FirstOrDefault().StreetType;
  130. }
  131. //}
  132. //catch (Exception e)
  133. //{ }
  134. }
  135. }
  136. }
  137.  
  138.  
  139.  
  140. internal static List<string> showAllCities()
  141. {
  142. RDCEntities context = new RDCEntities();
  143. List<string> cityList;
  144. cityList = (from p in context.KLADRMains where p.City != "ACR_NO_CITY" select p.City).Distinct().ToList();
  145. cityList.Sort();
  146. return cityList;
  147. }
  148.  
  149.  
  150. internal static void RenameCity(string city, string newCity)
  151. {
  152. RDCEntities context = new RDCEntities();
  153. foreach (KLADRMain obj in (from r in context.KLADRMains where r.City == city select r))
  154. {
  155. obj.City = newCity;
  156. }
  157. context.SaveChanges();
  158. }
  159.  
  160. //internal static List<string> ShowStreetsInCity(string city)
  161. //{
  162. // RDCEntities context = new RDCEntities();
  163. //// return (from r in context.KLADRMains where r.City == city select new { r.StreetName, r.StreetType }).ToList();
  164. //}
  165.  
  166.  
  167.  
  168. internal static void fullFilldoubleOpt1(ref List<string[]> table, int streetNameCol, int streetTypeCol, string cityName)
  169. {
  170. RDCEntities context = new RDCEntities();
  171. string streetType;
  172. string streetName;
  173. foreach (string[] line in table)
  174. {
  175.  
  176. streetType = line[streetTypeCol].Trim();
  177. streetName = Utils.indexToFirst(line[streetNameCol].Trim());
  178. line[streetNameCol] = streetName;
  179. int tempTryParse;
  180. if (string.IsNullOrWhiteSpace(streetType) && !string.IsNullOrWhiteSpace(streetName))
  181. {
  182. // try
  183. //{
  184. if (context.KLADRMains.Where(p => p.City == cityName).Where(p => p.StreetName == streetName).Count() == 1)
  185. {
  186. line[streetTypeCol] = (from p in context.KLADRMains where p.City == cityName && p.StreetName == streetName select p).FirstOrDefault().StreetType;
  187. }
  188.  
  189.  
  190. else
  191. {
  192.  
  193. if (!int.TryParse(streetName.Substring(0, 1), out tempTryParse) && streetName.Split().Length == 2)
  194. {
  195. string partOfStreetName = streetName.Split()[1];
  196. var possibleResults = (from p in context.KLADRMains where p.City == cityName && p.StreetName.Contains(partOfStreetName) select p);
  197. foreach (var possibleResult in possibleResults)
  198. {
  199. if (possibleResult.StreetName.Split(' ').Length == 2 && possibleResult.StreetName.Split(' ')[1] == partOfStreetName)
  200. {
  201. line[streetNameCol] = possibleResult.StreetName;
  202. line[streetTypeCol] = possibleResult.StreetType;
  203.  
  204. }
  205.  
  206. }
  207. }
  208.  
  209.  
  210.  
  211.  
  212. //if (context.KLADRMains.Where(p => p.City == cityName).Where(p => p.StreetName.Contains(streetName.Substring(2).Trim())).Count() == 1) ;
  213. //{
  214.  
  215. //}
  216.  
  217.  
  218. //}
  219. //catch (Exception e)
  220. //{ }
  221. }
  222. }
  223. }
  224. }
  225.  
  226. internal static void fullFilldoubleOpt1E(ref List<string[]> table, int streetNameCol, int streetTypeCol, string cityName)
  227. {
  228. RDCEntities context = new RDCEntities();
  229. string streetType;
  230. string streetName;
  231. foreach (string[] line in table)
  232. {
  233.  
  234. streetType = line[streetTypeCol].Trim();
  235. streetName = Utils.indexToFirst(line[streetNameCol].Trim());
  236. line[streetNameCol] = streetName;
  237. int tempTryParse;
  238. if (string.IsNullOrWhiteSpace(streetType) && !string.IsNullOrWhiteSpace(streetName))
  239. {
  240. // try
  241. //{
  242. if (context.KLADRMains.Where(p => p.City == cityName).Where(p => p.StreetName == streetName).Count() == 1)
  243. {
  244. line[streetTypeCol] = (from p in context.KLADRMains where p.City == cityName && p.StreetName == streetName select p).FirstOrDefault().StreetType;
  245. }
  246.  
  247.  
  248. else
  249. {
  250.  
  251. if (!int.TryParse(streetName.Substring(0, 1), out tempTryParse) && streetName.Split().Length == 2) // если двойное название
  252. {
  253. string partOfStreetName = streetName.Split()[1];
  254. var possibleResults = (from p in context.KLADRMains where p.City == cityName && p.StreetName.Contains(partOfStreetName) select p);
  255. foreach (var possibleResult in possibleResults)
  256. {
  257. if (possibleResult.StreetName.Split(' ').Length == 2 && possibleResult.StreetName.Split(' ')[1] == partOfStreetName)
  258. {
  259. line[streetNameCol] = possibleResult.StreetName;
  260. line[streetTypeCol] = possibleResult.StreetType;
  261.  
  262. }
  263.  
  264. }
  265. }
  266.  
  267. if (!int.TryParse(streetName.Substring(0, 1), out tempTryParse) && streetName.Split().Length == 1) // если название из одного слова
  268. {
  269. var possibleResults = (from p in context.KLADRMains where p.City == cityName && p.StreetName.Contains(streetName) select p);
  270. List<string[]> possibleStreets = new List<string[]>();
  271. foreach (var possibleResult in possibleResults)
  272. {
  273. if (possibleResult.StreetName.Split(' ').Length == 2 && possibleResult.StreetName.Split(' ')[1] == streetName)
  274. {
  275. //line[streetNameCol] = possibleResult.StreetName;
  276. //line[streetTypeCol] = possibleResult.StreetType;
  277. possibleStreets.Add(new string[] { possibleResult.StreetName, possibleResult.StreetType });
  278. }
  279.  
  280. }
  281. if (possibleStreets.Count == 1)
  282. {
  283. line[streetNameCol] = possibleStreets[0][0];
  284. line[streetTypeCol] = possibleStreets[0][1];
  285. }
  286.  
  287. }
  288.  
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
Add Comment
Please, Sign In to add comment