定时任务票务数据写入mongodb

This commit is contained in:
XinWei 2024-07-24 10:21:07 +08:00
parent f18d321415
commit d87d9ba6b9
3 changed files with 249 additions and 40 deletions

View File

@ -133,5 +133,10 @@ public interface SaleDataService {
*/
BigDecimal findyearJun(List<SaleDataDO> list);
/**
* 插入售票信息到mongodb中
* @param saleDataSaveReqDTO
* @return java.lang.String
*/
String createSaleDataByMongoDB(SaleDataSaveReqDTO saleDataSaveReqDTO);
}

View File

@ -35,4 +35,11 @@ public class TickingDateUtils {
public static String getNowDayFormat(){
return TickingDateUtils.format(new Date());
}
public static String getPreviousDayFormat(){
long time = new Date().getTime();
// 获取前一天的当前时间 TODO 可能需要考虑时区使用Calendar或java.time
time = time - (24 * 60 * 60 * 1000);
return TickingDateUtils.format(new Date(time));
}
}

View File

@ -27,7 +27,9 @@ import java.util.Map;
@Component
public class TicketDataMigration {
private static Logger logger = LoggerFactory.getLogger(TicketDataMigration.class);
// 目标地址
private static String OBJECT_URL = "http://shinanlundu.pro.jiutianda.com/joytime-erp-eportal/console/openapi/handler";
// 每次读取条数
private static final int PAGE_SIZE = 500;
@Resource
private TicketSaleDataApi ticketSaleDataApi;
@ -35,20 +37,24 @@ public class TicketDataMigration {
@Resource
private TicketCheckTicketApi ticketCheckTicketApi;
/**
* 存入售票数据的定时任务mysql
*/
@XxlJob("getSaleData")
public void getSaleDataHandler() throws Exception {
int failedCount = 0;
int pageNumber = 1;
XxlJobHelper.log("抽数服务:销售数据开始抽数...");
XxlJobHelper.log("抽数服务:mysql销售数据开始抽数...");
// 准备请求参数
TicketingSamplingReqVO ticketingSamplingReqVO = new TicketingSamplingReqVO();
ticketingSamplingReqVO.setService("apiSaleService");
ticketingSamplingReqVO.setMethod("SaleDetail");
ticketingSamplingReqVO.setQueryDate(TickingDateUtils.getNowDayFormat());
// 抽取前一天的所有售票数据
ticketingSamplingReqVO.setQueryDate(TickingDateUtils.getPreviousDayFormat());
ticketingSamplingReqVO.setPageNumber(pageNumber);
ticketingSamplingReqVO.setPageSize(PAGE_SIZE);
// 先发起第一次请求
String str = HttpUtil.post("http://shinanlundu.pro.jiutianda.com/joytime-erp-eportal/console/openapi/handler", JSONUtil.toJsonStr(ticketingSamplingReqVO));
String str = HttpUtil.post(OBJECT_URL, JSONUtil.toJsonStr(ticketingSamplingReqVO));
// 解析读取响应数据
TicketingSamplingRespVO ticketingSamplingRespVO = new ObjectMapper().readValue(str, TicketingSamplingRespVO.class);
// 如果访问失败则让调度中心重新调用
@ -73,7 +79,7 @@ public class TicketDataMigration {
// 处理剩余页的数据
for (; pageNumber <= totalPages; pageNumber++) {
ticketingSamplingReqVO.setPageNumber(pageNumber);
str = HttpUtil.post("http://shinanlundu.pro.jiutianda.com/joytime-erp-eportal/console/openapi/handler", JSONUtil.toJsonStr(ticketingSamplingReqVO));
str = HttpUtil.post(OBJECT_URL, JSONUtil.toJsonStr(ticketingSamplingReqVO));
ticketingSamplingRespVO = new ObjectMapper().readValue(str, TicketingSamplingRespVO.class);
if (!ticketingSamplingRespVO.isPassflag()) {
@ -95,21 +101,24 @@ public class TicketDataMigration {
XxlJobHelper.log("抽数服务:销售数据抽数正常结束!总共:{}条数据,{}条数据插入失败!", totalRows, failedCount);
}
/**
* 存入检票数据的定时任务mysql
*/
@XxlJob("getCheckTicket")
public void getCheckTicketHandler() throws Exception {
int failedCount = 0;
int pageNumber = 1;
XxlJobHelper.log("抽数服务:检票数据开始抽数...");
XxlJobHelper.log("抽数服务:mysql检票数据开始抽数...");
// 准备请求参数
TicketingSamplingReqVO ticketingSamplingReqVO = new TicketingSamplingReqVO();
ticketingSamplingReqVO.setService("apiCheckService");
ticketingSamplingReqVO.setMethod("CheckDetail");
ticketingSamplingReqVO.setQueryDate(TickingDateUtils.getNowDayFormat());
// 抽取前一天的所有检票数据
ticketingSamplingReqVO.setQueryDate(TickingDateUtils.getPreviousDayFormat());
ticketingSamplingReqVO.setPageNumber(pageNumber);
ticketingSamplingReqVO.setPageSize(PAGE_SIZE);
// 先发起第一次请求
String str = HttpUtil.post("http://shinanlundu.pro.jiutianda.com/joytime-erp-eportal/console/openapi/handler", JSONUtil.toJsonStr(ticketingSamplingReqVO));
String str = HttpUtil.post(OBJECT_URL, JSONUtil.toJsonStr(ticketingSamplingReqVO));
// 解析读取响应数据
TicketingSamplingRespVO ticketingSamplingRespVO = new ObjectMapper().readValue(str, TicketingSamplingRespVO.class);
@ -135,7 +144,7 @@ public class TicketDataMigration {
// 处理剩余页的数据
for (; pageNumber <= totalPages; pageNumber++) {
ticketingSamplingReqVO.setPageNumber(pageNumber);
str = HttpUtil.post("http://shinanlundu.pro.jiutianda.com/joytime-erp-eportal/console/openapi/handler", JSONUtil.toJsonStr(ticketingSamplingReqVO));
str = HttpUtil.post(OBJECT_URL, JSONUtil.toJsonStr(ticketingSamplingReqVO));
ticketingSamplingRespVO = new ObjectMapper().readValue(str, TicketingSamplingRespVO.class);
if (!ticketingSamplingRespVO.isPassflag()) {
@ -158,7 +167,136 @@ public class TicketDataMigration {
XxlJobHelper.log("抽数服务:检票数据抽数正常结束!总共:{}条数据,{}条数据插入失败!", totalRows, failedCount);
}
/**
* 插入销售数据的方法
* 存入销售数据的定时任务mongodb
*/
@XxlJob("getSaleDataByMongoDB")
public void getSaleDataByMongoDBHandler() throws Exception {
int failedCount = 0;
int pageNumber = 1;
XxlJobHelper.log("抽数服务mongodb销售数据开始抽数...");
// 准备请求参数
TicketingSamplingReqVO ticketingSamplingReqVO = new TicketingSamplingReqVO();
ticketingSamplingReqVO.setService("apiSaleService");
ticketingSamplingReqVO.setMethod("SaleDetail");
// 抽取前一天的所有售票数据
ticketingSamplingReqVO.setQueryDate(TickingDateUtils.getPreviousDayFormat());
ticketingSamplingReqVO.setPageNumber(pageNumber);
ticketingSamplingReqVO.setPageSize(PAGE_SIZE);
// 先发起第一次请求
String str = HttpUtil.post(OBJECT_URL, JSONUtil.toJsonStr(ticketingSamplingReqVO));
// 解析读取响应数据
TicketingSamplingRespVO ticketingSamplingRespVO = new ObjectMapper().readValue(str, TicketingSamplingRespVO.class);
// 如果访问失败则让调度中心重新调用
if (!ticketingSamplingRespVO.isPassflag()) {
// 让调度中心重新调用
XxlJobHelper.log("抽数服务:销售数据抽数失败,返回消息:" + ticketingSamplingRespVO.getRtnMsg());
XxlJobHelper.handleFail("销售数据抽数失败,请重试");
return;
}
int totalRows = ticketingSamplingRespVO.getTotalRows();
List<Map<String, Object>> mapList = ticketingSamplingRespVO.getDataMapList();
// 总条数小于等于每页条数 则直接按照总条数进行插入即可
if (totalRows <= PAGE_SIZE) {
failedCount += insertSaleDataByMongoDB(mapList, totalRows, pageNumber);
} else {
// 总条数大于每页条数 执行完第一次插入后根据返回的页数继续进行请求和插入
// 插入第一页的数据
failedCount += insertSaleDataByMongoDB(mapList, PAGE_SIZE, pageNumber);
pageNumber++;
// 获得总页数
int totalPages = ticketingSamplingRespVO.getTotalPages();
// 处理剩余页的数据
for (; pageNumber <= totalPages; pageNumber++) {
ticketingSamplingReqVO.setPageNumber(pageNumber);
str = HttpUtil.post(OBJECT_URL, JSONUtil.toJsonStr(ticketingSamplingReqVO));
ticketingSamplingRespVO = new ObjectMapper().readValue(str, TicketingSamplingRespVO.class);
if (!ticketingSamplingRespVO.isPassflag()) {
XxlJobHelper.log("抽数服务:销售数据抽数失败,返回消息:" + ticketingSamplingRespVO.getRtnMsg());
XxlJobHelper.handleFail("销售数据抽数失败,请重试");
return;
}
mapList = ticketingSamplingRespVO.getDataMapList();
// 对最后一页进行处理得到最后一页的实际条数
if (pageNumber != totalPages) {
failedCount += insertSaleDataByMongoDB(mapList, PAGE_SIZE, pageNumber);
} else {
failedCount += insertSaleDataByMongoDB(mapList, totalRows % PAGE_SIZE, pageNumber);
}
}
}
XxlJobHelper.log("抽数服务:销售数据抽数正常结束!总共:{}条数据,{}条数据插入失败!", totalRows, failedCount);
}
/**
* 存入检票数据的定时任务mongodb
*/
@XxlJob("getCheckTicketByMongoDB")
public void getCheckTicketByMongoDBHandler() throws Exception {
int failedCount = 0;
int pageNumber = 1;
XxlJobHelper.log("抽数服务mongodb检票数据开始抽数...");
// 准备请求参数
TicketingSamplingReqVO ticketingSamplingReqVO = new TicketingSamplingReqVO();
ticketingSamplingReqVO.setService("apiCheckService");
ticketingSamplingReqVO.setMethod("CheckDetail");
// 抽取前一天的所有检票数据
ticketingSamplingReqVO.setQueryDate(TickingDateUtils.getPreviousDayFormat());
ticketingSamplingReqVO.setPageNumber(pageNumber);
ticketingSamplingReqVO.setPageSize(PAGE_SIZE);
// 先发起第一次请求
String str = HttpUtil.post(OBJECT_URL, JSONUtil.toJsonStr(ticketingSamplingReqVO));
// 解析读取响应数据
TicketingSamplingRespVO ticketingSamplingRespVO = new ObjectMapper().readValue(str, TicketingSamplingRespVO.class);
// 如果访问失败则让调度中心重新调用
if (!ticketingSamplingRespVO.isPassflag()) {
// 让调度中心重新调用
XxlJobHelper.log("抽数服务:检票数据抽数失败,返回消息:" + ticketingSamplingRespVO.getRtnMsg());
XxlJobHelper.handleFail("销售数据抽数失败,请重试");
return;
}
int totalRows = ticketingSamplingRespVO.getTotalRows();
List<Map<String, Object>> mapList = ticketingSamplingRespVO.getDataMapList();
// 总条数小于等于每页条数 则直接按照总条数进行插入即可
if (totalRows <= PAGE_SIZE) {
failedCount += insertCheckTicketByMongoDB(mapList, totalRows, pageNumber);
} else {
// 总条数大于每页条数 执行完第一次插入后根据返回的页数继续进行请求和插入
// 插入第一页的数据
failedCount += insertCheckTicketByMongoDB(mapList, PAGE_SIZE, pageNumber);
pageNumber++;
// 获得总页数
int totalPages = ticketingSamplingRespVO.getTotalPages();
// 处理剩余页的数据
for (; pageNumber <= totalPages; pageNumber++) {
ticketingSamplingReqVO.setPageNumber(pageNumber);
str = HttpUtil.post(OBJECT_URL, JSONUtil.toJsonStr(ticketingSamplingReqVO));
ticketingSamplingRespVO = new ObjectMapper().readValue(str, TicketingSamplingRespVO.class);
if (!ticketingSamplingRespVO.isPassflag()) {
XxlJobHelper.log("抽数服务:销售数据抽数失败,返回消息:" + ticketingSamplingRespVO.getRtnMsg());
XxlJobHelper.handleFail("销售数据抽数失败,请重试");
return;
}
mapList = ticketingSamplingRespVO.getDataMapList();
// 对最后一页进行处理得到最后一页的实际条数
if (pageNumber != totalPages) {
failedCount += insertCheckTicketByMongoDB(mapList, PAGE_SIZE, pageNumber);
} else {
failedCount += insertCheckTicketByMongoDB(mapList, totalRows % PAGE_SIZE, pageNumber);
}
}
}
XxlJobHelper.log("抽数服务:检票数据抽数正常结束!总共:{}条数据,{}条数据插入失败!", totalRows, failedCount);
}
/**
* 插入销售数据的方法mysql
* @param mapList 数据集合数组
* @param listSize 实际个数
* @param pageNumber 每页显示条数
@ -168,6 +306,99 @@ public class TicketDataMigration {
int failedCount = 0;
for (int i = 0; i < listSize; i++) {
Map<String, Object> currentMap = mapList.get(i);
SaleDataSaveReqDTO saleData = mapToSaleDataSaveReqDTO(currentMap);
if (ticketSaleDataApi.createSaleData(saleData).getCode() != 0) {
failedCount++;
XxlJobHelper.log("第{}条数据插入失败,数据值为:{}", ((pageNumber - 1) * PAGE_SIZE + i + 1), currentMap);
}
}
return failedCount;
}
/**
* 插入检票数据的方法mysql
* @param mapList 数据集合数组
* @param listSize 实际个数
* @param pageNumber 每页显示条数
* @return int 失败条数
*/
private int insertCheckTicket(List<Map<String, Object>> mapList, int listSize, int pageNumber) {
int failedCount = 0;
for (int i = 0; i < listSize; i++) {
Map<String, Object> currentMap = mapList.get(i);
// 把获取到的数据转为插入检票的参数类型
CheckTicketSaveReqDTO checkTicket = mapToCheckTicketSaveReqDTO(currentMap);
if (ticketCheckTicketApi.createCheckTicket(checkTicket).getCode() != 0) {
failedCount++;
XxlJobHelper.log("第{}条数据插入失败,数据值为:{}", ((pageNumber - 1) * PAGE_SIZE + i + 1), currentMap);
}
}
return failedCount;
}
/**
* 插入销售数据的方法mongodb
* @param mapList 数据集合数组
* @param listSize 实际个数
* @param pageNumber 每页显示条数
* @return int 失败条数
*/
private int insertSaleDataByMongoDB(List<Map<String, Object>> mapList, int listSize, int pageNumber) {
int failedCount = 0;
for (int i = 0; i < listSize; i++) {
Map<String, Object> currentMap = mapList.get(i);
SaleDataSaveReqDTO saleData = mapToSaleDataSaveReqDTO(currentMap);
if (!"添加成功".equals(ticketSaleDataApi.createSaleDataByMongoDB(saleData).getData())) {
failedCount++;
XxlJobHelper.log("第{}条数据插入失败,数据值为:{}", ((pageNumber - 1) * PAGE_SIZE + i + 1), currentMap);
}
}
return failedCount;
}
/**
* 插入检票数据的方法mongodb
* @param mapList 数据集合数组
* @param listSize 实际个数
* @param pageNumber 每页显示条数
* @return int 失败条数
*/
private int insertCheckTicketByMongoDB(List<Map<String, Object>> mapList, int listSize, int pageNumber) {
int failedCount = 0;
for (int i = 0; i < listSize; i++) {
Map<String, Object> currentMap = mapList.get(i);
// 把获取到的数据转为插入检票的参数类型
CheckTicketSaveReqDTO checkTicket = mapToCheckTicketSaveReqDTO(currentMap);
if (!"添加成功".equals(ticketCheckTicketApi.createCheckTicketByMongoDB(checkTicket).getData())) {
failedCount++;
XxlJobHelper.log("第{}条数据插入失败,数据值为:{}", ((pageNumber - 1) * PAGE_SIZE + i + 1), currentMap);
}
}
return failedCount;
}
/**
* 集合转检票类型
* @param currentMap 当前集合
* @return cn.iocoder.yudao.module.ticket.api.checkticket.dto.CheckTicketSaveReqDTO 插入检票数据的参数
*/
private CheckTicketSaveReqDTO mapToCheckTicketSaveReqDTO(Map<String, Object> currentMap){
CheckTicketSaveReqDTO checkTicket = new CheckTicketSaveReqDTO();
checkTicket.setDataId((String) currentMap.get("dataId"));
checkTicket.setTicket((String) currentMap.get("ticket"));
checkTicket.setCheckticketdate((String) currentMap.get("checkticketdate"));
checkTicket.setChecktickettime((String) currentMap.get("checktickettime"));
checkTicket.setPersoncount((Integer) currentMap.get("personcount"));
checkTicket.setCheckstation((String) currentMap.get("checkstation"));
checkTicket.setCheckstationname((String) currentMap.get("checkstationname"));
checkTicket.setSdshipping((String) currentMap.get("sdshipping"));
checkTicket.setSalepropetyvaluename((String) currentMap.get("salepropetyvaluename"));
checkTicket.setTenant_id(1L);
return checkTicket;
}
/**
* 集合转售票类型
* @param currentMap 当前集合
* @return cn.iocoder.yudao.module.ticket.api.checkticket.dto.CheckTicketSaveReqDTO 插入检票数据的参数
*/
private SaleDataSaveReqDTO mapToSaleDataSaveReqDTO(Map<String, Object> currentMap){
SaleDataSaveReqDTO saleData = new SaleDataSaveReqDTO();
saleData.setDataId((String) currentMap.get("dataId"));
saleData.setSdno((String) currentMap.get("sdno"));
@ -185,40 +416,6 @@ public class TicketDataMigration {
saleData.setItemname((String) currentMap.get("itemname"));
saleData.setSalepropetyvaluename((String) currentMap.get("salepropetyvaluename"));
saleData.setTenant_id(1L);
if (ticketSaleDataApi.createSaleData(saleData).getCode() != 0) {
failedCount++;
XxlJobHelper.log("第{}条数据插入失败,数据值为:{}", ((pageNumber - 1) * PAGE_SIZE + i + 1), currentMap);
}
}
return failedCount;
}
/**
* 插入检票数据的方法
* @param mapList 数据集合数组
* @param listSize 实际个数
* @param pageNumber 每页显示条数
* @return int 失败条数
*/
private int insertCheckTicket(List<Map<String, Object>> mapList, int listSize, int pageNumber) {
int failedCount = 0;
for (int i = 0; i < listSize; i++) {
Map<String, Object> currentMap = mapList.get(i);
CheckTicketSaveReqDTO checkTicket = new CheckTicketSaveReqDTO();
checkTicket.setDataId((String) currentMap.get("dataId"));
checkTicket.setTicket((String) currentMap.get("ticket"));
checkTicket.setCheckticketdate((String) currentMap.get("checkticketdate"));
checkTicket.setChecktickettime((String) currentMap.get("checktickettime"));
checkTicket.setPersoncount((Integer) currentMap.get("personcount"));
checkTicket.setCheckstation((String) currentMap.get("checkstation"));
checkTicket.setCheckstationname((String) currentMap.get("checkstationname"));
checkTicket.setSdshipping((String) currentMap.get("sdshipping"));
checkTicket.setSalepropetyvaluename((String) currentMap.get("salepropetyvaluename"));
checkTicket.setTenant_id(1L);
if (ticketCheckTicketApi.createCheckTicket(checkTicket).getCode() != 0) {
failedCount++;
XxlJobHelper.log("第{}条数据插入失败,数据值为:{}", ((pageNumber - 1) * PAGE_SIZE + i + 1), currentMap);
}
}
return failedCount;
return saleData;
}
}