HomePageManager.java 16.6 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.CommunityConstant;
刘基明's avatar
刘基明 committed
11
import com.tanpu.community.api.beans.qo.FollowQo;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.api.beans.req.homepage.FollowRelReq;
刘基明's avatar
刘基明 committed
13 14
import com.tanpu.community.api.beans.req.homepage.QueryFollowReq;
import com.tanpu.community.api.beans.req.page.Page;
吴泽佳's avatar
吴泽佳 committed
15
import com.tanpu.community.api.beans.req.page.Pageable;
16
import com.tanpu.community.api.beans.resp.Customer;
刘基明's avatar
刘基明 committed
17
import com.tanpu.community.api.beans.vo.feign.activity.OfflineActivitySimpleResp;
吴泽佳's avatar
吴泽佳 committed
18
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoNewChief;
19
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoOrg;
20 21
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.beans.vo.feign.fund.FundCompanySimpleVO;
刘基明's avatar
刘基明 committed
22 23 24 25 26 27
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
28
import com.tanpu.community.cache.RedisCache;
29 30
import com.tanpu.community.dao.entity.community.FollowRelEntity;
import com.tanpu.community.dao.mapper.community.FollowRelMapper;
刘基明's avatar
刘基明 committed
31
import com.tanpu.community.feign.activity.FeignClientForActivity;
吴泽佳's avatar
吴泽佳 committed
32
import com.tanpu.community.feign.course.FeignClientForCourse;
33 34
import com.tanpu.community.feign.diagnose.FeignClientForDiagnose;
import com.tanpu.community.feign.fatools.FeignClientForFatools;
35
import com.tanpu.community.feign.product.FeignClientForProducts;
吴泽佳's avatar
吴泽佳 committed
36 37
import com.tanpu.community.feign.product.FeignForFund;
import com.tanpu.community.feign.product.FeignForPublicFund;
刘基明's avatar
刘基明 committed
38
import com.tanpu.community.service.FollowRelService;
刘基明's avatar
刘基明 committed
39
import com.tanpu.community.service.NotificationService;
刘基明's avatar
刘基明 committed
40
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
41
import com.tanpu.community.util.PageUtils;
42
import org.apache.commons.collections.CollectionUtils;
43 44
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
45
import org.springframework.beans.factory.annotation.Autowired;
张辰's avatar
张辰 committed
46 47
import org.springframework.stereotype.Service;

48
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
49 50 51 52 53 54 55 56
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;
57
import java.util.function.Function;
刘基明's avatar
刘基明 committed
58
import java.util.stream.Collectors;
张辰's avatar
张辰 committed
59

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

张辰's avatar
张辰 committed
62 63 64
@Service
public class HomePageManager {

吴泽佳's avatar
吴泽佳 committed
65
    @Resource
刘基明's avatar
刘基明 committed
66
    private FollowRelService followRelService;
吴泽佳's avatar
吴泽佳 committed
67
    @Resource
68 69 70 71 72
    private FeignClientForFatools feignClientForFatools;
    @Resource
    private FollowRelMapper followRelMapper;
    @Resource
    private FeignClientForDiagnose feignClientForDiagnose;
吴泽佳's avatar
吴泽佳 committed
73 74
    @Resource
    private FeignClientForCourse feignClientForCourse;
75
    @Resource
吴泽佳's avatar
吴泽佳 committed
76
    private FeignForFund feignForFund;
77
    @Resource
吴泽佳's avatar
吴泽佳 committed
78
    private FeignForPublicFund feignForPublicFund;
刘基明's avatar
刘基明 committed
79 80
    @Resource
    private RedisCache redisCache;
刘基明's avatar
刘基明 committed
81 82
    @Resource
    private FeignClientForActivity feignClientForActivity;
83 84
    @Resource
    private FeignClientForProducts feignClientForProducts;
85

刘基明's avatar
刘基明 committed
86 87 88
    @Autowired
    private NotificationService notificationService;

89
    //查询 个人中心 相关信息
90
    public UserInfoResp queryUsersInfo(String userIdMyself, String userId) {
王亚雷's avatar
王亚雷 committed
91
        CommonResp<UserInfoResp> queryUsersListNew = feignClientForFatools.queryUserInfoNew(userId);
刘基明's avatar
刘基明 committed
92 93
        if (queryUsersListNew.isNotSuccess() || !ObjectUtils.anyNotNull(queryUsersListNew.getData()))
            throw new BizException("内部接口调用失败");
94
        UserInfoResp userInfoNew = queryUsersListNew.getData();
吴泽佳's avatar
吴泽佳 committed
95

王亚雷's avatar
王亚雷 committed
96 97 98 99 100 101 102 103 104 105 106
        if (StringUtils.isNotBlank(userIdMyself)) {
            if (!StringUtils.equals(userIdMyself, userId)) {
                // 查询别人的个人主页
                // 关注 按钮的显示逻辑
                FollowRelEntity followRelEntity = followRelService.queryRecord(userId, userIdMyself);
                if (ObjectUtils.allNotNull(followRelEntity) && BizStatus.DeleteTag.tag_init == followRelEntity.getDeleteTag()) {
                    userInfoNew.setShowFollowStatus(ShowFollowStatusEnum.FOLLOWED.getCode()); // 已关注
                } else {
                    userInfoNew.setShowFollowStatus(ShowFollowStatusEnum.NOT_FOLLOWED.getCode()); // 未关注
                }
                if (userInfoNew.getWorkshopStatus() == 2) userInfoNew.setWorkshopStatus(1); //别人的主页不需要展示 工作室
107 108
            }
        }
吴泽佳's avatar
吴泽佳 committed
109
        //获取粉丝数 关注数
110 111 112
        getFansNUmAndFollowNum(userInfoNew);

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


刘基明's avatar
刘基明 committed
136
        } else if (UserTypeEnum.USER_ORG.getCode() == userInfoNew.getUserType()) {
137
            // 机构账号
吴泽佳's avatar
吴泽佳 committed
138
            userInfoNew.setPersonalCenterType(PersonalCenterTypeEnum.ORG.getCode());
吴泽佳's avatar
吴泽佳 committed
139
            if (FundCompanyTypeEnum.PUBLIC.getCode() == userInfoNew.getUserInfoNewOrg().getRelationFundCompanyType() && StringUtils.isNotBlank(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId())) { // 公募
140
                // 设置 基金数
吴泽佳's avatar
吴泽佳 committed
141
                CommonResp<FundCompanySimpleVO> companyFundCount = feignForPublicFund.getCompanyFundCount(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
142 143
                userInfoNew.setFundNumber(companyFundCount.isSuccess() ? companyFundCount.getData().getFundCount() : 0);
                // 设置成立时间 和 备案编号(公募没有备案编号)
吴泽佳's avatar
吴泽佳 committed
144
                CommonResp<FundCompanySimpleVO> fundCompanyInfoSimple = feignForPublicFund.getFundCompanyInfoSimple(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
145 146
                if (fundCompanyInfoSimple.isSuccess()) {
                    FundCompanySimpleVO data = fundCompanyInfoSimple.getData();
147
                    userInfoNew.setFounded(data.getEstablishDate() > 0 ? DateUtils.formatYMD(new Date(data.getEstablishDate())) : null);
148 149
                    userInfoNew.setRecordNumber(data.getRegisterNumber());
                }
150
            }
吴泽佳's avatar
吴泽佳 committed
151
            if (FundCompanyTypeEnum.PRIVATE.getCode() == userInfoNew.getUserInfoNewOrg().getRelationFundCompanyType() && StringUtils.isNotBlank(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId())) { // 私募
152
                // 设置成立时间 和 备案编号
吴泽佳's avatar
吴泽佳 committed
153
                CommonResp<FundCompanySimpleVO> fundCompanyInfoSimple = feignForFund.getFundCompanyInfoSimple(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
154 155
                if (fundCompanyInfoSimple.isSuccess()) {
                    FundCompanySimpleVO data = fundCompanyInfoSimple.getData();
156
                    userInfoNew.setFounded(data.getEstablishDate() > 0 ? DateUtils.formatYMD(new Date(data.getEstablishDate())) : null);
157
                    userInfoNew.setRecordNumber(data.getRegisterNumber());
158
                }
159
                // 设置 基金数
吴泽佳's avatar
吴泽佳 committed
160
                CommonResp<FundCompanySimpleVO> companyFundCount = feignForFund.getCompanyFundCount(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
161
                userInfoNew.setFundNumber(companyFundCount.isSuccess() ? companyFundCount.getData().getFundCount() : 0);
162
            }
163 164 165 166 167 168 169 170 171 172 173 174
            if (FundCompanyTypeEnum.WHITELIST.getCode() == userInfoNew.getUserInfoNewOrg().getRelationFundCompanyType() && StringUtils.isNotBlank(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId())) { // 私募
                // 设置成立时间 和 备案编号
                CommonResp<FundCompanySimpleVO> fundCompanyInfoSimple = feignClientForProducts.getFundCompanyInfoSimple(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
                if (fundCompanyInfoSimple.isSuccess()) {
                    FundCompanySimpleVO data = fundCompanyInfoSimple.getData();
                    userInfoNew.setFounded(data.getEstablishDate() > 0 ? DateUtils.formatYMD(new Date(data.getEstablishDate())) : null);
                    userInfoNew.setRecordNumber(data.getRegisterNumber());
                }
                // 设置 基金数
                CommonResp<FundCompanySimpleVO> companyFundCount = feignClientForProducts.getCompanyFundCount(userInfoNew.getUserInfoNewOrg().getRelationFundCompanyId());
                userInfoNew.setFundNumber(companyFundCount.isSuccess() ? companyFundCount.getData().getFundCount() : 0);
            }
175 176
            //设置团队成员
            CommonResp<List<UserInfoOrg>> usetInfoByOrgUserId = feignClientForFatools.getUsetInfoByOrgUserId(userId);
吴泽佳's avatar
吴泽佳 committed
177
            if (usetInfoByOrgUserId.isSuccess() && CollectionUtils.isNotEmpty(usetInfoByOrgUserId.getData())) {
178 179 180 181
                List<UserInfoOrg> userInfoOrgs = usetInfoByOrgUserId.getData();
                // 设置关注列表
                List<String> collect = userInfoOrgs.stream().map(UserInfoOrg::getUserId).collect(Collectors.toList());
                List<FollowRelEntity> followRelEntities = followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
182 183
                        .in(FollowRelEntity::getIdolId, collect)
                        .eq(FollowRelEntity::getFansId, userIdMyself)
184
                        .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
刘基明's avatar
刘基明 committed
185
                Map<String, FollowRelEntity> collect1 = followRelEntities.stream().collect(Collectors.toMap(FollowRelEntity::getIdolId, Function.identity()));
186
                userInfoOrgs.forEach(userInfoOrg -> {
刘基明's avatar
刘基明 committed
187 188
                    if (collect1.containsKey(userInfoOrg.getUserId()))
                        userInfoOrg.setIsFollower(ShowFollowStatusEnum.FOLLOWED.getCode());//1已关注
189 190 191
                });
                userInfoNew.setUserInfoOrgList(userInfoOrgs);
            }
刘基明's avatar
刘基明 committed
192
            // 家办活动
刘基明's avatar
刘基明 committed
193
            CommonResp<List<OfflineActivitySimpleResp>> activitySimpleResps = feignClientForActivity.simpleListByOrgid(userInfoNew.getUserInfoNewOrg().getCorpId());
刘基明's avatar
刘基明 committed
194
            if (activitySimpleResps.isSuccess() && CollectionUtils.isNotEmpty(activitySimpleResps.getData())) {
刘基明's avatar
刘基明 committed
195 196 197 198 199
                List<OfflineActivitySimpleResp> activities = activitySimpleResps.getData();
                if (CollectionUtils.isNotEmpty(activities)){
                    userInfoNew.setHoldActivities(activities.stream().filter(o->o.getActivityType().equals(1)).collect(Collectors.toList()));
                    userInfoNew.setTeamRecruitment(activities.stream().filter(o->o.getActivityType().equals(2)).collect(Collectors.toList()));
                }
刘基明's avatar
刘基明 committed
200
            }
201 202 203

        } else {
            //普通主页
吴泽佳's avatar
吴泽佳 committed
204
            userInfoNew.setPersonalCenterType(PersonalCenterTypeEnum.GENERAL.getCode());
吴泽佳's avatar
吴泽佳 committed
205 206
            //设置课程数(学习数)
            CommonResp<Integer> integerCommonResp = feignClientForCourse.getStudyCourseCount(userId);
207 208
            if (integerCommonResp.isSuccess()) userInfoNew.setCourseNumber(integerCommonResp.getData());
        }
刘基明's avatar
刘基明 committed
209 210 211 212 213 214 215 216
        // 主页背景图
        if (CommunityConstant.BACKGROUND_IMG_URL_MAP.containsKey(userInfoNew.getUserId())){
            userInfoNew.setBackgroundImgUrl(CommunityConstant.BACKGROUND_IMG_URL_MAP.get(userInfoNew.getUserId()));
        }else {
            userInfoNew.setBackgroundImgUrl(CommunityConstant.BACKGROUND_IMG_URL_MAP.get("default"));

        }

刘基明's avatar
刘基明 committed
217 218 219
        //刷新用户缓存
        redisCache.put(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId), userInfoNew, 60);

220 221 222
        return userInfoNew;
    }

吴泽佳's avatar
吴泽佳 committed
223
    public Page<UserInfoNewChief> queryChiefFinancialAdviserList(Pageable page) {
吴泽佳's avatar
吴泽佳 committed
224
        CommonResp<Page<UserInfoNewChief>> pageCommonResp = feignClientForFatools.queryChiefFinancialAdviserList(page.pageNumber, page.pageSize);
吴泽佳's avatar
吴泽佳 committed
225 226 227 228
        return pageCommonResp.getData();

    }

229
    private void getFansNUmAndFollowNum(UserInfoResp userInfoNew) {
刘基明's avatar
刘基明 committed
230
        Integer fansNumber = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getIdolId, userInfoNew.getUserId())
231
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
刘基明's avatar
刘基明 committed
232
        Integer followNumber = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getFansId, userInfoNew.getUserId())
233 234 235 236
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
        userInfoNew.setFollowNumber(followNumber);
        userInfoNew.setFansNumber(fansNumber);
    }
刘基明's avatar
刘基明 committed
237

238 239 240 241 242
    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
243

张辰's avatar
张辰 committed
244

刘基明's avatar
刘基明 committed
245 246
    /**
     * 用户关注列表
刘基明's avatar
刘基明 committed
247 248
     *
     * @param req    目标用户
刘基明's avatar
刘基明 committed
249 250 251
     * @param userId 当前用户
     * @return
     */
刘基明's avatar
刘基明 committed
252
    public Page<FollowQo> queryFollow(QueryFollowReq req, String userId) {
刘基明's avatar
刘基明 committed
253 254 255 256 257 258 259
        //数据库分页
        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
260
        List<FollowQo> followQos = new ArrayList<>();
刘基明's avatar
刘基明 committed
261 262
        if (!CollectionUtils.isEmpty(userIdsPage.getContent())) {
            List<UserInfoResp> userInfoNews = feignClientForFatools.queryUserListNew(userIdsPage.getContent());
刘基明's avatar
刘基明 committed
263
            List<FollowQo> collect = userInfoNews.stream().map(ConvertUtil::userInfoNew2FollowQo).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
264 265
            followQos = judgeFollowed(collect, userId);
        }
刘基明's avatar
刘基明 committed
266
        return PageUtils.page(userIdsPage, followQos);
张辰's avatar
张辰 committed
267 268
    }

刘基明's avatar
刘基明 committed
269

刘基明's avatar
刘基明 committed
270
    //判断返回列表中的用户是否被当前用户关注
刘基明's avatar
刘基明 committed
271
    public List<FollowQo> judgeFollowed(List<FollowQo> followQos, String followerId) {
刘基明's avatar
刘基明 committed
272
        Set<String> idolSet = new HashSet<>(followRelService.queryIdolsByFansId(followerId));
刘基明's avatar
刘基明 committed
273 274
        return followQos.stream().map(o -> {
            if (idolSet.contains(o.getUserId())) {
刘基明's avatar
刘基明 committed
275 276 277 278 279 280 281
                o.setFollowed(true);
            }
            return o;
        }).collect(Collectors.toList());

    }

刘基明's avatar
刘基明 committed
282 283
    public void addFollowRel(FollowRelReq req, String followerId) {
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
284 285 286
            // 第一次关注才有消息通知
            if (followRelService.addFollowRel(req.getFollowUserId(), followerId)) {
                notificationService.insert(followerId, req.getFollowUserId(), NotificationTypeEnum.FOLLOW, req.getFollowUserId(), null);
287
                notificationService.putNotifyCache(req.getFollowUserId(), followerId,NotificationTypeEnum.FOLLOW);
刘基明's avatar
刘基明 committed
288
            }
刘基明's avatar
刘基明 committed
289 290 291
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
            followRelService.deleteFollowRel(req.getFollowUserId(), followerId);
        }
张辰's avatar
张辰 committed
292
    }
293

刘基明's avatar
刘基明 committed
294 295 296 297 298 299 300
    private UserInfoResp getUserInfo(String authorId) {
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(authorId);
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }
301

张辰's avatar
张辰 committed
302
}