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

import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
5
import com.tanpu.community.api.beans.resp.CoursePackageSimpleResp;
刘基明's avatar
刘基明 committed
6
import com.tanpu.community.api.beans.vo.feign.activity.OfflineActivitySimpleResp;
张辰's avatar
张辰 committed
7
import com.tanpu.community.api.beans.vo.feign.course.CourseSimpleResp;
张辰's avatar
张辰 committed
8
import com.tanpu.community.api.beans.vo.feign.course.ShortVideoBaseInfoResp;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.api.beans.vo.feign.product.FundCompanyVO;
张辰's avatar
张辰 committed
11 12
import com.tanpu.community.api.beans.vo.feign.product.ProductInfoVO;
import com.tanpu.community.api.beans.vo.feign.zhibo.ZhiboListResp;
张辰's avatar
张辰 committed
13
import com.tanpu.community.cache.LocalCache;
刘基明's avatar
刘基明 committed
14
import com.tanpu.community.feign.activity.FeignClientForActivity;
张辰's avatar
张辰 committed
15
import com.tanpu.community.feign.course.FeignClientForCourse;
张辰's avatar
张辰 committed
16
import com.tanpu.community.feign.fatools.FeignClientForFatools;
张辰's avatar
张辰 committed
17 18 19 20 21
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;
22
import lombok.extern.slf4j.Slf4j;
张辰'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 29 30
import java.util.List;
import java.util.function.Function;

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

张辰's avatar
张辰 committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    @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
57 58 59 60

    @Resource
    private FeignClientForActivity feignClientForActivity;

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

张辰's avatar
张辰 committed
64

65
    public UserInfoResp getUserInfoById(String userId) {
66
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(userId);
张辰's avatar
张辰 committed
67 68 69 70 71
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }
张辰's avatar
张辰 committed
72 73

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

张辰's avatar
张辰 committed
85 86 87 88 89 90 91 92 93
    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
94 95 96 97 98 99 100 101 102 103 104 105 106 107
                });
    }

    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
108 109 110 111 112 113 114 115 116 117 118 119 120

    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
121 122 123 124
    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
125 126 127 128 129 130 131 132
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

张辰's avatar
张辰 committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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
    public List<UserInfoResp> getUserList(List<String> userIds) {
        return batchExecute("getUserList_", userIds, UserInfoResp.class,
                UserInfoResp::getUserId, ids -> {
                    return feignClientForFatools.queryUserListNew(ids);
                });
    }

    public List<ProductInfoVO> getProductInfoByIds(List<String> fundIds) {
        return batchExecute("getProductInfoByIds_", fundIds, ProductInfoVO.class,
                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
188 189 190 191 192 193 194 195 196 197 198 199
    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
200

201 202 203 204 205
    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
206 207 208 209 210 211 212 213 214 215 216
        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);
            }
217
        }
张辰's avatar
张辰 committed
218

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

223 224 225 226 227
            if (remoteList != null) {
                retList.addAll(remoteList);
                retList.forEach(r -> {
                    localCache.putObject(keyPrefix + getKeyFunc.apply(r), r);
                });
张辰's avatar
张辰 committed
228 229
            }
        }
230
//        log.info("batchExecute {} : {} -> {}", keyPrefix, JSON.toJSONString(keys), JSON.toJSONString(retList));
张辰's avatar
张辰 committed
231 232
        return retList;
    }
张辰's avatar
张辰 committed
233
}