HomePageManager.java 14.9 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.activity.OfflineActivityListResp;
刘基明'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;
吴泽佳's avatar
吴泽佳 committed
35 36
import com.tanpu.community.feign.product.FeignForFund;
import com.tanpu.community.feign.product.FeignForPublicFund;
刘基明's avatar
刘基明 committed
37
import com.tanpu.community.service.FollowRelService;
刘基明's avatar
刘基明 committed
38
import com.tanpu.community.service.NotificationService;
刘基明's avatar
刘基明 committed
39
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
40
import com.tanpu.community.util.PageUtils;
41
import org.apache.commons.collections.CollectionUtils;
42 43
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
44
import org.springframework.beans.factory.annotation.Autowired;
张辰's avatar
张辰 committed
45 46
import org.springframework.stereotype.Service;

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

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

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

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

刘基明's avatar
刘基明 committed
83 84 85
    @Autowired
    private NotificationService notificationService;

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

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

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


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

        } else {
            //普通主页
吴泽佳's avatar
吴泽佳 committed
187
            userInfoNew.setPersonalCenterType(PersonalCenterTypeEnum.GENERAL.getCode());
吴泽佳's avatar
吴泽佳 committed
188 189
            //设置课程数(学习数)
            CommonResp<Integer> integerCommonResp = feignClientForCourse.getStudyCourseCount(userId);
190 191
            if (integerCommonResp.isSuccess()) userInfoNew.setCourseNumber(integerCommonResp.getData());
        }
刘基明's avatar
刘基明 committed
192 193 194
        //刷新用户缓存
        redisCache.put(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId), userInfoNew, 60);

195 196 197
        return userInfoNew;
    }

吴泽佳's avatar
吴泽佳 committed
198
    public Page<UserInfoNewChief> queryChiefFinancialAdviserList(Pageable page) {
吴泽佳's avatar
吴泽佳 committed
199
        CommonResp<Page<UserInfoNewChief>> pageCommonResp = feignClientForFatools.queryChiefFinancialAdviserList(page.pageNumber, page.pageSize);
吴泽佳's avatar
吴泽佳 committed
200 201 202 203
        return pageCommonResp.getData();

    }

204
    private void getFansNUmAndFollowNum(UserInfoResp userInfoNew) {
刘基明's avatar
刘基明 committed
205
        Integer fansNumber = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getIdolId, userInfoNew.getUserId())
206
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
刘基明's avatar
刘基明 committed
207
        Integer followNumber = followRelMapper.selectCount(new LambdaQueryWrapper<FollowRelEntity>().eq(FollowRelEntity::getFansId, userInfoNew.getUserId())
208 209 210 211
                .eq(FollowRelEntity::getDeleteTag, BizStatus.DeleteTag.tag_init));
        userInfoNew.setFollowNumber(followNumber);
        userInfoNew.setFansNumber(fansNumber);
    }
刘基明's avatar
刘基明 committed
212

213 214 215 216 217
    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
218

张辰's avatar
张辰 committed
219

刘基明's avatar
刘基明 committed
220 221
    /**
     * 用户关注列表
刘基明's avatar
刘基明 committed
222 223
     *
     * @param req    目标用户
刘基明's avatar
刘基明 committed
224 225 226
     * @param userId 当前用户
     * @return
     */
刘基明's avatar
刘基明 committed
227
    public Page<FollowQo> queryFollow(QueryFollowReq req, String userId) {
刘基明's avatar
刘基明 committed
228 229 230 231 232 233 234
        //数据库分页
        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
235
        List<FollowQo> followQos = new ArrayList<>();
刘基明's avatar
刘基明 committed
236 237
        if (!CollectionUtils.isEmpty(userIdsPage.getContent())) {
            List<UserInfoResp> userInfoNews = feignClientForFatools.queryUserListNew(userIdsPage.getContent());
刘基明's avatar
刘基明 committed
238
            List<FollowQo> collect = userInfoNews.stream().map(ConvertUtil::userInfoNew2FollowQo).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
239 240
            followQos = judgeFollowed(collect, userId);
        }
刘基明's avatar
刘基明 committed
241
        return PageUtils.page(userIdsPage, followQos);
张辰's avatar
张辰 committed
242 243
    }

刘基明's avatar
刘基明 committed
244

刘基明's avatar
刘基明 committed
245
    //判断返回列表中的用户是否被当前用户关注
刘基明's avatar
刘基明 committed
246
    public List<FollowQo> judgeFollowed(List<FollowQo> followQos, String followerId) {
刘基明's avatar
刘基明 committed
247
        Set<String> idolSet = new HashSet<>(followRelService.queryIdolsByFansId(followerId));
刘基明's avatar
刘基明 committed
248 249
        return followQos.stream().map(o -> {
            if (idolSet.contains(o.getUserId())) {
刘基明's avatar
刘基明 committed
250 251 252 253 254 255 256
                o.setFollowed(true);
            }
            return o;
        }).collect(Collectors.toList());

    }

刘基明's avatar
刘基明 committed
257 258
    public void addFollowRel(FollowRelReq req, String followerId) {
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
259 260 261
            // 第一次关注才有消息通知
            if (followRelService.addFollowRel(req.getFollowUserId(), followerId)) {
                notificationService.insert(followerId, req.getFollowUserId(), NotificationTypeEnum.FOLLOW, req.getFollowUserId(), null);
262
                notificationService.putNotifyCache(req.getFollowUserId(), followerId,NotificationTypeEnum.FOLLOW);
刘基明's avatar
刘基明 committed
263
            }
刘基明's avatar
刘基明 committed
264 265 266
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
            followRelService.deleteFollowRel(req.getFollowUserId(), followerId);
        }
张辰's avatar
张辰 committed
267
    }
268

刘基明's avatar
刘基明 committed
269 270 271 272 273 274 275
    private UserInfoResp getUserInfo(String authorId) {
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(authorId);
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }
276

张辰's avatar
张辰 committed
277
}