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

3
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
张辰's avatar
张辰 committed
4 5
import com.tanpu.biz.common.enums.user.UserLevelEnum;
import com.tanpu.biz.common.enums.user.UserTypeEnum;
6 7 8
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.exception.BizException;
9
import com.tanpu.common.util.DateUtils;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.api.beans.qo.FollowQo;
刘基明's avatar
刘基明 committed
11
import com.tanpu.community.api.beans.req.homepage.FollowRelReq;
刘基明's avatar
刘基明 committed
12 13
import com.tanpu.community.api.beans.req.homepage.QueryFollowReq;
import com.tanpu.community.api.beans.req.page.Page;
吴泽佳's avatar
吴泽佳 committed
14
import com.tanpu.community.api.beans.req.page.Pageable;
15
import com.tanpu.community.api.beans.resp.Customer;
吴泽佳's avatar
吴泽佳 committed
16
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoNewChief;
17
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoOrg;
18 19
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.beans.vo.feign.fund.FundCompanySimpleVO;
刘基明's avatar
刘基明 committed
20 21 22 23 24 25
import com.tanpu.community.api.enums.FundCompanyTypeEnum;
import com.tanpu.community.api.enums.NotificationTypeEnum;
import com.tanpu.community.api.enums.OperationTypeEnum;
import com.tanpu.community.api.enums.PersonalCenterTypeEnum;
import com.tanpu.community.api.enums.QueryFollowTypeEnum;
import com.tanpu.community.api.enums.ShowFollowStatusEnum;
刘基明's avatar
刘基明 committed
26
import com.tanpu.community.cache.RedisCache;
27 28
import com.tanpu.community.dao.entity.community.FollowRelEntity;
import com.tanpu.community.dao.mapper.community.FollowRelMapper;
吴泽佳's avatar
吴泽佳 committed
29
import com.tanpu.community.feign.course.FeignClientForCourse;
30 31
import com.tanpu.community.feign.diagnose.FeignClientForDiagnose;
import com.tanpu.community.feign.fatools.FeignClientForFatools;
吴泽佳's avatar
吴泽佳 committed
32 33
import com.tanpu.community.feign.product.FeignForFund;
import com.tanpu.community.feign.product.FeignForPublicFund;
刘基明's avatar
刘基明 committed
34
import com.tanpu.community.service.FollowRelService;
刘基明's avatar
刘基明 committed
35
import com.tanpu.community.service.NotificationService;
刘基明's avatar
刘基明 committed
36
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
37
import com.tanpu.community.util.PageUtils;
38
import org.apache.commons.collections.CollectionUtils;
39 40
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
41
import org.springframework.beans.factory.annotation.Autowired;
张辰's avatar
张辰 committed
42 43
import org.springframework.stereotype.Service;

44
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
45 46 47 48 49 50 51 52
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
53
import java.util.function.Function;
刘基明's avatar
刘基明 committed
54
import java.util.stream.Collectors;
张辰's avatar
张辰 committed
55

刘基明's avatar
刘基明 committed
56 57
import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO;

张辰's avatar
张辰 committed
58 59 60
@Service
public class HomePageManager {

吴泽佳's avatar
吴泽佳 committed
61
    @Resource
刘基明's avatar
刘基明 committed
62
    private FollowRelService followRelService;
吴泽佳's avatar
吴泽佳 committed
63
    @Resource
64 65 66 67 68
    private FeignClientForFatools feignClientForFatools;
    @Resource
    private FollowRelMapper followRelMapper;
    @Resource
    private FeignClientForDiagnose feignClientForDiagnose;
吴泽佳's avatar
吴泽佳 committed
69 70
    @Resource
    private FeignClientForCourse feignClientForCourse;
71
    @Resource
吴泽佳's avatar
吴泽佳 committed
72
    private FeignForFund feignForFund;
73
    @Resource
吴泽佳's avatar
吴泽佳 committed
74
    private FeignForPublicFund feignForPublicFund;
刘基明's avatar
刘基明 committed
75 76
    @Resource
    private RedisCache redisCache;
77

刘基明's avatar
刘基明 committed
78 79 80
    @Autowired
    private NotificationService notificationService;

81
    //查询 个人中心 相关信息
82
    public UserInfoResp queryUsersInfo(String userIdMyself, String userId) {
83
        CommonResp<UserInfoResp> queryUsersListNew = feignClientForFatools.queryUserInfoNew(StringUtils.isNotBlank(userId) ? userId : userIdMyself);
刘基明's avatar
刘基明 committed
84 85
        if (queryUsersListNew.isNotSuccess() || !ObjectUtils.anyNotNull(queryUsersListNew.getData()))
            throw new BizException("内部接口调用失败");
86
        UserInfoResp userInfoNew = queryUsersListNew.getData();
吴泽佳's avatar
吴泽佳 committed
87

吴泽佳's avatar
吴泽佳 committed
88
        if (StringUtils.isNotBlank(userId) && !StringUtils.equals(userIdMyself, userId)) { //查询别人的个人主页
吴泽佳's avatar
吴泽佳 committed
89
            // 关注 按钮的显示逻辑
90
            FollowRelEntity followRelEntity = followRelService.queryRecord(userId, userIdMyself);
刘基明's avatar
刘基明 committed
91
            if (ObjectUtils.allNotNull(followRelEntity) && BizStatus.DeleteTag.tag_init == followRelEntity.getDeleteTag()) {
吴泽佳's avatar
吴泽佳 committed
92
                userInfoNew.setShowFollowStatus(ShowFollowStatusEnum.FOLLOWED.getCode()); // 已关注
93
            } else {
吴泽佳's avatar
吴泽佳 committed
94
                userInfoNew.setShowFollowStatus(ShowFollowStatusEnum.NOT_FOLLOWED.getCode()); // 未关注
95
            }
吴泽佳's avatar
吴泽佳 committed
96
            if (userInfoNew.getWorkshopStatus() == 2) userInfoNew.setWorkshopStatus(1); //别人的主页不需要展示 工作室
97 98 99 100
        } else {
            // 查询自己的主页
            userId = userIdMyself;
        }
吴泽佳's avatar
吴泽佳 committed
101
        //获取粉丝数 关注数
102 103 104
        getFansNUmAndFollowNum(userInfoNew);

        // 主页类型 显示逻辑
刘基明's avatar
刘基明 committed
105
        if (UserLevelEnum.USER_CHIEF_FINANCIAL_ADVISER.getCode() == userInfoNew.getLevelGrade()) {
吴泽佳's avatar
吴泽佳 committed
106
            // 首席投顾
吴泽佳's avatar
吴泽佳 committed
107
            userInfoNew.setPersonalCenterType(PersonalCenterTypeEnum.CHIEF.getCode());
108 109
            // 成功案例客户
            // 默认显示该投顾管理的客户里,总资产最高的客户
吴泽佳's avatar
吴泽佳 committed
110
            if (StringUtils.isBlank(userInfoNew.getUserInfoNewChief().getClientId())) {
111
                List<Customer> customers = queryUserCustomerList(userId);
吴泽佳's avatar
吴泽佳 committed
112
                Optional<Customer> first = customers.stream().max(Comparator.comparing(Customer::getTotalMarket));
113
                if (first.isPresent()) {
吴泽佳's avatar
吴泽佳 committed
114
                    userInfoNew.getUserInfoNewChief().setClientId(first.get().getCustomerId());
115
                } else {
吴泽佳's avatar
吴泽佳 committed
116
                    userInfoNew.getUserInfoNewChief().setClientId(null);
117 118
                }
            }
119 120
            // 查询首席投顾数量
            CommonResp<Page<UserInfoNewChief>> pageCommonResp = feignClientForFatools.queryChiefFinancialAdviserList(1, 1);
刘基明's avatar
刘基明 committed
121
            if (pageCommonResp.isSuccess()) {
吴泽佳's avatar
吴泽佳 committed
122
                userInfoNew.getUserInfoNewChief().setChiefCount(pageCommonResp.getData().getTotalSize());
123
            } else {
吴泽佳's avatar
吴泽佳 committed
124
                userInfoNew.getUserInfoNewChief().setChiefCount(0L);
125
            }
126 127


刘基明's avatar
刘基明 committed
128
        } else if (UserTypeEnum.USER_ORG.getCode() == userInfoNew.getUserType()) {
129
            // 机构账号
吴泽佳's avatar
吴泽佳 committed
130
            userInfoNew.setPersonalCenterType(PersonalCenterTypeEnum.ORG.getCode());
吴泽佳's avatar
吴泽佳 committed
131
            if (FundCompanyTypeEnum.PUBLIC.getCode() == userInfoNew.getUserInfoNewOrg().getRelationFundCompanyType() && StringUtils.isNotBlank(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId())) { // 公募
132
                // 设置 基金数
吴泽佳's avatar
吴泽佳 committed
133
                CommonResp<FundCompanySimpleVO> companyFundCount = feignForPublicFund.getCompanyFundCount(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
134 135
                userInfoNew.setFundNumber(companyFundCount.isSuccess() ? companyFundCount.getData().getFundCount() : 0);
                // 设置成立时间 和 备案编号(公募没有备案编号)
吴泽佳's avatar
吴泽佳 committed
136
                CommonResp<FundCompanySimpleVO> fundCompanyInfoSimple = feignForPublicFund.getFundCompanyInfoSimple(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
137 138
                if (fundCompanyInfoSimple.isSuccess()) {
                    FundCompanySimpleVO data = fundCompanyInfoSimple.getData();
139
                    userInfoNew.setFounded(data.getEstablishDate() > 0 ? DateUtils.formatYMD(new Date(data.getEstablishDate())) : null);
140 141
                    userInfoNew.setRecordNumber(data.getRegisterNumber());
                }
142
            }
吴泽佳's avatar
吴泽佳 committed
143
            if (FundCompanyTypeEnum.PRIVATE.getCode() == userInfoNew.getUserInfoNewOrg().getRelationFundCompanyType() && StringUtils.isNotBlank(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId())) { // 私募
144
                // 设置成立时间 和 备案编号
吴泽佳's avatar
吴泽佳 committed
145
                CommonResp<FundCompanySimpleVO> fundCompanyInfoSimple = feignForFund.getFundCompanyInfoSimple(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
146 147
                if (fundCompanyInfoSimple.isSuccess()) {
                    FundCompanySimpleVO data = fundCompanyInfoSimple.getData();
148
                    userInfoNew.setFounded(data.getEstablishDate() > 0 ? DateUtils.formatYMD(new Date(data.getEstablishDate())) : null);
149
                    userInfoNew.setRecordNumber(data.getRegisterNumber());
150
                }
151
                // 设置 基金数
吴泽佳's avatar
吴泽佳 committed
152
                CommonResp<FundCompanySimpleVO> companyFundCount = feignForFund.getCompanyFundCount(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
153
                userInfoNew.setFundNumber(companyFundCount.isSuccess() ? companyFundCount.getData().getFundCount() : 0);
154 155 156
            }
            //设置团队成员
            CommonResp<List<UserInfoOrg>> usetInfoByOrgUserId = feignClientForFatools.getUsetInfoByOrgUserId(userId);
吴泽佳's avatar
吴泽佳 committed
157
            if (usetInfoByOrgUserId.isSuccess() && CollectionUtils.isNotEmpty(usetInfoByOrgUserId.getData())) {
158 159 160 161
                List<UserInfoOrg> userInfoOrgs = usetInfoByOrgUserId.getData();
                // 设置关注列表
                List<String> collect = userInfoOrgs.stream().map(UserInfoOrg::getUserId).collect(Collectors.toList());
                List<FollowRelEntity> followRelEntities = followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
162 163
                        .in(FollowRelEntity::getIdolId, collect)
                        .eq(FollowRelEntity::getFansId, userIdMyself)
164
                        .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
刘基明's avatar
刘基明 committed
165
                Map<String, FollowRelEntity> collect1 = followRelEntities.stream().collect(Collectors.toMap(FollowRelEntity::getIdolId, Function.identity()));
166
                userInfoOrgs.forEach(userInfoOrg -> {
刘基明's avatar
刘基明 committed
167 168
                    if (collect1.containsKey(userInfoOrg.getUserId()))
                        userInfoOrg.setIsFollower(ShowFollowStatusEnum.FOLLOWED.getCode());//1已关注
169 170 171 172 173 174
                });
                userInfoNew.setUserInfoOrgList(userInfoOrgs);
            }

        } else {
            //普通主页
吴泽佳's avatar
吴泽佳 committed
175
            userInfoNew.setPersonalCenterType(PersonalCenterTypeEnum.GENERAL.getCode());
吴泽佳's avatar
吴泽佳 committed
176 177
            //设置课程数(学习数)
            CommonResp<Integer> integerCommonResp = feignClientForCourse.getStudyCourseCount(userId);
178 179
            if (integerCommonResp.isSuccess()) userInfoNew.setCourseNumber(integerCommonResp.getData());
        }
刘基明's avatar
刘基明 committed
180 181 182
        //刷新用户缓存
        redisCache.put(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId), userInfoNew, 60);

183 184 185
        return userInfoNew;
    }

吴泽佳's avatar
吴泽佳 committed
186
    public Page<UserInfoNewChief> queryChiefFinancialAdviserList(Pageable page) {
吴泽佳's avatar
吴泽佳 committed
187
        CommonResp<Page<UserInfoNewChief>> pageCommonResp = feignClientForFatools.queryChiefFinancialAdviserList(page.pageNumber, page.pageSize);
吴泽佳's avatar
吴泽佳 committed
188 189 190 191
        return pageCommonResp.getData();

    }

192
    private void getFansNUmAndFollowNum(UserInfoResp userInfoNew) {
刘基明's avatar
刘基明 committed
193
        Integer fansNumber = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getIdolId, userInfoNew.getUserId())
194
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
刘基明's avatar
刘基明 committed
195
        Integer followNumber = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getFansId, userInfoNew.getUserId())
196 197 198 199
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
        userInfoNew.setFollowNumber(followNumber);
        userInfoNew.setFansNumber(fansNumber);
    }
刘基明's avatar
刘基明 committed
200

201 202 203 204 205
    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
206

张辰's avatar
张辰 committed
207

刘基明's avatar
刘基明 committed
208 209
    /**
     * 用户关注列表
刘基明's avatar
刘基明 committed
210 211
     *
     * @param req    目标用户
刘基明's avatar
刘基明 committed
212 213 214
     * @param userId 当前用户
     * @return
     */
刘基明's avatar
刘基明 committed
215
    public Page<FollowQo> queryFollow(QueryFollowReq req, String userId) {
刘基明's avatar
刘基明 committed
216 217 218 219 220 221 222
        //数据库分页
        Integer pageSize = req.page.pageSize;
        Integer pageNumber = req.page.pageNumber;

        Page<String> userIdsPage = QueryFollowTypeEnum.QUERY_FANS.getCode().equals(req.getQueryType()) ?
                followRelService.queryFansByIdolId(req.userId, pageNumber, pageSize)
                : followRelService.queryIdolsByFansId(req.userId, pageNumber, pageSize);
刘基明's avatar
刘基明 committed
223
        List<FollowQo> followQos = new ArrayList<>();
刘基明's avatar
刘基明 committed
224 225
        if (!CollectionUtils.isEmpty(userIdsPage.getContent())) {
            List<UserInfoResp> userInfoNews = feignClientForFatools.queryUserListNew(userIdsPage.getContent());
刘基明's avatar
刘基明 committed
226
            List<FollowQo> collect = userInfoNews.stream().map(ConvertUtil::userInfoNew2FollowQo).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
227 228
            followQos = judgeFollowed(collect, userId);
        }
刘基明's avatar
刘基明 committed
229
        return PageUtils.page(userIdsPage, followQos);
张辰's avatar
张辰 committed
230 231
    }

刘基明's avatar
刘基明 committed
232

刘基明's avatar
刘基明 committed
233
    //判断返回列表中的用户是否被当前用户关注
刘基明's avatar
刘基明 committed
234
    public List<FollowQo> judgeFollowed(List<FollowQo> followQos, String followerId) {
刘基明's avatar
刘基明 committed
235
        Set<String> idolSet = new HashSet<>(followRelService.queryIdolsByFansId(followerId));
刘基明's avatar
刘基明 committed
236 237
        return followQos.stream().map(o -> {
            if (idolSet.contains(o.getUserId())) {
刘基明's avatar
刘基明 committed
238 239 240 241 242 243 244
                o.setFollowed(true);
            }
            return o;
        }).collect(Collectors.toList());

    }

刘基明's avatar
刘基明 committed
245 246
    public void addFollowRel(FollowRelReq req, String followerId) {
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
247 248 249
            // 第一次关注才有消息通知
            if (followRelService.addFollowRel(req.getFollowUserId(), followerId)) {
                notificationService.insert(followerId, req.getFollowUserId(), NotificationTypeEnum.FOLLOW, req.getFollowUserId(), null);
250
                notificationService.putNotifyCache(req.getFollowUserId(), followerId,NotificationTypeEnum.FOLLOW);
刘基明's avatar
刘基明 committed
251
            }
刘基明's avatar
刘基明 committed
252 253 254
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
            followRelService.deleteFollowRel(req.getFollowUserId(), followerId);
        }
张辰's avatar
张辰 committed
255
    }
256

刘基明's avatar
刘基明 committed
257 258 259 260 261 262 263
    private UserInfoResp getUserInfo(String authorId) {
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(authorId);
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }
264

张辰's avatar
张辰 committed
265
}