新增license分页接口及实现

This commit is contained in:
aoshiguchen
2022-08-06 19:34:40 +08:00
parent 2b8c538a7b
commit bc7ecaac6e
15 changed files with 322 additions and 17 deletions
@@ -26,12 +26,13 @@ import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.annotation.Param;
import fun.asgc.neutrino.core.aop.Invocation;
import fun.asgc.neutrino.core.aop.interceptor.Interceptor;
import fun.asgc.neutrino.core.cache.MemoryCache;
import fun.asgc.neutrino.core.db.page.Page;
import fun.asgc.neutrino.core.db.template.JdbcTemplate;
import fun.asgc.neutrino.core.util.*;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -59,7 +60,26 @@ public class SqlMapperInterceptor implements Interceptor {
if (sqlParser.isReturnCollection()) {
res = jdbcTemplate.queryForList(resultComponentType, sql, inv.getArgs());
} else {
res = jdbcTemplate.query(resultType, sql, inv.getArgs());
if (Page.class.isAssignableFrom(inv.getTargetMethod().getParameters()[0].getType())) {
// 分页查询 TODO 此处暂时临时处理,假设后面的参数是一个DO对象
Page page = (Page) inv.getArgs()[0];
// Map<String, Object> params = new HashMap<>();
// for (int i = 1; i < inv.getTargetMethod().getParameterCount(); i++) {
// Parameter parameter = inv.getTargetMethod().getParameters()[i];
// Param param = parameter.getAnnotation(Param.class);
// if (null == param || StringUtil.isEmpty(param.value())) {
// continue;
// }
// params.put(param.value(), inv.getArgs()[i]);
// }
int offset = (page.getCurrentPage() - 1) * page.getPageSize();
long total = jdbcTemplate.queryForLongByModel(String.format("select count(1) from (%s) T", sql), inv.getArgs()[1]);
List resultList = jdbcTemplate.queryForListByModel(resultComponentType, String.format("%s limit %s,%s", sql, offset, page.getPageSize()), inv.getArgs()[1]);
page.setTotal(total);
page.setRecords(resultList);
} else {
res = jdbcTemplate.query(resultType, sql, inv.getArgs());
}
}
} else if (sqlParser.isInsert() || sqlParser.isDelete() || sqlParser.isUpdate()) {
int argsCount = ArrayUtil.isEmpty(inv.getArgs()) ? 0 : inv.getArgs().length;
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.page;
import lombok.Data;
import java.util.List;
/**
*
* @author: aoshiguchen
* @date: 2022/8/6
*/
@Data
public class Page<T> extends PageQuery {
/**
* 数据总数
*/
private Long total;
/**
* 结果数据
*/
private List<T> records;
public static <T> Page<T> create(PageQuery pageQuery) {
Page page = new Page();
page.setPageSize(pageQuery.getPageSize());
page.setCurrentPage(pageQuery.getCurrentPage());
return page;
}
}
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.db.page;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @author: aoshiguchen
* @date: 2022/8/6
*/
public class PageQuery {
/**
* 当前页
*/
private int currentPage = 1;
/**
* 分页大小
*/
private int pageSize = 10;
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
@@ -21,12 +21,16 @@
*/
package fun.asgc.neutrino.core.db.template;
import fun.asgc.neutrino.core.util.ArrayUtil;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
@@ -257,6 +261,19 @@ public class JdbcTemplate {
Connection conn = null;
try {
// TODO 临时处理集合参数
if (ArrayUtil.notEmpty(params)) {
for (int i = 0; i < params.length; i++) {
Object item = params[i];
if (null != item && Collection.class.isAssignableFrom(item.getClass()) && !((Collection)item).isEmpty()) {
Object first = ((Collection)item).stream().findFirst().get();
if (Integer.class.isAssignableFrom(first.getClass())) {
params[i] = ((Collection)item).stream().map(String::valueOf).collect(Collectors.joining(","));
}
}
}
}
conn = dataSourceHolder.getConnection();
res = jdbcOperations.executeQueryForList(conn, clazz, sql, params);
} finally {
@@ -22,11 +22,13 @@
package fun.asgc.neutrino.core.db.template;
import fun.asgc.neutrino.core.base.Orderly;
import fun.asgc.neutrino.core.util.ArrayUtil;
import fun.asgc.neutrino.core.util.ReflectUtil;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* sql语句+sql参数的封装,用于支持以下3种用法
@@ -126,8 +126,8 @@ public class TypeUtil {
* 基本类型列表(用于计算类型提升距离)
*/
public static final List<Class<?>> basicTypeList = Lists.newArrayList(
boolean.class, byte.class, short.class, char.class, int.class, long.class, float.class, double.class,
Boolean.class, Byte.class, Short.class, Character.class, Integer.class, Long.class, Float.class, Double.class
boolean.class, Boolean.class, byte.class, Byte.class, short.class, Short.class, char.class, Character.class,
int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class, Double.class
);
/**
@@ -265,7 +265,10 @@ public class HttpRequestHandler {
for (Field field : fields) {
String name = field.getName();
Object value = TypeUtil.conversion(HttpContextHolder.getHttpRequestWrapper().getParameter(name), field.getType());
ReflectUtil.setFieldValue(field, obj, value);
// 临时处理默认值被覆盖的问题
if (!(null != value && TypeUtil.isStrictBasicType(field) && value.equals(TypeUtil.getDefaultValue(field.getType())))) {
ReflectUtil.setFieldValue(field, obj, value);
}
}
} catch (Exception e) {
e.printStackTrace();
@@ -29,14 +29,14 @@ package fun.asgc.neutrino.proxy.server.controller;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.db.page.Page;
import fun.asgc.neutrino.core.db.page.PageQuery;
import fun.asgc.neutrino.core.web.annotation.*;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseCreateReq;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseListReq;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseUpdateEnableStatusReq;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseUpdateReq;
import fun.asgc.neutrino.proxy.server.controller.res.LicenseCreateRes;
import fun.asgc.neutrino.proxy.server.controller.res.LicenseDetailRes;
import fun.asgc.neutrino.proxy.server.controller.res.LicenseUpdateEnableStatusRes;
import fun.asgc.neutrino.proxy.server.controller.res.LicenseUpdateRes;
import fun.asgc.neutrino.proxy.server.controller.res.*;
import fun.asgc.neutrino.proxy.server.service.LicenseService;
@NonIntercept
@@ -47,6 +47,12 @@ public class LicenseController {
@Autowired
private LicenseService licenseService;
@GetMapping("page")
public Page<LicenseListRes> page(PageQuery pageQuery, LicenseListReq req) {
// TODO 参数校验
return licenseService.page(pageQuery, req);
}
@PostMapping("create")
public LicenseCreateRes create(@RequestBody LicenseCreateReq req) {
// TODO 参数校验
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.controller.req;
import lombok.Data;
/**
*
* @author: aoshiguchen
* @date: 2022/8/6
*/
@Data
public class LicenseListReq {
}
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.controller.res;
import fun.asgc.neutrino.core.db.annotation.Id;
import fun.asgc.neutrino.proxy.server.base.rest.constant.OnlineStatusEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
*
* @author: aoshiguchen
* @date: 2022/8/6
*/
@Accessors(chain = true)
@Data
public class LicenseListRes {
@Id
private Integer id;
/**
* 名称
*/
private String name;
/**
* licenseKey
*/
private String key;
/**
* 用户ID
*/
private Integer userId;
/**
* 用户名
*/
private String userName;
/**
* 是否在线
* {@link OnlineStatusEnum}
*/
private Integer isOnline;
/**
* 启用状态
* {@link fun.asgc.neutrino.proxy.server.base.rest.constant.EnableStatusEnum}
*/
private Integer enable;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
}
@@ -27,6 +27,9 @@ import fun.asgc.neutrino.core.db.annotation.Delete;
import fun.asgc.neutrino.core.db.annotation.Select;
import fun.asgc.neutrino.core.db.annotation.Update;
import fun.asgc.neutrino.core.db.mapper.SqlMapper;
import fun.asgc.neutrino.core.db.page.Page;
import fun.asgc.neutrino.core.db.page.PageQuery;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseListReq;
import fun.asgc.neutrino.proxy.server.dal.entity.LicenseDO;
import java.util.Date;
@@ -39,6 +42,13 @@ import java.util.Date;
@Component
public interface LicenseMapper extends SqlMapper {
/**
* 查询license分页
* @param page
* @param req
*/
void page(Page page, LicenseListReq req);
/**
* 新增license
* @param license
@@ -22,10 +22,14 @@
package fun.asgc.neutrino.proxy.server.dal;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.db.annotation.ResultType;
import fun.asgc.neutrino.core.db.annotation.Select;
import fun.asgc.neutrino.core.db.mapper.SqlMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import java.util.List;
import java.util.Set;
/**
*
* @author: aoshiguchen
@@ -50,4 +54,7 @@ public interface UserMapper extends SqlMapper {
@Select("select * from user where id = ?")
UserDO findById(Integer id);
@ResultType(UserDO.class)
@Select("select * from user where id in (?)")
List<UserDO> findByIds(Set<Integer> ids);
}
@@ -23,22 +23,24 @@ package fun.asgc.neutrino.proxy.server.service;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.db.page.Page;
import fun.asgc.neutrino.core.db.page.PageQuery;
import fun.asgc.neutrino.core.util.CollectionUtil;
import fun.asgc.neutrino.proxy.server.base.rest.constant.EnableStatusEnum;
import fun.asgc.neutrino.proxy.server.base.rest.constant.OnlineStatusEnum;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseCreateReq;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseListReq;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseUpdateEnableStatusReq;
import fun.asgc.neutrino.proxy.server.controller.req.LicenseUpdateReq;
import fun.asgc.neutrino.proxy.server.controller.res.LicenseCreateRes;
import fun.asgc.neutrino.proxy.server.controller.res.LicenseDetailRes;
import fun.asgc.neutrino.proxy.server.controller.res.LicenseUpdateEnableStatusRes;
import fun.asgc.neutrino.proxy.server.controller.res.LicenseUpdateRes;
import fun.asgc.neutrino.proxy.server.controller.res.*;
import fun.asgc.neutrino.proxy.server.dal.LicenseMapper;
import fun.asgc.neutrino.proxy.server.dal.UserMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.LicenseDO;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import java.util.Date;
import java.util.UUID;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* license服务
@@ -53,6 +55,23 @@ public class LicenseService {
@Autowired
private UserMapper userMapper;
public Page<LicenseListRes> page(PageQuery pageQuery, LicenseListReq req) {
Page<LicenseListRes> page = Page.create(pageQuery);
licenseMapper.page(page, req);
if (!CollectionUtil.isEmpty(page.getRecords())) {
Set<Integer> userIds = page.getRecords().stream().map(LicenseListRes::getUserId).collect(Collectors.toSet());
List<UserDO> userList = userMapper.findByIds(userIds);
Map<Integer, UserDO> userMap = userList.stream().collect(Collectors.toMap(UserDO::getId, Function.identity()));
for (LicenseListRes item : page.getRecords()) {
UserDO userDO = userMap.get(item.getUserId());
if (null != userDO) {
item.setUserName(userDO.getName());
}
}
}
return page;
}
/**
* 创建license
* @param req
@@ -1,5 +1,9 @@
<mapper namespace = "fun.asgc.neutrino.proxy.server.dal.LicenseMapper">
<select id="page" resultType="fun.asgc.neutrino.proxy.server.controller.res.LicenseListRes">
select * from `license`
</select>
<insert id="add">
insert into `license`(`name`,`key`,`user_id`,`is_online`,`enable`,`create_time`,`update_time`)
values (:name, :key, :userId, :isOnline, :enable, :createTime, :updateTime)
@@ -1,3 +1,3 @@
#
insert into `user`(`id`, `name`,`login_name`,`login_password`,`enable`,`create_time`, `update_time`) values
(1, '管理员', 'admin', 'e10adc3949ba59abbe56e057f20f883e', 1, datetime('now', 'localtime'), datetime('now', 'localtime'));
(1, '管理员', 'admin', 'e10adc3949ba59abbe56e057f20f883e', 1, STRFTIME('%s000', 'NOW'), STRFTIME('%s000', 'NOW'));