| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- package com.cyksj.task;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.date.DateUtil;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.cyksj.common.constant.Constant;
- import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
- import com.cyksj.mapper.GoodsDonMapper;
- import com.cyksj.mapper.RegisterAccountDataMapper;
- import com.cyksj.mapper.RegisterOrderDonMapper;
- import com.cyksj.mapper.manage.statistics.AppleOneDataMapper;
- import com.cyksj.mapper.manage.statistics.RealGoodsDataMapper;
- import com.cyksj.mapper.manage.statistics.StatisticsUserDataMapper;
- import com.cyksj.mapper.manage.statistics.StreamingAccountDataMapper;
- import com.cyksj.model.entity.*;
- import com.cyksj.model.views.CorpUserTagView;
- import com.cyksj.service.mange.CmsAccountService;
- import com.cyksj.service.mange.CmsGoodsDonSkuService;
- import com.cyksj.service.order.OrderDonService;
- import com.ejlchina.searcher.BeanSearcher;
- import com.ejlchina.searcher.param.Operator;
- import com.ejlchina.searcher.util.MapUtils;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import java.math.BigDecimal;
- import java.util.*;
- import java.util.stream.Collectors;
- /*
- *项目名: netflix
- *文件名: StatisticsDataScheduler
- *创建者: JavaZou
- *创建时间:2022/10/18 12:07
- */
- @Component
- @Slf4j
- @RequiredArgsConstructor
- public class StatisticsDataScheduler {
- private final OrderDonService orderDonService;
- private final CmsGoodsDonSkuService goodsDonSkuService;
- private final CmsAccountService accountService;
- private static final GlobalThreadPoolTaskExecutor TASK_POOL = GlobalThreadPoolTaskExecutor.getInstance();
- private final AppleOneDataMapper appleOneDataMapper;
- private final StreamingAccountDataMapper streamingAccountDataMapper;
- private final static String APPLE_ONE = "Apple One";
- public static List<String> noOrderStatus = List.of("close", "noPayment");
- public static List<String> payType = List.of("月", "季", "年");
- private final static List<String> registerAccount = List.of("apple", "spotify", "google");
- private final GoodsDonMapper goodsDonMapper;
- private final RealGoodsDataMapper realGoodsDataMapper;
- private final RegisterOrderDonMapper registerOrderDonMapper;
- private final RegisterAccountDataMapper registerAccountDataMapper;
- private final StatisticsUserDataMapper statisticsUserDataMapper;
- private final BeanSearcher beanSearcher;
- /**
- * 后台数据统计 定时器
- */
- @Scheduled(cron = "0 0 1 * * ?")
- public void statisticsData() {
- Date thisZeroDate = DateUtil.beginOfDay(DateTime.now());
- Date yesZeroDate = DateTime.of(thisZeroDate.getTime() - Constant.DAY_MILLS);
- //统计AppleOne数据
- TASK_POOL.execute(() -> statisticsAppleOneData(yesZeroDate, thisZeroDate, false));
- //统计流媒体账号数据
- TASK_POOL.execute(() -> statisticsStreamingAccountData(yesZeroDate, thisZeroDate, false));
- //统计实物数据
- TASK_POOL.execute(() -> statisticsRealGoodsData(yesZeroDate, thisZeroDate, false));
- //注册账号数据
- TASK_POOL.execute(() -> statisticsRegisterAccountData(yesZeroDate, thisZeroDate, false));
- //用户数据统计
- statisticsUserData(yesZeroDate, thisZeroDate);
- }
- /**
- * 用户数据统计
- */
- private void statisticsUserData(Date yesZeroDate, Date thisZeroDate) {
- StatisticsUserData yesData = statisticsUserDataMapper.selectOne(Wrappers.lambdaQuery(StatisticsUserData.class).orderByDesc(StatisticsUserData::getId).select(StatisticsUserData::getNumOfUser, StatisticsUserData::getPcNumOfUser).last("limit 1"));
- Integer yesNumOfUser = yesData == null ? 0 : yesData.getNumOfUser();
- Integer yesPcNumOfUser = yesData == null ? 0 : yesData.getPcNumOfUser();
- StatisticsUserData statisticsUserData = new StatisticsUserData();
- statisticsUserData.setNumOfNewUser(statisticsUserDataMapper.getNumOfNewUser(yesZeroDate, thisZeroDate));
- statisticsUserData.setNumOfActiveUser(statisticsUserDataMapper.getNumOfActiveUser(yesZeroDate, thisZeroDate));
- statisticsUserData.setNumOfUser(yesNumOfUser + statisticsUserData.getNumOfNewUser());
- statisticsUserData.setNumOfDistributeUser(statisticsUserDataMapper.getNumOfDistributeUserToday(yesZeroDate, thisZeroDate));
- List<CorpUserTagView> corpUserLists = Optional.ofNullable(beanSearcher.searchAll(CorpUserTagView.class, MapUtils.builder().field(CorpUserTagView::getCreatedTime, yesZeroDate, thisZeroDate).op(Operator.Between).build())).orElse(new ArrayList<>());
- statisticsUserData.setNumOfCorpUser(corpUserLists.size());
- statisticsUserData.setNumOfCorpEquipmentUser(corpUserLists.stream().filter(data -> "设备".equals(data.getTagName())).collect(Collectors.toList()).size());
- statisticsUserData.setNumOfCorpAccountUser(corpUserLists.stream().filter(data -> "账号".equals(data.getTagName())).collect(Collectors.toList()).size());
- statisticsUserData.setPcNumOfNewUser(statisticsUserDataMapper.getPcNumOfNewUser(yesZeroDate, thisZeroDate));
- statisticsUserData.setPcNumOfActiveUser(statisticsUserDataMapper.getPcNumOfActiveUser(yesZeroDate, thisZeroDate));
- statisticsUserData.setPcNumOfUser(yesPcNumOfUser + statisticsUserData.getPcNumOfNewUser());
- statisticsUserData.setStatisticsDate(yesZeroDate);
- statisticsUserDataMapper.insert(statisticsUserData);
- }
- /**
- * 注册账号数据
- */
- 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
- */
- 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 -> {
- String areaName = sku.getSpecVal().substring(0, sku.getSpecVal().indexOf("区") + 1);
- if (!area.contains(areaName)) {
- area.add(areaName);
- }
- });
- Map<String, List<GoodsDonSku>> areaDataMap = new HashMap<>();
- for (String name : area) {
- List<GoodsDonSku> sku = Optional.ofNullable(goodsDonSkuService.list(Wrappers.lambdaQuery(GoodsDonSku.class).like(GoodsDonSku::getSpecVal, name).select(GoodsDonSku::getId, GoodsDonSku::getGoodsId))).orElse(new ArrayList<>());
- areaDataMap.put(name, sku);
- }
- List<AbstractBackData> appleOneSave = new ArrayList<>();
- List<AppleOneData> appleOneData = new ArrayList<>();
- areaDataMap.forEach((areaName, skus) -> {
- AppleOneData appleOne = new AppleOneData();
- appleOne.setAreaName(areaName);
- //通过skuIds找到对应的appleOne统计数据
- List<Long> skuIds = skus.stream().map(GoodsDonSku::getId).collect(Collectors.toList());
- List<Long> goodsId = skus.stream().map(GoodsDonSku::getGoodsId).distinct().collect(Collectors.toList());
- 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) {
- if (CollUtil.isNotEmpty(orderDons)) {
- //用户人数
- Set<Long> numOfUser = Optional.ofNullable(orderDons.stream().map(OrderDon::getUserId).collect(Collectors.toSet())).orElse(new HashSet<>());
- appleOne.setNumOfUser(numOfUser.size());
- //订单数
- appleOne.setNumOfOrder(orderDons.size());
- //兑换码订单数
- List<OrderDon> exCodeOrders = Optional.ofNullable(orderDons.stream().filter(order -> order.getType() == OrderDon.Type.EX_CODE).collect(Collectors.toList())).orElse(new ArrayList<>());
- appleOne.setNumOfCodeOrder(exCodeOrders.size());
- //金额
- Optional<BigDecimal> money = orderDons.stream().map(OrderDon::getMoney).reduce(BigDecimal::add);
- if (money.isPresent()) {
- appleOne.setMoney(money.get());
- } else appleOne.setMoney(BigDecimal.ZERO);
- //分销订单
- List<OrderDon> distributeOrders = Optional.ofNullable(orderDons.stream().filter(ord -> ord.getIsDistribute()).collect(Collectors.toList())).orElse(new ArrayList<>());
- appleOne.setNumOfDistributeOrder(distributeOrders.size());
- Optional<BigDecimal> distributeMoney = distributeOrders.stream().map(OrderDon::getMoney).reduce(BigDecimal::add);
- appleOne.setDistributeOrderMoney(distributeMoney.isPresent() ? distributeMoney.get() : BigDecimal.ZERO);
- getNumOfOrderByPayType(orderDons, appleOne);
- }
- appleOneSave.add(appleOne);
- }
- /**
- * 统计流媒体账号数据
- */
- 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());
- accountMaps.forEach((goodsId, accountList) -> {
- StreamingAccountData streamingAccount = new StreamingAccountData();
- streamingAccount.setAccountName(accountList.get(0).getTitle());
- streamingAccount.setGoodsId(goodsId);
- streamingAccountDataStatistics(goodsId, streamingAccount, streamingAccountDataSave, yesZeroDate, thisZeroDate);
- });
- if (today) {
- return streamingAccountDataSave;
- }
- for (StreamingAccountData streamingAccountData : streamingAccountDataSave) {
- streamingAccountData.setStatisticsDate(yesZeroDate);
- streamingAccountDataMapper.insert(streamingAccountData);
- }
- return null;
- }
- /**
- * 统计实物数据
- */
- 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()) {
- goodsDons.forEach(goods -> {
- List<GoodsDonSku> skuList = goodsDonSkuService.list(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId()));
- if (!skuList.isEmpty()) {
- skuList.forEach(sku -> {
- RealGoodsData realGoodsData = new RealGoodsData();
- realGoodsData.setGoodsId(goods.getId());
- realGoodsData.setGoodsTitle(goods.getTitle());
- realGoodsData.setSkuId(sku.getId());
- realGoodsData.setSpecVal(sku.getSpecVal());
- //订单
- List<OrderDon> orderDon = Optional.ofNullable(orderDonService.list(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getSkuId, sku.getId()).between(OrderDon::getCreatedTime, yesZeroDate, thisZeroDate).notIn(OrderDon::getStatus, noOrderStatus).select(OrderDon::getUserId, OrderDon::getMoney, OrderDon::getId))).orElse(new ArrayList<>());
- Set<Long> userList = Optional.ofNullable(orderDon.stream().map(OrderDon::getUserId).collect(Collectors.toSet())).orElse(new HashSet<>());
- //用户数
- realGoodsData.setNumOfUser(userList.size());
- //订单数
- realGoodsData.setNumOfOrder(orderDon.size());
- //金额
- Optional<BigDecimal> money = orderDon.stream().map(OrderDon::getMoney).reduce(BigDecimal::add);
- realGoodsData.setMoney(money.isPresent() ? money.get() : BigDecimal.ZERO);
- realGoodsDataList.add(realGoodsData);
- });
- }
- });
- }
- if (today) {
- return realGoodsDataList;
- }
- if (!realGoodsDataList.isEmpty()) {
- realGoodsDataList.forEach(data -> {
- data.setStatisticsDate(yesZeroDate);
- realGoodsDataMapper.insert(data);
- });
- }
- return null;
- }
- /**
- * @param goodsId 平台id
- * @param streamingAccountData
- * @param dataSave 入库实体集合
- * @param yesZeroDate 昨日零时
- * @param thisZeroDate 今日零时
- */
- private void streamingAccountDataStatistics(Long goodsId, StreamingAccountData streamingAccountData, List<StreamingAccountData> dataSave, Date yesZeroDate, Date thisZeroDate) {
- //规格
- List<GoodsDonSku> list = Optional.ofNullable(goodsDonSkuService.list(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goodsId).select(GoodsDonSku::getId))).orElse(new ArrayList<>());
- List<Long> skuIds = list.stream().map(GoodsDonSku::getId).collect(Collectors.toList());
- if (CollUtil.isNotEmpty(skuIds)) {
- List<OrderDon> orderDons = Optional.ofNullable(accountService.getNoAppleOneOrderDon(skuIds, yesZeroDate, thisZeroDate, noOrderStatus)).orElse(new ArrayList<>());
- //用户数
- streamingAccountData.setNumOfUser(orderDons.stream().map(OrderDon::getUserId).collect(Collectors.toSet()).size());
- getNumOfOrderByPayType(orderDons, streamingAccountData);
- //兑换码订单数
- List<OrderDon> exCodeOrders = Optional.ofNullable(orderDons.stream().filter(order -> order.getType() == OrderDon.Type.EX_CODE).collect(Collectors.toList())).orElse(new ArrayList<>());
- streamingAccountData.setNumOfCodeOrder(exCodeOrders.size());
- streamingAccountData.setNumOfOrder(orderDons.size());
- Optional<BigDecimal> reduce = orderDons.stream().map(OrderDon::getMoney).reduce(BigDecimal::add);
- BigDecimal totalMoney = BigDecimal.valueOf(0);
- if (reduce.isPresent()) {
- totalMoney = reduce.get();
- }
- //金额
- streamingAccountData.setMoney(totalMoney);
- //分销订单
- List<OrderDon> distributeOrders = Optional.ofNullable(orderDons.stream().filter(ord -> ord.getIsDistribute()).collect(Collectors.toList())).orElse(new ArrayList<>());
- streamingAccountData.setNumOfDistributeOrder(distributeOrders.size());
- Optional<BigDecimal> distributeMoney = distributeOrders.stream().map(OrderDon::getMoney).reduce(BigDecimal::add);
- streamingAccountData.setDistributeOrderMoney(distributeMoney.isPresent() ? distributeMoney.get() : BigDecimal.ZERO);
- }
- dataSave.add(streamingAccountData);
- }
- /**
- * 根据不同支付条件统计对应月、季、年订单
- */
- private void getNumOfOrderByPayType(List<OrderDon> orderDons, AbstractBackData backData) {
- Integer numOfMonthOrder = 0;
- Integer numOfSeasonOrder = 0;
- Integer numOfYearOrder = 0;
- Map<Long, List<OrderDon>> skuIdMaps = Optional.ofNullable(orderDons.stream().collect(Collectors.groupingBy(OrderDon::getSkuId))).orElse(new HashMap<>());
- if (!skuIdMaps.isEmpty()) {
- List<GoodsDonSku> goodsDonSkus = Optional.ofNullable(goodsDonSkuService.list(Wrappers.lambdaQuery(GoodsDonSku.class).in(GoodsDonSku::getId, skuIdMaps.keySet()))).orElse(new ArrayList<>());
- for (GoodsDonSku donSkus : goodsDonSkus) {
- int size = skuIdMaps.get(donSkus.getId()).size();
- if (donSkus.getSpecVal().contains(payType.get(0)) && !donSkus.getSpecVal().contains(payType.get(2))) {
- numOfMonthOrder += size;
- } else if (donSkus.getSpecVal().contains(payType.get(1))) {
- numOfSeasonOrder += size;
- } else if (donSkus.getSpecVal().contains(payType.get(2))) {
- numOfYearOrder += size;
- }
- }
- }
- //月付
- backData.setNumOfMonthOrder(numOfMonthOrder);
- //季付
- backData.setNumOfSeasonOrder(numOfSeasonOrder);
- //年付
- backData.setNumOfYearOrder(numOfYearOrder);
- }
- }
|