大屏接口请求效率优化,票务接口改造 #42

Merged
root merged 2 commits from zzw-one into master 2024-08-26 13:03:16 +08:00
4 changed files with 131 additions and 24 deletions

View File

@ -8,6 +8,9 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -51,14 +54,26 @@ public class SaleDataApi {
@GetMapping("/gender") @GetMapping("/gender")
@Operation(summary = "获得所有数据的男女数量") @Operation(summary = "获得所有数据的男女数量")
public List<Map<String, String>> findByGender() { public List<Map<String, String>> findByGender(String starTime, String endTime) {
return saleDataService.findByGender(); if (starTime != null && endTime != null){
String replace = starTime.replace("-", "");
String replace1 = endTime.replace("-", "");
return saleDataService.findByGender(replace, replace1);
}else{
return saleDataService.findByGender(starTime, endTime);
}
} }
@GetMapping("/age") @GetMapping("/age")
@Operation(summary = "获得所有数据的年龄分段和数量") @Operation(summary = "获得所有数据的年龄分段和数量")
public List<Map<String, String>> findByage() { public List<Map<String, String>> findByage(String starTime, String endTime) {
return saleDataService.findByAge(); if (starTime != null && endTime != null){
String replace = starTime.replace("-", "");
String replace1 = endTime.replace("-", "");
return saleDataService.findByAge(replace, replace1);
}else{
return saleDataService.findByAge(starTime, endTime);
}
} }
@GetMapping("/lastyear/region") @GetMapping("/lastyear/region")
@ -75,14 +90,26 @@ public class SaleDataApi {
@GetMapping("/salemethod") @GetMapping("/salemethod")
@Operation(summary = "查询各个销售渠道的购票人数") @Operation(summary = "查询各个销售渠道的购票人数")
public List<Map<String, String>> findBysaleMethod() { public List<Map<String, String>> findBysaleMethod(String starTime, String endTime) {
return saleDataService.findBySaleMethod(); if (starTime != null && endTime != null){
String replace = starTime.replace("-", "");
String replace1 = endTime.replace("-", "");
return saleDataService.findBySaleMethod(replace, replace1);
}else{
return saleDataService.findBySaleMethod(starTime, endTime);
}
} }
@GetMapping("/itemType") @GetMapping("/itemType")
@Operation(summary = "查询各个产品类型的购票人数") @Operation(summary = "查询各个产品类型的购票人数")
public List<Map<String, String>> findByItemType() { public List<Map<String, String>> findByItemType(String starTime, String endTime) {
return saleDataService.findByDoItemType(); if (starTime != null && endTime != null){
String replace = starTime.replace("-", "");
String replace1 = endTime.replace("-", "");
return saleDataService.findByDoItemType(replace, replace1);
}else{
return saleDataService.findByDoItemType(starTime, endTime);
}
} }
@GetMapping("/wuyi/{x}") @GetMapping("/wuyi/{x}")

View File

@ -10,6 +10,7 @@ import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query; import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@Repository @Repository
public interface SaleDataRepository extends MongoRepository<SaleData,String> { public interface SaleDataRepository extends MongoRepository<SaleData,String> {
@ -30,6 +31,15 @@ public interface SaleDataRepository extends MongoRepository<SaleData,String> {
AggregationVO findAllByCertificateno(); AggregationVO findAllByCertificateno();
@Aggregation(pipeline = { @Aggregation(pipeline = {
"{$match: {sddate: { $gte: ?0, $lte: ?1 }}}",
"{$project: {certLength: {$strLenCP: '$certificateno'}, secondLastChar: {$substrCP: ['$certificateno', {$subtract: [{$strLenCP: '$certificateno'}, 2]}, 1]}}}",
"{$group: {_id: null, maleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 1]}, then: 1, else: 0}}}, femaleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 0]}, then: 1, else: 0}}}, totalCount: {$sum: 1}}}",
"{$project: {_id: 0, maleCount: 1, femaleCount: 1, maleRatio: {$divide: ['$maleCount', '$totalCount']}}}"
})
AggregationVO findAllByCertificatenoSDTime(String starTime, String endTime);
@Aggregation(pipeline = {
"{$match: {sddate: { $gte: ?0, $lte: ?1 }}}",
"{$match: {certificateno: {$regex: '^.{18}$'}}}", "{$match: {certificateno: {$regex: '^.{18}$'}}}",
"{$project: {birthYear: {$toInt: {$substr: ['$certificateno', 6, 4]}}, currentYear: {$year: '$$NOW'}}}", "{$project: {birthYear: {$toInt: {$substr: ['$certificateno', 6, 4]}}, currentYear: {$year: '$$NOW'}}}",
"{$addFields: {age: {$subtract: ['$currentYear', '$birthYear']}}}", "{$addFields: {age: {$subtract: ['$currentYear', '$birthYear']}}}",
@ -40,9 +50,15 @@ public interface SaleDataRepository extends MongoRepository<SaleData,String> {
"five: {$sum: {$cond: [{$and: [{$gt: ['$age', 50]}, {$lte: ['$age', 60]}]}, 1, 0]}}, " + "five: {$sum: {$cond: [{$and: [{$gt: ['$age', 50]}, {$lte: ['$age', 60]}]}, 1, 0]}}, " +
"six: {$sum: {$cond: [{$gt: ['$age', 60]}, 1, 0]}}}}" "six: {$sum: {$cond: [{$gt: ['$age', 60]}, 1, 0]}}}}"
}) })
AgeVo findByAge(); AgeVo findByAge(String starTime, String endTime);
@Query(value = "{}", fields = "{ 'itemtypename': 1 }") @Query(value = "{}", fields = "{ 'itemtypename': 1 }")
List<SaleDataItemTypeVo> findAllByItemtypename(); List<SaleDataItemTypeVo> findAllByItemtypename();
@Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'itemtypename': 1 }")
List<SaleDataItemTypeVo> findAllByItemtypenameTime(String starTime, String endTime);
@Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'transactiontypeno': 1 }")
List<String> findAllByTransactiontypenoTime(String starTime, String endTime);
} }

View File

@ -31,13 +31,13 @@ public interface SaleDataService {
* 查询男女人数 * 查询男女人数
* @return 男女人数 * @return 男女人数
*/ */
public List<Map<String, String>> findByGender(); public List<Map<String, String>> findByGender(String starTime, String endTime);
/** /**
* 查询各个年龄段的人数 * 查询各个年龄段的人数
* @return 各个年龄段的人数 * @return 各个年龄段的人数
*/ */
public List<Map<String, String>> findByAge(); public List<Map<String, String>> findByAge(String starTime, String endTime);
/** /**
* 查询去年1月1日到12月31日的全部数据 * 查询去年1月1日到12月31日的全部数据
@ -63,13 +63,13 @@ public interface SaleDataService {
* 查询销售渠道 * 查询销售渠道
* @return * @return
*/ */
public List<Map<String, String>> findBySaleMethod(); public List<Map<String, String>> findBySaleMethod(String starTime, String endTime);
/** /**
* 查询各个产品类型的购票人数 * 查询各个产品类型的购票人数
* @return * @return
*/ */
public List<Map<String, String>> findByDoItemType(); public List<Map<String, String>> findByDoItemType(String starTime, String endTime);
/** /**
* 查询最近前x年五一期间的数据 * 查询最近前x年五一期间的数据

View File

@ -16,6 +16,8 @@ import java.math.BigDecimal;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -62,8 +64,20 @@ public class SaleDataServiceImpl implements SaleDataService {
} }
@Override @Override
public List<Map<String, String>> findByGender() { public List<Map<String, String>> findByGender(String starTime, String endTime) {
AggregationVO allByCertificateno = saleDataRepository.findAllByCertificateno(); AggregationVO allByCertificateno;
if (starTime == null || endTime == null){
// 获取当前日期
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = currentDate.format(formatter);
// 将当前日期加一天
LocalDate nextDay = currentDate.plusDays(1);
String nextDayFormatter = nextDay.format(formatter);
allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(formattedDate, nextDayFormatter);
}else {
allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(starTime, endTime);
}
List<Map<String, String>> map = new ArrayList<>(); List<Map<String, String>> map = new ArrayList<>();
Map<String,String>map1=new HashMap<>(); Map<String,String>map1=new HashMap<>();
Map<String,String>map2=new HashMap<>(); Map<String,String>map2=new HashMap<>();
@ -77,8 +91,20 @@ public class SaleDataServiceImpl implements SaleDataService {
} }
@Override @Override
public List<Map<String, String>> findByAge() { public List<Map<String, String>> findByAge(String starTime, String endTime) {
AgeVo byAge = saleDataRepository.findByAge(); AgeVo byAge;
if (starTime == null || endTime == null){
// 获取当前日期
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = currentDate.format(formatter);
// 将当前日期加一天
LocalDate nextDay = currentDate.plusDays(1);
String nextDayFormatter = nextDay.format(formatter);
byAge = saleDataRepository.findByAge(formattedDate,nextDayFormatter);
}else {
byAge = saleDataRepository.findByAge(starTime,endTime);
}
List<Map<String,String>>map=new ArrayList<>(); List<Map<String,String>>map=new ArrayList<>();
Map<String,String>map1=new LinkedHashMap<>(); Map<String,String>map1=new LinkedHashMap<>();
Map<String,String>map2=new LinkedHashMap<>(); Map<String,String>map2=new LinkedHashMap<>();
@ -171,13 +197,25 @@ public class SaleDataServiceImpl implements SaleDataService {
@Override @Override
public List<Map<String, String>> findBySaleMethod() { public List<Map<String, String>> findBySaleMethod(String starTime, String endTime) {
List<SaleData> saleDataList = saleDataRepository.findAll(); List<String> saleDataList;
if (starTime == null || endTime == null){
// 获取当前日期
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = currentDate.format(formatter);
// 将当前日期加一天
LocalDate nextDay = currentDate.plusDays(1);
String nextDayFormatter = nextDay.format(formatter);
saleDataList = saleDataRepository.findAllByTransactiontypenoTime(formattedDate, nextDayFormatter);
}else {
saleDataList = saleDataRepository.findAllByTransactiontypenoTime(starTime, endTime);
}
int[] saleMethodCounts = new int[5]; int[] saleMethodCounts = new int[5];
Map<String,String>map=new LinkedHashMap<>(); Map<String,String>map=new LinkedHashMap<>();
for (SaleData saleData : saleDataList) { for (String saleData : saleDataList) {
String transactiontypeno = saleData.getTransactiontypeno(); // String transactiontypeno = saleData.getTransactiontypeno();
switch (transactiontypeno){ switch (saleData){
case "SD01": case "SD01":
saleMethodCounts[0]++; saleMethodCounts[0]++;
break; break;
@ -213,8 +251,20 @@ public class SaleDataServiceImpl implements SaleDataService {
} }
@Override @Override
public List<Map<String, String>> findByDoItemType() { public List<Map<String, String>> findByDoItemType(String starTime, String endTime) {
List<SaleDataItemTypeVo> list = saleDataRepository.findAllByItemtypename(); List<SaleDataItemTypeVo> list;
if (starTime == null || endTime == null){
// 获取当前日期
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = currentDate.format(formatter);
// 将当前日期加一天
LocalDate nextDay = currentDate.plusDays(1);
String nextDayFormatter = nextDay.format(formatter);
list = saleDataRepository.findAllByItemtypenameTime(formattedDate, nextDayFormatter);
}else {
list = saleDataRepository.findAllByItemtypenameTime(starTime, endTime);
}
Map<String, Long> countMap = list.stream() Map<String, Long> countMap = list.stream()
.collect(Collectors.groupingBy(SaleDataItemTypeVo::getItemtypename, Collectors.counting())); .collect(Collectors.groupingBy(SaleDataItemTypeVo::getItemtypename, Collectors.counting()));
List<Map<String, String>> mapArrayList = new ArrayList<>(); List<Map<String, String>> mapArrayList = new ArrayList<>();
@ -225,6 +275,20 @@ public class SaleDataServiceImpl implements SaleDataService {
mapArrayList.add(map); mapArrayList.add(map);
// map.clear(); // map.clear();
} }
if (mapArrayList.size() == 0){
HashMap<String, String> map = new HashMap<>();
map.put("TypeName","景点");
map.put("count","0");
mapArrayList.add(map);
HashMap<String, String> map1 = new HashMap<>();
map1.put("TypeName","公园");
map1.put("count","0");
mapArrayList.add(map1);
HashMap<String, String> map2 = new HashMap<>();
map2.put("TypeName","遗迹");
map2.put("count","0");
mapArrayList.add(map2);
}
return mapArrayList; return mapArrayList;
} }