FeignService.java 9.81 KB
Newer Older
张辰's avatar
张辰 committed
1 2 3
package com.tanpu.community.service;

import com.tanpu.common.api.CommonResp;
刘基明's avatar
刘基明 committed
4
import com.tanpu.community.api.beans.resp.CoursePackageSimpleResp;
刘基明's avatar
刘基明 committed
5
import com.tanpu.community.api.beans.vo.feign.activity.OfflineActivitySimpleResp;
张辰's avatar
张辰 committed
6
import com.tanpu.community.api.beans.vo.feign.course.CourseSimpleResp;
张辰's avatar
张辰 committed
7
import com.tanpu.community.api.beans.vo.feign.course.ShortVideoBaseInfoResp;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.api.beans.vo.feign.product.FundCompanyVO;
张辰's avatar
张辰 committed
10 11
import com.tanpu.community.api.beans.vo.feign.product.ProductInfoVO;
import com.tanpu.community.api.beans.vo.feign.zhibo.ZhiboListResp;
张辰's avatar
张辰 committed
12
import com.tanpu.community.cache.LocalCache;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.feign.activity.FeignClientForActivity;
张辰's avatar
张辰 committed
14
import com.tanpu.community.feign.course.FeignClientForCourse;
张辰's avatar
张辰 committed
15
import com.tanpu.community.feign.fatools.FeignClientForFatools;
张辰's avatar
张辰 committed
16 17 18 19 20
import com.tanpu.community.feign.product.FeignClientForProducts;
import com.tanpu.community.feign.product.FeignForFund;
import com.tanpu.community.feign.product.FeignForPublicFund;
import com.tanpu.community.feign.tanpuroom.FeignClientForTanpuroom;
import com.tanpu.community.feign.zhibo.FeignClientForZhibo;
21
import lombok.extern.slf4j.Slf4j;
刘基明's avatar
刘基明 committed
22
import org.apache.commons.collections4.CollectionUtils;
张辰's avatar
张辰 committed
23 24
import org.springframework.stereotype.Service;

张辰's avatar
张辰 committed
25 26
import javax.annotation.Resource;
import java.util.ArrayList;
刘基明's avatar
刘基明 committed
27
import java.util.Arrays;
刘基明's avatar
刘基明 committed
28
import java.util.Collections;
张辰's avatar
张辰 committed
29 30 31
import java.util.List;
import java.util.function.Function;

32
@Slf4j
张辰's avatar
张辰 committed
33 34 35
@Service
public class FeignService {

张辰's avatar
张辰 committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    @Resource
    private FeignClientForTanpuroom feignClientForTanpuroom;

    @Resource
    private FeignClientForZhibo feignClientForZhibo;

    @Resource
    private FeignClientForProducts feignForProduct;

    @Resource
    private FeignClientForCourse feignForCourse;


    @Resource
    private FeignForFund feignForFund;

    @Resource
    private FeignForPublicFund feignForPublicFund;

    @Resource
    private FeignClientForFatools feignClientForFatools;

刘基明's avatar
刘基明 committed
58 59 60 61

    @Resource
    private FeignClientForActivity feignClientForActivity;

张辰's avatar
张辰 committed
62 63 64
    @Resource
    private LocalCache localCache;

张辰's avatar
张辰 committed
65

66
    public UserInfoResp getUserInfoById(String userId) {
刘基明's avatar
刘基明 committed
67

刘基明's avatar
刘基明 committed
68
        List<UserInfoResp> userList = getUserList(Collections.singletonList(userId));
刘基明's avatar
刘基明 committed
69
        if (CollectionUtils.isEmpty(userList)) {
刘基明's avatar
刘基明 committed
70
            return null;
张辰's avatar
张辰 committed
71
        }
刘基明's avatar
刘基明 committed
72
        return userList.get(0);
张辰's avatar
张辰 committed
73
    }
张辰's avatar
张辰 committed
74 75

    public List<ShortVideoBaseInfoResp> batchGetShortVideoBaseInfo(List<String> sourceIds) {
76 77
        return batchExecute("batchGetShortVideoBaseInfo_", sourceIds, ShortVideoBaseInfoResp.class,
                ShortVideoBaseInfoResp::getSourceId, ids -> {
刘基明's avatar
刘基明 committed
78 79 80 81 82 83 84
                    CommonResp<List<ShortVideoBaseInfoResp>> resp = feignClientForTanpuroom.getShortVideoBaseInfo(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
张辰's avatar
张辰 committed
85 86
    }

张辰's avatar
张辰 committed
87 88 89 90 91 92 93 94 95
    public List<CourseSimpleResp> getCourseSimpleList(List<String> courseIds) {
        return batchExecute("getCourseSimpleList_", courseIds, CourseSimpleResp.class,
                CourseSimpleResp::getCourseId, ids -> {
                    CommonResp<List<CourseSimpleResp>> resp = feignForCourse.getCourseSimpleList(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
刘基明's avatar
刘基明 committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109
                });
    }

    public List<CoursePackageSimpleResp> getCoursePackageList(List<String> coursePackageIds) {
        return batchExecute("getCoursePackageList", coursePackageIds, CoursePackageSimpleResp.class,
                CoursePackageSimpleResp::getCoursePackageId, ids -> {
                    CommonResp<List<CoursePackageSimpleResp>> resp = feignForCourse.getCoursePackageListByUserId(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }
张辰's avatar
张辰 committed
110 111 112 113 114 115 116 117 118 119 120 121 122

    public List<ZhiboListResp> getZhiboSimpleList(List<String> zhiboIds) {
        return batchExecute("getZhiboSimpleList_", zhiboIds, ZhiboListResp.class,
                ZhiboListResp::getId, ids -> {
                    CommonResp<List<ZhiboListResp>> resp = feignClientForZhibo.simpleList(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

刘基明's avatar
刘基明 committed
123 124 125 126
    public List<OfflineActivitySimpleResp> getActivitySimpleList(List<String> activityIds) {
        return batchExecute("getActivitySimpleList_", activityIds, OfflineActivitySimpleResp.class,
                OfflineActivitySimpleResp::getActivityId, ids -> {
                    CommonResp<List<OfflineActivitySimpleResp>> resp = feignClientForActivity.simpleListByIds(ids);
刘基明's avatar
刘基明 committed
127 128 129 130 131 132 133 134
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

张辰's avatar
张辰 committed
135 136 137 138 139 140 141
    public List<UserInfoResp> getUserList(List<String> userIds) {
        return batchExecute("getUserList_", userIds, UserInfoResp.class,
                UserInfoResp::getUserId, ids -> {
                    return feignClientForFatools.queryUserListNew(ids);
                });
    }

刘基明's avatar
刘基明 committed
142
    public List<ProductInfoVO> getProductInfoByIdsForTopic(List<String> fundIds) {
刘基明's avatar
刘基明 committed
143
        return batchExecute("getProductInfoByIdsForTopic_", fundIds, ProductInfoVO.class,
刘基明's avatar
刘基明 committed
144 145 146 147 148 149 150 151 152 153 154
                ProductInfoVO::getFundId, ids -> {
                    CommonResp<List<ProductInfoVO>> resp = feignForProduct.getProductInfoByIds(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

    public List<ProductInfoVO> getProductInfoByIdsForTheme(List<String> fundIds) {
刘基明's avatar
刘基明 committed
155
        return batchExecute("getProductInfoByIdsForTheme_", fundIds, ProductInfoVO.class,
张辰's avatar
张辰 committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
                ProductInfoVO::getFundId, ids -> {
                    CommonResp<List<ProductInfoVO>> resp = feignForProduct.getProductInfoByIds(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

    public List<ProductInfoVO> getFundList(List<String> fundIds) {
        return batchExecute("getFundList", fundIds, ProductInfoVO.class,
                ProductInfoVO::getFundId, ids -> {
                    CommonResp<List<ProductInfoVO>> resp = feignForFund.getProductList(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

    public List<ProductInfoVO> getPrivateFundList(List<String> fundIds) {
        return batchExecute("getPrivateFundList", fundIds, ProductInfoVO.class,
                ProductInfoVO::getFundId, ids -> {
                    CommonResp<List<ProductInfoVO>> resp = feignForFund.getPrivateFundList(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

    public List<ProductInfoVO> getPublicFundList(List<String> fundIds) {
        return batchExecute("getPublicFundList", fundIds, ProductInfoVO.class,
                ProductInfoVO::getFundId, ids -> {
                    CommonResp<List<ProductInfoVO>> resp = feignForPublicFund.getProductList(ids);
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

刘基明's avatar
刘基明 committed
202 203 204 205 206 207 208 209 210 211 212 213
    public List<FundCompanyVO> getFundCompany(List<String> companyIds) {
        return batchExecute("getCoursePackageList", companyIds, FundCompanyVO.class,
                FundCompanyVO::getCompanyId, ids -> {
                    CommonResp<FundCompanyVO> resp = feignForProduct.getFundCompanyInfo(ids.get(0));
                    if (resp.isSuccess()) {
                        return Arrays.asList(resp.getData());
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

张辰's avatar
张辰 committed
214

215 216 217 218 219
    private <T> List<T> batchExecute(String keyPrefix,
                                     List<String> keys,
                                     Class<T> clz,
                                     Function<T, String> getKeyFunc,
                                     Function<List<String>, List<T>> feignFunc) {
张辰's avatar
张辰 committed
220 221 222 223 224 225 226 227 228 229 230
        List<T> retList = new ArrayList<>();
        List<String> feignIds = new ArrayList<>();
        for (String key : keys) {
            String cacheKey = keyPrefix + key;

            T ret = localCache.getObject(cacheKey, clz);
            if (ret != null) {
                retList.add(ret);
            } else {
                feignIds.add(key);
            }
231
        }
张辰's avatar
张辰 committed
232

233 234 235
        if (!feignIds.isEmpty()) {
            List<T> remoteList = feignFunc.apply(feignIds);
//            log.info("batchExecute feign returns {} -> {}", JSON.toJSONString(feignIds), JSON.toJSONString(remoteList));
张辰's avatar
张辰 committed
236

237 238 239 240 241
            if (remoteList != null) {
                retList.addAll(remoteList);
                retList.forEach(r -> {
                    localCache.putObject(keyPrefix + getKeyFunc.apply(r), r);
                });
张辰's avatar
张辰 committed
242 243
            }
        }
244
//        log.info("batchExecute {} : {} -> {}", keyPrefix, JSON.toJSONString(keys), JSON.toJSONString(retList));
张辰's avatar
张辰 committed
245 246
        return retList;
    }
张辰's avatar
张辰 committed
247
}