FeignService.java 7.96 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.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 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
import com.tanpu.community.cache.LocalCache;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.feign.activity.FeignClientForActivity;
张辰's avatar
张辰 committed
13
import com.tanpu.community.feign.course.FeignClientForCourse;
张辰's avatar
张辰 committed
14
import com.tanpu.community.feign.fatools.FeignClientForFatools;
张辰's avatar
张辰 committed
15 16 17 18 19
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;
20
import lombok.extern.slf4j.Slf4j;
张辰's avatar
张辰 committed
21 22
import org.springframework.stereotype.Service;

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

28
@Slf4j
张辰's avatar
张辰 committed
29 30 31
@Service
public class FeignService {

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

    @Resource
    private FeignClientForActivity feignClientForActivity;

张辰's avatar
张辰 committed
58 59 60
    @Resource
    private LocalCache localCache;

张辰's avatar
张辰 committed
61

62
    public UserInfoResp getUserInfoById(String userId) {
63
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(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
    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
91
                }); }
张辰's avatar
张辰 committed
92 93 94 95 96 97 98 99 100 101 102 103 104

    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
105 106 107 108
    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
109 110 111 112 113 114 115 116
                    if (resp.isSuccess()) {
                        return resp.getData();
                    } else {
                        return new ArrayList<>();
                    }
                });
    }

张辰's avatar
张辰 committed
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 163 164 165 166 167 168 169 170 171 172 173
    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<>();
                    }
                });
    }



174 175 176 177 178
    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
179 180 181 182 183 184 185 186 187 188 189
        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);
            }
190
        }
张辰's avatar
张辰 committed
191

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

196 197 198 199 200
            if (remoteList != null) {
                retList.addAll(remoteList);
                retList.forEach(r -> {
                    localCache.putObject(keyPrefix + getKeyFunc.apply(r), r);
                });
张辰's avatar
张辰 committed
201 202
            }
        }
203
//        log.info("batchExecute {} : {} -> {}", keyPrefix, JSON.toJSONString(keys), JSON.toJSONString(retList));
张辰's avatar
张辰 committed
204 205
        return retList;
    }
张辰's avatar
张辰 committed
206
}