HomePageManager.java 11 KB
Newer Older
张辰's avatar
张辰 committed
1 2
package com.tanpu.community.manager;

3 4 5 6
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
7
import com.tanpu.community.api.beans.qo.FollowQo;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.api.beans.req.homepage.FollowRelReq;
刘基明's avatar
刘基明 committed
9 10
import com.tanpu.community.api.beans.req.homepage.QueryFollowReq;
import com.tanpu.community.api.beans.req.page.Page;
11 12 13
import com.tanpu.community.api.beans.resp.Customer;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoNew;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoOrg;
刘基明's avatar
刘基明 committed
14
import com.tanpu.community.api.enums.OperationTypeEnum;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.api.enums.QueryFollowTypeEnum;
16 17 18 19 20
import com.tanpu.community.dao.entity.community.FollowRelEntity;
import com.tanpu.community.dao.entity.fund.CompanyInfo;
import com.tanpu.community.dao.entity.fund.FundInfo;
import com.tanpu.community.dao.entity.jydb.MfAdvisorscalerank;
import com.tanpu.community.dao.entity.jydb.MfInvestadvisoroutline;
刘基明's avatar
刘基明 committed
21
import com.tanpu.community.dao.entity.user.UserInfoEntity;
22 23 24 25 26 27 28 29
import com.tanpu.community.dao.mapper.community.FollowRelMapper;
import com.tanpu.community.dao.mapper.fund.CompanyInfoMapper;
import com.tanpu.community.dao.mapper.fund.FundInfoMapper;
import com.tanpu.community.dao.mapper.jydb.MfAdvisorscalerankMapper;
import com.tanpu.community.dao.mapper.jydb.MfInvestadvisoroutlineMapper;
import com.tanpu.community.feign.diagnose.FeignClientForDiagnose;
import com.tanpu.community.feign.fatools.FeignClientForFatools;
import com.tanpu.community.feign.tanpuroom.FeignClientForTanpuroom;
刘基明's avatar
刘基明 committed
30
import com.tanpu.community.service.FollowRelService;
刘基明's avatar
刘基明 committed
31
import com.tanpu.community.service.UserInfoService;
刘基明's avatar
刘基明 committed
32
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
33
import com.tanpu.community.util.PageUtils;
34
import org.apache.commons.collections.CollectionUtils;
35 36
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
张辰's avatar
张辰 committed
37 38 39
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

40 41 42
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
刘基明's avatar
刘基明 committed
43
import java.util.stream.Collectors;
张辰's avatar
张辰 committed
44 45 46 47 48

@Service
public class HomePageManager {

    @Autowired
刘基明's avatar
刘基明 committed
49
    private FollowRelService followRelService;
刘基明's avatar
刘基明 committed
50 51
    @Autowired
    private UserInfoService userInfoService;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    @Resource
    private FeignClientForFatools feignClientForFatools;
    @Resource
    private FollowRelMapper followRelMapper;
    @Resource
    private MfInvestadvisoroutlineMapper mfInvestadvisoroutlineMapper;
    @Resource
    private MfAdvisorscalerankMapper mfAdvisorscalerankMapper;
    @Resource
    private CompanyInfoMapper companyInfoMapper;
    @Resource
    private FundInfoMapper fundInfoMapper;
    @Resource
    private FeignClientForTanpuroom feignClientForTanpuroom;
    @Resource
    private FeignClientForDiagnose feignClientForDiagnose;

    //查询 个人中心 相关信息
    public UserInfoNew queryUsersListNew(String userIdMyself, String userId) {
        CommonResp<UserInfoNew> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(StringUtils.isNotBlank(userId) ? userId : userIdMyself);
        if (userInfoNewCommonResp.isNotSuccess())  new BizException("内部接口调用失败");
        UserInfoNew userInfoNew = userInfoNewCommonResp.getData();

        // 关注 按钮的显示逻辑
        if (StringUtils.isNotBlank(userId)) { //查询别人的个人主页
            userInfoNew.setIsShowFollowButton(0);
            //是否已关注
            FollowRelEntity followRelEntity = followRelService.queryRecord(userId, userIdMyself);
            if (ObjectUtils.allNotNull(followRelEntity) && BizStatus.DeleteTag.tag_init == followRelEntity.getDeleteTag()){
                userInfoNew.setShowFollowStatus(1); // 已关注
            } else {
                userInfoNew.setShowFollowStatus(0); // 未关注
            }
        } else {
            // 查询自己的主页
            userInfoNew.setIsShowFollowButton(1);
            userId = userIdMyself;
        }
        //设置粉丝数 关注数
        getFansNUmAndFollowNum(userInfoNew);

        // 主页类型 显示逻辑
        if (50 == userInfoNew.getLevelGrade()){
            userInfoNew.setPersonalCenterType(2);
            // 首席投顾 添加专栏信息 (前端自己取)
            // 成功案例客户
            // 默认显示该投顾管理的客户里,总资产最高的客户
            if (StringUtils.isBlank(userInfoNew.getClientId())) {
                List<Customer> customers = queryUserCustomerList(userId);
                Optional<Customer> first = customers.stream().sorted(Comparator.comparing(Customer::getTotalMarket)).findFirst();
                if (first.isPresent()) {
                    userInfoNew.setClientId(first.get().getUserId());
                } else {
                    userInfoNew.setClientId(null);
                }
            }
        } else if (2 == userInfoNew.getUserType()){
            // 机构账号
            userInfoNew.setPersonalCenterType(3);
            if (0 == userInfoNew.getBelongOrgType() && StringUtils.isNotBlank(userInfoNew.getBelongOrgId())){ // 公募
                //设置 基金数
                MfAdvisorscalerank mfAdvisorscalerank = mfAdvisorscalerankMapper.selectOne(new LambdaQueryWrapper<MfAdvisorscalerank>()
                        .eq(MfAdvisorscalerank::getInvestadvisorcode, userInfoNew.getBelongOrgId())
                        .last("limit 1"));
                userInfoNew.setFundNumber(ObjectUtils.anyNotNull(mfAdvisorscalerank) ? mfAdvisorscalerank.getTotalfundn() : null);
                //设置成立时间 和 备案编号(公募没有备案编号)
                MfInvestadvisoroutline mfInvestadvisoroutline = mfInvestadvisoroutlineMapper.selectOne(new LambdaQueryWrapper<MfInvestadvisoroutline>()
                        .eq(MfInvestadvisoroutline::getInvestadvisorcode, userInfoNew.getBelongOrgId())
                        .last("limit 1"));
                userInfoNew.setFounded(ObjectUtils.anyNotNull(mfInvestadvisoroutline) ? mfInvestadvisoroutline.getEstablishmentdate().getTime() : null);
            }
            if(1 == userInfoNew.getBelongOrgType() && StringUtils.isNotBlank(userInfoNew.getBelongOrgId())){ // 私募
                //设置 基金数
                Integer integer = fundInfoMapper.selectCount(new LambdaQueryWrapper<FundInfo>().eq(FundInfo::getTrustId, userInfoNew.getBelongOrgId()));
                userInfoNew.setFundNumber(integer);

                //设置成立时间 和 备案编号
                CompanyInfo companyInfo = companyInfoMapper.selectById(userInfoNew.getBelongOrgId());
                if (ObjectUtils.anyNotNull(companyInfo)) {
                    userInfoNew.setFounded(companyInfo.getEstablishDate().getTime());
                    userInfoNew.setRecordNumber(companyInfo.getRegisterNumber());
                }
            }
            //设置团队成员
            CommonResp<List<UserInfoOrg>> usetInfoByOrgUserId = feignClientForFatools.getUsetInfoByOrgUserId(userId);
            if (usetInfoByOrgUserId.isSuccess()){
                List<UserInfoOrg> userInfoOrgs = usetInfoByOrgUserId.getData();
                // 设置关注列表
                List<String> collect = userInfoOrgs.stream().map(UserInfoOrg::getUserId).collect(Collectors.toList());
                List<FollowRelEntity> followRelEntities = followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
                        .eq(FollowRelEntity::getFollowerId, userId)
                        .in(FollowRelEntity::getFollowUserId, collect)
                        .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
                Map<String, FollowRelEntity> collect1 = followRelEntities.stream().collect(Collectors.toMap(FollowRelEntity::getFollowUserId, Function.identity()));
                userInfoOrgs.forEach(userInfoOrg -> {
                    if (collect1.containsKey(userInfoOrg.getUserId())) userInfoOrg.setIsFollower(1);
                });
                userInfoNew.setUserInfoOrgList(userInfoOrgs);
            }

        } else {
            //普通主页
            userInfoNew.setPersonalCenterType(1);
            //设置课程数
            CommonResp<Integer> integerCommonResp = feignClientForTanpuroom.myOrderCount(userId);
            if (integerCommonResp.isSuccess()) userInfoNew.setCourseNumber(integerCommonResp.getData());
        }
        return userInfoNew;
    }

    private void getFansNUmAndFollowNum(UserInfoNew userInfoNew) {
        Integer followNumber = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getFollowUserId, userInfoNew.getUserId())
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
        Integer fansNumber = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getFollowerId, userInfoNew.getUserId())
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
        userInfoNew.setFollowNumber(followNumber);
        userInfoNew.setFansNumber(fansNumber);
    }
    public List<Customer> queryUserCustomerList(String userId) {
        CommonResp<List<Customer>> ifaCustomerList = feignClientForDiagnose.getIfaCustomerList(userId);
        if (ifaCustomerList.isSuccess()) return ifaCustomerList.getData();
        return new ArrayList<>();
    }
刘基明's avatar
刘基明 committed
175

张辰's avatar
张辰 committed
176

刘基明's avatar
刘基明 committed
177
    //获取用户关注、粉丝列表
刘基明's avatar
刘基明 committed
178 179 180
    public Page<FollowQo> queryFollow(QueryFollowReq req, String userId) {
        List<String> userIds = QueryFollowTypeEnum.QUERY_FANS.getCode().equals(req.getQueryType()) ?
                followRelService.queryFansByIdolId(req.getUserId()) : followRelService.queryFansByFollowerId(req.getUserId());
刘基明's avatar
刘基明 committed
181 182 183
        List<FollowQo> followQos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userIds)) {
            List<UserInfoEntity> userInfos = userInfoService.queryUserByIds(userIds);
刘基明's avatar
刘基明 committed
184
            List<FollowQo> collect = userInfos.stream().map(ConvertUtil::userInfoEntity2FollowQo).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
185 186 187
            followQos = judgeFollowed(collect, userId);

        }
刘基明's avatar
刘基明 committed
188
        //分页
刘基明's avatar
刘基明 committed
189
        return PageUtils.page(req.getPage(), followQos);
张辰's avatar
张辰 committed
190 191
    }

刘基明's avatar
刘基明 committed
192

刘基明's avatar
刘基明 committed
193
    //判断返回列表中的用户是否被当前用户关注
刘基明's avatar
刘基明 committed
194
    public List<FollowQo> judgeFollowed(List<FollowQo> followQos, String followerId) {
刘基明's avatar
刘基明 committed
195
        Set<String> idolSet = new HashSet<>(followRelService.queryFansByFollowerId(followerId));
刘基明's avatar
刘基明 committed
196 197
        return followQos.stream().map(o -> {
            if (idolSet.contains(o.getUserId())) {
刘基明's avatar
刘基明 committed
198 199 200 201 202 203 204
                o.setFollowed(true);
            }
            return o;
        }).collect(Collectors.toList());

    }

刘基明's avatar
刘基明 committed
205 206 207 208 209 210
    public void addFollowRel(FollowRelReq req, String followerId) {
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
            followRelService.addFollowRel(req.getFollowUserId(), followerId);
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
            followRelService.deleteFollowRel(req.getFollowUserId(), followerId);
        }
张辰's avatar
张辰 committed
211
    }
212 213 214



张辰's avatar
张辰 committed
215
}