houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
package com.iailab.module.data.gateway.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iailab.module.data.common.utils.PageUtils;
import com.iailab.module.data.common.utils.Query;
import com.iailab.framework.common.constant.CacheConstant;
import com.iailab.module.data.gateway.dao.ApiAppDao;
import com.iailab.module.data.gateway.dao.ApiAuthorizedDao;
import com.iailab.module.data.gateway.dao.ApiInfoDao;
import com.iailab.module.data.gateway.entity.ApiAppEntity;
import com.iailab.module.data.gateway.entity.ApiAuthorizedEntity;
import com.iailab.module.data.gateway.entity.ApiInfoEntity;
import com.iailab.module.data.gateway.service.ApiAuthorizedService;
import com.iailab.module.data.gateway.vo.SysGatewayApi;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
 
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2022年07月15日 14:02:00
 */
@Service("apiAuthorizedService")
public class ApiAuthorizedServiceImpl extends ServiceImpl<ApiAuthorizedDao, ApiAuthorizedEntity> implements ApiAuthorizedService {
 
    @Resource
    private ApiAppDao apiAppDao;
 
    @Resource
    private ApiInfoDao apiInfoDao;
 
    @Resource
    private StringRedisTemplate stringRedisTemplate;
 
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String appId = (String)params.get("appId");
        Map<String, Object> queryParams = new HashMap<>(1);
        queryParams.put("appId", appId);
        IPage<ApiAuthorizedEntity> page = getBaseMapper().queryList(
                new Query<ApiAuthorizedEntity>().getPage(params),
                queryParams
        );
        return new PageUtils(page);
    }
 
    @Override
    public List<ApiAuthorizedEntity> queryList(Map<String, Object> params) {
        String appId = (String)params.get("appId");
        return getBaseMapper().selectList(new QueryWrapper<ApiAuthorizedEntity>()
                .eq(StringUtils.isNotBlank(appId), "app_id", appId)
                .orderByDesc("create_time"));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void grantApi(String appId, List<String> apiIdList) {
        ApiAppEntity apiAppEntity = apiAppDao.selectById(appId);
        if (apiAppEntity == null) {
            return;
        }
        String appKey = apiAppEntity.getAppKey();
        getBaseMapper().delete(new QueryWrapper<ApiAuthorizedEntity>().eq("app_id", appId));
        if (!CollectionUtils.isEmpty(apiIdList)) {
            apiIdList = apiIdList.stream().distinct().collect(Collectors.toList());
            apiIdList.forEach(item -> {
                ApiInfoEntity apiInfoEntity = apiInfoDao.selectById(item);
                if (apiInfoEntity != null) {
                    ApiAuthorizedEntity apiAuthorizedEntity = new ApiAuthorizedEntity();
                    apiAuthorizedEntity.setId(UUID.randomUUID().toString().replace("-", ""));
                    apiAuthorizedEntity.setAppId(appId);
                    apiAuthorizedEntity.setAppKey(appKey);
                    apiAuthorizedEntity.setApiId(apiInfoEntity.getId());
                    apiAuthorizedEntity.setCreateTime(new Date());
                    this.save(apiAuthorizedEntity);
                }
            });
        }
        addApiRedis();
    }
 
    @Override
    public void add(ApiAuthorizedEntity apiAuthorizedEntity) {
        this.save(apiAuthorizedEntity);
        addApiRedis();
    }
 
    @Override
    public void update(ApiAuthorizedEntity apiAuthorizedEntity) {
        this.updateById(apiAuthorizedEntity);
        addApiRedis();
    }
 
    @Override
    public void deleteById(String id) {
        this.getBaseMapper().delete(new QueryWrapper<ApiAuthorizedEntity>().eq("id", id));
        addApiRedis();
    }
 
    @Override
    public ApiAuthorizedEntity getInfoById(String id) {
        return this.getById(id);
    }
 
    @Override
    public void addApiRedis() {
        Map<String, String> apis = new HashMap<>();
        Map<String, Object> params = new HashMap<>(1);
        params.put("serverStatus", 1);
        List<SysGatewayApi> gatewayApiList = getBaseMapper().queryListGatewayApi(params);
        if (!CollectionUtils.isEmpty(gatewayApiList)) {
            gatewayApiList.forEach(item -> {
                String url = "/" + item.getServerName() + item.getApiAddress();
                apis.put(item.getAppKey() + "_" + url, item.getApiStatus().toString());
            });
        }
        stringRedisTemplate.opsForValue().set(CacheConstant.GATEWAY_APIS, JSON.toJSONString(apis));
    }
 
}