Analysis.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace UAS_LabelMachine.PublicMethod
  6. {
  7. class Analysis
  8. {
  9. /// <summary>
  10. /// Brand对应品牌,item对应DateCode,LotNo等,Data对应具体解析的数据
  11. /// </summary>
  12. /// <param name="Brand"></param>
  13. /// <param name="item"></param>
  14. /// <param name="Data"></param>
  15. /// <returns></returns>
  16. //Brand对应品牌,item对应DateCode,LotNo等,Data对应具体解析的数据
  17. public static string AnalysisData(string Brand, string item, string Data)
  18. {
  19. string ReturnData = "";
  20. switch (Brand)
  21. {
  22. //采集怡海能达村田的LotNo的解析规则
  23. case "MURATA":
  24. if (item.ToUpper() == "LOTNO" || item.ToUpper() == "DATECODE")
  25. {
  26. string Year = Data.Substring(2, 1);
  27. string Month = Data.Substring(3, 1);
  28. string Day = Data.Substring(4, 2);
  29. //处理年份
  30. //当前年份的前三位
  31. string CurrentYear_3 = DateTime.Now.Year.ToString().Substring(0, 3);
  32. //采集到的年份
  33. int CollectYear = int.Parse(CurrentYear_3 + Year);
  34. //当前的年份
  35. int CurrentYear = int.Parse(DateTime.Now.Year.ToString());
  36. //如果采集到的年份大于当前年份,将十年数-1
  37. if (CollectYear > CurrentYear)
  38. {
  39. Year = int.Parse(CurrentYear_3) - 1 + Year;
  40. }
  41. else
  42. {
  43. Year = CollectYear.ToString();
  44. }
  45. //处理月份
  46. switch (Month)
  47. {
  48. case "O":
  49. Month = "10";
  50. break;
  51. case "N":
  52. Month = "11";
  53. break;
  54. case "D":
  55. Month = "12";
  56. break;
  57. default:
  58. Month = "0" + Month;
  59. break;
  60. }
  61. ReturnData = Year + Month + Day;
  62. }
  63. break;
  64. case "KEC":
  65. if (item.ToUpper() == "LOTNO" || item.ToUpper() == "DATECODE")
  66. {
  67. string Year = Data.Substring(0, 1);
  68. string Week = Data.Substring(1, 2);
  69. DateTime DateBegin = DateTime.Now;
  70. GetDaysOfWeeks(int.Parse("202" + Year), int.Parse(Week), out DateBegin);
  71. ReturnData = DateBegin.ToString("yyyyMMdd");
  72. }
  73. break;
  74. default:
  75. break;
  76. }
  77. return ReturnData;
  78. }
  79. public static bool GetDaysOfWeeks(int year, int index, out DateTime first)
  80. {
  81. first = DateTime.MinValue;
  82. if (year < 1700 || year > 9999)
  83. {
  84. //"年份超限"
  85. return false;
  86. }
  87. if (index < 1 || index > 53)
  88. {
  89. //"周数错误"
  90. return false;
  91. }
  92. DateTime startDay = new DateTime(year, 1, 1); //该年第一天
  93. DateTime endDay = new DateTime(year + 1, 1, 1).AddMilliseconds(-1);
  94. int dayOfWeek = 0;
  95. if (Convert.ToInt32(startDay.DayOfWeek.ToString("d")) > 0)
  96. dayOfWeek = Convert.ToInt32(startDay.DayOfWeek.ToString("d")); //该年第一天为星期几
  97. if (dayOfWeek == 7) { dayOfWeek = 0; }
  98. if (index == 1)
  99. {
  100. first = startDay;
  101. }
  102. else
  103. {
  104. first = startDay.AddDays((7 - dayOfWeek) + (index - 2) * 7); //index周的起始日期
  105. }
  106. if (first > endDay) //startDayOfWeeks不在该年范围内
  107. {
  108. //"输入周数大于本年最大周数";
  109. return false;
  110. }
  111. return true;
  112. }
  113. }
  114. }