From 90ecdcfffe16471147e5e38b22cbce70c2b2fb76 Mon Sep 17 00:00:00 2001 From: XinWei <2718030729@qq.com> Date: Fri, 16 Aug 2024 14:25:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A5=A8=E5=8A=A1mongodb=E6=8A=BD=E6=95=B0?= =?UTF-8?q?=E4=B8=8A=E4=B8=AA=E6=9C=88=E5=88=B0=E4=BB=8A=E5=A4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../job/ticketing/TicketDataMigration.java | 119 +++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) 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 22f6143f4..b9b08b472 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 @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.infra.job.ticketing; +import cn.hutool.core.date.DatePattern; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONUtil; import cn.iocoder.yudao.framework.common.util.date.TickingDateUtils; @@ -26,6 +27,9 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.math.BigDecimal; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; import java.util.List; import java.util.Map; @@ -190,6 +194,114 @@ public class TicketDataMigration { XxlJobHelper.log("抽数服务:检票数据抽数正常结束!总共:{}条数据,{}条数据重复,{}条数据插入成功!", totalRows, duplicatesCount, successCount); } + @XxlJob("getCheckTicketByMongoDBLastMonthToNow") + public void getCheckTicketByMongoDBLastMonthToNow() throws Exception { + XxlJobHelper.log("抽数服务:mongodb检票数据开始抽数..."); + + int successCount; + int duplicatesCount; + int pageNumber; + // 获取当前日期 + Calendar startDate = Calendar.getInstance(); + // 设置开始日期为上一个月的第一天 + startDate.set(Calendar.DAY_OF_MONTH, 1); // 先将日期设置为当前月的第一天 + startDate.add(Calendar.MONTH, -1); // 然后减去一个月 + + // 设置结束日期为今天 + Calendar endDate = Calendar.getInstance(); + + // 循环从开始日期到结束日期 + while (!startDate.after(endDate)) { + successCount = 0; + duplicatesCount = 0; + String dateString = TickingDateUtils.format(startDate.getTime()); + pageNumber = 1; + TicketingSamplingRespVO ticketingSamplingRespVO = this.getUrlResponseData(CHECK_TICKET_SERVICE, CHECK_TICKET_METHOD, dateString, pageNumber); + if (ticketingSamplingRespVO == null) { + startDate.add(Calendar.DAY_OF_MONTH, 1); + continue; + } + int totalRows = ticketingSamplingRespVO.getTotalRows(); + List> mapList; + + // 获得总页数 + int totalPages = ticketingSamplingRespVO.getTotalPages(); + + // 处理剩余页的数据 + for (; pageNumber <= totalPages; pageNumber++) { + ticketingSamplingRespVO = this.getUrlResponseData(CHECK_TICKET_SERVICE, CHECK_TICKET_METHOD, dateString, pageNumber); + mapList = ticketingSamplingRespVO.getDataMapList(); + List resultNum = checkTicketService.insertByMongoDB(BeanUtils.toBean(mapList, CheckTicket.class)); + + if (resultNum == null) { + XxlJobHelper.log("数据批量插入异常!", pageNumber); + } else { + successCount += resultNum.get(0); + duplicatesCount += resultNum.get(1); + } + } + int currentMonth = startDate.get(Calendar.MONTH) + 1; // 加1以得到1-12的月份 + int currentDay = startDate.get(Calendar.DAY_OF_MONTH); + XxlJobHelper.log("抽数服务:{}月{}日:检票数据抽数正常结束!总共:{}条数据,{}条数据重复,{}条数据插入成功!",currentMonth,currentDay ,totalRows, duplicatesCount, successCount); + // 日期递增一天 + startDate.add(Calendar.DAY_OF_MONTH, 1); + } + XxlJobHelper.log("抽数结束!!!"); + } + @XxlJob("getSaleDataByMongoDBLastMonthToNow") + public void getSaleDataByMongoDBLastMonthToNow() throws Exception { + XxlJobHelper.log("抽数服务:mongodb售票数据开始抽数..."); + + int successCount; + int duplicatesCount; + int pageNumber; + // 获取当前日期 + Calendar startDate = Calendar.getInstance(); + // 设置开始日期为上一个月的第一天 + startDate.set(Calendar.DAY_OF_MONTH, 1); // 先将日期设置为当前月的第一天 + startDate.add(Calendar.MONTH, -1); // 然后减去一个月 + + // 设置结束日期为今天 + Calendar endDate = Calendar.getInstance(); + + // 循环从开始日期到结束日期 + while (!startDate.after(endDate)) { + successCount = 0; + duplicatesCount = 0; + String dateString = TickingDateUtils.format(startDate.getTime()); + pageNumber = 1; + TicketingSamplingRespVO ticketingSamplingRespVO = this.getUrlResponseData(SALE_DATA_SERVICE, SALE_DATA_METHOD, dateString, pageNumber); + if (ticketingSamplingRespVO == null) { + startDate.add(Calendar.DAY_OF_MONTH, 1); + continue; + } + int totalRows = ticketingSamplingRespVO.getTotalRows(); + List> mapList; + + // 获得总页数 + int totalPages = ticketingSamplingRespVO.getTotalPages(); + + // 处理剩余页的数据 + for (; pageNumber <= totalPages; pageNumber++) { + ticketingSamplingRespVO = this.getUrlResponseData(SALE_DATA_SERVICE, SALE_DATA_METHOD, dateString, pageNumber); + mapList = ticketingSamplingRespVO.getDataMapList(); + List resultNum = saleDataService.insertByMongoDB(BeanUtils.toBean(mapList, SaleData.class)); + + if (resultNum == null) { + XxlJobHelper.log("数据批量插入异常!", pageNumber); + } else { + successCount += resultNum.get(0); + duplicatesCount += resultNum.get(1); + } + } + int currentMonth = startDate.get(Calendar.MONTH) + 1; // 加1以得到1-12的月份 + int currentDay = startDate.get(Calendar.DAY_OF_MONTH); + XxlJobHelper.log("抽数服务:{}月{}日:售票数据抽数正常结束!总共:{}条数据,{}条数据重复,{}条数据插入成功!",currentMonth,currentDay ,totalRows, duplicatesCount, successCount); + // 日期递增一天 + startDate.add(Calendar.DAY_OF_MONTH, 1); + } + XxlJobHelper.log("抽数结束!!!"); + } /** * 插入销售数据的方法(mysql) * @@ -262,9 +374,12 @@ public class TicketDataMigration { // TODO 需要再优化 if (ticketingSamplingRespVO != null && !ticketingSamplingRespVO.isPassflag()) { // 让调度中心重新调用 - XxlJobHelper.log("抽数服务:检票数据抽数失败,返回消息:" + ticketingSamplingRespVO.getRtnMsg()); - XxlJobHelper.handleFail("销售数据抽数失败,请重试"); + XxlJobHelper.log("数据抽数失败,返回消息:" + ticketingSamplingRespVO.getRtnMsg()); + XxlJobHelper.handleFail("数据抽数失败,请重试"); throw new RuntimeException(); + }else if (ticketingSamplingRespVO.getPageNumber() == null || ticketingSamplingRespVO.getPageSize() == null || ticketingSamplingRespVO.getTotalPages() == null || ticketingSamplingRespVO.getTotalRows() == null || ticketingSamplingRespVO.getDataMapList().isEmpty()){ + XxlJobHelper.log("{},请求抽数成功,但返回参数值存在问题:{}", date, ticketingSamplingRespVO); + return null; } return ticketingSamplingRespVO; }