Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace KioskLibrary
  6. {
  7. public class coStatistiques
  8. {
  9. private static readonly string _statsFile = "/KioskStats";
  10.  
  11. private static readonly string _filePath =
  12. Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  13.  
  14. /// <summary>
  15. /// Ifs the file exist.
  16. /// </summary>
  17. /// <param name="weekNum">The week number.</param>
  18. /// <returns></returns>
  19. /// =====================================================================================
  20. /// Modification : Initial : 17/04/2018 | N.WILCKE (SESA474351)
  21. /// =====================================================================================
  22. public static bool IfFileExist(int weekNum)
  23. {
  24. try
  25. {
  26. if (File.Exists(_filePath + _statsFile + "." + weekNum + "." + DateTime.Now.Year + ".txt")) return true;
  27. return false;
  28. }
  29. catch
  30. {
  31. return false;
  32. }
  33. }
  34.  
  35. /// <summary>
  36. /// Whats kind of project is it.
  37. /// </summary>
  38. /// <param name="number">The number.</param>
  39. /// <returns></returns>
  40. /// =====================================================================================
  41. /// Modification : Initial : 17/04/2018 | N.WILCKE (SESA474351)
  42. /// =====================================================================================
  43. public static string WhatIsIt(string number)
  44. {
  45. try
  46. {
  47. switch (number.Substring(0, 1))
  48. {
  49. case "4":
  50. return "ODT";
  51.  
  52. case "7":
  53. return "CRT";
  54.  
  55. case "9":
  56. return "CRT";
  57.  
  58. case "8":
  59. return "MPT";
  60. }
  61.  
  62. return "NA";
  63. }
  64. catch
  65. {
  66. return "ERROR";
  67. }
  68. }
  69.  
  70. /// <summary>
  71. /// Writes the stats.
  72. /// </summary>
  73. /// <param name="number">The number.</param>
  74. /// <param name="writtenNumber">The written number. (numero de projet ou d'OF)</param>
  75. /// <param name="IsProject">if set to <c>true</c> [is project].</param>
  76. /// <param name="user">The user.</param>
  77. /// =====================================================================================
  78. /// Modification : Initial : 17/04/2018 | N.WILCKE (SESA474351)
  79. /// =====================================================================================
  80. public static void WriteStats(string number, string writtenNumber, bool IsProject, string user)
  81. {
  82. try
  83. {
  84. // CRT ODT or MPT
  85. var projectType = WhatIsIt(number);
  86. var isProjectText = "";
  87. var weekNum = DateTime.Now.DayOfYear / 7;
  88.  
  89. if (IsProject)
  90. isProjectText = "Projet";
  91. else
  92. isProjectText = "OF";
  93.  
  94. if (IfFileExist(weekNum))
  95. File.AppendAllText(_filePath + _statsFile + "." + weekNum + "." + DateTime.Now.Year + ".txt",
  96. isProjectText + ";" + projectType + ";" + writtenNumber + ";" + weekNum + ";" +
  97. DateTime.Now.ToLongDateString() + ";" + DateTime.Now.ToLongTimeString() + Environment.NewLine);
  98. else
  99. using (var sw =
  100. File.CreateText(_filePath + _statsFile + "." + weekNum + "." + DateTime.Now.Year + ".txt"))
  101. {
  102. sw.WriteLine(isProjectText + ";" + projectType + ";" + writtenNumber + ";" + weekNum + ";" +
  103. DateTime.Now.ToLongDateString() + ";" + DateTime.Now.ToLongTimeString());
  104. }
  105. }
  106. catch
  107. {
  108. }
  109. }
  110.  
  111. /// <summary>
  112. /// Reads the stats.
  113. /// </summary>
  114. /// <param name="weekNum">The week number.</param>
  115. /// <param name="date">The date.</param>
  116. /// <returns></returns>
  117. /// =====================================================================================
  118. /// Modification : Initial : 17/04/2018 | N.WILCKE (SESA474351)
  119. /// =====================================================================================
  120. public static List<string> ReadStats(string weekNum, int date)
  121. {
  122. var stats = new List<string>();
  123. var weekString = weekNum.Split(',');
  124. var test = _filePath + "\\" + weekNum;
  125.  
  126. if (File.Exists(test))
  127. {
  128. var file = File.ReadAllLines(test);
  129. for (var j = 0; j < file.Length; j += 1)
  130. {
  131. var line = file[j];
  132. stats.Add(line);
  133. }
  134.  
  135. return stats;
  136. }
  137.  
  138. return stats;
  139. }
  140.  
  141. public static List<List<string>> ReadAllStats()
  142. {
  143. List<List<string>> allWeeks = new List<List<string>>();
  144.  
  145.  
  146.  
  147. return allWeeks;
  148. }
  149.  
  150. /// <summary>
  151. /// Gets existing weeks.
  152. /// </summary>
  153. /// <returns></returns>
  154. /// =====================================================================================
  155. /// Modification : Initial : 17/04/2018 | N.WILCKE (SESA474351)
  156. /// =====================================================================================
  157. public static Dictionary<int, int> GetWeeks()
  158. {
  159. try
  160. {
  161. var semaines = new Dictionary<int, int>();
  162.  
  163. var firstWeek = 15; // First week of stats
  164. var firstYear = 2018; // First Year of stats
  165. var weekNum = DateTime.Now.DayOfYear / 7; // Current week number
  166.  
  167. while (firstYear < firstYear + 1)
  168. {
  169. while (firstWeek <= weekNum)
  170. {
  171. if (File.Exists(_filePath + _statsFile + "." + firstWeek + "." + firstYear + ".txt"))
  172. semaines.Add(firstWeek, firstYear);
  173. firstWeek++;
  174. if (firstWeek == 52)
  175. {
  176. firstYear++;
  177. firstWeek = 1;
  178. }
  179. }
  180.  
  181. }
  182.  
  183. return semaines;
  184. }
  185. catch
  186. {
  187. var semaines = new Dictionary<int, int>();
  188. return semaines;
  189. }
  190. }
  191.  
  192. public static List<Tuple<int, int, int>> GetAllWeeks()
  193. {
  194. try
  195. {
  196. var semaines = new List<Tuple<int, int, int>>();
  197.  
  198. var firstWeek = 15; // First week of stats
  199. var firstYear = 2018; // First Year of stats
  200. var weekNum = DateTime.Now.DayOfYear / 7; // Current week number
  201.  
  202. while (firstWeek <= weekNum && firstYear < firstYear + 1)
  203. {
  204. if (File.Exists(_filePath + _statsFile + "." + firstWeek + "." + firstYear + ".txt"))
  205. {
  206. int lines = 0;
  207. using (TextReader reader = File.OpenText(_filePath + _statsFile + "." + firstWeek + "." + firstYear + ".txt"))
  208. {
  209. while (reader.ReadLine() != null) { lines++; }
  210. }
  211. semaines.Add(new Tuple<int, int, int>(firstWeek, firstYear, lines));
  212. }
  213. firstWeek++;
  214. if (firstWeek == 52)
  215. {
  216. firstYear++;
  217. firstWeek = 1;
  218. }
  219. }
  220.  
  221. return semaines;
  222. }
  223. catch
  224. {
  225. var semaines = new List<Tuple<int, int, int>>();
  226. return semaines;
  227. }
  228. }
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement