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 implements ApiAuthorizedService { @Resource private ApiAppDao apiAppDao; @Resource private ApiInfoDao apiInfoDao; @Resource private StringRedisTemplate stringRedisTemplate; @Override public PageUtils queryPage(Map params) { String appId = (String)params.get("appId"); Map queryParams = new HashMap<>(1); queryParams.put("appId", appId); IPage page = getBaseMapper().queryList( new Query().getPage(params), queryParams ); return new PageUtils(page); } @Override public List queryList(Map params) { String appId = (String)params.get("appId"); return getBaseMapper().selectList(new QueryWrapper() .eq(StringUtils.isNotBlank(appId), "app_id", appId) .orderByDesc("create_time")); } @Override @Transactional(rollbackFor = Exception.class) public void grantApi(String appId, List apiIdList) { ApiAppEntity apiAppEntity = apiAppDao.selectById(appId); if (apiAppEntity == null) { return; } String appKey = apiAppEntity.getAppKey(); getBaseMapper().delete(new QueryWrapper().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().eq("id", id)); addApiRedis(); } @Override public ApiAuthorizedEntity getInfoById(String id) { return this.getById(id); } @Override public void addApiRedis() { Map apis = new HashMap<>(); Map params = new HashMap<>(1); params.put("serverStatus", 1); List 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)); } }