FeignService.java 7.16 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.vo.feign.course.CourseSimpleResp;
6
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
张辰's avatar
张辰 committed
7
import com.tanpu.community.api.beans.vo.feign.course.ShortVideoBaseInfoResp;
张辰's avatar
张辰 committed
8 9
import com.tanpu.community.api.beans.vo.feign.product.ProductInfoVO;
import com.tanpu.community.api.beans.vo.feign.zhibo.ZhiboListResp;
张辰's avatar
张辰 committed
10 11
import com.tanpu.community.cache.LocalCache;
import com.tanpu.community.feign.course.FeignClientForCourse;
张辰's avatar
张辰 committed
12
import com.tanpu.community.feign.fatools.FeignClientForFatools;
张辰's avatar
张辰 committed
13 14 15 16 17
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;
18
import lombok.extern.slf4j.Slf4j;
张辰's avatar
张辰 committed
19 20
import org.springframework.stereotype.Service;

张辰's avatar
张辰 committed
21 22 23 24 25
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

26
@Slf4j
张辰's avatar
张辰 committed
27 28 29
@Service
public class FeignService {

张辰's avatar
张辰 committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    @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;

    @Resource
    private LocalCache localCache;

张辰's avatar
张辰 committed
55

56
    public UserInfoResp getUserInfoById(String userId) {
57
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(userId);
张辰's avatar
张辰 committed
58 59 60 61 62
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }
张辰's avatar
张辰 committed
63 64

    public List<ShortVideoBaseInfoResp> batchGetShortVideoBaseInfo(List<String> sourceIds) {
65 66
        return batchExecute("batchGetShortVideoBaseInfo_", sourceIds, ShortVideoBaseInfoResp.class,
                ShortVideoBaseInfoResp::getSourceId, ids -> {
张辰's avatar
张辰 committed
67 68 69 70 71 72 73 74 75
            CommonResp<List<ShortVideoBaseInfoResp>> resp = feignClientForTanpuroom.getShortVideoBaseInfo(ids);
            if (resp.isSuccess()) {
                return resp.getData();
            } else {
                return new ArrayList<>();
            }
        });
    }

张辰's avatar
张辰 committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    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<>();
                    }
                });
    }

    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<>();
                    }
                });
    }

    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<>();
                    }
                });
    }



157 158 159 160 161
    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
162 163 164 165 166 167 168 169 170 171 172
        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);
            }
173
        }
张辰's avatar
张辰 committed
174

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

179 180 181 182 183
            if (remoteList != null) {
                retList.addAll(remoteList);
                retList.forEach(r -> {
                    localCache.putObject(keyPrefix + getKeyFunc.apply(r), r);
                });
张辰's avatar
张辰 committed
184 185
            }
        }
186
//        log.info("batchExecute {} : {} -> {}", keyPrefix, JSON.toJSONString(keys), JSON.toJSONString(retList));
张辰's avatar
张辰 committed
187 188
        return retList;
    }
张辰's avatar
张辰 committed
189
}