HighQualityCountService.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.nokia.tsl_data.service;
  2. import com.nokia.tsl_data.dao.HighQualityCountMapper;
  3. import com.nokia.tsl_data.dao.SysDataDictionaryRepository;
  4. import org.springframework.stereotype.Service;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. /**
  9. * 处理客户端统计数据
  10. */
  11. @Service
  12. public class HighQualityCountService {
  13. private final HighQualityCountMapper highQualityCountDao;
  14. private final SysDataDictionaryRepository sysDataDictionaryRepository;
  15. public HighQualityCountService(HighQualityCountMapper highQualityCountDao, SysDataDictionaryRepository sysDataDictionaryRepository) {
  16. this.highQualityCountDao = highQualityCountDao;
  17. this.sysDataDictionaryRepository = sysDataDictionaryRepository;
  18. }
  19. /**
  20. * 根据日期查询当月从1日起每天每个地市的客户端统计数据
  21. */
  22. public Map<String, int[]> getComplaintsForDay(String day) {
  23. // 预处理时间
  24. String monthId = day.substring(0, 6);
  25. int dayIndex = Integer.parseInt(day.substring(6));
  26. List<String> allCityName = sysDataDictionaryRepository.findAllCityName();
  27. Map<String, int[]> result = new HashMap<>();
  28. // 每个地市一个数组,数组保存dayIndex天数据
  29. for (String areaName : allCityName) {
  30. result.put(areaName, new int[dayIndex + 1]);
  31. }
  32. // 全省
  33. result.put("全省", new int[dayIndex + 1]);
  34. for (int i = 1; i <= dayIndex; i++) {
  35. // 拼接一个表示天的字符串,天如果小于10要补0
  36. String newDay = i >= 10 ? monthId + i : monthId + "0" + i;
  37. // 这里有一个查询操作
  38. List<Map<String, Object>> totalComplaintsForDay = highQualityCountDao
  39. .selectTotalComplaintsForDay(newDay);
  40. int provinceAll = 0;
  41. for (Map<String, Object> map : totalComplaintsForDay) {
  42. String city = map.get("businoareaname").toString();
  43. // 这里雄安新区需要特殊处理一下
  44. city = city.equals("雄安新区") ? "雄安" : city;
  45. int[] arr = result.get(city);
  46. arr[i - 1] = Integer.parseInt(map.get("total_complaints").toString());
  47. provinceAll += arr[i - 1];
  48. if (i == dayIndex) {
  49. arr[dayIndex] = arr[dayIndex - 1];
  50. }
  51. }
  52. result.get("全省")[i - 1] = provinceAll;
  53. if (i == dayIndex) {
  54. result.get("全省")[dayIndex] = provinceAll;
  55. }
  56. }
  57. // 以上得到的result是每天累加,需要计算一下每天的增量
  58. for (int[] arr : result.values()) {
  59. for (int i = dayIndex - 1; i >= 1; i--) {
  60. arr[i] = arr[i] - arr[i - 1];
  61. }
  62. }
  63. return result;
  64. }
  65. }