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

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

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

张辰's avatar
张辰 committed
64 65 66
@Service
public class HomePageManager {

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

刘基明's avatar
刘基明 committed
88 89 90
    @Autowired
    private NotificationService notificationService;

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

王亚雷's avatar
王亚雷 committed
98 99 100 101 102 103 104 105 106 107 108
        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); //别人的主页不需要展示 工作室
109 110
            }
        }
吴泽佳's avatar
吴泽佳 committed
111
        //获取粉丝数 关注数
112 113 114
        getFansNUmAndFollowNum(userInfoNew);

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


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

        } else {
            //普通主页
吴泽佳's avatar
吴泽佳 committed
206
            userInfoNew.setPersonalCenterType(PersonalCenterTypeEnum.GENERAL.getCode());
吴泽佳's avatar
吴泽佳 committed
207 208
            //设置课程数(学习数)
            CommonResp<Integer> integerCommonResp = feignClientForCourse.getStudyCourseCount(userId);
209 210
            if (integerCommonResp.isSuccess()) userInfoNew.setCourseNumber(integerCommonResp.getData());
        }
刘基明's avatar
刘基明 committed
211 212 213 214 215 216 217 218
        // 主页背景图
        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
219 220 221
        //刷新用户缓存
        redisCache.put(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId), userInfoNew, 60);

222 223 224
        return userInfoNew;
    }

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

    }

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

240 241 242 243 244
    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
245

张辰's avatar
张辰 committed
246

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

刘基明's avatar
刘基明 committed
273

刘基明's avatar
刘基明 committed
274
    //判断返回列表中的用户是否被当前用户关注
王亚雷's avatar
王亚雷 committed
275
    public void judgeFollowed(List<FollowQo> followQos, @NotEmpty String followerId) {
刘基明's avatar
刘基明 committed
276
        Set<String> idolSet = new HashSet<>(followRelService.queryIdolsByFansId(followerId));
王亚雷's avatar
王亚雷 committed
277
        followQos.stream().peek(o -> {
刘基明's avatar
刘基明 committed
278
            if (idolSet.contains(o.getUserId())) {
刘基明's avatar
刘基明 committed
279 280
                o.setFollowed(true);
            }
王亚雷's avatar
王亚雷 committed
281
        });
刘基明's avatar
刘基明 committed
282 283
    }

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

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

张辰's avatar
张辰 committed
304
}