From 96b3bd83efb6b5a6eb762943679a86396ca19794 Mon Sep 17 00:00:00 2001 From: XinWei <2718030729@qq.com> Date: Thu, 29 Aug 2024 16:36:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9E=84=E5=BB=BA=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone-base.yml | 4 +-- .drone-infra.yml | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ .drone.yml | 60 +++++++++++++++---------------- 3 files changed, 123 insertions(+), 32 deletions(-) create mode 100644 .drone-infra.yml diff --git a/.drone-base.yml b/.drone-base.yml index 381dedc5e..43bd9188c 100644 --- a/.drone-base.yml +++ b/.drone-base.yml @@ -98,11 +98,11 @@ steps: # 定义流水线执行步骤,这些步骤将顺序执行 - cd /ludu/maven/build/yudao-gateway/ - chmod +x ./run.sh # 更改为可执行脚本 - - ./run.sh || echo "ludu-module-ticketing build failed" # 运行脚本打包应用镜像并运行 + - ./run.sh || echo "yudao-gateway build failed" # 运行脚本打包应用镜像并运行 - cd /ludu/maven/build/yudao-module-system/ - chmod +x ./run.sh # 更改为可执行脚本 - - ./run.sh || echo "ludu-module-datacenter build failed" # 运行脚本打包应用镜像并运行 + - ./run.sh || echo "yudao-module-system build failed" # 运行脚本打包应用镜像并运行 - cd /ludu/maven/build/yudao-module-infra/ - chmod +x ./run.sh # 更改为可执行脚本 diff --git a/.drone-infra.yml b/.drone-infra.yml new file mode 100644 index 000000000..00ba0d943 --- /dev/null +++ b/.drone-infra.yml @@ -0,0 +1,91 @@ +kind: pipeline # 定义对象类型,还有secret和signature两种类型 + +type: docker # 定义流水线类型,还有kubernetes、exec、ssh等类型 + +name: ludu-system-drone # 定义流水线名称 + + +steps: # 定义流水线执行步骤,这些步骤将顺序执行 + + - name: package # 流水线名称 + + image: maven:3-jdk-8 # 定义创建容器的Docker镜像 + + volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置 + + - name: maven-cache + + path: /root/.m2 # 将maven下载依赖的目录挂载出来,防止重复下载 + + - name: maven-build + + path: /app/build # 将应用打包好的Jar和执行脚本挂载出来 + + commands: # 定义在Docker容器中执行的shell命令 + + - mvn package -Dmaven.test.skip=true # 应用打包命令 + + - mkdir -p /app/build/yudao-module-infra/target + - cp ./yudao-module-infra/yudao-module-infra-biz/target/yudao-module-infra-biz.jar /app/build/yudao-module-infra/target + - cp ./yudao-module-infra/yudao-module-infra-biz/Dockerfile /app/build/yudao-module-infra + - cp ./yudao-module-infra/yudao-module-infra-biz/run.sh /app/build/yudao-module-infra + + + - name: copy-to-object # 构建基础模块和复制票务停车场到轮渡服务器 + + image: appleboy/drone-ssh # SSH工具镜像 + + settings: + + host: 101.43.112.107 # 远程连接地址 + + username: root # 远程连接账号 + + password: + + from_secret: ssh_password # 从Secret中读取SSH密码 + + port: 22 # 远程连接端口 + + command_timeout: 15m # 远程执行命令超时时间 + + script: + + - echo "start copy" + - scp -r /ludu/maven/build/yudao-module-infra root@121.36.203.133:/ludu/maven/build + - echo "infra done" + + - name: object-build + + image: appleboy/drone-ssh # SSH工具镜像 + + settings: + + host: 121.36.203.133 # 远程连接地址 + + username: root # 远程连接账号 + + password: + + from_secret: ssh_password3 # 从Secret中读取SSH密码 + + port: 22 # 远程连接端口 + + command_timeout: 20m # 远程执行命令超时时间 + + script: + + - cd /ludu/maven/build/yudao-module-infra/ + - chmod +x ./run.sh # 更改为可执行脚本 + - ./run.sh || echo "yudao-module-infra build failed" # 运行脚本打包应用镜像并运行 + + +volumes: # 定义流水线挂载目录,用于共享数据 + + - name: maven-build + host: + path: /ludu/maven/build # 从宿主机中挂载的目录 + + - name: maven-cache + host: + path: /ludu/maven/cache # 从宿主机中挂载的目录 \ No newline at end of file diff --git a/.drone.yml b/.drone.yml index 95dbb5740..978866c63 100644 --- a/.drone.yml +++ b/.drone.yml @@ -55,10 +55,10 @@ steps: # 定义流水线执行步骤,这些步骤将顺序执行 - cp ./ludu-module-parking/ludu-module-parking-biz/Dockerfile /app/build/ludu-module-parking - cp ./ludu-module-parking/ludu-module-parking-biz/run.sh /app/build/ludu-module-parking - - mkdir -p /app/build/ludu-module-datacenter/target - - cp ./ludu-module-datacenter/ludu-module-datacenter-biz/target/ludu-module-datacenter-biz.jar /app/build/ludu-module-datacenter/target - - cp ./ludu-module-datacenter/ludu-module-datacenter-biz/Dockerfile /app/build/ludu-module-datacenter - - cp ./ludu-module-datacenter/ludu-module-datacenter-biz/run.sh /app/build/ludu-module-datacenter +# - mkdir -p /app/build/ludu-module-datacenter/target +# - cp ./ludu-module-datacenter/ludu-module-datacenter-biz/target/ludu-module-datacenter-biz.jar /app/build/ludu-module-datacenter/target +# - cp ./ludu-module-datacenter/ludu-module-datacenter-biz/Dockerfile /app/build/ludu-module-datacenter +# - cp ./ludu-module-datacenter/ludu-module-datacenter-biz/run.sh /app/build/ludu-module-datacenter - name: copy-to-object # 构建基础模块和复制票务停车场到轮渡服务器 @@ -93,32 +93,32 @@ steps: # 定义流水线执行步骤,这些步骤将顺序执行 - echo "infra done" - scp -r /ludu/maven/build/ludu-job-admin root@121.36.203.133:/ludu/maven/build - echo "job done" - - scp -r /ludu/maven/build/ludu-module-datacenter root@120.46.37.243:/ludu/maven/build - - echo "datacenter done" +# - scp -r /ludu/maven/build/ludu-module-datacenter root@120.46.37.243:/ludu/maven/build +# - echo "datacenter done" - - name: build-other-service - - image: appleboy/drone-ssh # SSH工具镜像 - - settings: - - host: 120.46.37.243 # 远程连接地址 - - username: root # 远程连接账号 - - password: - - from_secret: ssh_password2 # 从Secret中读取SSH密码 - - port: 22 # 远程连接端口 - - command_timeout: 5m # 远程执行命令超时时间 - - script: - - - cd /ludu/maven/build/ludu-module-datacenter/ - - chmod +x ./run.sh # 更改为可执行脚本 - - ./run.sh || echo "ludu-module-datacenter build failed" # 运行脚本打包应用镜像并运行 +# - name: build-other-service +# +# image: appleboy/drone-ssh # SSH工具镜像 +# +# settings: +# +# host: 120.46.37.243 # 远程连接地址 +# +# username: root # 远程连接账号 +# +# password: +# +# from_secret: ssh_password2 # 从Secret中读取SSH密码 +# +# port: 22 # 远程连接端口 +# +# command_timeout: 5m # 远程执行命令超时时间 +# +# script: +# +# - cd /ludu/maven/build/ludu-module-datacenter/ +# - chmod +x ./run.sh # 更改为可执行脚本 +# - ./run.sh || echo "ludu-module-datacenter build failed" # 运行脚本打包应用镜像并运行 - name: object-build image: appleboy/drone-ssh # SSH工具镜像 @@ -190,7 +190,7 @@ steps: # 定义流水线执行步骤,这些步骤将顺序执行 echo "Killing process $pid" kill -9 $pid fi - nohup java -Xms512m -Xmx512m -jar ludu-module-datacenter/ludu-module-datacenter-biz/target/ludu-module-datacenter-biz.jar --spring.profiles.active=dev > api.log & + nohup java -jar ludu-module-datacenter/ludu-module-datacenter-biz/target/ludu-module-datacenter-biz.jar --spring.profiles.active=dev > api.log & volumes: # 定义流水线挂载目录,用于共享数据 - name: maven-build -- 2.45.2 From 21b3850062f7192d1f49a2beda3063aed677497b Mon Sep 17 00:00:00 2001 From: XinWei <2718030729@qq.com> Date: Thu, 29 Aug 2024 16:37:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=8A=BD=E6=95=B0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/ticketing/TicketingController.java | 17 ++++++++++ .../CheckTicketTodayRepository.java | 17 ++++++++++ .../saledata/SaleDataTodayRepository.java | 9 +++++ .../job/ticketing/TicketDataMigration.java | 28 +++++----------- .../checkticket/CheckTicketService.java | 9 +++++ .../checkticket/CheckTicketServiceImpl.java | 33 +++++++++++++++++++ .../service/saledata/SaleDataService.java | 6 ++++ .../service/saledata/SaleDataServiceImpl.java | 31 ++++++++++++++++- 8 files changed, 129 insertions(+), 21 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/ticketing/TicketingController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/ticketing/TicketingController.java index 4a6c7a521..773d359f6 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/ticketing/TicketingController.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/ticketing/TicketingController.java @@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.infra.controller.admin.ticketing; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.util.date.TickingDateUtils; import cn.iocoder.yudao.module.infra.job.ticketing.TicketDataMigration; +import cn.iocoder.yudao.module.infra.service.checkticket.CheckTicketService; +import cn.iocoder.yudao.module.infra.service.saledata.SaleDataService; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -10,6 +12,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.Calendar; import java.util.Date; +import java.util.List; /** * @Description 测试抽数接口 @@ -22,6 +25,12 @@ public class TicketingController { @Resource TicketDataMigration ticketDataMigration; + @Resource + CheckTicketService checkTicketService; + + @Resource + SaleDataService saleDataService; + @GetMapping public CommonResult testTicketing(@RequestParam("startDate") String startDate, @RequestParam("endDate")String endDate, @RequestParam("method") String method) throws Exception { Calendar startCalendar = Calendar.getInstance(); @@ -30,4 +39,12 @@ public class TicketingController { endCalendar.setTime(TickingDateUtils.parse(endDate)); return CommonResult.success(ticketDataMigration.getTicketDataByTimeZonesToMongo(startCalendar, endCalendar, method)); } + @GetMapping("/test") + public CommonResult> testAll(){ + return CommonResult.success(checkTicketService.deleteTwoYearLastMonthAgo()); + } + @GetMapping("/testa") + public CommonResult> testAlla(){ + return CommonResult.success(saleDataService.deleteTwoYearLastMonthAgo()); + } } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mongodb/checkticket/CheckTicketTodayRepository.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mongodb/checkticket/CheckTicketTodayRepository.java index 4b78d339d..aa1c5adf2 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mongodb/checkticket/CheckTicketTodayRepository.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mongodb/checkticket/CheckTicketTodayRepository.java @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.infra.dal.mongodb.checkticket; import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicket; import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketToday; +import org.springframework.data.mongodb.repository.Aggregation; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.Query; import org.springframework.stereotype.Repository; @@ -27,4 +28,20 @@ public interface CheckTicketTodayRepository extends MongoRepository findNotInTimeRange(String startDate, String endDate); + /** + * 查询所有去重的 checkticketdate 字段值 + * @return 去重后的 checkticketdate 字段值列表 + */ + @Aggregation(pipeline = { + "{$group: {_id: '$checkticketdate'}}", + "{$project: {_id: 0, checkticketdate: '$_id'}}" + }) + List findDistinctDate(); + + /** + * 删除 checkticketdate 字段值在给定列表中的所有记录 + * @param dates 日期列表 + */ + @Query("{ 'checkticketdate': { $in: ?0 } }") + void deleteByCheckticketdateIn(List dates); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mongodb/saledata/SaleDataTodayRepository.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mongodb/saledata/SaleDataTodayRepository.java index c6620a348..241dc0158 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mongodb/saledata/SaleDataTodayRepository.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mongodb/saledata/SaleDataTodayRepository.java @@ -78,4 +78,13 @@ public interface SaleDataTodayRepository extends MongoRepository findNotInTimeRange(String startDate, String endDate); + /** + * 查询所有去重的 sddate 字段值 + * @return 去重后的 sddate 字段值列表 + */ + @Aggregation(pipeline = { + "{$group: {_id: '$sddate'}}", + "{$project: {_id: 0, sddate: '$_id'}}" + }) + List findDistinctDate(); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/ticketing/TicketDataMigration.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/ticketing/TicketDataMigration.java index a89643322..343715161 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/ticketing/TicketDataMigration.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/job/ticketing/TicketDataMigration.java @@ -309,35 +309,23 @@ public class TicketDataMigration { @XxlJob("deleteTwoYearOneMonthAgoAndUpdateLastYearTomorrow") public void deleteTwoYearOneMonthAgoAndUpdateLastYearTomorrow(){ XxlJobHelper.log("删除多余数据开始..."); - // 检票数据今年多余删除 - Calendar today = Calendar.getInstance(); - Calendar thirtyDaysAgo = (Calendar) today.clone(); // 克隆当前日期 - thirtyDaysAgo.add(Calendar.DAY_OF_MONTH, -31); - List thisYearCheckTicketList = checkTicketService.deleteNotInTimeZonesByMongoDB(TickingDateUtils.format(thirtyDaysAgo.getTime()), TickingDateUtils.format(today.getTime())); - XxlJobHelper.log("删除今年检票日期为{}", thisYearCheckTicketList.toString()); - // 检票数据去年多余删除 - Calendar lastYearToday = (Calendar) today.clone(); - lastYearToday.add(Calendar.YEAR, -1); - Calendar lastYearThirtyDaysAgo = (Calendar) lastYearToday.clone(); - lastYearThirtyDaysAgo.add(Calendar.DAY_OF_MONTH, -31); - List lastYearCheckTicketList = checkTicketService.deleteNotInTimeZonesByMongoDB(TickingDateUtils.format(lastYearThirtyDaysAgo.getTime()), TickingDateUtils.format(lastYearToday.getTime())); - XxlJobHelper.log("删除去年检票日期为{}", lastYearCheckTicketList.toString()); - // 售票数据今年多余删除 - List thisYearSaleDataList = saleDataService.deleteNotInTimeZonesByMongoDB(TickingDateUtils.format(thirtyDaysAgo.getTime()), TickingDateUtils.format(today.getTime())); - XxlJobHelper.log("删除今年售票日期为{}", thisYearSaleDataList.toString()); - // 售票数据去年多余删除 - List lastYearSaleDataList = checkTicketService.deleteNotInTimeZonesByMongoDB(TickingDateUtils.format(lastYearThirtyDaysAgo.getTime()), TickingDateUtils.format(lastYearToday.getTime())); - XxlJobHelper.log("删除去年检票日期为{}", lastYearSaleDataList.toString()); + // 检票数据今年去年多余删除 + XxlJobHelper.log("删除检票日期为{}", checkTicketService.deleteTwoYearLastMonthAgo().toString()); + // 售票数据今年去年多余删除 + XxlJobHelper.log("删除售票日期为{}", saleDataService.deleteTwoYearLastMonthAgo().toString()); // 抽取去年明天的检票和售票数据 - Calendar lastYearTomorrow = (Calendar) lastYearToday.clone(); + Calendar lastYearTomorrow = Calendar.getInstance(); lastYearTomorrow.add(Calendar.DAY_OF_MONTH, 1); + lastYearTomorrow.add(Calendar.YEAR, -1); try { this.getTicketDataByTimeZonesToMongo(lastYearTomorrow, lastYearTomorrow, "checkTicket"); + XxlJobHelper.log("抽取去年明天的检票数据成功,日期为{}", TickingDateUtils.format(lastYearTomorrow.getTime())); } catch (Exception e) { XxlJobHelper.log("抽取去年明天的检票数据失败,日期为{}", TickingDateUtils.format(lastYearTomorrow.getTime())); } try { this.getTicketDataByTimeZonesToMongo(lastYearTomorrow, lastYearTomorrow, "saleData"); + XxlJobHelper.log("抽取去年明天的售票数据成功,日期为{}", TickingDateUtils.format(lastYearTomorrow.getTime())); } catch (Exception e) { XxlJobHelper.log("抽取去年明天的售票数据失败,日期为{}", TickingDateUtils.format(lastYearTomorrow.getTime())); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/checkticket/CheckTicketService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/checkticket/CheckTicketService.java index 306523d88..6370b1eec 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/checkticket/CheckTicketService.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/checkticket/CheckTicketService.java @@ -47,4 +47,13 @@ public interface CheckTicketService { * @return java.util.List 删除的日期 */ List deleteNotInTimeZonesByMongoDB(String startDate,String endDate); + + /** + * 删除两年上个月以前的数据(mongodb) + * @param + * @return java.util.List + */ + List deleteTwoYearLastMonthAgo(); + + List findDistinctDate(); } \ No newline at end of file diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/checkticket/CheckTicketServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/checkticket/CheckTicketServiceImpl.java index 7576a91af..107c4a887 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/checkticket/CheckTicketServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/checkticket/CheckTicketServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.infra.service.checkticket; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.date.TickingDateUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicket; @@ -14,6 +15,7 @@ import com.baomidou.dynamic.datasource.annotation.Slave; import com.mongodb.MongoException; import org.springframework.data.domain.Example; import org.springframework.data.domain.ExampleMatcher; +import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; @@ -42,6 +44,8 @@ public class CheckTicketServiceImpl implements CheckTicketService { private CheckTicketRepository checkTicketRepository; @Resource private CheckTicketTodayRepository checkTicketTodayRepository; + @Resource + private MongoTemplate mongoTemplate; @Override public Long createCheckTicket(CheckTicketDO checkTicketDO) { @@ -116,4 +120,33 @@ public class CheckTicketServiceImpl implements CheckTicketService { return deletedDates; } + @Override + public List deleteTwoYearLastMonthAgo() { + List totalDate = checkTicketTodayRepository.findDistinctDate(); + List exitDate = new ArrayList<>(); + Calendar endDate = Calendar.getInstance(); + Calendar endDateLastYear = (Calendar) endDate.clone(); + endDateLastYear.add(Calendar.YEAR, -1); + for (int i = 0; i < 31; i++) { + exitDate.add(TickingDateUtils.format(endDate.getTime())); + exitDate.add(TickingDateUtils.format(endDateLastYear.getTime())); + endDate.add(Calendar.DAY_OF_MONTH, -1); + endDateLastYear.add(Calendar.DAY_OF_MONTH, -1); + } + List filteredDate = totalDate.stream() + .filter(date -> !exitDate.contains(date)) + .collect(Collectors.toList()); + // TODO 使用这种方法删除会报错,暂时使用mongoTemplate进行删除 +// checkTicketTodayRepository.deleteByCheckticketdateIn(filteredDate); + Query query = new Query(); + query.addCriteria(Criteria.where("sddate").in(filteredDate)); + mongoTemplate.remove(query, "sale_data_today"); + return filteredDate; + } + + @Override + public List findDistinctDate() { + return checkTicketTodayRepository.findDistinctDate(); + } + } \ No newline at end of file diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/saledata/SaleDataService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/saledata/SaleDataService.java index 49c014ba1..4d2a2d318 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/saledata/SaleDataService.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/saledata/SaleDataService.java @@ -49,4 +49,10 @@ public interface SaleDataService { * @return java.util.List 删除的日期 */ List deleteNotInTimeZonesByMongoDB(String startDate,String endDate); + /** + * 删除两年上个月以前的数据(mongodb) + * @param + * @return java.util.List + */ + List deleteTwoYearLastMonthAgo(); } \ No newline at end of file diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/saledata/SaleDataServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/saledata/SaleDataServiceImpl.java index 7eadfa01c..fad037a84 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/saledata/SaleDataServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/saledata/SaleDataServiceImpl.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.infra.service.saledata; +import cn.iocoder.yudao.framework.common.util.date.TickingDateUtils; import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicket; import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketToday; import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleData; @@ -9,6 +10,9 @@ import cn.iocoder.yudao.module.infra.dal.mongodb.saledata.SaleDataRepository; import cn.iocoder.yudao.module.infra.dal.mongodb.saledata.SaleDataTodayRepository; import cn.iocoder.yudao.module.infra.dal.mysql.saledata.SaleDataMapper; import com.mongodb.MongoException; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -34,6 +38,9 @@ public class SaleDataServiceImpl implements SaleDataService { @Resource private SaleDataMapper saleDataMapper; + @Resource + private MongoTemplate mongoTemplate; + @Override public Long createSaleData(SaleDataDO saleDataDO) { // 插入 @@ -106,5 +113,27 @@ public class SaleDataServiceImpl implements SaleDataService { .collect(Collectors.toList()); return deletedDates; } - + @Override + public List deleteTwoYearLastMonthAgo() { + List totalDate = saleDataTodayRepository.findDistinctDate(); + List exitDate = new ArrayList<>(); + Calendar endDate = Calendar.getInstance(); + Calendar endDateLastYear = (Calendar) endDate.clone(); + endDateLastYear.add(Calendar.YEAR, -1); + for (int i = 0; i < 31; i++) { + exitDate.add(TickingDateUtils.format(endDate.getTime())); + exitDate.add(TickingDateUtils.format(endDateLastYear.getTime())); + endDate.add(Calendar.DAY_OF_MONTH, -1); + endDateLastYear.add(Calendar.DAY_OF_MONTH, -1); + } + List filteredDate = totalDate.stream() + .filter(date -> !exitDate.contains(date)) + .collect(Collectors.toList()); + // TODO 使用这种方法删除会报错,暂时使用mongoTemplate进行删除 +// checkTicketTodayRepository.deleteByCheckticketdateIn(filteredDate); + Query query = new Query(); + query.addCriteria(Criteria.where("sddate").in(filteredDate)); + mongoTemplate.remove(query, "sale_data_today"); + return filteredDate; + } } \ No newline at end of file -- 2.45.2