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

3
import com.alibaba.fastjson.JSON;
张辰's avatar
张辰 committed
4 5
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException;
张辰's avatar
张辰 committed
6
import com.tanpu.community.api.beans.vo.feign.course.CourseSimpleResp;
7
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
张辰's avatar
张辰 committed
8
import com.tanpu.community.api.beans.vo.feign.course.ShortVideoBaseInfoResp;
张辰's avatar
张辰 committed
9 10
import com.tanpu.community.api.beans.vo.feign.product.ProductInfoVO;
import com.tanpu.community.api.beans.vo.feign.zhibo.ZhiboListResp;
张辰's avatar
张辰 committed
11 12
import com.tanpu.community.cache.LocalCache;
import com.tanpu.community.feign.course.FeignClientForCourse;
张辰's avatar
张辰 committed
13
import com.tanpu.community.feign.fatools.FeignClientForFatools;
张辰's avatar
张辰 committed
14 15 16 17 18
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;
19 20
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
张辰's avatar
张辰 committed
21
import org.springframework.beans.factory.annotation.Autowired;
张辰's avatar
张辰 committed
22 23
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
张辰's avatar
张辰 committed
24 25
import org.springframework.stereotype.Service;

张辰's avatar
张辰 committed
26 27 28 29 30 31
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
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 58 59 60
    @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
61

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

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

张辰's avatar
张辰 committed
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 157 158 159 160 161 162
    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<>();
                    }
                });
    }



163 164 165 166 167
    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
168 169 170 171 172 173 174 175 176 177 178
        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);
            }
179
        }
张辰's avatar
张辰 committed
180

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

185 186 187 188 189
            if (remoteList != null) {
                retList.addAll(remoteList);
                retList.forEach(r -> {
                    localCache.putObject(keyPrefix + getKeyFunc.apply(r), r);
                });
张辰's avatar
张辰 committed
190 191
            }
        }
192
//        log.info("batchExecute {} : {} -> {}", keyPrefix, JSON.toJSONString(keys), JSON.toJSONString(retList));
张辰's avatar
张辰 committed
193 194
        return retList;
    }
张辰's avatar
张辰 committed
195
}