TimeConverterUtil.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.database2.server;
  2. import com.database2.dao.ShowChartsMapper;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. /*
  9. 处理时间分段
  10. */
  11. @Service
  12. public class TimeConverterUtil {
  13. @Autowired
  14. ShowChartsMapper showChartsMapper;
  15. //x轴时间类型
  16. public List<String> timeConverter(String xColumnName, String tableName, String timeType){
  17. String timeGroup = "'YYYY-MM-DD'";
  18. List<String> value = new ArrayList<String>();
  19. if ("year".equals(timeType)){
  20. timeGroup = "'YYYY'";
  21. value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup);
  22. }else if ("month".equals(timeType)){
  23. timeGroup = "'YYYY-MM'";
  24. value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup);
  25. }else if ("day".equals(timeType) || "".equals(timeType)){
  26. value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup);
  27. }else if ("quarter".equals(timeType)){
  28. timeGroup = "'YYYY-Q'";
  29. value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup);
  30. }else if ("week".equals(timeType)){
  31. timeGroup = "'YYYY-WW'";
  32. value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup);
  33. }else if ("halfYear".equals(timeType)){
  34. value = showChartsMapper.getTimeYear(xColumnName, tableName);
  35. }else {
  36. return null;
  37. }
  38. return value;
  39. }
  40. //x轴时间类型包装
  41. public List<String> toRespons(List<String> dataList, String timeType){
  42. List<String> value = new ArrayList<String>();
  43. String dataLast = null;
  44. Iterator isDataList = dataList.iterator();
  45. while (isDataList.hasNext()){
  46. String data = (String) isDataList.next();
  47. if ("quarter".equals(timeType)){
  48. String[] str = data.split("-");
  49. dataLast = str[0] + "-Q" + str[1];
  50. }else if ("month".equals(timeType)){
  51. String[] str = data.split("-");
  52. dataLast = str[0] + "-M" + str[1];
  53. }else if ("week".equals(timeType)){
  54. String[] str = data.split("-");
  55. dataLast = str[0] + "-W" + str[1];
  56. }
  57. value.add(dataLast);
  58. }
  59. return value;
  60. }
  61. //无分组时间值
  62. public String getTimeValueConverter(String yColumn, String xColumn, String tableName, String dataType, String timeType, String xdata){
  63. String timeGroup = "'YYYY-MM-DD'";
  64. String value = null;
  65. //判断时间类型
  66. if ("halfYear".equals(timeType)){
  67. String[] str = xdata.split("-");
  68. if ("上半年".equals(str[1])){
  69. String firstIndex = str[0] + "-01";
  70. String afterIndex = str[0] + "-06";
  71. value = showChartsMapper.getTimeValueYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex);
  72. }else {
  73. String firstIndex = str[0] + "-07";
  74. String afterIndex = str[0] + "-12";
  75. value = showChartsMapper.getTimeValueYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex);
  76. }
  77. }else if ("year".equals(timeType)){
  78. timeGroup = "'YYYY'";
  79. value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup,xdata);
  80. }else if ("month".equals(timeType)){
  81. timeGroup = "'YYYY-MM'";
  82. value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup,xdata);
  83. }else if ("day".equals(timeType) || "".equals(timeType)){
  84. value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup,xdata);
  85. }else if ("quarter".equals(timeType)){
  86. timeGroup = "'YYYY-Q'";
  87. value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup,xdata);
  88. }else if ("week".equals(timeType)){
  89. timeGroup = "'YYYY-WW'";
  90. value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup,xdata);
  91. }
  92. return value;
  93. }
  94. //有分组时间类型值处理
  95. public String getGroupTimeConverter(String dataType, String yColumn, String tableName, String groupByName, String timeType,
  96. String groupsName, String xColumn, String xAxisDataOne){
  97. String timeGroup = "'YYYY-MMDD'";
  98. String value = null;
  99. if ("year".equals(timeType)){
  100. timeGroup = "'YYYY'";
  101. value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
  102. timeGroup, xAxisDataOne);
  103. }else if ("month".equals(timeType)){
  104. timeGroup = "'YYYY-MM'";
  105. value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
  106. timeGroup, xAxisDataOne);
  107. }else if ("quarter".equals(timeType)){
  108. timeGroup = "'YYYY-Q'";
  109. value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
  110. timeGroup, xAxisDataOne);
  111. }else if ("week".equals(timeType)){
  112. timeGroup = "'YYYY-WW'";
  113. value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
  114. timeGroup, xAxisDataOne);
  115. }else if ("day".equals(timeType) || "".equals(timeType)){
  116. value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
  117. timeGroup, xAxisDataOne);
  118. }else if ("halfYear".equals(timeType)){
  119. String[] str = xAxisDataOne.split("-");
  120. if ("上半年".equals(str[1])){
  121. String firstIndex = str[0] + "-01";
  122. String afterIndex = str[0] + "-06";
  123. value = showChartsMapper.getTimeValueHalfYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, groupByName, groupsName);
  124. }else {
  125. String firstIndex = str[0] + "-07";
  126. String afterIndex = str[0] + "-12";
  127. value = showChartsMapper.getTimeValueHalfYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, groupByName, groupsName);
  128. }
  129. }
  130. return value;
  131. }
  132. }