diff --git a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/app/saledata/SaleDataApi.java b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/app/saledata/SaleDataApi.java index a901d838f..fd1970086 100644 --- a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/app/saledata/SaleDataApi.java +++ b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/app/saledata/SaleDataApi.java @@ -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> findByGender() { - return saleDataService.findByGender(); + public List> 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> findByage() { - return saleDataService.findByAge(); + public List> 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> findBysaleMethod() { - return saleDataService.findBySaleMethod(); + public List> 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> findByItemType() { - return saleDataService.findByDoItemType(); + public List> 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}") diff --git a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/dal/mongodb/saledata/SaleDataRepository.java b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/dal/mongodb/saledata/SaleDataRepository.java index 19f421667..d333177f0 100644 --- a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/dal/mongodb/saledata/SaleDataRepository.java +++ b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/dal/mongodb/saledata/SaleDataRepository.java @@ -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 { @@ -30,6 +31,15 @@ public interface SaleDataRepository extends MongoRepository { 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 { "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 findAllByItemtypename(); + @Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'itemtypename': 1 }") + List findAllByItemtypenameTime(String starTime, String endTime); + + @Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'transactiontypeno': 1 }") + List findAllByTransactiontypenoTime(String starTime, String endTime); + } diff --git a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/service/saledata/SaleDataService.java b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/service/saledata/SaleDataService.java index a05bfa5e4..ef7e074f8 100644 --- a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/service/saledata/SaleDataService.java +++ b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/service/saledata/SaleDataService.java @@ -31,13 +31,13 @@ public interface SaleDataService { * 查询男女人数 * @return 男女人数 */ - public List> findByGender(); + public List> findByGender(String starTime, String endTime); /** * 查询各个年龄段的人数 * @return 各个年龄段的人数 */ - public List> findByAge(); + public List> findByAge(String starTime, String endTime); /** * 查询去年1月1日到12月31日的全部数据 @@ -63,13 +63,13 @@ public interface SaleDataService { * 查询销售渠道 * @return */ - public List> findBySaleMethod(); + public List> findBySaleMethod(String starTime, String endTime); /** * 查询各个产品类型的购票人数 * @return */ - public List> findByDoItemType(); + public List> findByDoItemType(String starTime, String endTime); /** * 查询最近前x年五一期间的数据 diff --git a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/service/saledata/SaleDataServiceImpl.java b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/service/saledata/SaleDataServiceImpl.java index c04d16a65..83ede1009 100644 --- a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/service/saledata/SaleDataServiceImpl.java +++ b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/service/saledata/SaleDataServiceImpl.java @@ -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,20 @@ public class SaleDataServiceImpl implements SaleDataService { } @Override - public List> findByGender() { - AggregationVO allByCertificateno = saleDataRepository.findAllByCertificateno(); + public List> 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); + // 将当前日期加一天 + LocalDate nextDay = currentDate.plusDays(1); + String nextDayFormatter = nextDay.format(formatter); + allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(formattedDate, nextDayFormatter); + }else { + allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(starTime, endTime); + } List> map = new ArrayList<>(); Mapmap1=new HashMap<>(); Mapmap2=new HashMap<>(); @@ -77,8 +91,20 @@ public class SaleDataServiceImpl implements SaleDataService { } @Override - public List> findByAge() { - AgeVo byAge = saleDataRepository.findByAge(); + public List> 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); + // 将当前日期加一天 + LocalDate nextDay = currentDate.plusDays(1); + String nextDayFormatter = nextDay.format(formatter); + byAge = saleDataRepository.findByAge(formattedDate,nextDayFormatter); + }else { + byAge = saleDataRepository.findByAge(starTime,endTime); + } List>map=new ArrayList<>(); Mapmap1=new LinkedHashMap<>(); Mapmap2=new LinkedHashMap<>(); @@ -171,13 +197,25 @@ public class SaleDataServiceImpl implements SaleDataService { @Override - public List> findBySaleMethod() { - List saleDataList = saleDataRepository.findAll(); + public List> findBySaleMethod(String starTime, String endTime) { + List 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]; Mapmap=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 +251,20 @@ public class SaleDataServiceImpl implements SaleDataService { } @Override - public List> findByDoItemType() { - List list = saleDataRepository.findAllByItemtypename(); + public List> findByDoItemType(String starTime, String endTime) { + List 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 countMap = list.stream() .collect(Collectors.groupingBy(SaleDataItemTypeVo::getItemtypename, Collectors.counting())); List> mapArrayList = new ArrayList<>(); @@ -225,6 +275,20 @@ public class SaleDataServiceImpl implements SaleDataService { mapArrayList.add(map); // map.clear(); } + if (mapArrayList.size() == 0){ + HashMap map = new HashMap<>(); + map.put("TypeName","景点"); + map.put("count","0"); + mapArrayList.add(map); + HashMap map1 = new HashMap<>(); + map1.put("TypeName","公园"); + map1.put("count","0"); + mapArrayList.add(map1); + HashMap map2 = new HashMap<>(); + map2.put("TypeName","遗迹"); + map2.put("count","0"); + mapArrayList.add(map2); + } return mapArrayList; }