zoujiajian 3 лет назад
Родитель
Сommit
a490cca661

+ 41 - 23
netflix-service/src/main/java/com/cyksj/service/mange/statistics/impl/StatisticsDataServiceImpl.java

@@ -64,6 +64,8 @@ public class StatisticsDataServiceImpl implements StatisticsDataService {
 
 	private final BeanSearcher beanSearcher;
 
+	private final StatisticsDataScheduler statistics;
+
 	@Override
 	public Result<BackgroundDataView> getStatisticsByCondition(Long startTime, Long endTime) {
 		BackgroundDataView backgroundDataView = new BackgroundDataView();
@@ -92,8 +94,12 @@ public class StatisticsDataServiceImpl implements StatisticsDataService {
 	 * 注册账号数据
 	 */
 	private void getRegisterAccountData(BackgroundDataView backgroundDataView, Date startDate, Date endDate) {
-		List<RegisterAccountData> registerAccountData = realGoodsDataMapper.statisticsRegisterAccountData(startDate, endDate);
-		backgroundDataView.setRegisterAccountDataViews(registerAccountData);
+		if (DateUtil.beginOfDay(DateTime.now()).equals(startDate) && DateUtil.endOfDay(DateTime.now()).equals(endDate)) {
+			backgroundDataView.setRegisterAccountDataViews(statistics.statisticsRegisterAccountData(startDate, endDate, true));
+		}else {
+			List<RegisterAccountData> registerAccountData = realGoodsDataMapper.statisticsRegisterAccountData(startDate, endDate);
+			backgroundDataView.setRegisterAccountDataViews(registerAccountData);
+		}
 	}
 
 	@Override
@@ -128,33 +134,45 @@ public class StatisticsDataServiceImpl implements StatisticsDataService {
 
 	private void getAppleOneDataView(BackgroundDataView backgroundDataView, Date startDate, Date endDate) {
 		List<AppleOneData> appleOneData = appleOneDataMapper.statisticsAppleOneData(startDate, endDate);
-		backgroundDataView.setAppleOneDataViews(appleOneData);
+		if (DateUtil.beginOfDay(DateTime.now()).equals(startDate) && DateUtil.endOfDay(DateTime.now()).equals(endDate)) {
+			backgroundDataView.setAppleOneDataViews(statistics.statisticsAppleOneData(startDate, endDate, true));
+		}else {
+			backgroundDataView.setAppleOneDataViews(appleOneData);
+		}
 	}
 
 	private void getStreamingAccountDataView(BackgroundDataView backgroundDataView, Date startDate, Date endDate) {
-		List<StreamingAccountData> streamingAccountData = streamingAccountDataMapper.statisticsStreamingData(startDate, endDate);
-		Optional.ofNullable(streamingAccountData).ifPresent(list->list.forEach(data->{
-			GoodsDon goodsDon = goodsDonMapper.selectOne(Wrappers.lambdaQuery(GoodsDon.class).eq(GoodsDon::getId, data.getGoodsId()).last("limit 1"));
-			if (goodsDon != null) {
-				data.setAccountName(goodsDon.getTitle());
-			}
-		}));
-		backgroundDataView.setStreamAccountDataViews(streamingAccountData);
+		if (DateUtil.beginOfDay(DateTime.now()).equals(startDate) && DateUtil.endOfDay(DateTime.now()).equals(endDate)) {
+			backgroundDataView.setStreamAccountDataViews(statistics.statisticsStreamingAccountData(startDate, endDate, true));
+		}else {
+			List<StreamingAccountData> streamingAccountData = streamingAccountDataMapper.statisticsStreamingData(startDate, endDate);
+			Optional.ofNullable(streamingAccountData).ifPresent(list->list.forEach(data->{
+				GoodsDon goodsDon = goodsDonMapper.selectOne(Wrappers.lambdaQuery(GoodsDon.class).eq(GoodsDon::getId, data.getGoodsId()).last("limit 1"));
+				if (goodsDon != null) {
+					data.setAccountName(goodsDon.getTitle());
+				}
+			}));
+			backgroundDataView.setStreamAccountDataViews(streamingAccountData);
+		}
 	}
 
 	private void getRealGoodsData(BackgroundDataView backgroundDataView, Date startDate, Date endDate) {
-		List<RealGoodsData> realGoodsData = realGoodsDataMapper.statisticsRealGoodsData(startDate, endDate);
-		Optional.ofNullable(realGoodsData).ifPresent(list -> list.forEach(data -> {
-			GoodsDon goodsDon = goodsDonMapper.selectOne(Wrappers.lambdaQuery(GoodsDon.class).eq(GoodsDon::getId, data.getGoodsId()).last("limit 1"));
-			if (goodsDon != null) {
-				data.setGoodsTitle(goodsDon.getTitle());
-			}
-			GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getId, data.getSkuId()).last("limit 1"));
-			if (goodsDonSku != null) {
-				data.setSpecVal(goodsDonSku.getSpecVal());
-			}
-		}));
-		backgroundDataView.setRealGoodsDataViews(realGoodsData);
+		if (DateUtil.beginOfDay(DateTime.now()).equals(startDate) && DateUtil.endOfDay(DateTime.now()).equals(endDate)) {
+			backgroundDataView.setRealGoodsDataViews(statistics.statisticsRealGoodsData(startDate, endDate, true));
+		}else {
+			List<RealGoodsData> realGoodsData = realGoodsDataMapper.statisticsRealGoodsData(startDate, endDate);
+			Optional.ofNullable(realGoodsData).ifPresent(list -> list.forEach(data -> {
+				GoodsDon goodsDon = goodsDonMapper.selectOne(Wrappers.lambdaQuery(GoodsDon.class).eq(GoodsDon::getId, data.getGoodsId()).last("limit 1"));
+				if (goodsDon != null) {
+					data.setGoodsTitle(goodsDon.getTitle());
+				}
+				GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getId, data.getSkuId()).last("limit 1"));
+				if (goodsDonSku != null) {
+					data.setSpecVal(goodsDonSku.getSpecVal());
+				}
+			}));
+			backgroundDataView.setRealGoodsDataViews(realGoodsData);
+		}
 	}
 
 	private void headerData(BackgroundDataView backgroundDataView, Date startDate, Date endDate) {

+ 43 - 14
netflix-service/src/main/java/com/cyksj/task/StatisticsDataScheduler.java

@@ -82,13 +82,13 @@ public class StatisticsDataScheduler {
 		Date thisZeroDate = DateUtil.beginOfDay(DateTime.now());
 		Date yesZeroDate = DateTime.of(thisZeroDate.getTime() - DAY_MILLS);
 		//统计AppleOne数据
-		TASK_POOL.execute(() -> statisticsAppleOneData(yesZeroDate, thisZeroDate));
+		TASK_POOL.execute(() -> statisticsAppleOneData(yesZeroDate, thisZeroDate, false));
 		//统计流媒体账号数据
-		TASK_POOL.execute(() -> statisticsStreamingAccountData(yesZeroDate, thisZeroDate));
+		TASK_POOL.execute(() -> statisticsStreamingAccountData(yesZeroDate, thisZeroDate, false));
 		//统计实物数据
-		TASK_POOL.execute(()->statisticsRealGoodsData(yesZeroDate, thisZeroDate));
+		TASK_POOL.execute(() -> statisticsRealGoodsData(yesZeroDate, thisZeroDate, false));
 		//注册账号数据
-		TASK_POOL.execute(() -> statisticsRegisterAccountData(yesZeroDate, thisZeroDate));
+		TASK_POOL.execute(() -> statisticsRegisterAccountData(yesZeroDate, thisZeroDate, false));
 		//用户数据统计
 		statisticsUserData(yesZeroDate, thisZeroDate);
 
@@ -123,26 +123,37 @@ public class StatisticsDataScheduler {
 	/**
 	 * 注册账号数据
 	 */
-	private void statisticsRegisterAccountData(Date yesZeroDate, Date thisZeroDate) {
-		registerAccount.forEach(platform->{
+	public List<RegisterAccountData> statisticsRegisterAccountData(Date yesZeroDate, Date thisZeroDate, Boolean today) {
+		List<RegisterAccountData> list = new ArrayList<>();
+		registerAccount.forEach(platform -> {
 			RegisterAccountData entity = Optional.ofNullable(registerOrderDonMapper.registerAccountDataEveryDay(yesZeroDate, thisZeroDate, platform)).orElse(new RegisterAccountData());
 			entity.setPlatform(platform);
 			RegisterAccountData dto = Optional.ofNullable(registerOrderDonMapper.selectRefundOrderDetail(yesZeroDate, thisZeroDate, platform)).orElse(new RegisterAccountData());
 			entity.setNumOfRefundOrder(dto.getNumOfRefundOrder());
 			entity.setRefundMoney(dto.getRefundMoney());
 			entity.setStatisticsDate(yesZeroDate);
+			if (today) {
+				list.add(entity);
+				return;
+			}
 			registerAccountDataMapper.insert(entity);
 		});
+		if (today) {
+			return list;
+		}
+		return null;
 	}
 
 	/**
 	 * 统计AppleOne数据
+	 *
+	 * @return
 	 */
-	private void statisticsAppleOneData(Date yesZeroDate, Date thisZeroDate) {
+	public List<AppleOneData> statisticsAppleOneData(Date yesZeroDate, Date thisZeroDate, Boolean today) {
 		GoodsDon goodsDon = goodsDonMapper.selectOne(Wrappers.lambdaQuery(GoodsDon.class).eq(GoodsDon::getTitle, APPLE_ONE).last("limit 1"));
 		List<GoodsDonSku> list = goodsDonSkuService.list(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goodsDon.getId()).select(GoodsDonSku::getId, GoodsDonSku::getSpecVal));
 		Set<String> area = new HashSet<>();
-		list.forEach(sku->{
+		list.forEach(sku -> {
 			String areaName = sku.getSpecVal().substring(0, sku.getSpecVal().indexOf("区") + 1);
 			if (!area.contains(areaName)) {
 				area.add(areaName);
@@ -155,6 +166,7 @@ public class StatisticsDataScheduler {
 		}
 
 		List<AbstractBackData> appleOneSave = new ArrayList<>();
+		List<AppleOneData> appleOneData = new ArrayList<>();
 		areaDataMap.forEach((areaName, skus) -> {
 			AppleOneData appleOne = new AppleOneData();
 			appleOne.setAreaName(areaName);
@@ -164,10 +176,19 @@ public class StatisticsDataScheduler {
 			List<OrderDon> orderDons = Optional.ofNullable(orderDonService.getAppleOneOrderDons(APPLE_ONE, skuIds, goodsId, noOrderStatus, yesZeroDate, thisZeroDate)).orElse(new ArrayList<>());
 			appleOneDataStatistics(orderDons, appleOne, appleOneSave);
 		});
+
 		for (AbstractBackData appleOne : appleOneSave) {
 			appleOne.setStatisticsDate(yesZeroDate);
+			if (today) {
+				appleOneData.add((AppleOneData) appleOne);
+				continue;
+			}
 			appleOneDataMapper.insert((AppleOneData) appleOne);
 		}
+		if (today) {
+			return appleOneData;
+		}
+		return null;
 	}
 
 	private void appleOneDataStatistics(List<OrderDon> orderDons, AppleOneData appleOne, List<AbstractBackData> appleOneSave) {
@@ -199,26 +220,30 @@ public class StatisticsDataScheduler {
 	/**
 	 * 统计流媒体账号数据
 	 */
-	private void statisticsStreamingAccountData(Date yesZeroDate, Date thisZeroDate) {
+	public List<StreamingAccountData> statisticsStreamingAccountData(Date yesZeroDate, Date thisZeroDate, Boolean today) {
 		List<Account> accounts = accountService.getNoAppleOneAccount(APPLE_ONE);
 		Map<Long, List<Account>> accountMaps = accounts.stream().collect(Collectors.groupingBy(Account::getGoodsId));
-		List<StreamingAccountData> StreamingAccountDataSave = new ArrayList<>(accounts.size());
+		List<StreamingAccountData> streamingAccountDataSave = new ArrayList<>(accounts.size());
 		accountMaps.forEach((goodsId, accountList) -> {
 			StreamingAccountData streamingAccount = new StreamingAccountData();
 			streamingAccount.setAccountName(accountList.get(0).getTitle());
 			streamingAccount.setGoodsId(goodsId);
-			streamingAccountDataStatistics(goodsId, streamingAccount, StreamingAccountDataSave, yesZeroDate, thisZeroDate);
+			streamingAccountDataStatistics(goodsId, streamingAccount, streamingAccountDataSave, yesZeroDate, thisZeroDate);
 		});
-		for (StreamingAccountData streamingAccountData : StreamingAccountDataSave) {
+		if (today) {
+			return streamingAccountDataSave;
+		}
+		for (StreamingAccountData streamingAccountData : streamingAccountDataSave) {
 			streamingAccountData.setStatisticsDate(yesZeroDate);
 			streamingAccountDataMapper.insert(streamingAccountData);
 		}
+		return null;
 	}
 
 	/**
 	 * 统计实物数据
 	 */
-	private void statisticsRealGoodsData(Date yesZeroDate, Date thisZeroDate) {
+	public List<RealGoodsData> statisticsRealGoodsData(Date yesZeroDate, Date thisZeroDate, Boolean today) {
 		List<GoodsDon> goodsDons = goodsDonMapper.selectList(Wrappers.lambdaQuery(GoodsDon.class).eq(GoodsDon::getType, 2));
 		List<RealGoodsData> realGoodsDataList = new ArrayList<>();
 		if (!goodsDons.isEmpty()) {
@@ -246,16 +271,20 @@ public class StatisticsDataScheduler {
 				}
 			});
 		}
+		if (today) {
+			return realGoodsDataList;
+		}
 		if (!realGoodsDataList.isEmpty()) {
 			realGoodsDataList.forEach(data -> {
 				data.setStatisticsDate(yesZeroDate);
 				realGoodsDataMapper.insert(data);
 			});
 		}
+		return null;
 	}
 
 	/**
-	 * @param goodsId             平台id
+	 * @param goodsId              平台id
 	 * @param streamingAccountData
 	 * @param dataSave             入库实体集合
 	 * @param yesZeroDate          昨日零时