Merge pull request '修改票务服务节假日数据和历年数据接口' (#103) from cgt into master
Reviewed-on: #103
This commit is contained in:
commit
7a1e03a8cd
@ -142,7 +142,6 @@ public class SaleDataApi {
|
||||
@GetMapping("/thisyear/nianjunCount")
|
||||
@Operation(summary = "查询某年年度售票数量")
|
||||
public Integer findCountthisyear(Integer year) {
|
||||
System.out.println(saleDataService.findEventsByYear(year));
|
||||
return saleDataService.findEventsByYear(year).size();
|
||||
}
|
||||
|
||||
@ -157,9 +156,8 @@ public class SaleDataApi {
|
||||
|
||||
@GetMapping("/holidayCount")
|
||||
@Operation(summary = "查询节假日售票数量")
|
||||
public CommonResult<HolidayTree> selectHolidayCount(String year) {
|
||||
Integer currentYear = Integer.parseInt(year);
|
||||
return saleDataService.selectHolidayCount(currentYear);
|
||||
public CommonResult<HolidayTree> selectHolidayCount(String holidayName) {
|
||||
return saleDataService.selectHolidayCount(holidayName);
|
||||
}
|
||||
// @GetMapping("/test")
|
||||
// public void testMethod(){
|
||||
@ -167,4 +165,9 @@ public class SaleDataApi {
|
||||
// webSocketSenderApi.sendObject("1","1","666");
|
||||
// }
|
||||
|
||||
@GetMapping("/yearsSaleCount")
|
||||
@Operation(summary = "查询历年售票数量")
|
||||
public CommonResult<Map<String, Integer>> selectYearsSaleCount() {
|
||||
return saleDataService.selectYearsSaleCount();
|
||||
}
|
||||
}
|
||||
|
@ -4,58 +4,38 @@ import java.util.*;
|
||||
|
||||
public class HolidayTree {
|
||||
|
||||
// 第一层: 年份 -> 节假日数据
|
||||
// 第一层: 年份 -> 前五日数据
|
||||
private Map<String, YearNode> yearMap = new HashMap<>();
|
||||
|
||||
public void addHolidayData(String year, String holidayName, Integer day, Integer count) {
|
||||
YearNode yearNode = yearMap.computeIfAbsent(year, k -> new YearNode(year));
|
||||
yearNode.addHolidayData(holidayName, day, count);
|
||||
// 添加年份数据
|
||||
public void addHolidayData(String year, Integer day, Integer count) {
|
||||
YearNode yearNode = yearMap.computeIfAbsent(year, k -> new YearNode());
|
||||
yearNode.addDayData(day, count);
|
||||
}
|
||||
|
||||
// 第一层节点: 年份
|
||||
static class YearNode {
|
||||
private String year;
|
||||
private Map<String, HolidayNode> holidayMap = new HashMap<>();
|
||||
// private String year;
|
||||
private Map<Integer, Integer> dayDataMap = new HashMap<>(); // 第几天 -> 数量
|
||||
|
||||
public YearNode(String year) {
|
||||
this.year = year;
|
||||
// public YearNode(String year) {
|
||||
// this.year = year;
|
||||
// }
|
||||
|
||||
// 添加某一天的数据
|
||||
public void addDayData(Integer day, Integer count) {
|
||||
// 只记录前五天的数据
|
||||
if (day >= 1 && day <= 5) {
|
||||
dayDataMap.put(day, count);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加节假日数据
|
||||
public void addHolidayData(String holidayName, Integer day, Integer count) {
|
||||
HolidayNode holidayNode = holidayMap.computeIfAbsent(holidayName, k -> new HolidayNode(holidayName));
|
||||
holidayNode.addHolidayDay(day, count);
|
||||
}
|
||||
// public String getYear() {
|
||||
// return year;
|
||||
// }
|
||||
|
||||
public String getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public Map<String, HolidayNode> getHolidayMap() {
|
||||
return holidayMap;
|
||||
}
|
||||
}
|
||||
|
||||
// 第二层节点: 节假日
|
||||
static class HolidayNode {
|
||||
private String holidayName;
|
||||
private Map<Integer, Integer> holidayDays = new HashMap<>(); // 第几天 -> 数量
|
||||
|
||||
public HolidayNode(String holidayName) {
|
||||
this.holidayName = holidayName;
|
||||
}
|
||||
|
||||
// 添加节假日的具体天数与数量
|
||||
public void addHolidayDay(Integer day, Integer count) {
|
||||
holidayDays.put(day, count);
|
||||
}
|
||||
|
||||
public String getHolidayName() {
|
||||
return holidayName;
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> getHolidayDays() {
|
||||
return holidayDays;
|
||||
public Map<Integer, Integer> getDayDataMap() {
|
||||
return dayDataMap;
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,24 +45,22 @@ public class HolidayTree {
|
||||
}
|
||||
|
||||
// 测试打印树形数据
|
||||
// public static void main(String[] args) {
|
||||
// HolidayTree tree = new HolidayTree();
|
||||
//
|
||||
// // 添加一些示例数据
|
||||
// tree.addHolidayData("2023", "New Year", 1, 100);
|
||||
// tree.addHolidayData("2023", "New Year", 2, 120);
|
||||
// tree.addHolidayData("2023", "Spring Festival", 1, 200);
|
||||
// tree.addHolidayData("2024", "New Year", 1, 150);
|
||||
//
|
||||
// // 打印树形数据
|
||||
// tree.getYearMap().forEach((year, yearNode) -> {
|
||||
// System.out.println("Year: " + year);
|
||||
// yearNode.getHolidayMap().forEach((holiday, holidayNode) -> {
|
||||
// System.out.println(" Holiday: " + holiday);
|
||||
// holidayNode.getHolidayDays().forEach((day, count) -> {
|
||||
// System.out.println(" Day " + day + ": " + count);
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
public static void main(String[] args) {
|
||||
HolidayTree tree = new HolidayTree();
|
||||
|
||||
// 添加一些示例数据
|
||||
tree.addHolidayData("2023", 1, 100);
|
||||
tree.addHolidayData("2023", 2, 120);
|
||||
tree.addHolidayData("2023", 6, 200); // 这条数据不会被记录
|
||||
tree.addHolidayData("2024", 1, 150);
|
||||
tree.addHolidayData("2024", 3, 130);
|
||||
|
||||
// 打印树形数据
|
||||
tree.getYearMap().forEach((year, yearNode) -> {
|
||||
System.out.println("Year: " + year);
|
||||
yearNode.getDayDataMap().forEach((day, count) -> {
|
||||
System.out.println(" Day " + day + ": " + count);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -103,5 +103,7 @@ public interface SaleDataService {
|
||||
|
||||
Integer selectSaleCountByDate(String dateParam);
|
||||
|
||||
CommonResult<HolidayTree> selectHolidayCount(Integer date);
|
||||
CommonResult<HolidayTree> selectHolidayCount(String holidayName);
|
||||
|
||||
CommonResult<Map<String, Integer>> selectYearsSaleCount();
|
||||
}
|
@ -453,58 +453,105 @@ public class SaleDataServiceImpl implements SaleDataService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<HolidayTree> selectHolidayCount(Integer currentYear) {
|
||||
public CommonResult<HolidayTree> selectHolidayCount(String holidayName) {
|
||||
HolidayTree holidayTree = new HolidayTree();
|
||||
|
||||
// 获取当前年份及前两年
|
||||
// int startYear = currentYear - 2;
|
||||
// int endYear = currentYear;
|
||||
// 获取当前年份及前两年
|
||||
|
||||
int currentYear = LocalDate.now().getYear();
|
||||
int startYear = currentYear - 2;
|
||||
int endYear = currentYear;
|
||||
|
||||
// 节假日名称列表
|
||||
List<String> holidays = Arrays.asList("五一", "国庆", "春节");
|
||||
// List<String> holidays = Arrays.asList("五一", "国庆", "春节");
|
||||
|
||||
// 遍历每一年
|
||||
// for (int year = startYear; year <= endYear; year++) {
|
||||
List<String> holidayDates = getHolidayDates(holidayName, startYear, endYear); // 获取每年的该节假日的五天日期
|
||||
// for (int i = 0; i < 3; i++) {
|
||||
// 获取每个节假日的前五天数据
|
||||
for (String holiday : holidays) {
|
||||
List<String> holidayDates = getHolidayDates(holiday, currentYear); // 获取节假日的五天日期
|
||||
// for (String holiday : holidays) {
|
||||
// System.out.println(holidayDates);
|
||||
for (String date : holidayDates) {
|
||||
// 查询每个日期的数量
|
||||
System.out.println(date);
|
||||
int count = saleDataRepository.countBySddate(date);
|
||||
holidayTree.addHolidayData(String.valueOf(currentYear), holiday, Integer.parseInt(date.substring(6)), count);
|
||||
System.out.println(count);
|
||||
holidayTree.addHolidayData(date.substring(0,4), Integer.parseInt(date.substring(6)), count);
|
||||
}
|
||||
}
|
||||
// }
|
||||
// }
|
||||
return CommonResult.success(holidayTree);
|
||||
}
|
||||
|
||||
// 根据节假日和年份获取节假日及其五天的数据(从节假日当天开始的五天)
|
||||
private List<String> getHolidayDates(String holiday, int year) {
|
||||
|
||||
// private List<String> getHolidayDates(String holiday, int year) {
|
||||
// List<String> dates = new ArrayList<>();
|
||||
//
|
||||
// if (holiday.equals("五一")) {
|
||||
// // 五一:5月1日到5月5日
|
||||
// for (int i = 0; i < 5; i++) {
|
||||
// String date = String.format("%04d05%02d", year, 1 + i); // 构建 20230501 格式的日期
|
||||
// dates.add(date);
|
||||
// }
|
||||
// } else if (holiday.equals("国庆")) {
|
||||
// // 国庆:10月1日到10月5日
|
||||
// for (int i = 0; i < 5; i++) {
|
||||
// String date = String.format("%04d10%02d", year, 1 + i); // 构建 20231001 格式的日期
|
||||
// dates.add(date);
|
||||
// }
|
||||
// } else if (holiday.equals("春节")) {
|
||||
// // 春节假设固定日期2月1日-2月5日
|
||||
// for (int i = 0; i < 5; i++) {
|
||||
// String date = String.format("%04d02%02d", year, 1 + i); // 构建 20230201 格式的日期
|
||||
// dates.add(date);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// return dates;
|
||||
// }
|
||||
|
||||
// 根据节假日和起止年份获取年份及其五天的数据(从节假日当天开始的五天)
|
||||
|
||||
private List<String> getHolidayDates(String holiday, int startYear, int endYear) {
|
||||
List<String> dates = new ArrayList<>();
|
||||
|
||||
if (holiday.equals("五一")) {
|
||||
// 五一:5月1日到5月5日
|
||||
for (int i = 0; i < 5; i++) {
|
||||
String date = String.format("%04d05%02d", year, 1 + i); // 构建 20230501 格式的日期
|
||||
dates.add(date);
|
||||
// 遍历指定年份范围
|
||||
for (int year = startYear; year <= endYear; year++) {
|
||||
if (holiday.equals("五一")) {
|
||||
// 五一:5月1日到5月5日
|
||||
for (int i = 0; i < 5; i++) {
|
||||
String date = String.format("%04d05%02d", year, 1 + i); // 构建 20230501 格式的日期
|
||||
dates.add(date);
|
||||
}
|
||||
} else if (holiday.equals("国庆")) {
|
||||
// 国庆:10月1日到10月5日
|
||||
for (int i = 0; i < 5; i++) {
|
||||
String date = String.format("%04d10%02d", year, 1 + i); // 构建 20231001 格式的日期
|
||||
dates.add(date);
|
||||
}
|
||||
} else if (holiday.equals("春节")) {
|
||||
// 春节假设固定日期2月1日-2月5日
|
||||
for (int i = 0; i < 5; i++) {
|
||||
String date = String.format("%04d02%02d", year, 1 + i); // 构建 20230201 格式的日期
|
||||
dates.add(date);
|
||||
}
|
||||
}
|
||||
} else if (holiday.equals("国庆")) {
|
||||
// 国庆:10月1日到10月5日
|
||||
for (int i = 0; i < 5; i++) {
|
||||
String date = String.format("%04d10%02d", year, 1 + i); // 构建 20231001 格式的日期
|
||||
dates.add(date);
|
||||
}
|
||||
} else if (holiday.equals("春节")) {
|
||||
// 春节假设固定日期2月1日-2月5日
|
||||
for (int i = 0; i < 5; i++) {
|
||||
String date = String.format("%04d02%02d", year, 1 + i); // 构建 20230201 格式的日期
|
||||
dates.add(date);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return dates;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CommonResult<Map<String, Integer>> selectYearsSaleCount() {
|
||||
// 查询23年开始往后十年的数据
|
||||
Map<String, Integer> result = new HashMap<>();
|
||||
for (int i = 2023; i <= 2033; i++) {
|
||||
int count = saleDataRepository.countBySddateToTime(i + "0101", i + "1231").size();
|
||||
result.put(String.valueOf(i), count);
|
||||
}
|
||||
return CommonResult.success(result);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user