admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/user/invite/TeamFansInfoServiceImpl.java
@@ -10,72 +10,380 @@
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;
   @Override
   public void save(TeamFansInfo record) {
      teamFansInfoDao.save(record);
   @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;
   /**
    * 初始化数据
    * @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) {
      LogHelper.test("addFansInfo-新增-" + workerId);
      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());
      focusInfo.setCrateTime(new Date());
      // 初始化值
      initData(focusInfo);
      teamFansInfoDao.save(focusInfo);
   }
   @Override
   public void updateMemoName(Long workerId, int type, String memoName) {
      teamFansInfoDao.updateMemoName(workerId, type, memoName);
      LogHelper.test("updateMemoName-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      if (type == 1) {
         teamFansInfo.setMemoName(memoName);
      } else {
         teamFansInfo.setMemoNameSup(memoName);
      }
      update(teamFansInfo);
   }
   @Override
   public void updateTags(Long workerId, int type, String tags) {
      teamFansInfoDao.updateTags(workerId, type, tags);
      LogHelper.test("updateTags-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      if (type == 1) {
         teamFansInfo.setTags(tags);
      } else {
         teamFansInfo.setTagsSup(tags);
      }
      update(teamFansInfo);
   }
   @Override
   public void updateNickName(Long workerId, String nickName) {
      teamFansInfoDao.updateNickName(workerId, nickName);
      LogHelper.test("updateNickName-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setNickName(nickName);
      update(teamFansInfo);
   }
   @Override
   public void updateStateValid(Long workerId, boolean stateValid) {
      teamFansInfoDao.updateStateValid(workerId, stateValid);
      LogHelper.test("updateStateValid-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setStateValid(stateValid);
      update(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);
      LogHelper.test("updateTaobaoBind-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setTaobaoBind(taobaoBind);
      update(teamFansInfo);
   }
   @Override
   public void updateActiveTime(Long workerId, Date activeTime) {
      teamFansInfoDao.updateActiveTime(workerId, activeTime);
      LogHelper.test("updateActiveTime-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setActiveTime(activeTime);
      update(teamFansInfo);
   }
   @Override
   public void updateFansNum(Long workerId) {
      long firstTeam = threeSaleSerivce.countFirstTeam(workerId);
      long secondTeam = threeSaleSerivce.countSecondTeam(workerId);
      int fansNum = (int) (firstTeam + secondTeam);
      teamFansInfoDao.updateFansNum(workerId, fansNum);
   public void updateWeixinId(Long workerId, String weixinId) {
      LogHelper.test("updateWeixinId-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setWeixinId(weixinId);
      if (StringUtil.isNullOrEmpty(weixinId)) {
         teamFansInfo.setWeixinIdExist(false);
      } else {
         teamFansInfo.setWeixinIdExist(true);
      }
      update(teamFansInfo);
   }
   @Override
   public void updateInviteCode(Long workerId, String inviteCode) {
      LogHelper.test("updateInviteCode-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setInviteCode(inviteCode);
      update(teamFansInfo);
   }
   @Override
   public void updatePhone(Long workerId, String phone) {
      LogHelper.test("updatePhone-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setPhone(phone);
      update(teamFansInfo);
   }
   @Override
   public void updateLevel(Long workerId, UserLevelEnum level) {
      LogHelper.test("updateLevel-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setLevel(level);
      teamFansInfo.setWorkerId(workerId);
      update(teamFansInfo);
   }
   @Override
   public void updatePhoneOpen(Long workerId, boolean phoneOpen) {
      LogHelper.test("updatePhoneOpen-更新-" + workerId);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setPhoneOpen(phoneOpen);
      update(teamFansInfo);
   }
   @Override
   public void updateIncome(Long workerId, BigDecimal income) {
      teamFansInfoDao.updateIncome(workerId, income);
      LogHelper.test("updateIncome-更新-" + workerId);
      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());
      update(teamFansInfo);
   }
   @Override
   public void updateFansNum(Long workerId) {
      LogHelper.test("updateFansNum-更新-" + workerId);
      long firstTeam = threeSaleSerivce.countFirstTeam(workerId);
      long secondTeam = threeSaleSerivce.countSecondTeam(workerId);
      int fansNum = (int) (firstTeam + secondTeam);
      TeamFansInfo teamFansInfo = new TeamFansInfo();
      teamFansInfo.setWorkerId(workerId);
      teamFansInfo.setFansNum(fansNum);
      update(teamFansInfo);
   }
   private void update(TeamFansInfo record) {
      if (record == null || record.getWorkerId() == null) {
         return;
      }
      // 无邀请关系 不操作
      ThreeSale threeSale = threeSaleSerivce.selectByWorkerId(record.getWorkerId());
      if (threeSale == null)
         return;
      record.setId(record.getWorkerId().toString());
      teamFansInfoDao.updateSelective(record);
   }
   
@@ -83,14 +391,19 @@
   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);
   }
   @Override
   public List<TeamFansInfo> queryByUserInfo(String key) {
      return teamFansInfoDao.queryByUserInfo(key);
   }
}