admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
fanli/src/main/java/com/yeshi/fanli/service/impl/user/invite/TeamFansInfoServiceImpl.java
@@ -10,59 +10,359 @@
import com.yeshi.fanli.dao.user.invite.TeamFansInfoDao;
import com.yeshi.fanli.dto.user.ThreeSaleFocusDTO;
import com.yeshi.fanli.dto.vip.UserVIPLevel;
import com.yeshi.fanli.entity.bus.user.TeamFansInfo;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.ThreeSaleExtraInfo;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.invite.TeamFansInfoService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleExtraInfoSerivce;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.invite.UserInviteService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.user.UserLevelUtil;
@Service
public class TeamFansInfoServiceImpl implements TeamFansInfoService {
   @Resource
   private TeamFansInfoDao teamFansInfoDao;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private UserInfoExtraService userInfoExtraService;
   @Resource
   private ThreeSaleExtraInfoSerivce threeSaleExtraInfoSerivce;
   @Resource
   private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
   @Resource
   private HongBaoV2CountService hongBaoV2CountService;
   @Resource
   private UserInviteService userInviteService;
   @Resource
   private CommonOrderService commonOrderService;
   @Resource
   private UserActiveLogService userActiveLogService;
   @Override
   public void save(TeamFansInfo record) {
      teamFansInfoDao.save(record);
      if (record == null || record.getWorkerId() == null) {
         return;
      }
      // 无邀请关系 不操作
      ThreeSale threeSale = threeSaleSerivce.selectByWorkerId(record.getWorkerId());
      if (threeSale == null)
         return;
      record.setId(record.getWorkerId().toString());
      TeamFansInfo teamFansInfo = teamFansInfoDao.get(record.getId());
      if (teamFansInfo != null) {
         teamFansInfoDao.updateSelective(record);
         LogHelper.test("队员粉丝信息:更新-" + record.getId());
      } else {
         initData(record);
         record.setCrateTime(new Date());
         LogHelper.test("队员粉丝信息:新增-" + record.getId());
         teamFansInfoDao.save(record);
      }
   }
   /**
    * 初始化数据
    * @param record
    */
   private void initData(TeamFansInfo record) {
      if (StringUtil.isNullOrEmpty(record.getNickName()))
         record.setNickName("");
      if (record.getLevel() == null)
         record.setLevel(UserLevelEnum.daRen);
      if (record.getStateValid() == null)
         record.setStateValid(false);
      if (record.getTaobaoBind() == null)
         record.setTaobaoBind(false);
      if (record.getWeixinId() == null)
         record.setWeixinId("");
      if (record.getPhone() == null)
         record.setPhone("");
      if (record.getPhoneOpen() == null)
         record.setPhoneOpen(true);
      if (record.getInviteCode() == null)
         record.setInviteCode("");
      if (record.getActiveTime() == null)
         record.setActiveTime(new Date());
      if (record.getFansNum() == null)
         record.setFansNum(0);
      if (record.getIncome() == null)
         record.setIncome(0);
      if (record.getIncomeTime() == null)
         record.setIncomeTime(new Date());
      if (record.getMemoName() == null)
         record.setMemoName("");
      if (record.getTags() == null)
         record.setTags("");
      if (record.getMemoNameSup() == null)
         record.setMemoNameSup("");
      if (record.getTagsSup() == null)
         record.setTagsSup("");
      if (StringUtil.isNullOrEmpty(record.getWeixinId())) {
         record.setWeixinIdExist(false);
      } else {
         record.setWeixinIdExist(true);
      }
   }
   @Override
   public void addFansInfo(Long workerId, Long bossId, Date successTime) {
      UserInfo userInfo = userInfoService.selectByPKey(workerId);
      if (userInfo == null) {
         return;
      }
      String phone = "";
      if (!StringUtil.isNullOrEmpty(userInfo.getPhone())) {
         phone = userInfo.getPhone();
      }
      // 备注名
      String memoName = null;
      ThreeSaleExtraInfo threeSaleExtraInfo = threeSaleExtraInfoSerivce.getbyBossIdAndWorkerId(bossId, workerId);
      if (threeSaleExtraInfo != null) {
         if (!StringUtil.isNullOrEmpty(threeSaleExtraInfo.getNickname())) {
            memoName = threeSaleExtraInfo.getNickname();
         }
      }
      // 微信号、活跃时间
      String inviteCode = "";
      String weixinId = "";
      boolean weixinBind = false;
      UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(workerId);
      if (userInfoExtra != null) {
         String weiXin = userInfoExtra.getWeiXin();
         if (!StringUtil.isNullOrEmpty(weiXin)) {
            weixinId = weiXin;
            weixinBind = true;
         }
         String inviteCodeVip = userInfoExtra.getInviteCodeVip();
         String inviteCode2 = userInfoExtra.getInviteCode();
         if (!StringUtil.isNullOrEmpty(inviteCode2)) {
            if (!StringUtil.isNullOrEmpty(inviteCodeVip)) {
               inviteCode = inviteCodeVip;
            } else {
               inviteCode = inviteCode2;
            }
         }
      }
      Date activeTime = new Date(1514736000000L); // 默认20181-1-1;
      UserActiveLog userActiveLog = userActiveLogService.getUserLatestActiveInfo(workerId);
      if (userActiveLog != null) {
         activeTime = userActiveLog.getCreateTime();
      }
      // 淘宝授权
      boolean taobaoBind = false;
      UserExtraTaoBaoInfo taoBaoInfo = userExtraTaoBaoInfoService.getByUid(workerId);
      if (taoBaoInfo != null && !StringUtil.isNullOrEmpty(taoBaoInfo.getRelationId())
            && !StringUtil.isNullOrEmpty(taoBaoInfo.getSpecialId())) {
         taobaoBind = true;
      }
      // 粉丝数量
      long firstTeam = threeSaleSerivce.countFirstTeam(workerId);
      long secondTeam = threeSaleSerivce.countSecondTeam(workerId);
      int fansNum = (int) (firstTeam + secondTeam);
      // 收入
      BigDecimal income = hongBaoV2CountService.sumMoneyArrivalByUidAndNearDay(workerId, 60);
      if (income == null)
         income = new BigDecimal(0);
      // 有效粉丝
      boolean stateValid = false;
      long countValid = commonOrderService.countOrderByUidAndSettled(workerId, BigDecimal.valueOf(1));
      if (countValid > 0) {
         stateValid = true;
      }
      // 查询粉丝等级
      UserLevelEnum level = null;
      UserVIPLevel vipLevel = userInviteService.getVIPLevelByUid(workerId);
      if (vipLevel != null && vipLevel.getLevel() != null) {
         level = UserLevelUtil.getByLevel(vipLevel.getLevel());
      }
      if (level == null) {
         level = UserLevelEnum.daRen;
      }
      level = UserLevelUtil.getShowLevel(level);
      // 是否存在上上级
      Long bossSuperId = null;
      ThreeSale superThree = threeSaleSerivce.selectByWorkerId(bossId);
      if (superThree != null)
         bossSuperId = superThree.getBoss().getId();
      TeamFansInfo focusInfo = new TeamFansInfo();
      focusInfo.setBossId(bossId);
      focusInfo.setWorkerId(workerId);
      focusInfo.setBossSuperId(bossSuperId);
      focusInfo.setJoinTime(successTime);
      focusInfo.setNickName(userInfo.getNickName());
      focusInfo.setMemoName(memoName);
      focusInfo.setWeixinIdExist(weixinBind);
      focusInfo.setWeixinId(weixinId);
      focusInfo.setPhone(phone);
      focusInfo.setPhoneOpen(true);
      focusInfo.setInviteCode(inviteCode);
      focusInfo.setActiveTime(activeTime);
      focusInfo.setFansNum(fansNum);
      focusInfo.setTaobaoBind(taobaoBind);
      focusInfo.setIncome(income.multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_DOWN).intValue());
      focusInfo.setIncomeTime(new Date());
      focusInfo.setStateValid(stateValid);
      focusInfo.setLevel(level);
      focusInfo.setId(workerId.toString());
      save(focusInfo);
   }
   @Override
   public void updateMemoName(Long workerId, int type, String memoName) {
      teamFansInfoDao.updateMemoName(workerId, type, memoName);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      if (type == 1) {
         teamFansInfo.setMemoName(memoName);
      } else {
         teamFansInfo.setMemoNameSup(memoName);
      }
      save(teamFansInfo);
   }
   @Override
   public void updateTags(Long workerId, int type, String tags) {
      teamFansInfoDao.updateTags(workerId, type, tags);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      if (type == 1) {
         teamFansInfo.setTags(tags);
      } else {
         teamFansInfo.setTagsSup(tags);
      }
      save(teamFansInfo);
   }
   @Override
   public void updateNickName(Long workerId, String nickName) {
      teamFansInfoDao.updateNickName(workerId, nickName);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setNickName(nickName);
      save(teamFansInfo);
   }
   @Override
   public void updateStateValid(Long workerId, boolean stateValid) {
      teamFansInfoDao.updateStateValid(workerId, stateValid);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setStateValid(stateValid);
      save(teamFansInfo);
   }
   @Override
   public void updateTaobaoBind(Long workerId, boolean taobaoBind) {
      teamFansInfoDao.updateTaobaoBind(workerId, taobaoBind);
   }
   @Override
   public void updateWeixinBind(Long workerId, boolean weixinBind) {
      teamFansInfoDao.updateWeixinBind(workerId, weixinBind);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setTaobaoBind(taobaoBind);
      save(teamFansInfo);
   }
   @Override
   public void updateActiveTime(Long workerId, Date activeTime) {
      teamFansInfoDao.updateActiveTime(workerId, activeTime);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setActiveTime(activeTime);
      save(teamFansInfo);
   }
   @Override
   public void updateWeixinId(Long workerId, String weixinId) {
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setWeixinId(weixinId);
      if (StringUtil.isNullOrEmpty(weixinId)) {
         teamFansInfo.setWeixinIdExist(false);
      } else {
         teamFansInfo.setWeixinIdExist(true);
      }
      save(teamFansInfo);
   }
   @Override
   public void updateInviteCode(Long workerId, String inviteCode) {
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setInviteCode(inviteCode);
      save(teamFansInfo);
   }
   @Override
   public void updatePhone(Long workerId, String phone) {
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setPhone(phone);
      save(teamFansInfo);
   }
   @Override
   public void updateLevel(Long workerId, UserLevelEnum level) {
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setLevel(level);
      teamFansInfo.setWorkerId(workerId);
      save(teamFansInfo);
   }
   @Override
   public void updatePhoneOpen(Long workerId, boolean phoneOpen) {
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setPhoneOpen(phoneOpen);
      save(teamFansInfo);
   }
   @Override
   public void updateIncome(Long workerId, BigDecimal income) {
      if (income == null) {
         income = BigDecimal.ZERO;
      }
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setIncome(income.multiply(BigDecimal.valueOf(100)).setScale(0, BigDecimal.ROUND_DOWN).intValue());
      teamFansInfo.setIncomeTime(new Date());
      save(teamFansInfo);
   }
   @Override
@@ -70,25 +370,22 @@
      long firstTeam = threeSaleSerivce.countFirstTeam(workerId);
      long secondTeam = threeSaleSerivce.countSecondTeam(workerId);
      int fansNum = (int) (firstTeam + secondTeam);
      teamFansInfoDao.updateFansNum(workerId, fansNum);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setFansNum(fansNum);
      save(teamFansInfo);
   }
   @Override
   public void updateIncome(Long workerId, BigDecimal income) {
      teamFansInfoDao.updateIncome(workerId, income);
   }
   @Override
   public List<TeamFansInfo> query(int start, int count, int type, Long uid, ThreeSaleFocusDTO focusDTO) {
      return teamFansInfoDao.query(start, count, type, uid, focusDTO);
   }
   @Override
   public long count(int type, Long uid, ThreeSaleFocusDTO focusDTO) {
      return teamFansInfoDao.count(type, uid, focusDTO);
   }
   @Override
   public TeamFansInfo getbyWorkerId(Long workerId) {
      return teamFansInfoDao.getbyWorkerId(workerId);