From bc32b140364c1320d9878ded6fc01bb2019df46b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=85=95=E4=B8=8B?= <484014559@qq.com> Date: Fri, 23 Aug 2024 16:24:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A4=A7=E5=B1=8F=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=95=88=E7=8E=87=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E7=A5=A8=E5=8A=A1=E6=8E=A5=E5=8F=A3=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/app/saledata/SaleDataApi.java | 43 ++++++++++--- .../mongodb/saledata/SaleDataRepository.java | 18 +++++- .../service/saledata/SaleDataService.java | 8 +-- .../service/saledata/SaleDataServiceImpl.java | 60 +++++++++++++++---- 4 files changed, 105 insertions(+), 24 deletions(-) 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..a18528bb8 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,17 @@ 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); + allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(formattedDate, formattedDate); + }else { + allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(starTime, endTime); + } List> map = new ArrayList<>(); Mapmap1=new HashMap<>(); Mapmap2=new HashMap<>(); @@ -77,8 +88,17 @@ 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); + byAge = saleDataRepository.findByAge(formattedDate,formattedDate); + }else { + byAge = saleDataRepository.findByAge(starTime,endTime); + } List>map=new ArrayList<>(); Mapmap1=new LinkedHashMap<>(); Mapmap2=new LinkedHashMap<>(); @@ -171,13 +191,22 @@ 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); + saleDataList = saleDataRepository.findAllByTransactiontypenoTime(formattedDate, formattedDate); + }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 +242,17 @@ 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); + list = saleDataRepository.findAllByItemtypenameTime(formattedDate, formattedDate); + }else { + list = saleDataRepository.findAllByItemtypenameTime(starTime, endTime); + } Map countMap = list.stream() .collect(Collectors.groupingBy(SaleDataItemTypeVo::getItemtypename, Collectors.counting())); List> mapArrayList = new ArrayList<>(); From 9c789c67ae94a9ed59fc12f4e2547abda1e42de6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=85=95=E4=B8=8B?= <484014559@qq.com> Date: Mon, 26 Aug 2024 12:01:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=A7=E5=B1=8F=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=95=88=E7=8E=87=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E7=A5=A8=E5=8A=A1=E6=8E=A5=E5=8F=A3=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/saledata/SaleDataServiceImpl.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) 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 a18528bb8..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 @@ -71,7 +71,10 @@ public class SaleDataServiceImpl implements SaleDataService { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); String formattedDate = currentDate.format(formatter); - allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(formattedDate, formattedDate); + // 将当前日期加一天 + LocalDate nextDay = currentDate.plusDays(1); + String nextDayFormatter = nextDay.format(formatter); + allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(formattedDate, nextDayFormatter); }else { allByCertificateno = saleDataRepository.findAllByCertificatenoSDTime(starTime, endTime); } @@ -95,7 +98,10 @@ public class SaleDataServiceImpl implements SaleDataService { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); String formattedDate = currentDate.format(formatter); - byAge = saleDataRepository.findByAge(formattedDate,formattedDate); + // 将当前日期加一天 + LocalDate nextDay = currentDate.plusDays(1); + String nextDayFormatter = nextDay.format(formatter); + byAge = saleDataRepository.findByAge(formattedDate,nextDayFormatter); }else { byAge = saleDataRepository.findByAge(starTime,endTime); } @@ -198,7 +204,10 @@ public class SaleDataServiceImpl implements SaleDataService { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); String formattedDate = currentDate.format(formatter); - saleDataList = saleDataRepository.findAllByTransactiontypenoTime(formattedDate, formattedDate); + // 将当前日期加一天 + LocalDate nextDay = currentDate.plusDays(1); + String nextDayFormatter = nextDay.format(formatter); + saleDataList = saleDataRepository.findAllByTransactiontypenoTime(formattedDate, nextDayFormatter); }else { saleDataList = saleDataRepository.findAllByTransactiontypenoTime(starTime, endTime); } @@ -249,7 +258,10 @@ public class SaleDataServiceImpl implements SaleDataService { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); String formattedDate = currentDate.format(formatter); - list = saleDataRepository.findAllByItemtypenameTime(formattedDate, formattedDate); + // 将当前日期加一天 + LocalDate nextDay = currentDate.plusDays(1); + String nextDayFormatter = nextDay.format(formatter); + list = saleDataRepository.findAllByItemtypenameTime(formattedDate, nextDayFormatter); }else { list = saleDataRepository.findAllByItemtypenameTime(starTime, endTime); } @@ -263,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; }