use of cn.hutool.db.Page in project Jpom by dromara.
the class BaseDbService method autoLoopClear.
/**
* 自动清理数据接口
*
* @param timeClo 时间字段
* @param maxCount 最大数量
* @param consumer 查询出超过范围的时间回调
*/
protected void autoLoopClear(String timeClo, int maxCount, Consumer<Entity> whereCon, Consumer<T> consumer) {
if (maxCount <= 0) {
return;
}
ThreadUtil.execute(() -> {
Entity entity = Entity.create(super.getTableName());
long timeValue = this.getLastTimeValue(timeClo, maxCount, whereCon);
if (timeValue <= 0) {
return;
}
if (whereCon != null) {
// 条件
whereCon.accept(entity);
}
entity.set(timeClo, "< " + timeValue);
while (true) {
Page page = new Page(1, 50);
page.addOrder(new Order(timeClo, Direction.DESC));
PageResultDto<T> pageResult = super.listPage(entity, page);
if (pageResult.isEmpty()) {
return;
}
pageResult.each(consumer);
List<String> ids = pageResult.getResult().stream().map(BaseDbModel::getId).collect(Collectors.toList());
super.delByKey(ids, null);
}
});
}
use of cn.hutool.db.Page in project Jpom by dromara.
the class RepositoryController method giteeRepos.
/**
* gitee 仓库
*
* @param token 个人令牌
* @param page 分页
* @return page
*/
private PageResultDto<JSONObject> giteeRepos(String token, Page page) {
//
HttpResponse userResponse = HttpUtil.createGet("https://gitee.com/api/v5/user").form("access_token", token).execute();
Assert.state(userResponse.isOk(), "令牌不正确:" + userResponse.body());
JSONObject userBody = JSONObject.parseObject(userResponse.body());
String username = userBody.getString("login");
// 拉取仓库信息
HttpResponse reposResponse = HttpUtil.createGet("https://gitee.com/api/v5/user/repos").form("access_token", token).form("sort", "pushed").form("page", page.getPageNumber()).form("per_page", page.getPageSize()).execute();
String body = reposResponse.body();
Assert.state(userResponse.isOk(), "拉取仓库信息错误:" + body);
String totalCountStr = reposResponse.header("total_count");
int totalCount = Convert.toInt(totalCountStr, 0);
// String totalPage = reposResponse.header("total_page");
JSONArray jsonArray = JSONArray.parseArray(body);
List<JSONObject> objects = jsonArray.stream().map(o -> {
JSONObject repo = (JSONObject) o;
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", repo.getString("name"));
String htmlUrl = repo.getString("html_url");
jsonObject.put("url", htmlUrl);
jsonObject.put("full_name", repo.getString("full_name"));
jsonObject.put("private", repo.getBooleanValue("private"));
jsonObject.put("description", repo.getString("description"));
//
jsonObject.put("username", username);
jsonObject.put("exists", RepositoryController.this.checkRepositoryUrl(null, htmlUrl));
return jsonObject;
}).collect(Collectors.toList());
//
PageResultDto<JSONObject> pageResultDto = new PageResultDto<>(page.getPageNumber(), page.getPageSize(), totalCount);
pageResultDto.setResult(objects);
return pageResultDto;
}
use of cn.hutool.db.Page in project Jpom by dromara.
the class RepositoryController method gitlabRepos.
/**
* gitlab 仓库
* <p>
* https://docs.gitlab.com/ee/api/projects.html#list-all-projects
*
* @param token 个人令牌
* @param page 分页
* @return page
*/
private PageResultDto<JSONObject> gitlabRepos(String token, Page page) {
//
HttpResponse userResponse = HttpUtil.createGet("https://gitlab.com/api/v4/user").form("access_token", token).execute();
Assert.state(userResponse.isOk(), "令牌不正确:" + userResponse.body());
JSONObject userBody = JSONObject.parseObject(userResponse.body());
String username = userBody.getString("username");
// 拉取仓库信息
HttpResponse reposResponse = HttpUtil.createGet("https://gitlab.com/api/v4/projects").form("private_token", token).form("membership", true).form("simple", true).form("order_by", "updated_at").form("page", page.getPageNumber()).form("per_page", Math.max(page.getPageSize(), 15)).execute();
String body = reposResponse.body();
Assert.state(userResponse.isOk(), "拉取仓库信息错误:" + body);
String totalCountStr = reposResponse.header("X-Total");
int totalCount = Convert.toInt(totalCountStr, 0);
// String totalPage = reposResponse.header("total_page");
JSONArray jsonArray = JSONArray.parseArray(body);
List<JSONObject> objects = jsonArray.stream().map(o -> {
JSONObject repo = (JSONObject) o;
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", repo.getString("name"));
String htmlUrl = repo.getString("http_url_to_repo");
jsonObject.put("url", htmlUrl);
jsonObject.put("full_name", repo.getString("path_with_namespace"));
jsonObject.put("private", StrUtil.equalsIgnoreCase("private", repo.getString("visibility")));
jsonObject.put("description", repo.getString("description"));
//
jsonObject.put("username", username);
jsonObject.put("exists", RepositoryController.this.checkRepositoryUrl(null, htmlUrl));
return jsonObject;
}).collect(Collectors.toList());
//
PageResultDto<JSONObject> pageResultDto = new PageResultDto<>(page.getPageNumber(), page.getPageSize(), totalCount);
pageResultDto.setResult(objects);
return pageResultDto;
}
use of cn.hutool.db.Page in project Jpom by dromara.
the class RepositoryController method authorizeRepos.
@GetMapping(value = "/build/repository/authorize_repos")
@Feature(method = MethodFeature.LIST)
public Object authorizeRepos() {
// 获取分页信息
HttpServletRequest request = getRequest();
Map<String, String> paramMap = ServletUtil.getParamMap(request);
Page page = repositoryService.parsePage(paramMap);
String token = paramMap.get("token");
Assert.hasText(token, "请填写个人令牌");
//
String type = paramMap.get("type");
PageResultDto<JSONObject> pageResultDto;
switch(type) {
case "gitee":
pageResultDto = this.giteeRepos(token, page);
break;
case "github":
pageResultDto = this.githubRepos(token, page);
break;
case "gitlab":
pageResultDto = this.gitlabRepos(token, page);
break;
default:
throw new IllegalArgumentException("不支持的类型");
}
return JsonMessage.toJson(HttpStatus.OK.value(), HttpStatus.OK.name(), pageResultDto);
}
use of cn.hutool.db.Page in project Jpom by dromara.
the class RepositoryController method githubRepos.
/**
* github 仓库
*
* @param token 个人令牌
* @param page 分页
* @return page
*/
private PageResultDto<JSONObject> githubRepos(String token, Page page) {
String accept = "application/vnd.github.v3+json";
HttpRequest request = HttpUtil.createGet("https://api.github.com/user");
request.header("Authorization", StrUtil.format("token {}", token));
request.header("Accept", accept);
HttpResponse httpResponse = request.execute();
String body = httpResponse.body();
Assert.state(httpResponse.isOk(), "令牌信息错误:" + body);
JSONObject userBody = JSONObject.parseObject(body);
String username = userBody.getString("login");
// 拉取仓库信息
HttpRequest httpRequestRepos = HttpUtil.createGet("https://api.github.com/user/repos");
httpRequestRepos.header("Authorization", StrUtil.format("token {}", token));
httpRequestRepos.header("Accept", accept);
HttpResponse reposResponse = httpRequestRepos.form("access_token", token).form("sort", "pushed").form("page", page.getPageNumber()).form("per_page", page.getPageSize()).execute();
body = reposResponse.body();
Assert.state(reposResponse.isOk(), "拉取仓库信息错误:" + body);
JSONArray jsonArray = JSONArray.parseArray(body);
List<JSONObject> objects = jsonArray.stream().map(o -> {
JSONObject repo = (JSONObject) o;
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", repo.getString("name"));
String cloneUrl = repo.getString("clone_url");
jsonObject.put("url", cloneUrl);
jsonObject.put("full_name", repo.getString("full_name"));
jsonObject.put("description", repo.getString("description"));
jsonObject.put("private", repo.getBooleanValue("private"));
//
jsonObject.put("username", username);
jsonObject.put("exists", RepositoryController.this.checkRepositoryUrl(null, cloneUrl));
return jsonObject;
}).collect(Collectors.toList());
//
PageResultDto<JSONObject> pageResultDto = new PageResultDto<>(page.getPageNumber(), page.getPageSize(), 1000);
pageResultDto.setResult(objects);
return pageResultDto;
}
Aggregations