admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
fanli/src/main/java/com/yeshi/fanli/service/impl/user/vip/TeamUserLevelStatisticServiceImpl.java
@@ -1,25 +1,27 @@
package com.yeshi.fanli.service.impl.user.vip;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.yeshi.fanli.dao.mybatis.user.vip.TeamUserLevelStatisticMapper;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.dao.mybatis.user.vip.UserLevelStatisticMapper;
import com.yeshi.fanli.dto.vip.UserLevelStatisticDTO;
import com.yeshi.fanli.entity.bus.user.vip.TeamUserLevelStatistic;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.vip.TeamUserLevelStatisticService;
import com.yeshi.fanli.service.manger.user.UserLevelManager;
import com.yeshi.fanli.util.user.UserLevelUtil;
@Service
public class TeamUserLevelStatisticServiceImpl implements TeamUserLevelStatisticService {
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private UserLevelManager userLevelManager;
@@ -27,62 +29,91 @@
   @Resource
   private TeamUserLevelStatisticMapper teamUserLevelStatisticMapper;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private UserLevelStatisticMapper userLevelStatisticMapper;
   @Override
   public TeamUserLevelStatistic selectByUid(Long uid) {
      return teamUserLevelStatisticMapper.selectByPrimaryKey(uid);
   }
   @Override
   public void initData(Long uid) {
      int state = ThreeSale.STATE_SUCCESS;
      long count = threeSaleSerivce.countFirstTeam(uid, state);
      List<ThreeSale> list = threeSaleSerivce.listFirstTeam(0L, (int) count, uid, state);
      int normalFirstCount = 0;
      int highFirstCount = 0;
      int superFirstCount = 0;
      int tearcherFirstCount = 0;
      for (ThreeSale ts : list) {
         UserLevelEnum level = userLevelManager.getUserLevel(ts.getWorker().getId());
         if (level == UserLevelEnum.normalVIP) {
            normalFirstCount++;
         } else if (level == UserLevelEnum.highVIP) {
            highFirstCount++;
         } else if (level == UserLevelEnum.superVIP) {
            superFirstCount++;
         } else if (level == UserLevelEnum.tearcher) {
            tearcherFirstCount++;
   public List<TeamUserLevelStatistic> listByUids(List<Long> uids) {
      List<TeamUserLevelStatistic> resultList = new ArrayList<>();
      List<TeamUserLevelStatistic> list = teamUserLevelStatisticMapper.listByUids(uids);
      // 放入Map中
      Map<Long, TeamUserLevelStatistic> map = new HashMap<>();
      if (list != null)
         for (TeamUserLevelStatistic s : list) {
            map.put(s.getId(), s);
         }
      for (Long uid : uids) {
         if (map.get(uid) == null) {
            initData(uid);
            TeamUserLevelStatistic statistic = selectByUid(uid);
            resultList.add(statistic);
         } else {
            resultList.add(map.get(uid));
         }
      }
      int normalSecondCount = 0;
      return resultList;
   }
   @Override
   public void initData(Long uid) {
      List<UserLevelStatisticDTO> firstList = userLevelStatisticMapper.statisticFirstTeamLevelCount(uid);
      List<UserLevelStatisticDTO> secondList = userLevelStatisticMapper.statisticSecondTeamLevelCount(uid);
      int daRenFirstCount = 0;
      int highFirstCount = 0;
      int superFirstCount = 0;
      int tearcherFirstCount = 0;
      for (UserLevelStatisticDTO dto : firstList) {
         UserLevelEnum level = UserLevelUtil.getByLevel(dto.getLevel());
         if (level == null)
            level = UserLevelEnum.daRen;
         if (level == UserLevelEnum.daRen || level == UserLevelEnum.normalVIP) {
            daRenFirstCount += dto.getNum();
         } else if (level == UserLevelEnum.highVIP)
            highFirstCount += dto.getNum();
         else if (level == UserLevelEnum.superVIP)
            superFirstCount += dto.getNum();
         else if (level == UserLevelEnum.tearcher)
            tearcherFirstCount += dto.getNum();
      }
      int daRenSecondCount = 0;
      int highSecondCount = 0;
      int superSecondCount = 0;
      int tearcherSecondCount = 0;
      long secondCount = threeSaleSerivce.countSecondTeam(uid, state);
      int pageSize = 1000;
      int toalPage = (int) (secondCount % pageSize == 0 ? secondCount / pageSize : secondCount / pageSize + 1);
      for (int i = 0; i < toalPage; i++) {
         list = threeSaleSerivce.listSecondTeam(i * pageSize, pageSize, uid, state);
         if (list != null)
            for (ThreeSale ts : list) {
               UserLevelEnum level = userLevelManager.getUserLevel(ts.getWorker().getId());
               if (level == UserLevelEnum.normalVIP) {
                  normalSecondCount++;
               } else if (level == UserLevelEnum.highVIP) {
                  highSecondCount++;
               } else if (level == UserLevelEnum.superVIP) {
                  superSecondCount++;
               } else if (level == UserLevelEnum.tearcher) {
                  tearcherSecondCount++;
               }
            }
      for (UserLevelStatisticDTO dto : secondList) {
         UserLevelEnum level = UserLevelUtil.getByLevel(dto.getLevel());
         if (level == null)
            level = UserLevelEnum.daRen;
         if (level == UserLevelEnum.daRen || level == UserLevelEnum.normalVIP) {
            daRenSecondCount += dto.getNum();
         } else if (level == UserLevelEnum.highVIP)
            highSecondCount += dto.getNum();
         else if (level == UserLevelEnum.superVIP)
            superSecondCount += dto.getNum();
         else if (level == UserLevelEnum.tearcher)
            tearcherSecondCount += dto.getNum();
      }
      UserLevelEnum level = userLevelManager.getUserLevel(uid);
      TeamUserLevelStatistic statistic = new TeamUserLevelStatistic();
      statistic.setNormalFirstCount(normalFirstCount);
      statistic.setNormalSecondCount(normalSecondCount);
      statistic.setDaRenFirstCount(daRenFirstCount);
      statistic.setDaRenSecondCount(daRenSecondCount);
      statistic.setHighFirstCount(highFirstCount);
      statistic.setHighSecondCount(highSecondCount);
      statistic.setSuperFirstCount(superFirstCount);
@@ -95,6 +126,22 @@
   }
   @Override
   public void setUserLevel(Long uid, UserLevelEnum level) {
      TeamUserLevelStatistic old = selectByUid(uid);
      if (old == null) {
         initData(uid);
         old = selectByUid(uid);
      }
      TeamUserLevelStatistic update = new TeamUserLevelStatistic();
      update.setId(old.getId());
      update.setLevel(level);
      update.setUpdateTime(new Date());
      teamUserLevelStatisticMapper.updateByPrimaryKeySelective(update);
   }
   @Override
   public void add(TeamUserLevelStatistic statistic) {
      TeamUserLevelStatistic old = selectByUid(statistic.getId());
      if (old == null) {