admin
2024-10-16 62a447d89331aee1feae7724c7616aa1bb2cfe79
fanli/src/main/java/com/yeshi/fanli/service/manger/order/OrderMoneyStatisticManager.java
@@ -1,221 +1,221 @@
package com.yeshi.fanli.service.manger.order;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.yeshi.fanli.dao.mybatis.order.UserOrderDailyStatisticDTO;
import com.yeshi.fanli.dao.mybatis.order.UserOrderDailyStatisticMapper;
import com.yeshi.fanli.entity.bus.user.ThreeSaleDetail;
import com.yeshi.fanli.entity.order.OrderMoneyDailyCount;
import com.yeshi.fanli.entity.order.OrderMoneyDailyCount.SourceTypeEnum;
import com.yeshi.fanli.entity.order.dividents.TeamDividentsSourceOrderUserMap;
import com.yeshi.fanli.service.inter.order.OrderMoneyDailyCountService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleDetailService;
import com.yeshi.fanli.util.TimeUtil;
@Component
public class OrderMoneyStatisticManager {
   @Resource
   private UserOrderDailyStatisticMapper userOrderDailyStatisticMapper;
   @Resource
   private OrderMoneyDailyCountService orderMoneyDailyCountService;
   @Resource
   private ThreeSaleDetailService threeSaleDetailService;
   private static SourceTypeEnum getSourceTypeByDesc(String name) {
      for (SourceTypeEnum type : SourceTypeEnum.values()) {
         if (type.getDesc().equalsIgnoreCase(name))
            return type;
      }
      return null;
   }
   /**
    * 同步自购与分享赚相关数据
    * @Title: syncFanliAndShareData
    * @Description:
    * @param startTime
    * @param endTime
    * void 返回类型
    * @throws
    */
   public void syncFanliAndShareData(Date startTime, Date endTime) {
      List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper
            .listFanLiAndShareByPlaceOrderTime(startTime, endTime);
      for (UserOrderDailyStatisticDTO dto : dtoList) {
         OrderMoneyDailyCount count = new OrderMoneyDailyCount();
         count.setUid(dto.getUid());
         count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
         if (dto.getMoney() != null)
            count.setIncome(
                  dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
         count.setOrderNum(dto.getOrderCount());
         count.setSourceType(getSourceTypeByDesc(dto.getType()));
         count.setYearMonth(
               TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
         orderMoneyDailyCountService.saveDailyCount(count);
      }
   }
   /**
    * 同步团队订单相关数据(1级/2级)
    * @Title: syncTeamData
    * @Description:
    * @param startTime
    * @param endTime
    * void 返回类型
    * @throws
    */
   public void syncTeamData(Date startTime, Date endTime) {
      List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper.listTeamByPlaceOrderTime(startTime,
            endTime);
      for (UserOrderDailyStatisticDTO dto : dtoList) {
         OrderMoneyDailyCount count = new OrderMoneyDailyCount();
         count.setUid(dto.getUid());
         count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
         if (dto.getMoney() != null)
            count.setIncomeTeam(
                  dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
         count.setOrderNumTeam(dto.getOrderCount());
         count.setSourceType(getSourceTypeByDesc(dto.getType()));
         count.setYearMonth(
               TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
         orderMoneyDailyCountService.saveDailyCount(count);
      }
   }
   /**
    * 同步订单数量相关数据
    * @Title: syncOrderNum
    * @Description:
    * @param minTime
    * @param maxTime
    * void 返回类型
    * @throws
    */
   // 同步订单数量数据
   public void syncOrderNum(Date minTime, Date maxTime) {
      List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper.listOrderCountByPlaceOrderTime(minTime,
            maxTime);
      Set<Long> uidSets = new HashSet<>();
      // 统计今天的数据
      for (UserOrderDailyStatisticDTO dto : dtoList) {
         uidSets.add(dto.getUid());
      }
      Map<Long, List<ThreeSaleDetail>> bossMap = new HashMap<>();
      Map<String, Integer> countMap = new HashMap<>();
      // 聚合数据
      for (UserOrderDailyStatisticDTO dto : dtoList) {
         if (bossMap.get(dto.getUid()) == null) {
            List<ThreeSaleDetail> list = threeSaleDetailService.listByWorkerUid(dto.getUid());
            bossMap.put(dto.getUid(), list);
         }
         List<ThreeSaleDetail> tsList = bossMap.get(dto.getUid());
         if (tsList != null)
            for (ThreeSaleDetail tsd : tsList) {
               String key = tsd.getBossUid() + "#" + dto.getDay() + "#";
               if (tsd.getLevel() < 3)
                  key += tsd.getLevel();
               else
                  key += 3;
               if (countMap.get(key) == null)
                  countMap.put(key, 0);
               countMap.put(key, countMap.get(key) + dto.getOrderCount());
            }
      }
      // 插入数据
      for (Iterator<String> its = countMap.keySet().iterator(); its.hasNext();) {
         String key = its.next();
         String uid = key.split("#")[0];
         String day = key.split("#")[1];
         int level = Integer.parseInt(key.split("#")[2]);
         OrderMoneyDailyCount count = new OrderMoneyDailyCount();
         count.setUid(Long.parseLong(uid));
         count.setCountDay(new Date(TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd")));
         if (level == 1)
            count.setDirectOrderNum(countMap.get(key));
         else if (level == 2)
            count.setInDirectOrderNum(countMap.get(key));
         else
            count.setBeyondOrderNum(countMap.get(key));
         count.setYearMonth(TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd"), "yyyy-MM"));
         orderMoneyDailyCountService.saveDailyCount(count);
      }
   }
   /**
    * 同步分红订单统计数据
    * @Title: syncDividents
    * @Description:
    * @param minTime
    * @param maxTime
    * void 返回类型
    * @throws
    */
   public void syncDividents(Date minTime, Date maxTime) {
      List<UserOrderDailyStatisticDTO> list = userOrderDailyStatisticMapper.listDividentsOrderByCreateTime(minTime,
            maxTime);
      List<UserOrderDailyStatisticDTO> countList = userOrderDailyStatisticMapper
            .listDividentsOrderCountByCreateTime(minTime, maxTime);
      Map<String, Integer> countMap = new HashMap<>();
      for (UserOrderDailyStatisticDTO dto : countList) {
         countMap.put(dto.getDay() + "#" + dto.getUid(), dto.getOrderCount());
      }
      if (list != null)
         for (UserOrderDailyStatisticDTO dto : list) {
            OrderMoneyDailyCount count = new OrderMoneyDailyCount();
            count.setUid(dto.getUid());
            count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
            if (Integer.parseInt(dto.getType()) == TeamDividentsSourceOrderUserMap.TYPE_TEAM_DIVIDENTS) {
               count.setTeamReward(dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP)
                     .multiply(new BigDecimal(100)).intValue());
               count.setTeamRewardNum(dto.getOrderCount());
            } else {
               count.setTeamSubsidy(dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP)
                     .multiply(new BigDecimal(100)).intValue());
               count.setTeamSubsidyNum(dto.getOrderCount());
            }
            Integer totalCount = countMap.get(dto.getDay() + "#" + dto.getUid());
            if(totalCount!=null){
               count.setTeamDividentsOrderNum(totalCount);
            }
            count.setYearMonth(
                  TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
            if (dto.getDay().equalsIgnoreCase("2020-05-03") && dto.getUid() == 389677L) {
               System.out.println(dto);
            }
            orderMoneyDailyCountService.saveDailyCount(count);
         }
   }
}
package com.yeshi.fanli.service.manger.order;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.yeshi.fanli.dao.mybatis.order.UserOrderDailyStatisticDTO;
import com.yeshi.fanli.dao.mybatis.order.UserOrderDailyStatisticMapper;
import com.yeshi.fanli.entity.bus.user.ThreeSaleDetail;
import com.yeshi.fanli.entity.order.OrderMoneyDailyCount;
import com.yeshi.fanli.entity.order.SourceTypeEnum;
import com.yeshi.fanli.entity.order.dividents.TeamDividentsSourceOrderUserMap;
import com.yeshi.fanli.service.inter.order.OrderMoneyDailyCountService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleDetailService;
import org.yeshi.utils.TimeUtil;
@Component
public class OrderMoneyStatisticManager {
   @Resource
   private UserOrderDailyStatisticMapper userOrderDailyStatisticMapper;
   @Resource
   private OrderMoneyDailyCountService orderMoneyDailyCountService;
   @Resource
   private ThreeSaleDetailService threeSaleDetailService;
   private static SourceTypeEnum getSourceTypeByDesc(String name) {
      for (SourceTypeEnum type : SourceTypeEnum.values()) {
         if (type.getDesc().equalsIgnoreCase(name))
            return type;
      }
      return null;
   }
   /**
    * 同步自购与分享赚相关数据
    * @Title: syncFanliAndShareData
    * @Description:
    * @param startTime
    * @param endTime
    * void 返回类型
    * @throws
    */
   public void syncFanliAndShareData(Date startTime, Date endTime) {
      List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper
            .listFanLiAndShareByPlaceOrderTime(startTime, endTime);
      for (UserOrderDailyStatisticDTO dto : dtoList) {
         OrderMoneyDailyCount count = new OrderMoneyDailyCount();
         count.setUid(dto.getUid());
         count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
         if (dto.getMoney() != null)
            count.setIncome(
                  dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
         count.setOrderNum(dto.getOrderCount());
         count.setSourceType(getSourceTypeByDesc(dto.getType()));
         count.setYearMonth(
               TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
         orderMoneyDailyCountService.saveDailyCount(count);
      }
   }
   /**
    * 同步团队订单相关数据(1级/2级)
    * @Title: syncTeamData
    * @Description:
    * @param startTime
    * @param endTime
    * void 返回类型
    * @throws
    */
   public void syncTeamData(Date startTime, Date endTime) {
      List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper.listTeamByPlaceOrderTime(startTime,
            endTime);
      for (UserOrderDailyStatisticDTO dto : dtoList) {
         OrderMoneyDailyCount count = new OrderMoneyDailyCount();
         count.setUid(dto.getUid());
         count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
         if (dto.getMoney() != null)
            count.setIncomeTeam(
                  dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
         count.setOrderNumTeam(dto.getOrderCount());
         count.setSourceType(getSourceTypeByDesc(dto.getType()));
         count.setYearMonth(
               TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
         orderMoneyDailyCountService.saveDailyCount(count);
      }
   }
   /**
    * 同步订单数量相关数据
    * @Title: syncOrderNum
    * @Description:
    * @param minTime
    * @param maxTime
    * void 返回类型
    * @throws
    */
   // 同步订单数量数据
   public void syncOrderNum(Date minTime, Date maxTime) {
      List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper.listOrderCountByPlaceOrderTime(minTime,
            maxTime);
      Set<Long> uidSets = new HashSet<>();
      // 统计今天的数据
      for (UserOrderDailyStatisticDTO dto : dtoList) {
         uidSets.add(dto.getUid());
      }
      Map<Long, List<ThreeSaleDetail>> bossMap = new HashMap<>();
      Map<String, Integer> countMap = new HashMap<>();
      // 聚合数据
      for (UserOrderDailyStatisticDTO dto : dtoList) {
         if (bossMap.get(dto.getUid()) == null) {
            List<ThreeSaleDetail> list = threeSaleDetailService.listByWorkerUid(dto.getUid());
            bossMap.put(dto.getUid(), list);
         }
         List<ThreeSaleDetail> tsList = bossMap.get(dto.getUid());
         if (tsList != null)
            for (ThreeSaleDetail tsd : tsList) {
               String key = tsd.getBossUid() + "#" + dto.getDay() + "#";
               if (tsd.getLevel() < 3)
                  key += tsd.getLevel();
               else
                  key += 3;
               if (countMap.get(key) == null)
                  countMap.put(key, 0);
               countMap.put(key, countMap.get(key) + dto.getOrderCount());
            }
      }
      // 插入数据
      for (Iterator<String> its = countMap.keySet().iterator(); its.hasNext();) {
         String key = its.next();
         String uid = key.split("#")[0];
         String day = key.split("#")[1];
         int level = Integer.parseInt(key.split("#")[2]);
         OrderMoneyDailyCount count = new OrderMoneyDailyCount();
         count.setUid(Long.parseLong(uid));
         count.setCountDay(new Date(TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd")));
         if (level == 1)
            count.setDirectOrderNum(countMap.get(key));
         else if (level == 2)
            count.setInDirectOrderNum(countMap.get(key));
         else
            count.setBeyondOrderNum(countMap.get(key));
         count.setYearMonth(TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd"), "yyyy-MM"));
         orderMoneyDailyCountService.saveDailyCount(count);
      }
   }
   /**
    * 同步分红订单统计数据
    * @Title: syncDividents
    * @Description:
    * @param minTime
    * @param maxTime
    * void 返回类型
    * @throws
    */
   public void syncDividents(Date minTime, Date maxTime) {
      List<UserOrderDailyStatisticDTO> list = userOrderDailyStatisticMapper.listDividentsOrderByCreateTime(minTime,
            maxTime);
      List<UserOrderDailyStatisticDTO> countList = userOrderDailyStatisticMapper
            .listDividentsOrderCountByCreateTime(minTime, maxTime);
      Map<String, Integer> countMap = new HashMap<>();
      for (UserOrderDailyStatisticDTO dto : countList) {
         countMap.put(dto.getDay() + "#" + dto.getUid(), dto.getOrderCount());
      }
      if (list != null)
         for (UserOrderDailyStatisticDTO dto : list) {
            OrderMoneyDailyCount count = new OrderMoneyDailyCount();
            count.setUid(dto.getUid());
            count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
            if (Integer.parseInt(dto.getType()) == TeamDividentsSourceOrderUserMap.TYPE_TEAM_DIVIDENTS) {
               count.setTeamReward(dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP)
                     .multiply(new BigDecimal(100)).intValue());
               count.setTeamRewardNum(dto.getOrderCount());
            } else {
               count.setTeamSubsidy(dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP)
                     .multiply(new BigDecimal(100)).intValue());
               count.setTeamSubsidyNum(dto.getOrderCount());
            }
            Integer totalCount = countMap.get(dto.getDay() + "#" + dto.getUid());
            if(totalCount!=null){
               count.setTeamDividentsOrderNum(totalCount);
            }
            count.setYearMonth(
                  TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
            if (dto.getDay().equalsIgnoreCase("2020-05-03") && dto.getUid() == 389677L) {
               System.out.println(dto);
            }
            orderMoneyDailyCountService.saveDailyCount(count);
         }
   }
}