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

This commit is contained in:
慕下 2024-08-23 16:24:57 +08:00
parent ef9e00e2d9
commit bc32b14036
4 changed files with 105 additions and 24 deletions

View File

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

View File

@ -10,6 +10,7 @@ import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.List;
@Repository
public interface SaleDataRepository extends MongoRepository<SaleData,String> {
@ -30,6 +31,15 @@ public interface SaleDataRepository extends MongoRepository<SaleData,String> {
AggregationVO findAllByCertificateno();
@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}$'}}}",
"{$project: {birthYear: {$toInt: {$substr: ['$certificateno', 6, 4]}}, currentYear: {$year: '$$NOW'}}}",
"{$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]}}, " +
"six: {$sum: {$cond: [{$gt: ['$age', 60]}, 1, 0]}}}}"
})
AgeVo findByAge();
AgeVo findByAge(String starTime, String endTime);
@Query(value = "{}", fields = "{ 'itemtypename': 1 }")
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 男女人数
*/
public List<Map<String, String>> findByGender();
public List<Map<String, String>> findByGender(String starTime, String endTime);
/**
* 查询各个年龄段的人数
* @return 各个年龄段的人数
*/
public List<Map<String, String>> findByAge();
public List<Map<String, String>> findByAge(String starTime, String endTime);
/**
* 查询去年1月1日到12月31日的全部数据
@ -63,13 +63,13 @@ public interface SaleDataService {
* 查询销售渠道
* @return
*/
public List<Map<String, String>> findBySaleMethod();
public List<Map<String, String>> findBySaleMethod(String starTime, String endTime);
/**
* 查询各个产品类型的购票人数
* @return
*/
public List<Map<String, String>> findByDoItemType();
public List<Map<String, String>> findByDoItemType(String starTime, String endTime);
/**
* 查询最近前x年五一期间的数据

View File

@ -16,6 +16,8 @@ import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@ -62,8 +64,17 @@ public class SaleDataServiceImpl implements SaleDataService {
}
@Override
public List<Map<String, String>> findByGender() {
AggregationVO allByCertificateno = saleDataRepository.findAllByCertificateno();
public List<Map<String, String>> findByGender(String starTime, String endTime) {
AggregationVO allByCertificateno;
if (starTime == null || endTime == null){
// 获取当前日期
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = currentDate.format(formatter);
allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(formattedDate, formattedDate);
}else {
allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(starTime, endTime);
}
List<Map<String, String>> map = new ArrayList<>();
Map<String,String>map1=new HashMap<>();
Map<String,String>map2=new HashMap<>();
@ -77,8 +88,17 @@ public class SaleDataServiceImpl implements SaleDataService {
}
@Override
public List<Map<String, String>> findByAge() {
AgeVo byAge = saleDataRepository.findByAge();
public List<Map<String, String>> findByAge(String starTime, String endTime) {
AgeVo byAge;
if (starTime == null || endTime == null){
// 获取当前日期
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = currentDate.format(formatter);
byAge = saleDataRepository.findByAge(formattedDate,formattedDate);
}else {
byAge = saleDataRepository.findByAge(starTime,endTime);
}
List<Map<String,String>>map=new ArrayList<>();
Map<String,String>map1=new LinkedHashMap<>();
Map<String,String>map2=new LinkedHashMap<>();
@ -171,13 +191,22 @@ public class SaleDataServiceImpl implements SaleDataService {
@Override
public List<Map<String, String>> findBySaleMethod() {
List<SaleData> saleDataList = saleDataRepository.findAll();
public List<Map<String, String>> findBySaleMethod(String starTime, String endTime) {
List<String> saleDataList;
if (starTime == null || endTime == null){
// 获取当前日期
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = currentDate.format(formatter);
saleDataList = saleDataRepository.findAllByTransactiontypenoTime(formattedDate, formattedDate);
}else {
saleDataList = saleDataRepository.findAllByTransactiontypenoTime(starTime, endTime);
}
int[] saleMethodCounts = new int[5];
Map<String,String>map=new LinkedHashMap<>();
for (SaleData saleData : saleDataList) {
String transactiontypeno = saleData.getTransactiontypeno();
switch (transactiontypeno){
for (String saleData : saleDataList) {
// String transactiontypeno = saleData.getTransactiontypeno();
switch (saleData){
case "SD01":
saleMethodCounts[0]++;
break;
@ -213,8 +242,17 @@ public class SaleDataServiceImpl implements SaleDataService {
}
@Override
public List<Map<String, String>> findByDoItemType() {
List<SaleDataItemTypeVo> list = saleDataRepository.findAllByItemtypename();
public List<Map<String, String>> findByDoItemType(String starTime, String endTime) {
List<SaleDataItemTypeVo> list;
if (starTime == null || endTime == null){
// 获取当前日期
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = currentDate.format(formatter);
list = saleDataRepository.findAllByItemtypenameTime(formattedDate, formattedDate);
}else {
list = saleDataRepository.findAllByItemtypenameTime(starTime, endTime);
}
Map<String, Long> countMap = list.stream()
.collect(Collectors.groupingBy(SaleDataItemTypeVo::getItemtypename, Collectors.counting()));
List<Map<String, String>> mapArrayList = new ArrayList<>();