BICommonWay.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using MathNet.Numerics.LinearAlgebra.Double;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace TEAMModelOS.SDK.Models.Service.BI
  6. {
  7. public static class BICommonWay
  8. {
  9. /// <summary>
  10. /// 字串类型分割成double类型数组,在指定位置赋值
  11. /// </summary>
  12. /// <param name="source">需要分割的字符</param>
  13. /// <param name="splitChar">分割字符</param>
  14. /// <param name="indexes">索引</param>
  15. /// <param name="num">需要加减数量</param>
  16. /// <returns></returns>
  17. public static string SplitStr(string source, char splitChar, int indexes, int num)
  18. {
  19. string str = null;
  20. if (source != null)
  21. {
  22. double[] temps = Array.ConvertAll<string, double>(source.Split(splitChar), s => double.Parse(s));
  23. temps[indexes] = temps[indexes] + num;
  24. str = string.Join(splitChar, temps);
  25. }
  26. if (str != null)
  27. return str;
  28. else
  29. return source;
  30. }
  31. /// <summary>
  32. /// 多个拼接in条件
  33. /// </summary>
  34. /// <param name="condName"></param>
  35. /// <param name="scIds"></param>
  36. /// <returns></returns>
  37. public static string ManyScSql(string condName, List<string> scIds, string addStr = null)
  38. {
  39. StringBuilder scSql = new();
  40. if (scIds.Count > 0)
  41. {
  42. scSql.Append($" {condName} in (");
  43. for (int i = 0; i < scIds.Count; i++)
  44. {
  45. if (i == scIds.Count - 1)
  46. {
  47. if (string.IsNullOrEmpty(addStr))
  48. scSql.Append($"'{scIds[i]}'");
  49. else
  50. scSql.Append($"'{addStr}{scIds[i]}'");
  51. }
  52. else
  53. {
  54. if (string.IsNullOrEmpty(addStr))
  55. scSql.Append($"'{scIds[i]}',");
  56. else
  57. scSql.Append($"'{addStr}{scIds[i]}',");
  58. }
  59. }
  60. scSql.Append($" )");
  61. }
  62. return scSql.ToString();
  63. }
  64. /// <summary>
  65. /// 每列合并 多个数组合并成一个数组 列合并
  66. /// </summary>
  67. /// <param name="doubles"></param>
  68. /// <returns></returns>
  69. public static List<double> ManyDoubleMerge(List<List<double>> doubles)
  70. {
  71. List<double> retDou = new();
  72. if (doubles.Count > 0)
  73. {
  74. var artYear = DenseMatrix.OfColumns(doubles);
  75. //var rc = artYear.RowCount;
  76. var cc = artYear.ColumnCount;
  77. for (int i = 0; i < artYear.RowCount; i++)
  78. {
  79. retDou.Add(artYear.SubMatrix(i, 1, 0, cc).ColumnSums().Sum());
  80. }
  81. }
  82. return retDou;
  83. }
  84. /// <summary>
  85. /// 计算一年中每周的数据
  86. /// </summary>
  87. /// <param name="doubles"></param>
  88. /// <param name="dateTime"></param>
  89. /// <returns></returns>
  90. public static List<double> weekDoubleMerge(List<List<double>> doubles, DateTimeOffset dateTime)
  91. {
  92. List<double> weekTrend = new();
  93. if (doubles.Count > 0)
  94. {
  95. int year = dateTime.Year;
  96. int dayOfweek = (int)DateTimeOffset.Parse($"{year}-1-1").DayOfWeek;
  97. int currentTime = dateTime.DayOfYear / 7 + 1;
  98. int currentTime1 = dateTime.DayOfYear / 7;
  99. int days = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? days = 366 : days = 365;
  100. var bmatrix = DenseMatrix.OfColumns(doubles);
  101. //开学第一周周内开课
  102. if (dayOfweek == 0)
  103. {
  104. dayOfweek = 7;
  105. }
  106. //第一周多少天
  107. var dd = 7 - dayOfweek + 1;
  108. //一年有几周
  109. int sweeks = days / 7;
  110. //查询天数
  111. int dayYear = 0;
  112. if (currentTime > 0)
  113. {
  114. for (int i = 0; i < currentTime; i++)
  115. {
  116. if (i == 0)
  117. {
  118. var bsum = bmatrix.SubMatrix(dayYear, dd, 0, bmatrix.ColumnCount).ColumnSums().Sum();
  119. dayYear += dd;
  120. //weeks.Add(i, bsum);
  121. weekTrend.Add(bsum);
  122. }
  123. else
  124. {
  125. var bsum = bmatrix.SubMatrix(dayYear, 7, 0, bmatrix.ColumnCount).ColumnSums().Sum();
  126. dayYear += 7;
  127. //weeks.Add(i, bsum);
  128. weekTrend.Add(bsum);
  129. }
  130. }
  131. }
  132. //最后一周是否有余
  133. int stary = days - dayYear;
  134. if (stary > 0 && stary < 7)
  135. {
  136. var bsum = bmatrix.SubMatrix(dayYear, stary - 1, 0, bmatrix.ColumnCount).ColumnSums().Sum();
  137. //weeks.Add((sweeks + 1), bsum);
  138. weekTrend.Add(bsum);
  139. }
  140. }
  141. return weekTrend;
  142. }
  143. }
  144. }