package com.changhong.epc.zuul.rest; import com.changhong.epc.rely.api.tool.FilterTool; import com.changhong.epc.rely.api.tool.VersionFilterApi; import com.changhong.epc.zuul.filter.VersionFilter; import com.changhong.epc.zuul.filter.version.Version; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import com.iemsoft.framework.cloud.redis.service.CacheUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.util.*; @RestController public class VersionRest { /** * 获得版本号 * @param id * @param key * @param request * @return */ @GetMapping("/get/version/{id}") public Map getVersion(@PathVariable("id") String id, HttpServletRequest request, @RequestParam(value = "key", required = false) String key){ FilterTool.FILTER_TYPE.get(FilterTool.FilterType.PRIVATE).filter(request); Version versionEntity = VersionFilter.getVersion(id); if(versionEntity == null){ return new HashMap<>(); } Set keys; if(ObjectUtil.empty(key)){ keys = versionEntity.getKeys(); }else{ keys = new HashSet<>(Arrays.asList(key)); } Map result = new HashMap<>(); keys.stream().forEach(fKey->result.put(fKey, getVersion(request, versionEntity, fKey))); return result; } protected Long getVersion(HttpServletRequest request, Version versionEntity, String key){ Long version = CacheUtils.get(key = versionEntity.getCacheKey(request, key), Long.class); if(version == null){ version = System.currentTimeMillis(); CacheUtils.set(key, version, 0); } return version; } @GetMapping("/get/version/master/{key}") public void setVersion(@PathVariable String key){ VersionFilterApi.filter(key, null, null); } }