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